Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » When to flip display/ backbuffer/ unresponsive events

This thread is locked; no one can reply to it. rss feed Print
When to flip display/ backbuffer/ unresponsive events
manderson
Member #13,821
December 2011

Happy weekend everyone,

I have a question regarding my game. We have made a lot of progress over the last week and we're coming down the home stretch for our audio centric game...A couple things that I can't wrap my head around, however:

##line 576 in code below:

al_set_target_bitmap();

I am currently setting the "mixer" as the target bitmap. My understanding is that it makes this a background so that everything else will be drawn on top of it (in terms of layers)...
al_set_target_backbuffer();

My understanding is that the display needs to be within this paren so that the 'set target bitmap' works...

al_clear_to_color();

Clears the screen obviously...

al_flip_display();

Flips whatever was in the backbuffer to the front of the screen...

Here's my question:

Why is it that when I click on any object it takes a slight movement from the mouse for the screen update to take effect? For instance, ##line 581: I check to see if a left mouse button has been clicked within the buttonOne bitmap. If so, then it sets that button to "on (true)" and the rest within the group to "off (false)". Then ##line 335: the buttonOne.isClicked flag is set to true now so it stops drawing the regular bitmap and draws it as a tinted one instead (reddish).

My understanding is that it is bad practice to do any drawing within the mouse events...however, I had tried putting the logic within ##line 335 into ##line 581 and the bitmap update was more responsive, but then the initial bitmap was not drawn to the screen. Aaarrrghghhh.

Is there a better placement for my backbuffer/display flipping code? For whatever reason, its current placement is optimal so that screen flickering doesn't occur when adjusting the faders.I posted a lot of the code in case you wanted to check it out but my question pertains to the specific numbers listed above. I deleted the arbitrary code such as loading bitmaps, samples, and destroying them. Everything works except the mouse stuff. I know the code/ logic is probably messy as hell but I'd say its a pretty good attempt at a first game ;D

Thanks!

#SelectExpand
1// 2// main.c 3 4#include <allegro5/allegro.h> 5#include <allegro5/allegro_image.h> 6#include <allegro5/allegro_font.h> 7#include <allegro5/allegro_ttf.h> 8#include <allegro5/allegro_primitives.h> 9#include <allegro5/allegro_audio.h> 10#include <allegro5/allegro_acodec.h> 11#include "mainStuff.h" 12#include <math.h> 13 14//GLOBAL VARIABLES 15bool isRoundOver = false; 16bool isStartMenu = true; 17bool isKennyG = false; 18bool isPDiddy = false; 19bool isKingTubby = false; 20bool isDimebagDarrell = false; 21 22#define WIDTH 950 23#define HEIGHT 800 24 25//Function Prototypes: 26void InitFader(Fader *fader, ALLEGRO_BITMAP *faderbmp); 27void InitMixer(Mixer *mixer, ALLEGRO_BITMAP *mixerbmp); 28void InitButtons(Button *button, ALLEGRO_BITMAP *buttonbmp); 29void InitMixButton(MixButton *mixbutton, ALLEGRO_BITMAP *mixbuttonbmp); 30void SetFaderOne(Fader *faderOne, Mixer *mixer); 31void SetFaderTwo(Fader *faderTwo, Mixer *mixer); 32void SetFaderThree(Fader *faderThree, Mixer *mixer); 33void SetFaderFour(Fader *faderFour, Mixer *mixer); 34void SetMixer(Mixer *mixer); 35 36void SetButtonOne(Button *buttonOne, Mixer *mixer); 37//SET THE REST OF THE BUTTONS HERE TO THEIR RESPECTIVE SCREEN POSITIONS 38 39void SetMixButton(MixButton *mixbutton); 40void UpdateFaderOne(Fader *faderOne, Mixer *mixer, ALLEGRO_EVENT *ev, ALLEGRO_BITMAP *faderbmp); 41void UpdateFaderTwo(Fader *faderTwo, Mixer *mixer, ALLEGRO_EVENT *ev, ALLEGRO_BITMAP *faderbmp); 42void UpdateFaderThree(Fader *faderThree, Mixer *mixer, ALLEGRO_EVENT *ev, ALLEGRO_BITMAP *faderbmp); 43void UpdateFaderFour(Fader *faderFour, Mixer *mixer, ALLEGRO_EVENT *ev, ALLEGRO_BITMAP *faderbmp); 44 45//Main Loop 46int main (int argc, char **argv) 47{ 48 //PRIMITIVE VARIABLES 49 bool done = false; 50 bool isScoreCalculated = false; 51 52 // Float values for scoring 53 //NOT IMPORTANT FOR THIS EXAMPLE 54 55 //used for round counter: 56 int i = 1; 57 58 //Allegro variables 59 ALLEGRO_DISPLAY *display = NULL; 60 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 61 62 //font ttf bitmaps 63 ALLEGRO_FONT *fontHeader = NULL; 64 ALLEGRO_FONT *fontMain = NULL; 65 ALLEGRO_FONT *fontScore = NULL; 66 67 //mixer, border, button, fader, and mixbutton bitmaps 68 ALLEGRO_BITMAP *mixerbmp = NULL; 69 ALLEGRO_BITMAP *mixerborderbmp = NULL; 70 ALLEGRO_BITMAP *buttonbmp = NULL; 71 ALLEGRO_BITMAP *faderbmp = NULL; 72 ALLEGRO_BITMAP *mixbuttonbmp = NULL; 73 74 //character bitmaps 75 ALLEGRO_BITMAP *kennygbmp = NULL; 76 //IN ADDITION TO ALL BITMAPS... 77 78 79 //ALLEGRO Samples 80 ALLEGRO_SAMPLE *hiphop1 = NULL; 81 //IN ADDITION TO ALL SAMPLES... 82 83 // ALLEGRO_SAMPLE *other1 = NULL; 84 ALLEGRO_SAMPLE_INSTANCE *hiphop1inst = NULL; 85 //IN ADDITION TO ALL SAMPLE INSTANCES... 86 87 //error checking to make sure allegro starts correctly: 88 if(!al_init()) 89 return -1; 90 //creates a new display and checks to see if it loaded correctly: 91 display = al_create_display(WIDTH, HEIGHT); 92 if(!display) 93 return -1; 94 //this installs the mouse to allow the use of mouse events: 95 if (!al_install_mouse()) { 96 printf("Could not init mouse!\n"); 97 return(-1); 98 } 99 //this loads the image addon to allow the importing/ usage of bitmaps 100 if (!al_init_image_addon()) { 101 printf("Could not init image addon!\n"); 102 return(-1); 103 } 104 //initializes the font addon 105 al_init_font_addon(); 106 107 //this loads the ttf addon to allow importing/ using custom ttf fonts 108 if (!al_init_ttf_addon()) { 109 printf("Could not init ttf addon!\n"); 110 return(-1); 111 } 112 //this is for drawing primitive shapes: 113 if (!al_init_primitives_addon()) { 114 printf("Could not init primitives addon!\n"); 115 return(-1); 116 } 117 118 //creates the audio addon and error checks to make sure it loads 119 if (!al_install_audio()) { 120 printf("Could not init sound!\n"); 121 return(-1); 122 } 123 //initialize the acodec allegro addon and error checks to make sure it loads 124 if (!al_init_acodec_addon()) { 125 printf("Could not init acodec addon!\n"); 126 return(-1); 127 } 128 //Reserve samples: 129 al_reserve_samples(16); 130 131 132 //dialog boxes bitmaps 133 uridialogbmp = al_load_bitmap("uri1.png"); 134 //and loads the rest of the dialog bitmaps... 135 136 //loads bitmaps for the mixer, border, fader, button and mix button: 137 mixerbmp = al_load_bitmap("mixernewbmp.png"); 138 mixerborderbmp = al_load_bitmap("mixerborder.png"); 139 faderbmp = al_load_bitmap("fader.png"); 140 buttonbmp = al_load_bitmap("buttonone.png"); 141 mixbuttonbmp = al_load_bitmap("mixbutton.png"); 142 143 //load bitmaps for all the characters and money: 144 moneybmp = al_load_bitmap("moneybmp.png"); 145 //and loads the rest of the bitmaps... 146 147 //Load all the fonts: 148 fontHeader = al_load_font("Comic Sans MS Bold.ttf", 50, 0); 149 fontMain = al_load_font("Arial.ttf", 18, 0); 150 fontScore = al_load_font("EuphemiaCASRegular.ttf", 25, 0); 151 152 //Create new Event Queue instance: 153 event_queue = al_create_event_queue(); 154 155 //loads all the samples into memory 156 hiphop1 = al_load_sample("hiphop1.wav"); 157 //loads the rest of the samples... 158 159 //creates sample instances for all the tracks: 160 hiphop1inst = al_create_sample_instance(hiphop1); 161 //and the rest of the sample instances... 162 163 // Set sample playback mode to LOOP 164 //------------------------------------------------------------------ 165 al_set_sample_instance_playmode(hiphop1inst, ALLEGRO_PLAYMODE_LOOP); 166 //and the rest of the playmodes for the songs... 167 168 // Attach sample instance to mixer 169 //------------------------------------------------------------------ 170 al_attach_sample_instance_to_mixer(hiphop1inst, al_get_default_mixer()); 171 //attach the rest of the instances to mixer... 172 173 //SET SAMPLE GAINS... 174 al_set_sample_instance_gain(hiphop1inst, 0); 175 //and inits all sample instance gains to 0 so muted... 176 177 //creates new instances of the mixer and the faders 178 Mixer mixer; 179 Fader faderOne; 180 Fader faderTwo; 181 Fader faderThree; 182 Fader faderFour; 183 184 //creates new instances of all buttons and the mix button 185 Button buttonOne; 186 //and all the buttons... 187 188 //initializes the mixer and faders 189 InitMixer(&mixer, mixerbmp); 190 InitFader(&faderOne, faderbmp); 191 InitFader(&faderTwo, faderbmp); 192 InitFader(&faderThree, faderbmp); 193 InitFader(&faderFour, faderbmp); 194 195 //Initialize the buttons to zero... 196 InitButtons(&buttonOne, buttonbmp); 197 //and all the buttons... 198 199 InitMixButton(&mixbutton, mixbuttonbmp); 200 201 al_register_event_source(event_queue, al_get_mouse_event_source()); 202 al_register_event_source(event_queue, al_get_display_event_source(display)); 203 204 //begins playback for all the samples 205 al_play_sample_instance(hiphop1inst); 206 //start the samples playing before main loop... 207 208 //This is the start of the main game loop 209 while (!done) { 210 211 //FIRST WE BEGIN ON WITH THE START MENU 212 while (isStartMenu && !done) { 213 214 //this is the union in which all game events lie: it includes mouse events, etc 215 ALLEGRO_EVENT ev; 216 al_wait_for_event (event_queue, &ev); 217 218 al_clear_to_color(al_map_rgb(0,0,0)); 219 220 //this displays uri's main bitmap 221 al_draw_bitmap(urimenubmp, 375, HEIGHT / 2 - al_get_font_line_height(fontHeader), 0); 222 223 //this displays uri's main dialog box 224 al_draw_scaled_bitmap(uridialogbmp, 0, 0, al_get_bitmap_width(uridialogbmp), 225 al_get_bitmap_height(uridialogbmp), WIDTH / 2 + 75, HEIGHT / 2 - 100, 226 al_get_bitmap_width(uridialogbmp) / 2, 227 al_get_bitmap_height(uridialogbmp) / 2, 0); 228 229 //displays the header text on the screen 230 al_draw_textf(fontHeader, al_map_rgb(255,0,255), WIDTH / 2, 100, 231 ALLEGRO_ALIGN_CENTRE, "MIX HERO!!!"); 232 al_draw_textf(fontMain, al_map_rgb(255,0,255), WIDTH / 2, 100 + 233 al_get_font_line_height(fontHeader), 234 ALLEGRO_ALIGN_CENTRE, "(click to begin)"); 235 236 al_flip_display(); 237 238 if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 239 240 isStartMenu = false; 241 al_clear_to_color(al_map_rgb(0,0,0)); 242 243 } 244 //check event types: 245 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 246 { 247 done = true; 248 } 249 } 250 //we won't use this bitmap anymore... 251 al_destroy_bitmap(uridialogbmp); 252 253 //this is the while loop where the main gameplay takes place. 254 while (!isRoundOver && !done) { 255 256 //this is a union that includes all possible game events (mouse input, display close, etc) 257 ALLEGRO_EVENT ev; 258 al_wait_for_event (event_queue, &ev); 259 260 SetMixer(&mixer); 261 262 //these bitmaps display the mixer, its border and uri: 263 al_draw_bitmap(mixerbmp, mixer.x, mixer.y, 0); 264 al_draw_bitmap(mixerborderbmp, mixer.x, mixer.y, 0); 265 al_draw_bitmap(urimenubmp, 20, HEIGHT / 2 - 100, 0); 266 267 //this will display the total score on the screen 268 al_draw_textf(fontScore, al_map_rgb(255,255,255), WIDTH / 2, 20, 269 ALLEGRO_ALIGN_CENTRE, "Cash Money: "); 270 271 //this adds space between the dollar sign so that it doesn't collide with the text as numbers get larger: 272 if ((jazzScore + rockScore + hiphopScore + reggaeScore) == 0) { 273 al_draw_textf(fontScore, al_map_rgb(34,139,34), WIDTH / 2, 20, 274 ALLEGRO_ALIGN_CENTRE, " $%d", 275 jazzScore + rockScore + hiphopScore + reggaeScore); 276 } 277 //simply shifts the score to the right so it looks nicer on the display 278 else { 279 al_draw_textf(fontScore, al_map_rgb(34,139,34), WIDTH / 2, 20, 280 ALLEGRO_ALIGN_CENTRE, " $%d", 281 jazzScore + rockScore + hiphopScore + reggaeScore); 282 } 283 284 //these if statements will display each character based on which round it is: 285 if (i == 1) { 286 al_draw_bitmap(kennygbmp, WIDTH - 250, 140, 0); 287 al_draw_scaled_bitmap(kennygdialogbmp, 0, 0, al_get_bitmap_width(kennygdialogbmp), 288 al_get_bitmap_height(kennygdialogbmp), WIDTH / 2 + 150, 100, 289 al_get_bitmap_width(kennygdialogbmp) / 4, 290 al_get_bitmap_height(kennygdialogbmp) / 4, 0); 291 } 292 if (i == 2) { 293 al_draw_bitmap(pdiddybmp, WIDTH - 200, HEIGHT - (al_get_bitmap_height(pdiddybmp) + 40), 0); 294 al_draw_scaled_bitmap(pdiddydialogbmp, 0, 0, al_get_bitmap_width(pdiddydialogbmp), 295 al_get_bitmap_height(pdiddydialogbmp), WIDTH / 2 + 230, 350, 296 al_get_bitmap_width(pdiddydialogbmp) / 4, 297 al_get_bitmap_height(pdiddydialogbmp) / 4, 0); 298 } 299 if (i == 3) { 300 al_draw_bitmap(kingtubbybmp, WIDTH - 250, 20, 0); 301 al_draw_scaled_bitmap(kingtubbydialogbmp, 0, 0, al_get_bitmap_width(kingtubbydialogbmp), 302 al_get_bitmap_height(kingtubbydialogbmp), WIDTH / 2 + 100, 75, 303 al_get_bitmap_width(kingtubbydialogbmp) / 4, 304 al_get_bitmap_height(kingtubbydialogbmp) / 4, 0); 305 } 306 if (i == 4) { 307 al_draw_bitmap(dimebagbmp, WIDTH - 250, HEIGHT - (al_get_bitmap_height(dimebagbmp) + 25), 0); 308 al_draw_scaled_bitmap(dimebagdialogbmp, 0, 0, al_get_bitmap_width(dimebagdialogbmp), 309 al_get_bitmap_height(dimebagdialogbmp), WIDTH / 2 + 230, 320, 310 al_get_bitmap_width(dimebagdialogbmp) / 4, 311 al_get_bitmap_height(dimebagdialogbmp) / 4, 0); 312 } 313 //these series of if statements will keep the fader in the 'middle position' while it has not been moved: 314 if (!faderOne.isMoved) 315 { 316 SetFaderOne(&faderOne, &mixer); 317 al_draw_bitmap(faderbmp, faderOne.x, faderOne.y, 0); 318 } 319 if (!faderTwo.isMoved) 320 { 321 SetFaderTwo(&faderTwo, &mixer); 322 al_draw_bitmap(faderbmp, faderTwo.x, faderTwo.y, 0); 323 } 324 if (!faderThree.isMoved) 325 { 326 SetFaderThree(&faderThree, &mixer); 327 al_draw_bitmap(faderbmp, faderThree.x, faderThree.y, 0); 328 } 329 if (!faderFour.isMoved) 330 { 331 SetFaderFour(&faderFour, &mixer); 332 al_draw_bitmap(faderbmp, faderFour.x, faderFour.y, 0); 333 } 334 //If the buttons are clicked, they turn a different color. If it hasn't been clicked, it is drawn in place 335 if (!buttonOne.isClicked) { 336 SetButtonOne(&buttonOne, &mixer); 337 al_draw_bitmap(buttonbmp, buttonOne.x, buttonOne.y, 0); 338 al_set_sample_instance_gain(hiphop1inst, 0); 339 } 340 else { 341 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonOne.x, buttonOne.y, 0); 342 } 343 if (!buttonTwo.isClicked) { 344 SetButtonTwo(&buttonTwo, &mixer); 345 al_draw_bitmap(buttonbmp, buttonTwo.x, buttonTwo.y, 0); 346 al_set_sample_instance_gain(rock1inst, 0); 347 } 348 else { 349 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonTwo.x, buttonTwo.y, 0); 350 } 351 if (!buttonThree.isClicked) { 352 SetButtonThree(&buttonThree, &mixer); 353 al_draw_bitmap(buttonbmp, buttonThree.x, buttonThree.y, 0); 354 al_set_sample_instance_gain(jazz1inst, 0); 355 } 356 else { 357 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonThree.x, buttonThree.y, 0); 358 } 359 if (!buttonFour.isClicked) { 360 SetButtonFour(&buttonFour, &mixer); 361 al_draw_bitmap(buttonbmp, buttonFour.x, buttonFour.y, 0); 362 al_set_sample_instance_gain(reggae1inst, 0); 363 } 364 else { 365 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonFour.x, buttonFour.y, 0); 366 } 367 if (!buttonFive.isClicked) { 368 SetButtonFive(&buttonFive, &mixer); 369 al_draw_bitmap(buttonbmp, buttonFive.x, buttonFive.y, 0); 370 al_set_sample_instance_gain(reggae2inst, 0); 371 } 372 else { 373 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonFive.x, buttonFive.y, 0); 374 } 375 if (!buttonSix.isClicked) { 376 SetButtonSix(&buttonSix, &mixer); 377 al_draw_bitmap(buttonbmp, buttonSix.x, buttonSix.y, 0); 378 al_set_sample_instance_gain(jazz2inst, 0); 379 } 380 else { 381 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonSix.x, buttonSix.y, 0); 382 } 383 if (!buttonSeven.isClicked) { 384 SetButtonSeven(&buttonSeven, &mixer); 385 al_draw_bitmap(buttonbmp, buttonSeven.x, buttonSeven.y, 0); 386 al_set_sample_instance_gain(rock2inst, 0); 387 } 388 else { 389 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonSeven.x, buttonSeven.y, 0); 390 } 391 if (!buttonEight.isClicked) { 392 SetButtonEight(&buttonEight, &mixer); 393 al_draw_bitmap(buttonbmp, buttonEight.x, buttonEight.y, 0); 394 al_set_sample_instance_gain(hiphop2inst, 0); 395 } 396 else { 397 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonEight.x, buttonEight.y, 0); 398 } 399 if (!buttonNine.isClicked) { 400 SetButtonNine(&buttonNine, &mixer); 401 al_draw_bitmap(buttonbmp, buttonNine.x, buttonNine.y, 0); 402 al_set_sample_instance_gain(rock3inst, 0); 403 } 404 else { 405 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonNine.x, buttonNine.y, 0); 406 } 407 if (!buttonTen.isClicked) { 408 SetButtonTen(&buttonTen, &mixer); 409 al_draw_bitmap(buttonbmp, buttonTen.x, buttonTen.y, 0); 410 al_set_sample_instance_gain(hiphop3inst, 0); 411 } 412 else { 413 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonTen.x, buttonTen.y, 0); 414 } 415 if (!buttonEleven.isClicked) { 416 SetButtonEleven(&buttonEleven, &mixer); 417 al_draw_bitmap(buttonbmp, buttonEleven.x, buttonEleven.y, 0); 418 al_set_sample_instance_gain(reggae3inst, 0); 419 } 420 else { 421 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonEleven.x, buttonEleven.y, 0); 422 } 423 if (!buttonTwelve.isClicked) { 424 SetButtonTwelve(&buttonTwelve, &mixer); 425 al_draw_bitmap(buttonbmp, buttonTwelve.x, buttonTwelve.y, 0); 426 al_set_sample_instance_gain(jazz3inst, 0); 427 } 428 else { 429 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonTwelve.x, buttonTwelve.y, 0); 430 } 431 if (!buttonThirteen.isClicked) { 432 SetButtonThirteen(&buttonThirteen, &mixer); 433 al_draw_bitmap(buttonbmp, buttonThirteen.x, buttonThirteen.y, 0); 434 al_set_sample_instance_gain(reggae4inst, 0); 435 } 436 else { 437 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonThirteen.x, buttonThirteen.y, 0); 438 } 439 if (!buttonFourteen.isClicked) { 440 SetButtonFourteen(&buttonFourteen, &mixer); 441 al_draw_bitmap(buttonbmp, buttonFourteen.x, buttonFourteen.y, 0); 442 al_set_sample_instance_gain(jazz4inst, 0); 443 } 444 else { 445 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonFourteen.x, buttonFourteen.y, 0); 446 } 447 if (!buttonFifteen.isClicked) { 448 SetButtonFifteen(&buttonFifteen, &mixer); 449 al_draw_bitmap(buttonbmp, buttonFifteen.x, buttonFifteen.y, 0); 450 al_set_sample_instance_gain(rock4inst, 0); 451 } 452 else { 453 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonFifteen.x, buttonFifteen.y, 0); 454 } 455 if (!buttonSixteen.isClicked) { 456 SetButtonSixteen(&buttonSixteen, &mixer); 457 al_draw_bitmap(buttonbmp, buttonSixteen.x, buttonSixteen.y, 0); 458 al_set_sample_instance_gain(hiphop4inst, 0); 459 } 460 else { 461 al_draw_tinted_bitmap(buttonbmp, al_map_rgb(255,0,0), buttonSixteen.x, buttonSixteen.y, 0); 462 } 463 //if the mix button is clicked, the round is over and the Score screen is initiated. 464 if (!mixbutton.isMixed) { 465 SetMixButton(&mixbutton); 466 al_draw_bitmap(mixbuttonbmp, mixbutton.x, mixbutton.y, 0); 467 } 468 else { 469 isRoundOver = true; 470 } 471 472 //check event types: 473 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 474 { 475 done = true; 476 } 477 //event will fire if the mouse is moving around inside of window. 478 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) 479 { 480 if (ev.mouse.x > WIDTH / 2 - 240 && 481 ev.mouse.x < WIDTH / 2 - 240 + faderOne.width && 482 ev.mouse.y > HEIGHT / 2 - 50 && 483 ev.mouse.y < HEIGHT / 2 + 150 && 484 ev.mouse.button & 1) { 485 486 487 if (buttonOne.isClicked) { 488 al_set_sample_instance_gain(hiphop1inst, (abs( (faderOne.y - 549) ) / (float) 198)); 489 } 490 if (buttonTwo.isClicked) { 491 al_set_sample_instance_gain(rock1inst, (abs( (faderOne.y - 549) ) / (float) 198)); 492 } 493 if (buttonThree.isClicked) { 494 al_set_sample_instance_gain(jazz1inst, (abs( (faderOne.y - 549) ) / (float) 198)); 495 } 496 if (buttonFour.isClicked) { 497 al_set_sample_instance_gain(reggae1inst, (abs( (faderOne.y - 549) ) / (float) 198)); 498 } 499 UpdateFaderOne(&faderOne, &mixer, &ev, faderbmp); 500 501 } 502 al_draw_bitmap(faderbmp, faderOne.x, faderOne.y, 0); 503 504 if (ev.mouse.x > WIDTH / 2 - 127 && 505 ev.mouse.x < WIDTH / 2 - 127 + faderTwo.width && 506 ev.mouse.y > HEIGHT / 2 - 50 && 507 ev.mouse.y < HEIGHT / 2 + 150 && 508 ev.mouse.button & 1) { 509 510 if (buttonFive.isClicked) { 511 al_set_sample_instance_gain(reggae2inst, (abs( (faderTwo.y - 549) ) / (float) 198)); 512 } 513 if (buttonSix.isClicked) { 514 al_set_sample_instance_gain(jazz2inst, (abs( (faderTwo.y - 549) ) / (float) 198)); 515 } 516 if (buttonSeven.isClicked) { 517 al_set_sample_instance_gain(rock2inst, (abs( (faderTwo.y - 549) ) / (float) 198)); 518 } 519 if (buttonEight.isClicked) { 520 al_set_sample_instance_gain(hiphop2inst, (abs( (faderTwo.y - 549) ) / (float) 198)); 521 } 522 UpdateFaderTwo(&faderTwo, &mixer, &ev, faderbmp); 523 524 } 525 al_draw_bitmap(faderbmp, faderTwo.x, faderTwo.y, 0); 526 527 if (ev.mouse.x > WIDTH / 2 + 95 && 528 ev.mouse.x < WIDTH / 2 + 95 + faderThree.width && 529 ev.mouse.y > HEIGHT / 2 - 50 && 530 ev.mouse.y < HEIGHT / 2 + 150 && 531 ev.mouse.button & 1) { 532 533 534 if (buttonNine.isClicked) { 535 al_set_sample_instance_gain(rock3inst, (abs( (faderThree.y - 549) ) / (float) 198)); 536 } 537 if (buttonTen.isClicked) { 538 al_set_sample_instance_gain(hiphop3inst, (abs( (faderThree.y - 549) ) / (float) 198)); 539 } 540 if (buttonEleven.isClicked) { 541 al_set_sample_instance_gain(reggae3inst, (abs( (faderThree.y - 549) ) / (float) 198)); 542 } 543 if (buttonTwelve.isClicked) { 544 al_set_sample_instance_gain(jazz3inst, (abs( (faderThree.y - 549) ) / (float) 198)); 545 } 546 UpdateFaderThree(&faderThree, &mixer, &ev, faderbmp); 547 548 } 549 550 al_draw_bitmap(faderbmp, faderThree.x, faderThree.y, 0); 551 552 if (ev.mouse.x > WIDTH / 2 + 199 && 553 ev.mouse.x < WIDTH / 2 + 199 + faderFour.width && 554 ev.mouse.y > HEIGHT / 2 - 50 && 555 ev.mouse.y < HEIGHT / 2 + 150 && 556 ev.mouse.button & 1) { 557 558 559 if (buttonThirteen.isClicked) { 560 al_set_sample_instance_gain(reggae4inst, (abs( (faderFour.y - 549) ) / (float) 198)); 561 } 562 if (buttonFourteen.isClicked) { 563 al_set_sample_instance_gain(jazz4inst, (abs( (faderFour.y - 549) ) / (float) 198)); 564 } 565 if (buttonFifteen.isClicked) { 566 al_set_sample_instance_gain(rock4inst, (abs( (faderFour.y - 549) ) / (float) 198)); 567 } 568 if (buttonSixteen.isClicked) { 569 al_set_sample_instance_gain(hiphop4inst, (abs( (faderFour.y - 549) ) / (float) 198)); 570 } 571 UpdateFaderFour(&faderFour, &mixer, &ev, faderbmp); 572 573 } 574 al_draw_bitmap(faderbmp, faderFour.x, faderFour.y, 0); 575 576 al_set_target_bitmap(mixerbmp); 577 al_set_target_backbuffer(display); 578 al_flip_display(); 579 al_clear_to_color(al_map_rgb(0,0,0)); 580 } 581 if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) 582 { 583 if (ev.mouse.x > buttonOne.x && 584 ev.mouse.x < buttonOne.x + buttonOne.width && 585 ev.mouse.y > buttonOne.y && 586 ev.mouse.y < buttonOne.y + buttonOne.height && 587 ev.mouse.button & 1) { 588 589 if (buttonOne.isClicked == false) { 590 buttonOne.isClicked = true; 591 buttonTwo.isClicked = false; 592 buttonThree.isClicked = false; 593 buttonFour.isClicked = false; 594 } 595 else 596 buttonOne.isClicked = false; 597 598 al_set_sample_instance_gain(hiphop1inst, (abs( (faderOne.y - 549) ) / (float) 198)); 599 } 600 else if (ev.mouse.x > buttonTwo.x && 601 ev.mouse.x < buttonTwo.x + buttonTwo.width && 602 ev.mouse.y > buttonTwo.y && 603 ev.mouse.y < buttonTwo.y + buttonTwo.height && 604 ev.mouse.button & 1) { 605 606 if (buttonTwo.isClicked == false) { 607 buttonOne.isClicked = false; 608 buttonTwo.isClicked = true; 609 buttonThree.isClicked = false; 610 buttonFour.isClicked = false; 611 } 612 else 613 buttonTwo.isClicked = false; 614 615 al_set_sample_instance_gain(rock1inst, (abs( (faderOne.y - 549) ) / (float) 198)); 616 } 617 else if (ev.mouse.x > buttonThree.x && 618 ev.mouse.x < buttonThree.x + buttonThree.width && 619 ev.mouse.y > buttonThree.y && 620 ev.mouse.y < buttonThree.y + buttonThree.height && 621 ev.mouse.button & 1) { 622 623 if (buttonThree.isClicked == false) { 624 buttonOne.isClicked = false; 625 buttonTwo.isClicked = false; 626 buttonThree.isClicked = true; 627 buttonFour.isClicked = false; 628 } 629 else 630 buttonThree.isClicked = false; 631 al_set_sample_instance_gain(jazz1inst, (abs( (faderOne.y - 549) ) / (float) 198)); 632 } 633 else if (ev.mouse.x > buttonFour.x && 634 ev.mouse.x < buttonFour.x + buttonFour.width && 635 ev.mouse.y > buttonFour.y && 636 ev.mouse.y < buttonFour.y + buttonFour.height && 637 ev.mouse.button & 1) { 638 639 if (buttonFour.isClicked == false) { 640 buttonOne.isClicked = false; 641 buttonTwo.isClicked = false; 642 buttonThree.isClicked = false; 643 buttonFour.isClicked = true; 644 } 645 else 646 buttonFour.isClicked = false; 647 al_set_sample_instance_gain(reggae1inst, (abs( (faderOne.y - 549) ) / (float) 198)); 648 } 649 else if (ev.mouse.x > buttonFive.x && 650 ev.mouse.x < buttonFive.x + buttonFive.width && 651 ev.mouse.y > buttonFive.y && 652 ev.mouse.y < buttonFive.y + buttonFive.height && 653 ev.mouse.button & 1) { 654 655 if (buttonFive.isClicked == false) { 656 buttonFive.isClicked = true; 657 buttonSix.isClicked = false; 658 buttonSeven.isClicked = false; 659 buttonEight.isClicked = false; 660 } 661 else 662 buttonFive.isClicked = false; 663 664 al_set_sample_instance_gain(reggae2inst, (abs( (faderTwo.y - 549) ) / (float) 198)); 665 } 666 else if (ev.mouse.x > buttonSix.x && 667 ev.mouse.x < buttonSix.x + buttonSix.width && 668 ev.mouse.y > buttonSix.y && 669 ev.mouse.y < buttonSix.y + buttonSix.height && 670 ev.mouse.button & 1) { 671 672 if (buttonSix.isClicked == false) { 673 buttonFive.isClicked = false; 674 buttonSix.isClicked = true; 675 buttonSeven.isClicked = false; 676 buttonEight.isClicked = false; 677 } 678 else 679 buttonSix.isClicked = false; 680 al_set_sample_instance_gain(jazz2inst, (abs( (faderTwo.y - 549) ) / (float) 198)); 681 } 682 else if (ev.mouse.x > buttonSeven.x && 683 ev.mouse.x < buttonSeven.x + buttonSeven.width && 684 ev.mouse.y > buttonSeven.y && 685 ev.mouse.y < buttonSeven.y + buttonSeven.height && 686 ev.mouse.button & 1) { 687 688 if (buttonSeven.isClicked == false) { 689 buttonFive.isClicked = false; 690 buttonSix.isClicked = false; 691 buttonSeven.isClicked = true; 692 buttonEight.isClicked = false; 693 } 694 else 695 buttonSeven.isClicked = false; 696 al_set_sample_instance_gain(rock2inst, (abs( (faderTwo.y - 549) ) / (float) 198)); 697 } 698 else if (ev.mouse.x > buttonEight.x && 699 ev.mouse.x < buttonEight.x + buttonEight.width && 700 ev.mouse.y > buttonEight.y && 701 ev.mouse.y < buttonEight.y + buttonEight.height && 702 ev.mouse.button & 1) { 703 704 if (buttonEight.isClicked == false) { 705 buttonFive.isClicked = false; 706 buttonSix.isClicked = false; 707 buttonSeven.isClicked = false; 708 buttonEight.isClicked = true; 709 } 710 else 711 buttonEight.isClicked = false; 712 al_set_sample_instance_gain(hiphop2inst, (abs( (faderTwo.y - 549) ) / (float) 198)); 713 } 714 else if (ev.mouse.x > buttonNine.x && 715 ev.mouse.x < buttonNine.x + buttonNine.width && 716 ev.mouse.y > buttonNine.y && 717 ev.mouse.y < buttonNine.y + buttonNine.height && 718 ev.mouse.button & 1) { 719 720 if (buttonNine.isClicked == false) { 721 buttonNine.isClicked = true; 722 buttonTen.isClicked = false; 723 buttonEleven.isClicked = false; 724 buttonTwelve.isClicked = false; 725 } 726 else 727 buttonNine.isClicked = false; 728 729 al_set_sample_instance_gain(rock3inst, (abs( (faderThree.y - 549) ) / (float) 198)); 730 } 731 else if (ev.mouse.x > buttonTen.x && 732 ev.mouse.x < buttonTen.x + buttonTen.width && 733 ev.mouse.y > buttonTen.y && 734 ev.mouse.y < buttonTen.y + buttonTen.height && 735 ev.mouse.button & 1) { 736 737 if (buttonTen.isClicked == false) { 738 buttonNine.isClicked = false; 739 buttonTen.isClicked = true; 740 buttonEleven.isClicked = false; 741 buttonTwelve.isClicked = false; 742 } 743 else 744 buttonTen.isClicked = false; 745 al_set_sample_instance_gain(hiphop3inst, (abs( (faderThree.y - 549) ) / (float) 198)); 746 747 } 748 else if (ev.mouse.x > buttonEleven.x && 749 ev.mouse.x < buttonEleven.x + buttonEleven.width && 750 ev.mouse.y > buttonEleven.y && 751 ev.mouse.y < buttonEleven.y + buttonEleven.height && 752 ev.mouse.button & 1) { 753 754 if (buttonEleven.isClicked == false) { 755 buttonNine.isClicked = false; 756 buttonTen.isClicked = false; 757 buttonEleven.isClicked = true; 758 buttonTwelve.isClicked = false; 759 } 760 else 761 buttonEleven.isClicked = false; 762 763 al_set_sample_instance_gain(reggae3inst, (abs( (faderThree.y - 549) ) / (float) 198)); 764 } 765 else if (ev.mouse.x > buttonTwelve.x && 766 ev.mouse.x < buttonTwelve.x + buttonTwelve.width && 767 ev.mouse.y > buttonTwelve.y && 768 ev.mouse.y < buttonTwelve.y + buttonTwelve.height && 769 ev.mouse.button & 1) { 770 771 if (buttonTwelve.isClicked == false) { 772 buttonNine.isClicked = false; 773 buttonTen.isClicked = false; 774 buttonEleven.isClicked = false; 775 buttonTwelve.isClicked = true; 776 } 777 else 778 buttonTwelve.isClicked = false; 779 al_set_sample_instance_gain(jazz3inst, (abs( (faderThree.y - 549) ) / (float) 198)); 780 } 781 else if (ev.mouse.x > buttonThirteen.x && 782 ev.mouse.x < buttonThirteen.x + buttonThirteen.width && 783 ev.mouse.y > buttonThirteen.y && 784 ev.mouse.y < buttonThirteen.y + buttonThirteen.height && 785 ev.mouse.button & 1) { 786 787 if (buttonThirteen.isClicked == false) { 788 buttonThirteen.isClicked = true; 789 buttonFourteen.isClicked = false; 790 buttonFifteen.isClicked = false; 791 buttonSixteen.isClicked = false; 792 } 793 else 794 buttonThirteen.isClicked = false; 795 796 al_set_sample_instance_gain(reggae4inst, (abs( (faderFour.y - 549) ) / (float) 198)); 797 798 } 799 else if (ev.mouse.x > buttonFourteen.x && 800 ev.mouse.x < buttonFourteen.x + buttonFourteen.width && 801 ev.mouse.y > buttonFourteen.y && 802 ev.mouse.y < buttonFourteen.y + buttonFourteen.height && 803 ev.mouse.button & 1) { 804 805 if (buttonFourteen.isClicked == false) { 806 buttonThirteen.isClicked = false; 807 buttonFourteen.isClicked = true; 808 buttonFifteen.isClicked = false; 809 buttonSixteen.isClicked = false; 810 } 811 else 812 buttonFourteen.isClicked = false; 813 al_set_sample_instance_gain(jazz4inst, (abs( (faderFour.y - 549) ) / (float) 198)); 814 } 815 else if (ev.mouse.x > buttonFifteen.x && 816 ev.mouse.x < buttonFifteen.x + buttonFifteen.width && 817 ev.mouse.y > buttonFifteen.y && 818 ev.mouse.y < buttonFifteen.y + buttonFifteen.height && 819 ev.mouse.button & 1) { 820 821 if (buttonFifteen.isClicked == false) { 822 buttonThirteen.isClicked = false; 823 buttonFourteen.isClicked = false; 824 buttonFifteen.isClicked = true; 825 buttonSixteen.isClicked = false; 826 } 827 else 828 buttonFifteen.isClicked = false; 829 al_set_sample_instance_gain(rock4inst, (abs( (faderFour.y - 549) ) / (float) 198)); 830 831 } 832 else if (ev.mouse.x > buttonSixteen.x && 833 ev.mouse.x < buttonSixteen.x + buttonSixteen.width && 834 ev.mouse.y > buttonSixteen.y && 835 ev.mouse.y < buttonSixteen.y + buttonSixteen.height && 836 ev.mouse.button & 1) { 837 838 if (buttonSixteen.isClicked == false) { 839 buttonThirteen.isClicked = false; 840 buttonFourteen.isClicked = false; 841 buttonFifteen.isClicked = false; 842 buttonSixteen.isClicked = true; 843 } 844 else 845 buttonSixteen.isClicked = false; 846 al_set_sample_instance_gain(hiphop4inst, (abs( (faderFour.y - 549) ) / (float) 198)); 847 } 848 else if (ev.mouse.x > mixbutton.x && 849 ev.mouse.x < mixbutton.x + mixbutton.width && 850 ev.mouse.y > mixbutton.y && 851 ev.mouse.y < mixbutton.y + mixbutton.height && 852 ev.mouse.button & 1) { 853 854 //this flag is set to true so that we enter the score screen when we click the 855 //mix button 856 isRoundOver = true; 857 858 //the counter variable 'i' is checked before we enter the score screen so that 859 //we enter the correct calculations 860 if (i == 1) 861 isKennyG = true; 862 if (i == 2) 863 isPDiddy = true; 864 if (i == 3) 865 isKingTubby = true; 866 if (i == 4) 867 isDimebagDarrell = true; 868 869 } 870 } 871 //this is the beginning of the score screen where the ratios and score will be calculated 872 //SCORE CALCULATION STUFF HERE: NOT IMPORTANT...PUTS US BACK INTO THE MAIN ROUND SCREEN 873 } 874 } 875 } //end MAIN while loop 876 877 //Game over screen 878 //InitGameOver(); 879 880//DESTROY STUFF: 881 882 return 0; 883}

Peter Wang
Member #23
April 2000

al_wait_for_event blocks (will not return) until there is an event in the queue. When you move the mouse an event is generated, that's when it returns and allows your program to progress. Look on these forums and the wiki about main loops. Most solutions will use timers.

manderson
Member #13,821
December 2011

Ah, that makes sense...so should I call something like

al_is_event_queue_empty();

if I want something within the ALLEGRO_BUTTON_DOWN_ to happen right away?

Edgar Reynaldo
Member #8,592
May 2007
avatar

The basic idea is to process all available events and if necessary, set a redraw flag to true (which usually happens every time a timer ticks).

To show you what I mean :

#SelectExpand
1//... Setup stuff... 2 3// Event loop 4while (!quit) { 5 do { 6 ALLEGRO_EVENT ev; 7 al_wait_for_event(queue , &ev); 8 if (ev.type == ALLEGRO_EVENT_TIMER) { 9 redraw = true; 10 } 11 } while (!al_is_event_queue_empty(queue)); 12 if (redraw) { 13 redraw = false; 14 Draw(); 15 } 16}

So, the do loop uses up all the events, a timer event sets a redraw flag, and when the events are all dealt with, it checks if it needs to redraw.

manderson
Member #13,821
December 2011

Thanks for the replies.

I didn't set up a timer initially for this game because everything is static (besides the samples playing); no animation or anything. Is the only option to set up a timer or is the redraw flag sufficient? I.E from your example:

#SelectExpand
1/... Setup stuff... 2//... Setup stuff... 3 4// Event loop 5while (!quit) { 6 do { 7 ALLEGRO_EVENT ev; 8 al_wait_for_event(queue , &ev); 9 if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 10 11 if (buttonOne.isClicked) { 12 buttonOne.isClicked = false: 13 14 redraw = true; 15 } 16 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) { 17 //check fader postions etc... 18 19 redraw = true; 20 21 } 22 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 23 quit = true; 24} 25 26//etc... 27 28//once it gets to this point, the event queue should be empty...??? 29 30 } while (!al_is_event_queue_empty(queue)); 31 if (redraw) { 32 redraw = false; 33 Draw(); 34 } 35}

I will tool around with it some more. I get what you're saying, just not sure if I need the timer or not. :-/

Edgar Reynaldo
Member #8,592
May 2007
avatar

Well, you could use 'dirty rectangles' and only update the screen when something actually changes, but that is A LOT more work than simply redrawing the screen every time a timer ticks, and with A5's hardware acceleration, you shouldn't have any problems.

Thomas Fjellstrom
Member #476
June 2000
avatar

manderson said:

I didn't set up a timer initially for this game because everything is static (besides the samples playing); no animation or anything. Is the only option to set up a timer or is the redraw flag sufficient?

Yeah, you can trigger it manually when you get other events.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

manderson
Member #13,821
December 2011

I finally figured it out, thanks to ya'lls advice/ suggestions! Ultimately it was just a matter of someone explaining how the mechanics are actually working.

My loop is now on a timer and very responsive! Thanks ;D

I'll give you all street cred for my game for being such a greatly supportive community...and I'll probably have many more questions along the way hehe.

manderson

Go to: