A5 Displays (Resize/Stretching)
jason perkins

Hi, I'm just wondering how to go about getting everything I've drawn onto a display's back buffer to line up after a resize event.

I'm a little un-sure how this works, but it seems to me that during a resize the buffer is stretched to match this display's new width/height (This is what I'd like to happen), but after the event completes the buffer is also re-sized so I end up with empty space on the right and bottom sides.

What I'm trying to do is, when the window is re-sized have whatever's drawn on the back buffer stretch to match the display.

I think this is all the relevant code:

#SelectExpand
1const int SCREEN_W = 640; 2const int SCREEN_H = 480; 3 4al_set_new_display_flags(ALLEGRO_RESIZABLE); 5display = al_create_display(SCREEN_W, SCREEN_H); 6 7if(!display) 8{ 9 fprintf(stderr, "could not create the display!\n"); 10 return -1; 11} 12 13al_register_event_source(event_queue, al_get_display_event_source(display)); 14 15 // main loop 16{... 17 al_set_target_bitmap(al_get_backbuffer(ab.getDisplay())); 18 19 while (!doExit) 20 { 21 ALLEGRO_EVENT ev; 22 al_wait_for_event(ab.getEventQueue(), &ev); 23 if (ev.type == ALLEGRO_EVENT_TIMER) 24 { 25 im.checkInput(); 26 god.getInput(); 27 al_clear_to_color(al_map_rgb(0,0,0)); 28 god.draw(); 29 30 al_flip_display(); 31 } 32 33 if(ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) 34 al_acknowledge_resize(ab.getDisplay()); 35 } 36}

Everything I draw throughout the project is based on SCREEN_W/SCREEN_H, so it would be a lot easier to leave them as is, and just alter the way the buffer is drawn on to the display. Sorry for needing spoon feeding.

SiegeLord

If you want stretching, then you can use transformations. For your purposes I think this will be sufficient:

if(ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE)
{
  al_acknowledge_resize(ab.getDisplay());
  ALLEGRO_TRANSFORMATION trans;
  al_identity_transform(&trans);
  al_scale_transform(al_get_display_width(ab.getDisplay()) / SCREEN_W, al_get_display_height(ab.getDisplay()) / SCREEN_H);
  al_use_transform(&trans);
}

EDIT: Fixed fractions... :-/

jason perkins

Thanks, I feel kind of bad. Now that transforms exist there's tonnes of questions with this exact answer. Thanks for the quick reply

weapon_S

SiegeLord, you also forgot the first argument to al_scale_transform. I'm not familiar with this function, but when I read the code I thought there was a magic global or something?!
SCREEN_W and SCREEN_H are the original window size (from the start of your program).

SiegeLord

The compiler will remind me (or anyone using that code) of what I forgotten :P

Thread #607285. Printed from Allegro.cc