Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » If on bitmaps

This thread is locked; no one can reply to it. rss feed Print
If on bitmaps
paparam
Member #16,847
April 2018

Hello.
Im trying to make if on bitmap.
I have randomly loaded bitmap in loop and i want to do so if it rolls a certain one, lets call it xxx.bmp i want to make something different than others but i can't quite find out how to make a instruction.

If i do like this

if(something=="xxx.bmp"){
(...do something)
}else
(...do something)

it doesnt work so i guess im doing it the wrong way.
What's the proprer way to do thing like this?

Edgar Reynaldo
Member #8,592
May 2007
avatar

#SelectExpand
1ALLEGRO_BITMAP* bmp1 = al_load_bitmap("xxx.bmp"); 2ALLEGRO_BITMAP* bmp2 = al_load_bitmap("xxy.bmp"); 3ALLEGRO_BITMAP* bmp = bmp1; 4 5bool quit = false; 6 7while (!quit) { 8 al_set_target_backbuffer(display); 9 al_clear_to_color(al_map_rgb(0,0,0)); 10 if (bmp) { 11 al_draw_bitmap(bmp , 0 , 0 , 0); 12 } 13 al_flip_display(); 14 15 do { 16 ALLEGRO_EVENT ev; 17 al_wait_for_event(queue , &ev); 18 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_Y) { 19 bmp = bmp2; 20 } 21 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_X) { 22 bmp = bmp1; 23 } 24 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_SPACE) { 25 bmp = 0; 26 } 27 if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 28 quit = true; 29 } 30 } while (!al_is_event_queue_empty(queue)); 31}

EDIT
Perhaps I misunderstood. Maybe you meant how do I choose a certain bitmap?

Have you ever heard of enums or arrays? switch statements? those would be useful here. you can also use string comparisons, for example in a std::map.

Audric
Member #907
January 2001

From looking at your other post, I think you're making the mistake of mixing the loading of resources with the identification of things (what the user is clicking, what he clicked last)
You should really separate :

#SelectExpand
1// The storage of graphic resources. If you have many, you may prefer an array. 2BITMAP* sprite_king; 3BITMAP* sprite_knight; 4 5// The current state of what the player has selected 6int selected_piece=0; // 0= none, 1=king, 2=knight 7BITMAP * current_image=NULL; // image relevant to the selected piece 8 9// The loading of resources, should run only once, when program starts (but after allegro init) 10load_graphics() 11{ 12 sprite_king = load_bitmap("king.bmp"); 13 sprite_knight = load_bitmap("knight.bmp"); 14} 15 16// Collision detection : when you detect the player points at something 17if(mouse_x>45&&mouse_x<370&&mouse_y>132&&mouse_y<190) 18 if(mouse_b==1){ // king 19 selected_piece=1; 20 current_image = sprite_king; 21 } 22} 23if(/*other coordinates*/) 24 if(mouse_b==1){ // knight 25 selected_piece=2; 26 current_image = sprite_knight; 27 } 28} 29 30// Checking this information, for gameplay purpose 31if (selected_piece == 1) // when the king is selected 32{ 33 .... 34} 35 36// drawing whatever piece is currently selected 37if (current_image) 38{ 39 draw_sprite(screen, current_image , x, y); 40}

Go to: