Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with drawing inventory

This thread is locked; no one can reply to it. rss feed Print
Problem with drawing inventory
David Sopala
Member #5,056
September 2004

Ok I have been working on this for a few days all I am trying to do at this point is get the inventory section of the code to run properly. I have pretty much all the support code in so I started on the draw routines. So all is going well I have the layout displaying and the key input working now I go test to display an item and the program crashes - ok well thats odd so I figure it is bad code somewhere after many hours of testing I can not find bad code and when testing in the main redraw for the game it works fine same code different result is there ANYTHING that would make a blit crash in one funtion and work in another?

#SelectExpand
1void Run_Inventory() 2{ 3 Item *dispitem; 4 dispitem = resman->Get_Item(2); //just giving it a picture to display 5 while(key[KEY_I]); 6 7 while(!key[KEY_I]) 8 { 9 blit(LAYOUT,BUFFER,0,0,0,0,scrx,scry); //toss the backdrop up. 10 masked_blit(INVENTORY,BUFFER,0,0,0,0,scrx,scry); //toss the grids. 11 12 masked_blit(dispitem->GetPic(),BUFFER,0,0,0,0,64,64);//crash here this line does it 13 //DRAW INVENTORY 14 15 //END DRAW INVENTORY 16 blit(BUFFER,screen,0,0,0,0,scrx,scry); 17 } 18 while(key[KEY_I]); 19 20}

Ben Delacob
Member #6,141
August 2005
avatar

Are you sure the dispitem pointer (or resman?) isn't null or erronious in one of the functions? You should probably use ASSERT for some degree of assurance for all sorts of pointer work. Also investigate what GetPic() returns. If the function is the same elsewhere, that may not tell you what the problem is but it would narrow it down slightly. Maybe try printing out and comparing the pointers with what the other function uses.

note: [code] blocks now use < and > like HTML here.

__________________________________
Allegro html mockup code thread -website-
"two to the fighting eighth power"

David Sopala
Member #5,056
September 2004

Completely sure of that resman is just a resource manager so when it loads the items and spells etc in it must be declared(it is actually a global wrapper more or less).

#SelectExpand
1#ifndef RESMAN_H 2#define RESMAN_H 3 4#include "Item.h" 5#include "spell.h" 6class resource_manager 7{ 8 public: 9 resource_manager(); 10 ~resource_manager(); 11 void Load_Items(char *ItemFilePath); 12 void Load_Spells(char *SpellFilePath); 13 Item *Get_Item(int which); 14 Spell *Get_Spell(int which); 15 int GetNumSpell(); 16 int GetNumItem(); 17 private: 18 int Num_Item; 19 int Num_Spell; 20 Item **Item_List; 21 Spell **Spell_List; 22}; 23 24 25#endif

#SelectExpand
1#include "resman.h" 2#include <fstream> 3 4using namespace std; 5resource_manager::resource_manager() 6{ 7 Num_Item = 0; 8 Num_Spell = 0; 9 Item_List = NULL; 10 Spell_List = NULL; 11} 12resource_manager::~resource_manager() 13{ 14 if(Item_List) 15 { 16 for(int x=0;x < Num_Item;x++) 17 { 18 delete Item_List[x]; 19 } 20 delete[] Item_List; 21 } 22 if(Spell_List) 23 { 24 for(int x=0;x < Num_Spell;x++) 25 { 26 delete Spell_List[x]; 27 } 28 delete[] Spell_List; 29 } 30} 31void resource_manager::Load_Items(char *ItemFilePath) 32{ 33 int loadarray[17]; 34 char temp[255]; 35 ifstream loadstream; 36 loadstream.open(ItemFilePath); 37 loadstream>>Num_Item; 38 Item_List = new Item*[Num_Item]; 39 for(int lcv = 0;lcv < Num_Item;lcv++) 40 { 41 Item_List[lcv] = new Item; 42 } 43 for(int lcv = 0;lcv < Num_Item;lcv++) 44 { 45 loadstream>>loadarray[0]>>temp; 46 Item_List[lcv]->SetItemName(temp); 47 loadstream>>temp; 48 Item_List[lcv]->SetPicPath(temp,NULL); 49 for(int zz=1;zz < 12;zz++) 50 { 51 loadstream>>loadarray[zz]; 52 } 53 loadarray[12] = 0; 54 loadarray[13] = 0; 55 loadarray[14] = 0; 56 loadarray[15] = 0; 57 loadarray[16] = 0; 58 Item_List[lcv]->LOAD(loadarray); 59 loadstream>>temp; 60 61 Item_List[lcv]->SetUseSnd(temp); 62 loadstream>>temp; 63 64 Item_List[lcv]->SetSwingSnd(temp); 65 loadstream>>temp; 66 67 Item_List[lcv]->SetHitSnd(temp); 68 loadstream>>temp; 69 70 Item_List[lcv]->SetMissSnd(temp); 71 } 72 loadstream.close(); 73} 74void resource_manager::Load_Spells(char *SpellFilePath) 75{ 76 char temp[255]; 77 int loadarray[10]; 78 ifstream loadstream; 79 loadstream.open(SpellFilePath); 80 loadstream>>Num_Spell; 81 Spell_List = new Spell*[Num_Spell]; 82 for(int z=0;z<Num_Spell;z++) 83 { 84 loadstream>>loadarray[0]>>temp>>loadarray[1]>>loadarray[2]>>loadarray[3]>>loadarray[4]>>loadarray[5]>>loadarray[6]>>loadarray[7]>>loadarray[8]>>loadarray[9]; 85 Spell_List[z] = new Spell(temp,loadarray[1],loadarray[3],loadarray[2],loadarray[6],loadarray[7],NULL); 86 } 87 loadstream.close(); 88} 89Item* resource_manager::Get_Item(int which) 90{ 91 if(which >= 0 && which < Num_Item)return Item_List[which]; 92} 93Spell* resource_manager::Get_Spell(int which) 94{ 95 if(which >= 0 && which < Num_Spell)return Spell_List[which]; 96} 97int resource_manager::GetNumSpell() 98{ 99 return Num_Spell; 100} 101int resource_manager::GetNumItem() 102{ 103 return Num_Item; 104}

class Item
{
public:
BITMAP* GetPic();
//other stuff
private:
BITMAP* PIC;
//other stuff
};

Constructor sets PIC to NULL just in case. Destructor kills it with destroy_bitmap();.
Assignment sets PIC = rhs.PIC;.

BITMAP* Item::GetPic()  //gives the display picture
{
   return PIC;        
}

void Item::SetPicPath(char *path,RGB *pal)
{
    if(strcmp(path,"NULL") != 0)
    { 
      if(PIC)destroy_bitmap(PIC);
      picpath = new char[strlen(path)+1];
      strcpy(picpath,path);
        PIC = load_bitmap(picpath,pal);
        if(!PIC)allegro_message("ERROR LOADING ITEM");
    }
}

Dario ff
Member #10,065
August 2008
avatar

Have you tried isolating the whole item and resource manager code into another program and test if it works?

Also, another non-related memory leak, in Item::SetPicPath, you're allocating picpath, but you never delete it.

Also, you're using C++... use std::string and call it a day for any char related functions. ( .c_str() will solve your problems with them).

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

David Sopala
Member #5,056
September 2004

Normally I use char arrays because they seem more natural to me I still use the strcat cmp cpy etc commands I have no idea why they seem more natural, but thats a side point from the issue.

As for isolating the problem I have two global functions. One to redraw the map play npcs monsters etc and one for inventory management(the one I am working on).

If I try to masked_blit into the first function it works fine.

If I try to use it in the inventory function it dies horribly.

Thanks for the memory leak I will go fix that right now.

piccolo
Member #3,163
January 2003
avatar

check your arrays. It sounds like your going out bounce.

wow
-------------------------------
i am who you are not am i

David Sopala
Member #5,056
September 2004

I would expect to see a memory referance to the crash not that the program has stopped responding on an array issue note that when I load and allocate my arrays I save the variable - for how many things are in the array and use that to compare in all the redraws and access to said array.

UPDATE:

I am very confused now - I have my code working - in part now for an allegro specific question.

Would having SAMPLE * and BITMAP * in the same class give issues where calls to that class trying to get a pointer will crash it?

I commented out all my SAMPLE * code (since I haven't implemented sound yet) and the BITMAP * Code works 100% if I put the SAMPLE * back in there it will crash when returning BITMAP * ideas?

Dario ff
Member #10,065
August 2008
avatar

Are you using an IDE? Which one? You're not using Dev-C++ by any chance are you?

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

David Sopala
Member #5,056
September 2004

Yes, bloodshed why?

Dario ff
Member #10,065
August 2008
avatar

Did you try a full recompile when modifying the headers? Dev-C++ is kind of stupid when it comes to compiling with F9 when you modify a header. You see, you need to compile each source that includes the header for making the changes effective.

Dev-C++ won't recompile the sources needed automatically unlike all other standard IDEs. The only way is to hunt them yourself and delete the objects, or do a full recompile and call it a day.

Try a full recompile. CTRL+F11

EDIT: Also, I'd suggest switching to a much better IDE. Code::Blocks(Works with MinGW) and Microsoft Visual C++ Express are 2 good options. AFAIK, C::B can import your old Dev-C++ project.

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

David Sopala
Member #5,056
September 2004

Dev++ normal stuff to compile

Clean - because I can't trust it
Rebuild All

Edit: I never use f9

Dario ff
Member #10,065
August 2008
avatar

Ah, that's OK. You see, the situation you described about adding the SAMPLE in the class was that led me to believe that.

Then I don't really have a clue what's the problem.

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

David Sopala
Member #5,056
September 2004

Installed codeblocks;
uncommented SAMPLE* lines;
Compiled with codeblocks;
Code works - wtf does dev-C++ do or not do correctly in this situation?

LennyLen
Member #5,313
December 2004
avatar

wtf does dev-C++ do or not do correctly in this situation?

Who knows. Dev-C++ was outdated years ago, and nobody should use it any more.

Go to: