Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » My vector has lost it's Bitmaps!

This thread is locked; no one can reply to it. rss feed Print
My vector has lost it's Bitmaps!
Taiko Keiji
Member #8,307
February 2007
avatar

I have a small question about vectors. I recently started using them but i'm having some trouble. I have a Sprite class and I made a vector of these sprites

vector<Sprite> Sprites;

but that's not the problem, the vector works fine. It's when I make a new sprite and add it into the vector for some reason the Bitmap data from the sprite is lost, or something.

I was using a for loop to draw the sprites

for(int i;i<Sprites.size();i++)
        {            
            masked_blit(Sprites.at(i).image,buffer,spos,sypos,x,y,16,20);
        }

but like I said. when I do this it doesn't draw the sprite.
I also tried loading the sprite into a temporary sprite.

for(int i;i<Sprites.size();i++)
        {
            pSprite=Sprites.at(i);
            masked_blit(pSprite.image,buffer,spos,sypos,x,y,16,20);
        }

but that doesn't work either.

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

X-G
Member #856
December 2000
avatar

Let me guess; your Sprite class does destroy_bitmap in its destructor. :P

Either don't do that, or implement complete copy semantics, or store pointers to sprites instead of sprites.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Archon
Member #4,195
January 2004
avatar

If the bitmap was messed up, wouldn't it crash the program?

Taiko Keiji
Member #8,307
February 2007
avatar

Ok, first. My destructor doesn't destroy anything. It may just be that I'm having the program draw the bitmap wrong or something because if it didn't load the bitmap i would expect it to crash aswell. I have no idea other than that.

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

Indeterminatus
Member #737
November 2000
avatar

Quote:

for(int i;i<Sprites.size();i++)

You're using variable i that is not initialized. Bad thing!

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

X-G
Member #856
December 2000
avatar

Then show the Sprite class.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

GullRaDriel
Member #3,861
September 2003
avatar

I think Indeterminatus is somewhat right. You should use int for( int i=0;

And X-G is right too. Show us the class.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Taiko Keiji
Member #8,307
February 2007
avatar

What do you mean I isn't initialized?

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

X-G
Member #856
December 2000
avatar

... exactly what he said. You declare int i in the for loop, but you never initialize it to 0. Also, you're not listening, because you didn't post your Sprite class.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Taiko Keiji
Member #8,307
February 2007
avatar

I initialized i, don't know how I forgot.
Heres my sprite class if you dearly want to see it.

1 
2#ifndef _SPRITECLASS_H_
3#define _SPRITECLASS_H_
4 
5#include <allegro.h>
6#include <vector>
7#include "globals.h"
8 
9//---------------------The Class itself----------------------------------------
10 
11class Sprite
12{
13 protected:
14 int Frames,
15 Dir,
16 CurFrame,
17 HP,
18 MaxHP,
19 MP,
20 MaxMP,
21 Special,
22 MaxSpecial,
23 xPos,
24 yPos,
25 xscreen,
26 yscreen,
27 attrad,
28 magrad,
29 aggression,
30 defense,
31 magdef,
32 exp,
33 maxExp;
34 BITMAP* image;
35 
36 public:
37 Sprite();
38 Sprite(BITMAP* Image);
39 Sprite(BITMAP* Image,int XPos,int YPos,int frames);
40 Sprite(BITMAP* Image,int Hp,int Mp,int special,int XPos,int YPos, int AttRad,int MagRad,int Agg,
41 int Def,int MagDef,int frames);
42 Sprite(BITMAP* Image,int Hp,int Mp,int special,int XPos,int YPos, int AttRad,int MagRad,int Agg,
43 int Def,int MagDef,int experience,int maxexperience,int frames);
44 virtual ~Sprite();
45 void level_up();
46 void edit_hp(int amt);
47 void edit_mp(int amt);
48 void edit_mhp(int amt);
49 void edit_mmp(int amt);
50 void edit_special(int amt);
51 void edit_mspecial(int amt);
52 void edit_xpos(int amt);
53 void edit_ypos(int amt);
54 void edit_xscreen(int amt);
55 void edit_yscreen(int amt);
56
57 int GetFrames() {return Frames;};
58 int GetDir() {return Dir;};
59 int GetCurFrame() {return CurFrame;};
60 int GetHP() {return HP;};
61 int GetMaxHP() {return MaxHP;};
62 int GetMP() {return MP;};
63 int GetMaxMP() {return MaxMP;};
64 int GetSpecial() {return Special;};
65 int GetMaxSpecial() {return MaxSpecial;};
66 int GetxPos() {return xPos;};
67 int GetyPos() {return yPos;};
68 int Getxscreen() {return xscreen;};
69 int Getyscreen() {return yscreen;};
70 int Getattrad() {return attrad;};
71 int Getmagrad() {return magrad;};
72 int Getaggression() {return aggression;};
73 int Getdefense() {return defense;};
74 int Getmagdef() {return magdef;};
75 int Getexp() {return exp;};
76 int GetmaxExp() {return maxExp;};
77 BITMAP* GetImage() {return image;};
78};
79 
80 
81void add_sprite(Sprite* sname);
82 
83#endif

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

HoHo
Member #4,534
April 2004
avatar

Actually the contents of ~Sprite() and Sprite() are the things that matter anything. Function names don't say much of what is going on in the functions themselves.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Onewing
Member #6,152
August 2005
avatar

Quote:

I initialized i, don't know how I forgot.

for(int i;i<Sprites.size();i++)
What do you think i starts at in the above code? Maybe it's 32423, or maybe 5436345, it. Is. Not. Initialized.

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Taiko Keiji
Member #8,307
February 2007
avatar

I got it figured out. Thanks for trying guys. I cant particularly say what it was but I think it has to do with the fact that I didn't initialize i.

Life could be better, machines could program themselves.

Taiko Keiji-
http://lostotaku.net

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

I got it figured out. (...) I cant particularly say what it was but I think it has to do with the fact that I didn't initialize i.

That is a rather nasty contradiction you have there...

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

BAF
Member #2,981
December 2002
avatar

Quote:

for(int i;i<Sprites.size();i++)

Shouldn't that be:

for(Vector<Sprite>::iterator = Sprites.begin(); i != Sprites.end(); i++)

or something like that? (Not sure if that begin/end is correct because I haven't used C++ STL in a while, I'm still in C#/.net generic mode).

Go to: