Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [OpenLayer] Bitmaps and Iterators

Credits go to CGamesPlay and ImLeftFooted for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
[OpenLayer] Bitmaps and Iterators
ImLeftFooted
Member #3,935
October 2003
avatar

Which is why strict typing is a wonderful language feature.

Matthew Dalrymple
Member #7,922
October 2006
avatar

DD said:

Which is why strict typing is a wonderful language feature.

Can you point me in a direction so I can learn more about this? I Google'd and Wikipedia'd this subject with no specific results. I don't see how this subject directly effects my current project, are you referring to the use of templates in the STL?

EDIT:

I think I might have narrowed down the problem. I am now trying to use an iterator just to draw AvatarList to the screen but that isn't working. So my problem is probably how I'm loading the avatars. Here's my loading code. Is the tempAvatar being erased after it leaves the scope of the do-while loop? Should AvatarList maybe be a vector of Avatar objects and not a vector of pointers to Avatar objects?

Anyway I'll be trying this out while I post the code here:

1bool Game::LoadAvatars()
2{
3 Log::log("[LIST OF AVATARS]");
4 string AvatarDirectory("images/avatars/");
5
6 // The allegro file info structure
7 al_ffblk info;
8
9 // Checking to see if there are PNG files in there
10 if(al_findfirst("images/avatars/*.png", &info, FA_ALL) != 0)
11 {
12 Log::warning("No avatars were found");
13 return false;
14 }
15 // While there are PNG files, lets grab them and load them
16 do
17 {
18 Log::log("%s", info.name);
19 string path = AvatarDirectory + info.name;
20 
21 /* PROBABLE PROBLEM AREA */
22 
23 Avatar tempAvatar(path);
24 tempAvatar.SetName(info.name);
25
26 this->AvatarList.push_back(&tempAvatar);
27 }
28 while (al_findnext(&info) == 0);
29
30 // Close the file search
31 al_findclose(&info);
32
33 Log::log("[/LIST OF AVATARS]");
34 
35 return true;
36}

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

ImLeftFooted
Member #3,935
October 2003
avatar

  do
  {
    Log::log("%s", info.name);
    string path = AvatarDirectory + info.name;

    /* PROBABLE PROBLEM AREA */

    Avatar tempAvatar(path);
    tempAvatar.SetName(info.name);
    
    this->AvatarList.push_back(&tempAvatar);
  } // tempAvater's destructor is called here

Corrected (don't forget to call delete later!):

  do
  {
    Log::log("%s", info.name);
    string path = AvatarDirectory + info.name;

    /* PROBABLE PROBLEM AREA */

    Avatar *tempAvatar = new Avatar(path);
    tempAvatar->SetName(info.name);
    
    this->AvatarList.push_back(tempAvatar);
  }

Matthew Dalrymple
Member #7,922
October 2006
avatar

By calling delete later you mean after I push it onto AvatarList correct? Bah things still aren't drawing and the program is crashing when I try and end it by pressing escape.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

ImLeftFooted
Member #3,935
October 2003
avatar

No when you're done with this->AvatarList. For example in the destructor. But do it wherever is appropriate for you.

When you give up ownership of your memory is up to you.

Matthew Dalrymple
Member #7,922
October 2006
avatar

Yes but tempAvatar is only in the scope of that do-while loop. Maybe I'm missing something here.

Oh and the crashing error I was having was caused by a new not fully implemented feature of my logging system.

Ok well things are totally drawing now, as I'm writing this I just removed the delete tempAvatar line and yeah.. shh <sarcasm> there probably isn't a memory leak now </sarcasm>

Now I'll go around deleting their allocated memory in the game's de-constructor. I'll get back in a little bit with an update.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

ImLeftFooted
Member #3,935
October 2003
avatar

I believe your ownership of tempAvatar extends beyond the do loop. Do you ever access the pointer again?

Matthew Dalrymple
Member #7,922
October 2006
avatar

I don't ever access tempAvatar but I do access AvatarList.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

CGamesPlay
Member #2,559
July 2002
avatar

Don't forget how pointers work! When you copy a pointer, you don't copy the data it points to, you copy the directions for how to get to the data. Since you made the data yourself (using new), you need to get rid of it yourself, using delete. When do you do that? When you don't need the data any more. Whenever you would erase the avatar from the avatar list, instead of just deleting it, delete the data, then remove it from the list.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Matthew Dalrymple
Member #7,922
October 2006
avatar

Well what I was getting at was specifically having to delete tempAvatar, as tempAvatar was only used to load objects into AvatarList. This was done in the constructor of my Game class... So in the de-constructor I did this:

vector< Avatar* >::iterator curAvatar;
for(curAvatar = CurrentList.begin(); curAvatar != CurrentList.end(); curAvatar++)
  delete *curAvatar;

I believe that is correct. What I was trying to get at with my questions was more specifically was if you guys were telling me to delete tempAvatar, which I thought you were which was confusing me. I keep poking at this because I'm a bit on the unsure side and I hate being unsure and still delving into things.

Things are working now and everything is drawing and logging correctly. But because I am ME, this has shown me new errors. Haha now the images aren't randomly drawing to the screen... they are drawing at the same random spots every time I run the program. I'm not even sure all 15 are being drawn.

I might also be having a PWD problem to, as when I compile + run in Dev-C++ I think all the *.png avatars show up... but when I double click on the executable only two show up.

I'll try and solve those problems on my own first of course as programming is about solving problems :P. I might just add on to this thread if I need more help as I don't like to create lots of threads.

EDIT: Haha I'm totally dumb. I forgot I was using the constructor that takes a path to load a file. So when I'm logging stuff in the other constructors I was freaking out on why they weren't getting called... That fixed the X and Y random coord thing I was working on.

EDIT 2: Well imma just give props as the problem is fixed and now I can focus on the logic of my game and have a early version of it up in a day or two on the depot.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

 1   2 


Go to: