- Online Community Forums » Programming Questions » RLE_SPRITE and class

This thread is locked; no one can reply to it. rss feed Print
RLE_SPRITE and class
Ilyas Salman
Member #6,762
January 2006

I declare a RLE_SPRITE, when using it in the Draw function, the comiler says it's undefined:

1#include <allegro.h>
2class AI
5 int x, y, chips, ice, water, cola, chance;
6 AI()
7 {
8 x=0,y=300;
9 BITMAP* ab,* bb,* cb;
10 ab=load_bitmap("man1_1.bmp",NULL);
11 bb=load_bitmap("man1_2.bmp",NULL);
12 cb=load_bitmap("man1_3.bmp",NULL);
13 RLE_SPRITE *a,*b,*c;
14 a=get_rle_sprite(ab);
15 b=get_rle_sprite(bb);
16 c=get_rle_sprite(cb);
17 chance=rand()%4;
18 if(chance==1)
19 {
20 }
21 }
22 void Draw(BITMAP* buffer)
23 {
24 draw_rle_sprite(buffer,a,50,50);
25 }

WHy doesn't the compiler see the RLE_SPRITE?

Member #1,090
July 2003

Ilyas Salman said:

WHy doesn't the compiler see the RLE_SPRITE?

It isn't available in your "Draw" method, because it is local to your constructor. You are also creating memory leaks in there.
For a quick fix make "BITMAP* ab,* bb,* cb;" and "RLE_SPRITE *a,*b,*c;" private vars of your class and make sure you use error handling (loading the bitmaps or getting the RLE sprites can fail), also add a destructor in which you free the bitmaps and sprites, using "destroy_bitmap" and "destroy_rle_sprite".

It is bad design though to have the object load any bitmaps in case there will be more than one instance of it in your application, since that would have each instance unnecessarily keep a full copy of the same bitmaps in memory, which is quite wasteful. It would be better to load the bitmaps(and RLE sprites, if you really want to use them) just once to somewhere else and just give your objects a pointer to them.

Go to: