- Online Community Forums » Programming Questions » Help with a small function

This thread is locked; no one can reply to it. rss feed Print
Help with a small function
Member #5,301
December 2004

I use this to load more then one sprite from a data file. I am trying to lose about 15-20 lines of code by making this a function.

temp = (BITMAP *)data[EXPLODE_BMP].dat;
     for (n = 0;n<6;n++)
         explode[n] = grabframe(temp,17,17,0,0,1,n);

I just have no idea where to start. I just want it to be universal like

void sprite_load(Bitmap in datafile,int framex,int framey,int frames,bitmap)
temp = Bitmap in datafile;
for (n = 0;n < frames; n++)
bitmap[n] to load = grabframe(temp,framex,framey,0,0,1,n);

can anyone help on this I am lost. More so on the
temp = (BITMAP *)data[EXPLODE_BMP].dat;
and the

-I edit a lot. Prepare thyself.

Kris Asick
Member #1,424
July 2001

I think an easier thing to do in your case would be to load the datafile, keep it in memory, then simply create an array of pointers for your sprites to point to the objects in the datafile. For instance:

1#include "gamedata.h" // Contains datafile constants
3#define MAX_FPAS 16 // Max Frames per Animated Sprite
6 public:
7 BITMAP *sprite[MAX_FPAS];
8 int numframes;
9 void AddFrame(BITMAP *bitmap);
12DATAFILE *data;
13T_ANIMSPRITE sprite_explode;
15void T_ANIMSPRITE::AddFrame(BITMAP *bitmap)
17 sprite[numframes] = bitmap;
18 numframes++;
21int main (void)
23 int z;
25 // Initialization Code Goes Here
27 data = load_datafile("GAMEDATA.DAT");
28 for (z = 0; z < 12; z++) // There are 12 frames in the explosion.
29 sprite_explode.AddFrame((BITMAP*)data[EXPLODEFRAME_00+z].dat);
31 // Cleanup Code Goes Here
33 return 0;

Totally untested and written just now as C++ code. (IE: Useless if you only use C.) Gives you a basic idea of how to do it, right?

The only reason you would need to do it the way you're thinking is if all your frames are located on just ONE bitmap, instead of each frame being its own bitmap. Then what you could do is add some coordinate picking to the routines above and create sub_bitmaps off of the bitmaps in the datafile so as not to waste memory.

Why load, copy and use when you can just load and use? ;)

Once its loaded, to actually play the animation back you'll need to write a routine to fit your game engine so that it can loop through the frames and display the correct one. This process will completely depend on what kind of game engine you're going to make so it's hard to give you an example.

Just try what feels right and you'll get it.

--- Kris Asick (Gemini)

--- Kris Asick (Gemini)

Member #5,301
December 2004

Well I get why I couldn't figure that out. I need to learn more about what

:: does and the + in EXPLODEFRAME_00+z should start looking up class stuff as well...I guess I should get started on learning more on C++ instead of getting more into Allegro.

Thanks for the help I will test it out as soon as I understand how to apply it.


Why load, copy and use when you can just load and use?

I can do that with an animation?

-I edit a lot. Prepare thyself.

Member #5,313
December 2004


I guess I should get started on learning more on C++ instead of getting more into Allegro.

A knowledge of C++ isn't required to use allegro, though it can certainly help in some cases.


I need to learn more about what :: does

That's the scope operator. It is used in this case to show what class a function belongs to.



That's the same as the + in 1 + 1. EXPLODEFRAME_00 is just a macro definition for an integer value (the index from the .dat file), eg 17. Using data[17].dat would point to the same file in the data file. The next file in the .dat is going to be data[18].dat, which is the same as data[17 + 1].dat, which is the same as data[EXPLODEFRAME_00 + 1].dat.


I can do that with an animation?

Kris Aswick's method assumes that there is a seperate bitmap for every frame of the animation, which is not how you have done things.

A better way for you to do things using one bitmap with all the frames is to use create_sub_bitmap().

Kris Asick
Member #1,424
July 2001

You don't HAVE to learn C++. I just prefer it because I'm used to OOP. (Object Oriented Programming.) Heck, some of the greatest programmers out there swear that C++ is the worst implementation of it possible and stay clear of it like the plague!

Not me though.

But yeah, once something's loaded in memory in C or C++, you can just reference it with pointers instead of creating more and more memory for all of it to go into. For instance, if you wanted four extra bitmaps to all be the exact same as one you create:

BITMAP *primary_bitmap;
BITMAP *bitmap_pointer;
BITMAP *bparray[3];

primary_bitmap = create_bitmap(64,64);
bitmap_pointer = primary_bitmap;
bparray[0] = primary_bitmap;
bparray[1] = primary_bitmap;
bparray[2] = primary_bitmap;

No matter which of those you access, you will get the bitmap you created. Which means if you MODIFY that bitmap, ALL of them are modified, and if you remove it from memory, all the other pointers now reference an area of memory that's no longer used and should be manually set back to NULL. For example:

// Crash your program real FAST with this code!

BITMAP *my_bitmap;
BITMAP *draw_bitmap;

my_bitmap = create_bitmap(64,64);
draw_bitmap = my_bitmap;

No matter which bitmap you destroy in the above example, if you try to draw either of them afterwards you will end up crashing the program!

The trick is to realize that a variable and the contents of a variable are two completely different things in C. A pointer is simply the memory address of something in memory, like a bitmap or a sound effect or even an array.

So really, for what you want to do, you don't need to go learning C++. Just learn pointers and the rest should be obvious!

--- Kris Asick (Gemini)

--- Kris Asick (Gemini)

Go to: