My vector has lost it's Bitmaps!
Taiko Keiji

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.

X-G

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.

Archon

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

Taiko Keiji

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.

Indeterminatus
Quote:

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

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

X-G

Then show the Sprite class.

GullRaDriel

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

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

Taiko Keiji

What do you mean I isn't initialized?

X-G

... 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.

Taiko Keiji

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

HoHo

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.

Onewing
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.

Taiko Keiji

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.

Tobias Dammers
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...

BAF
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).

Thread #590617. Printed from Allegro.cc