Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Center primitive after resize

This thread is locked; no one can reply to it. rss feed Print
Center primitive after resize
AleX-G Squadron
Member #13,593
October 2011
avatar

I am having some problems centering a primitive after i resize the window using a native dialog box. It will work only if i put 1:1 ratio resolutions such as 300x300 or 500x500, otherwise the primitive object wont be at the center. I want to use a resolution of 800x600 and make the primitive appear at the center. How am i supposed to do that? Here is the logic i am using:

#SelectExpand
1int resolution = al_show_native_message_box(window, "Change resolution", "800x600?", 2 "Do you really want to change resolution?", "Of course!|No", 3 4ALLEGRO_MESSAGEBOX_YES_NO); 5 6 if(resolution == 1) 7 { 8 width = width + 300; 9 height = height + 500; 10 al_resize_display(window, height, width); 11 al_flip_display(); 12 }

www.anothergames.com

jmasterx
Member #11,410
October 2009

Can you show the code that draws the primitive (a messagebox is not a primitive).

AleX-G Squadron
Member #13,593
October 2011
avatar

It is a triangle:

al_draw_triangle(x,y-15,x-15,y+15,x+15,y+15,al_map_rgb(0,255,0),2.0);

x is width/2 and y is height/2

www.anothergames.com

Trent Gamblin
Member #261
April 2000
avatar

Are you recalculating width, height, x and y after resizing the display?

AleX-G Squadron
Member #13,593
October 2011
avatar

Yes, i have to, because it doesnt work too.

www.anothergames.com

Trent Gamblin
Member #261
April 2000
avatar

    width = width + 300;
    height = height + 500;

That looks suspicious. Are you starting out with a 500x100 window?

EDIT: Instead of playing detective, just show the whole code. :P

jmasterx
Member #11,410
October 2009

To draw an isosceles triangle of width W and height H at center cx, cy:

cx = al_get_display_width / 2;
cy = al_get_display_height / 2;
p1.x = cx;
p1.y = cy - (H / 2.0f);

p2.x = cx - (W / 2.0f);
p2.y = cy + (H / 2.0f);

p3.x = cx + (W / 2.0f);
p3.y = cy + (H / 2.0f);

AleX-G Squadron
Member #13,593
October 2011
avatar

I changed some things but the idea is exactly the same

#SelectExpand
1#include <allegro5\allegro.h> 2#include <allegro5\allegro_primitives.h> 3#include <allegro5\allegro_image.h> 4#include <allegro5\allegro_native_dialog.h> 5 6enum BUTTONS{W, S, A, D}; 7 8int main (void) 9{ 10 al_init(); 11 al_init_primitives_addon(); 12 al_init_image_addon(); 13 al_install_keyboard(); 14 15 int width = 300; 16 int height = 300; 17 18 bool quit = false; 19 bool buttons[4] = {false, false, false, false}; 20 21 ALLEGRO_DISPLAY *window = al_create_display(width,height); 22 ALLEGRO_BITMAP *icon = al_load_bitmap("image/1.png"); 23 ALLEGRO_EVENT_QUEUE *event = NULL; 24 25 al_set_display_icon(window,icon); 26 27 al_flip_display(); 28 29 int resolution = al_show_native_message_box(window, "Change resolution", "600x600?", 30 "Do you really want to change resolution?", "Of course!|No", ALLEGRO_MESSAGEBOX_YES_NO); 31 32 if(resolution == 1) 33 { 34 width += 300; 35 height += 500; 36 al_resize_display(window, height, width); 37 al_flip_display(); 38 } 39 40 int x = width / 2; 41 int y = height / 2; 42 43 event = al_create_event_queue(); 44 al_register_event_source(event, al_get_keyboard_event_source()); 45 al_register_event_source(event, al_get_display_event_source(window)); 46 47 while(!quit) 48 { 49 ALLEGRO_EVENT e; 50 al_wait_for_event(event, &e); 51 52 if(e.type == ALLEGRO_EVENT_KEY_DOWN) 53 { 54 switch(e.keyboard.keycode) 55 { 56 case ALLEGRO_KEY_W: 57 buttons[W] = true; 58 break; 59 60 case ALLEGRO_KEY_S: 61 buttons[S] = true; 62 break; 63 64 case ALLEGRO_KEY_A: 65 buttons[A] = true; 66 break; 67 68 case ALLEGRO_KEY_D: 69 buttons[D] = true; 70 break; 71 } 72 } 73 else if (e.type == ALLEGRO_EVENT_KEY_UP) 74 { 75 switch(e.keyboard.keycode) 76 { 77 case ALLEGRO_KEY_W: 78 buttons[W] = false; 79 break; 80 81 case ALLEGRO_KEY_S: 82 buttons[S] = false; 83 break; 84 85 case ALLEGRO_KEY_A: 86 buttons[A] = false; 87 break; 88 89 case ALLEGRO_KEY_D: 90 buttons[D] = false; 91 break; 92 case ALLEGRO_KEY_ESCAPE: 93 quit = true; 94 break; 95 } 96 } 97 else if (e.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 98 { 99 quit = true; 100 } 101 else if(x <= 0) 102 { 103 x += 10; 104 } 105 else if(y >= height - 15) 106 { 107 y = height - 15; 108 } 109 else if(x >= width - 5) 110 { 111 x = width-15; 112 } 113 else if(y <= 15) 114 { 115 y += 10; 116 } 117 118 x -= buttons[A] * 10; 119 x += buttons[D] * 10; 120 y -= buttons[W] * 10; 121 y += buttons[S] * 10; 122 123 al_draw_triangle(x,y-15,x-15,y+15,x+15,y+15,al_map_rgb(0,255,0),2.0); 124 al_flip_display(); 125 al_clear_to_color(al_map_rgb(0,0,0)); 126 al_set_window_title(window, "Game programming"); 127 128 } 129 130 al_destroy_bitmap(icon); 131 al_destroy_display(window); 132 133 return 0; 134}

www.anothergames.com

Slartibartfast
Member #8,789
June 2007
avatar

Go to: