Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Basic Game Structure (allegro5)

This thread is locked; no one can reply to it. rss feed Print
Basic Game Structure (allegro5)
ButterQuark
Member #12,916
June 2011

I, along with a group of my friends, are attempting to start making a game. We plan on taking it very slowly and planning very thoroughly. I'm looking for advice for basic game structure.

We want to make a 2D sidescroller. We have a lot of cool ideas for gameplay, but none of us have coded a game before. I found Allegro5 and thought this would simplify a lot of what we want to do.

Does anyone have a skeleton project for displaying sprites in an environment? and maybe simple animation? Or maybe there is an engine that works with Allegro5 or replaces it that does all that. I'm having a hard time starting up Allegro5 just because its so new, but I don't want to roll back to allegro4 since its, well, old.

Any advice would be good, or a really simple game that someone has programed in Allegro5 that i can pick a part to learn more about how it works.

Thanks for all your help,
ButterQuark

jmasterx
Member #11,410
October 2009

Not to be pessimistic, but if you have little to no experience coding in c or c++, you might find coding a whole game with low level routines a bit difficult to orchestrate. Have you looked into XNA? XNA is a framework that would probably make it much easier. You also do not have to manage memory nor deal with pointers.

If however you really would like to do it in c / c++, I'd advise against your first game project to be a large elaborate one. Try making Pong, then Tetris, and build your way up to more complex projects.

The thing about a sidescroller is you first have to decide: tile based /sprite based, both, etc. Tile based is easiest and generally does a pretty good job.

If you delve into it without prior coding experience, you might end up with a working product, but the code might not look too pretty since you'll be learning at the same time and then you'll hack in new ideas and whatnot.

But regardless of how you choose to do it, good luck and have fun :)

ButterQuark
Member #12,916
June 2011

Thanks for the reply. We do have quite a bit of coding experience in C++. This is the language we really know and understand.
Are there prewritten low level sprite animations and the like for Allegro5?

EDIT:
By going through the Tutorial, I already got a playable one player pong game (ball bounces off the walls, move paddle to keep it in play). This took me about an hour.

jmasterx
Member #11,410
October 2009

Allegro 5 really focuses on doing the low level things well so there is no built in functionality to do sprite animation, but to do your own shouldn't be too hard:

for reasons of efficiency, you should use a tile sheet (1 big bitmap with several tiles) then you'll want to draw sub bitmaps of that bitmap, this avoids the graphics card reloading textures into memory which is slow,

after that just run through the sub bitmaps, Allegro can selectively draw only a portion of a bitmap so using this would be useful for animation.

The API docs are here if you were unsure where to find them:
http://www.liballeg.org/a5docs/refman/index.html

http://www.liballeg.org/a5docs/refman/graphics.html#al_create_sub_bitmap

In terms of displaying sprites in an environment, create an Entity base class and then create an IRenderable interface with a pure virtual render method, and keep track of your renderable entities, then draw them to the screen at their layer. You may also instead want to decouple rendering in which case you might want to store a tile id in your entity and have it interpreted by a renderer. Then you just change the tile id each frame to get animation.

ButterQuark
Member #12,916
June 2011

Thanks! I had found the API already, but I think I missed a huge chunk of the bitmap functions.

jmasterx
Member #11,410
October 2009

See my revision

Also, for the terrain, if it is tile based, Allegro has kind of camera functions. You can transform the view matrix easily, so you can offset everything by your camera position, this way when you draw you dont have to do -camer.x, -camera.y.

You could also draw your level by having a 2D array of int representing each tile.

Just putting some ideas out there :p

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Animation is really pretty simple, and you don't need much of a framework to do it with. All you need is a structure to keep track of time passed and the current frame of the animation :

#SelectExpand
1class Animation { 2private : 3 ALLEGRO_BITMAP** frames; 4 double duration; 5 double time_passed; 6 int frame_number; 7 int num_frames; 8public : 9 Animation() : 10 frames(0), 11 duration(0.0), 12 time_passed(0.0), 13 frame_number(0), 14 num_frames(0) 15 {} 16 Animation(int framecount , double time_length) : 17 frames(0), 18 duration(0.0), 19 time_passed(0.0), 20 frame_number(0), 21 num_frames(0) 22 { 23 Reallocate(framecount , time_length); 24 } 25 ~Animation() {Free();} 26 void Free() { 27 if (frames) {delete [] frames;frames = 0;} 28 num_frames = 0; 29 } 30 bool Reallocate(int framecount , double time_length) { 31 if (framecount < 1) {return false;} 32 if (time_length <= 0.0) {return false;} 33 Free(); 34 duration = time_length; 35 time_passed = 0.0; 36 frame_number = 0; 37 try { 38 frames = new ALLEGRO_BITMAP*[framecount]; 39 } 40 catch { 41 frames = 0; 42 return false; 43 } 44 num_frames = framecount; 45 } 46 47 void SetBitmap(int index , ALLEGRO_BITMAP* bmp) { 48 assert((index >= 0) && (index < num_frames)); 49 frames[index] = bmp; 50 } 51 52 void AdvanceFrameTime(double delta_time) { 53 SetFrameTime(time_passed + delta_time); 54 } 55 void SetFrameTime(double frame_time) { 56 // simple forward repeating animation 57 time_passed = frame_time; 58 while (time_passed >= duration) {time_passed -= duration;} 59 while (time_passed < 0.0) {time_passed += duration;} 60 frame_num = (int)(time_passed / duration); 61 // shouldn't be necessary, but just in case 62 if (frame_num < 0) {frame_num = 0;} 63 if (frame_num >= num_frames) {frame_num = num_frames - 1;} 64 } 65 66 void Draw(int x , int y) { 67 al_draw_bitmap(frames[frame_num] , x , y , 0); 68 } 69 70};

This took about 10-15 minutes to write, but only includes forward repeating animations and no way to tell when an animation is finished. Each character would then hold several animations and switch between them as necessary. During your logic processing you call AdvanceFrameTime with the amount of time passed, and then call Draw during your drawing phase.

blargmob
Member #8,356
February 2007
avatar

Fuuuuu use a component based entity approach...

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

ButterQuark
Member #12,916
June 2011

Thanks for the ideas. I've got a bit more free time this week, so I'm gonna try and crank something out. I'll be back if I get stuck again.

Go to: