Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Newbie Allegro programming

This thread is locked; no one can reply to it. rss feed Print
Newbie Allegro programming
dsds dsds
Member #13,430
August 2011

hi everyone! i'm sorry for my bad english but i'm not a master in this language xD
however i hope you understand me.
i'm a newbie in Allegro programming.
i know C ,in detail i know how loops,pointers,arrays and matrices work.
i studied also stack,hash-table,tree and list.
I don't know C++ yet.I saw many games done with allegro and C/C++ and i decided to use this lib.
I would create a platform game but i don't find any type of tutorial about it.I see many sourcecode of platform done with allegro on youtube and i observe that to create the map of the game are used classes...is it possible to create a map (in C) without classes? maybe only with the coordinates of the ostacles?
I also try to study a sourcecode of a platform game but is too difficult for me...and also it was written in C++ that i don't understand.
can you advise me some platform-tutorial? or a good book?

thx all! ;)

Audric
Member #907
January 2001

See the allegro demo game (the small shoot-them-up). It should already give you a good example of a game loop, and it will show you which allegro4 functions a game usually uses.

dsds dsds said:

is it possible to create a map (in C) without classes

Of course. Instead of a 2D array of classes, you'll use a 2D array of struct, etc.

About platform games examples/tutorials, I don't have specific example in mind. I checked on the depot, here's list of platform games with source code available:
http://www.allegro.cc/depot/action,platform/listing/?o=Title&players=&online=&source=Yes&os=

Of these, I checked Alex4 as the game and its author are pretty famous, and the source code seems rather pretty :
http://www.allegro.cc/depot/AlextheAllegator4

type568
Member #8,381
March 2007
avatar

You could find some examples here. Compiling them(and modifying in order to understand them better) could be a good start.

Good luck.

dsds dsds
Member #13,430
August 2011

i see many of these games but they are too complex for me :-/
i need a tutorial to begin becouse these games are split in more files and i'm not able to follow them.
how can i use a structure to make a map? can you write me a small code?

thx all

Neil Walker
Member #210
April 2000
avatar

Perhaps this is a good case for writing a book before that dreadful Game Programming All In One is updated ;)

Or hey, why not email the authors asking if they want it updated and donate 10% of commission to Matthew's hosting bill.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Audric
Member #907
January 2001

You can look at the Mappy Tile Map Editor :
Under "Playback libraries", you have one for allegro, it contains a few examples written in C for Allegro 4 that display tile maps and use the information "this tile is flagged 'solid'" to handle collision.

For designing levels, it's pretty handy to have an already-made editor that supports cut and paste, undo, etc...

The library is designed so you don't have to understand how it is implemented, but you can still look in mappyal.c : for example MapDrawBG() is a classic loop on x and y, and for each cell it calls one of the allegro functions blit() or masked_blit().

Just one critic about these examples: They were written long ago, so they attempt to open tiny modes like 320x240 in 16bpp. If they don't run at all on your machine, first try to modify them to call set_color_depth(32) right before they call set_gfx_mode().

J-Gamer
Member #12,491
January 2011
avatar

Better yet: set_color_depth(desktop_color_depth())

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

dsds dsds
Member #13,430
August 2011

can you write for me a small code of a tilemap?

taron 
Member #10,584
January 2009
avatar

The most simple tilemap I can think of.
This code is very simple, too simple.
It's also untested but I don't think I made a big mistake anywhere.

int map[4][4] = {{1,1,1,1},{1,0,0,1},{1,0,0,1},{1,1,1,1}}
int i;
int t;
for (i = 0; i < 4; i++){
   for (t = 0; t < 4; t++){
       if (map[i][t] == 1){
           // Assuming you're using allegro 4
           rectfill(buffer, t * 32, i * 32, t * 32 + 32, i * 32 + 32, makecol(255,255,255));
       }
    }
}

dsds dsds
Member #13,430
August 2011

edit:

ok it works so with this technique i can draw a map?
and i can controll olso collision?

thx

taron 
Member #10,584
January 2009
avatar

Well replace rectfill with al_draw_filled_rectangle

al_draw_filled_rectangle(t * 32, i *32, t * 32 +32, i *32 + 32, al_map_rgb(255,255,255), 1);

I assume you know how to get a basic game loop running (if you don't, there should be an allegro 5 tutorials about this, this one should be good http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial ).
Also, if you are going to use this function, make sure that you call al_init_primitives_addon()
And for a better effect, draw images, not simple rectangles.

dsds dsds
Member #13,430
August 2011

thx taron

another question:
i have to make another matrix for the objects of the map? and controll the collision with this matrix?
can you explain me (simply) how can i create a collision engine?

taron 
Member #10,584
January 2009
avatar

You don't have to create another matrix for objects on the map. I'd call it a 2d-array though, in programming it's mostly called a 2d array unless you actually use matrix operations on it.
If you want your objects to be tile-based, you can create another 2d array.
For things like enemies, you best store them in arrays.

In order to make objects collide with the tiles on the map you'll have to use simple maths.
I'll explain how you can use (axis-aligned) bounding boxes for collision.

I'll create a simple player struct for this.

I did not test this, but this is the basic idea, it's very simple, this way however your sprites can not be larger than 2 tiles wide or 2 tiles high, if they are, the collision (and response) might not work properly. If something does not work I think you can fix it, the collision detection and movement are also 'slow', not optimized at all

#SelectExpand
1typedef struct PLAYER{ 2 int x; 3 int y; 4 int width; // For your bounding box 5 int height; 6 int gravity; 7 // Add other data here, like for example the state the player is in (on ground, dead, whatever) 8 // Perhaps a bitmap to represent the player 9}; 10 11void create_player(PLAYER *player, int x, int y, int width, int height){ 12 // * means pointer 13 // This variable does not actually contain the value of PLAYER 14 // But it contains the address to a PLAYER 15 player->x = x; 16 player->y = y; 17 player->width = width; 18 player->height = height; 19} 20 21void move_player(PLAYER *player, int map[4][4], int speed_x, int speed_y){ 22 int left; // Bounding box 23 int right; 24 int top = player->y / 32; // Divided by the tile height 25 // If your tiles are not 32x32 change this 26 int bottom = (player->y + height) / 32; // On a screen, the top left is 0x0, not the bottom left 27 28 // Divide pixel coordinates by the height and width of the tiles to get the tile coordinates. 29 while (speed_x > 0){ // moving to the right 30 speed_x--; 31 player->x++; 32 right = (player->x + player->width) / 32; 33 if (map[top][right] || (map[bottom][right]){ // if this cell is not zero, the player will have collided 34 player->x--; // collision response 35 break; // jump out the while loop 36 } 37 } 38 while (speed_x < 0){ 39 speed_x++; 40 player->x--; 41 left = player->x / 32; 42 if (map[top][left] || (map[bottom][left]){ 43 player->x++; 44 break; 45 } 46 } 47 right = (player->x + player->width) / 32; 48 left = player->x / 32; 49 while (speed_y > 0){ 50 speed_y--; 51 player->y++; 52 bottom = (player->y + height) / 32; 53 if (map[bottom][left] || (map[bottom][right]){ 54 player->y++; 55 player->gravity = 0; // Setting the gravity to zero if player lands 56 break; 57 } 58 } 59 while (speed_y < 0){ 60 speed_y++; 61 player->y--; 62 top = player->y / 32; 63 if (map[top][left] || map[top][right]){ 64 player->++; 65 break; 66 } 67} 68 69void step_player(PLAYER *player){ // should be called every frame 70 player->gravity++; 71 move_player(player, 0, player->gravity); 72 // You could add your controls here 73 // If you use allegro 4, you can use the key array 74 // If you use allegro 5, you can use al_get_keyboard_state() 75 // if (key[KEY_RIGHT]){ 76 // move_player(player, 2, 0); 77 // } 78 } 79} 80 81void draw_player(PLAYER *player){ // Should be called after step_player 82// It's strongly recommended to first to the logic in your game and then draw everything in your game 83 rectfill(buffer, player->x, player->, player->x + player->width, player->y + player->height, makecol(0,255,0)); // Add your own drawing procedure here 84}

And how to use

// Somewhere in your initialization
PLAYER player;
create_player(&player, 32, 32, 16, 32); // Use whatever values you like
                                        // the & symbol means address of
                                        // We only pass the address of the player to the functions, not the whole player structure itself

// In your main loop

step_player(&player);
// after doing all the logic
draw_player(&player);

dsds dsds
Member #13,430
August 2011

Ok i'll study it soon xD thx taron you are very kind.
move_player has 4 arguments but you send 3 arguments to the function that are: player, 2, 0.
is it correct?

#SelectExpand
1 75 // if (key[KEY_RIGHT]){ 2 76 // move_player(player, 2, 0);

taron 
Member #10,584
January 2009
avatar

Sorry made a mistake there. You'll also have to pass your map as a parameter.

Steve Terry
Member #1,989
March 2002
avatar

You can port anything in C++ to C or anything in C to C++, you just have to change the constructs. Don't be so discouraged of other projects just because they have several "files". Separating your classes/methods into several files helps organize the project and makes it much easier to manage. You should be doing the same.

Anyway, don't discourage yourself because you think it's too hard, making a game takes time, lots of time, and persistence. Writing a good game with well managed source and classes/structs takes even longer and requires experience. Start with something smaller than a tilemap and work your way up.

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Go to: