Archived

This topic is now archived and is closed to further replies.

Destroyer

The odd C++ error with my classes... !

Recommended Posts

Well I''m programming my own sprite class and here it is...
    
class Sprite
{

public:

	int x;		// x pos

	int y;		// y pos

	int xv;		// x velocity

	int yv;		// y velocity

	int width;	//width

	int height;	//height


	//Function Declarations


	Sprite();
	Sprite(int xx, int yy, int wwidth, int hheight, int mode); // mode - set to DDSCAPS_VIDEOMEMORY or DDSCAPS_SYSTEMMEMORY

	~Sprite();
	Sprite& Sprite::operator=(Sprite spritecopy);
    HRESULT Load_Sprite_Bitmap(char* bmpName);          
	HRESULT Draw_Sprite(LPDIRECTDRAWSURFACE7 dest);
	//HRESULT Draw_Scaled_Sprite(Sprite sprite, int swidth, int sheight,LPDIRECTDRAWSURFACE7 dest);

	HRESULT Load_Frame_Sprite(int frame, int cx, int cy, int mode);                   // cx,cy are absolute coords

	HRESULT Animate_Sprite();
	HRESULT Move_Sprite();
	HRESULT Load_Animation_Sprite(int anim_index, int num_frames, int *sequence);
	//HRESULT Set_Anim_Speed_Sprite(Sprite sprite,int speed);

	HRESULT Set_Animation_Sprite(int anim_index);
	//HRESULT Hide_Sprite(Sprite sprite);

	//HRESULT Show_Sprite(Sprite sprite);

	//bool Collision_Sprites(Sprite sprite1, Sprite sprite2);


private:

    int curr_frame;     // current animation frame


    int num_frames;     // total number of animation frames

    int curr_animation; // index of current animation

    int anim_counter;   // used to time animation transitions

    int anim_index;     // animation element index

    int anim_count_max; // number of cycles before animation

    int *animations[NUM_SPRITE_FRAMES]; // animation sequences


	RECT images[NUM_SPRITE_FRAMES];	//hold the blit info (the frames)

	DDSURFACEDESC2 s_ddsd;
	LPDIRECTDRAWSURFACE7 lpddssprite;
};
  
Now here''s the definition of the function taht seems to be causing some errors ...
  
HRESULT Load_Frame_Sprite(int frame, int cx, int cy, int mode)
{
	cx = cx * (Sprite::width + 1) + 1; // !!!

	cy = cy * (Sprite::height + 1) + 1;

	RECT temprect;
	temprect.top = cy;
	temprect.left = cx;
	temprect.bottom = cy + height; // - 1 ?

	temprect.right = cx + width; // - 1 ?


	Sprite::images[frame] = temprect;

	return g_ddrval;
}
  
It shows these types of errors... error C2597: illegal reference to data member ''Sprite::width'' in a static member function error C2597: illegal reference to data member ''Sprite::height'' in a static member function c:\eric\space\ericddlib.cpp(228) : error C2597: illegal reference to data member ''Sprite::images'' in a static member function And it goes on like this for about 40 times... :0 Any ideas? Thanks, Destroyer

Share this post


Link to post
Share on other sites
Well, that''s your problem! That as in this:
Sprite::width

You do not access member variables through the scope resolution operator.

But wait, that''s the same function that you have in the class. The first thing you''ve gotten wrong is that Load_Frame_Sprite is a member function of the class Sprite.

Thus in its definition it should be:
HRESULT Sprite::Load_Frame_Sprite(int frame, int cx, int cy, int mode)

Note the correct use of the :: operator.


To access the member variable Width and Height, you do not use any operator.

cx = cx * (width + 1) + 1; // !!!
cy = cy * (height + 1) + 1;

Your problem would be corrected.

Share this post


Link to post
Share on other sites
You shouldn''t need to use the Sprite:: scope when referencing the width and height member variables since both are members of the same class as LoadFrame. Have you tried doing away with these?

cx = cx * (width + 1) + 1; // !!!
cy = cy * (height + 1) + 1;

Are you loading your frames of animation from one large bitmap made up of individual frames?

Share this post


Link to post
Share on other sites
Yes - I''m just loading one big bitmap and reading in the rects so I can blit em

thanks for the C++ help ! :D

Sincerely,

Destroyer

Share this post


Link to post
Share on other sites