Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Drawing to bitmaps and collisions not working

This thread is locked; no one can reply to it. rss feed Print
Drawing to bitmaps and collisions not working
KirbyDS11
Member #15,167
June 2013
avatar

I'm having a problem picking up collisions with a bitmap that I have drawn to. I have a background class that will draw the land and a player class that draws the player and picks up the collision. But it won't pick up the collision where I add the bitmap to the land.

#SelectExpand
1class Background 2{ 3public: 4 Background() 5 { 6 girder=al_load_bitmap("images/girder.png"); 7 gw=al_get_bitmap_width(girder); 8 gh=al_get_bitmap_height(girder); 9 } 10 void load_map() 11 { 12 land=al_load_bitmap("images/testmap.png"); 13 } 14 void drawland() 15 { 16 al_draw_bitmap(land,0,0,0); 17 } 18 void addgirder(int gx,int gy,ALLEGRO_DISPLAY *display) 19 { 20 //this is where the bitmap is drawn to the land 21 al_set_target_bitmap(land); 22 al_draw_bitmap(girder,gx-gw/2,gy-gh/2,0); 23 al_set_target_bitmap(al_get_backbuffer(display)); 24 } 25 ALLEGRO_BITMAP *getland() 26 { 27 return land; 28 } 29 ~Background() 30 { 31 al_destroy_bitmap(land); 32 al_destroy_bitmap(girder); 33 } 34private: 35 int gw,gh; 36 ALLEGRO_BITMAP *land; 37 ALLEGRO_BITMAP *girder; 38}; 39class Player 40{ 41public: 42 Player(float px,float py) 43 { 44 x=px; 45 y=py; 46 gravity=0.125; 47 ymove=0; 48 player=al_load_bitmap("images/player.png"); 49 penw=al_get_bitmap_width(player); 50 penh=al_get_bitmap_height(player); 51 radius=al_get_bitmap_height(player)/2; 52 } 53 void movepen() 54 { 55 ymove+=gravity; 56 if(ymove>=12) 57 { 58 ymove=12; 59 } 60 } 61 void collision(ALLEGRO_BITMAP* land) 62 { 63 //this is the collision 64 al_lock_bitmap(land,al_get_bitmap_format(land),ALLEGRO_LOCK_READONLY); 65 for(int f=0;f<penh;f++) 66 { 67 for(int c=0;c<penw;c++) 68 { 69 color=al_get_pixel(land,x+c,y+f); 70 dis=sqrt(pow((penw/2)-c,2)+pow((penh/2)-f,2)); 71 if(color.a!=0 && dis<=radius) 72 { 73 for(int fy=0;fy<=ymove+1;fy++) 74 { 75 color=al_get_pixel(land,x+penw/2,y+penh-fy); 76 if(color.a==0) 77 { 78 y-=fy; 79 fy=ymove+2; 80 } 81 } 82 ymove=0; 83 } 84 } 85 } 86 y+=ymove; 87 } 88 void draw() 89 { 90 al_draw_bitmap(player,floor(x),floor(y),flip); 91 } 92 ~Player() 93 { 94 al_destroy_bitmap(player); 95 } 96private: 97 bool flip; 98 int penw,penh,radius; 99 float x,y,ymove,gravity,dis; 100 ALLEGRO_COLOR color; 101 ALLEGRO_BITMAP *player; 102}; 103int main() 104{ 105 //inits and creating the window 106 Player pen(256,32,display); 107 Background drawback(); 108 drawback.load_map(); 109 //create stuff such as a timer, events, and variables 110 while(!done) 111 { 112 ALLEGRO_EVENT events; 113 al_wait_for_event(event_queue,&events); 114 if(events.type==ALLEGRO_EVENT_KEY_UP) 115 { 116 switch(events.keyboard.keycode) 117 { 118 case ALLEGRO_KEY_ESCAPE: 119 done=true; 120 break; 121 } 122 } 123 else if(events.type==ALLEGRO_EVENT_DISPLAY_CLOSE) 124 { 125 done=true; 126 } 127 else if (events.type==ALLEGRO_EVENT_MOUSE_AXES) 128 { 129 mx=events.mouse.x; 130 my=events.mouse.y; 131 } 132 else if (events.type==ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 133 { 134 if(events.mouse.button & 1) 135 { 136 drawback.addgirder(mx,my,display); 137 } 138 } 139 if(events.type==ALLEGRO_EVENT_TIMER) 140 { 141 pen.movepen(); 142 draw=true; 143 } 144 if(draw) 145 { 146 pen.collision(drawback.getland()); 147 drawback.drawland(); 148 pen.draw(); 149 al_flip_display(); 150 al_clear_to_color(al_map_rgb(50,50,50)); 151 draw=false; 152 } 153 } 154 //destroy stuff 155 return 0; 156}

Is there some sort of function to update the bitmap or something or did I just do something wrong.???

Woops

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

KirbyDS11
Member #15,167
June 2013
avatar

You have to change it back to the backbuffer using al_set_target_bitmap(al_get_backbuffer(display));

this code is already in the function addgirder which is on line 18 of the code. Everything gets drawn fine but the pixel information is not picked up by al_get_pixel

Woops

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

KirbyDS11
Member #15,167
June 2013
avatar

Thank you.
unlocking the bitmap worked.

Woops

Go to: