Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro bitmaps: clearing to color

This thread is locked; no one can reply to it. rss feed Print
Allegro bitmaps: clearing to color
Jason Marmon
Member #14,859
January 2013

I'm trying to make a rendition of pong for my first allegro game. In the code shown below, I get an error because I set the target bitmap in order to allow the clearing of the previous bitmap to re-draw the new one (i.e. targeting p1 -> clearing p1->drawing p1). If I don't include the targeting, I get no error, rather trying to operate either player will make the other disappear. The code is as follows

#SelectExpand
1#include <allegro5/allegro.h> 2 #include <allegro5\allegro_native_dialog.h> 3 #include <allegro5\allegro_image.h> 4 #include <allegro5\allegro_color.h> 5 #include <stdio.h> 6 int main() 7 { 8 al_init(); 9 al_init_image_addon(); 10 al_install_mouse(); 11 al_install_keyboard(); 12 ALLEGRO_DISPLAY*display=al_create_display(800,600); 13 ALLEGRO_BITMAP*p1=al_load_bitmap("p1.bmp"); 14 ALLEGRO_BITMAP*p2=al_load_bitmap("p2.bmp");; 15 ALLEGRO_COLOR null_col=al_map_rgb(0,0,0); 16 //al_set_target_bitmap(test); 17 boolean done=false; 18 int p1x, p1y, p2x, p2y; 19 p1x=100; 20 p1y=200; 21 p2x=600; 22 p2y=200; 23 al_draw_bitmap(p1, p1x, p1y, NULL); 24 al_draw_bitmap(p2, p2x, p2y, NULL); 25 al_flip_display(); 26 while(!done) 27 { 28 29 ALLEGRO_KEYBOARD_STATE kstate; 30 al_get_keyboard_state(&kstate); 31 ALLEGRO_MOUSE_STATE mstate; 32 if (al_key_down(&kstate, ALLEGRO_KEY_ESCAPE)) //allows user to quit upon hitting the escape key 33 done=true; 34 al_get_mouse_state(&mstate); 35 /* 36 beginning of the player 2 movement 37 38 */ 39 if (al_key_down(&kstate, ALLEGRO_KEY_DOWN)) 40 { 41 al_set_target_bitmap(p2); 42 al_clear_to_color(null_col); 43 if (p2y<470) 44 p2y+=1; 45 /* 46 here is where the error occurs (in every case) 47 i've tried setting the target bitmap back to p1 after clearing to color, 48 but then no player will move 49 */ 50 al_draw_bitmap(p2, p2x, p2y, NULL); 51 al_flip_display(); 52 } 53 if (al_key_down(&kstate, ALLEGRO_KEY_UP)) 54 { 55 al_set_target_bitmap(p2); 56 al_clear_to_color(null_col); 57 if (p2y>0) 58 p2y-=1; 59 al_draw_bitmap(p2, p2x, p2y, NULL); 60 al_flip_display(); 61 } 62// end of p2 movemeent 63//start of p1 movement 64 if (al_key_down(&kstate, ALLEGRO_KEY_W)) 65 { 66 al_set_target_bitmap(p1); 67 al_clear_to_color(null_col); 68 if (p1y>0) 69 p1y-=1; 70 al_draw_bitmap(p1, p1x, p1y, NULL); 71 al_flip_display(); 72 } 73 if (al_key_down(&kstate, ALLEGRO_KEY_S)) 74 { 75 al_set_target_bitmap(p1); 76 al_clear_to_color(null_col); 77 if (p1y<470) 78 p1y+=1; 79 al_draw_bitmap(p1, p1x, p1y, NULL); 80 al_flip_display(); 81 } 82 } 83 al_destroy_bitmap(p1); 84 al_destroy_display(display); 85 86 return 0; 87 }

torhu
Member #2,727
September 2002
avatar

Just remove all the calls to al_set_target_bitmap, draw directly to the backbuffer.

For each time through the main loop, here's what you should do:

  1. Check if each key you care about is pressed, update the coordinates of p1 and p2 based on that.

  2. Clear the screen.

  3. Draw the bitmaps at their new positions.

  4. Flip.

In other words, don't do 2, 3, and 4 until number 1 is done and you are ready to draw the complete new frame.

I recommend you read about timers and events and use them instead of what you're doing now, though. But first get it working.

Jason Marmon
Member #14,859
January 2013

alright thanks :D

Go to: