<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>How to make a resizable window in Allegro 5</title>
		<link>http://www.allegro.cc/forums/view/617238</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 26 Jan 2018 17:17:07 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello guys, this is my first post on this site so I hope I&#39;m not making anything wrong.<br />Straight to the point:<br />I&#39;m developing a simple game in C, that consists of zombies chasing humans, and is executed and played on the ubuntu console but I wanted to make another window with an actual dynamic interface. So what I did was create a separate file that reads the program and executes an allegro window to show it.<br />I already have it working perfectly, but I wanted the window to be resizable!! Problem is I was able to make it resizable but only the display, all of the contents (like the background and the zombies and humans) stay in the same place...</p><p>I use three functions in the document for the visualization of the world, one to create it (showworld_new), one to update it (showworld_update) and one to destroy it (showworld_destroy), which I then call on the main program.</p><p>If you could help me with any tips u have</p><p>#include &quot;showworld.h&quot;<br />#include &lt;stdio.h&gt;<br />#include &lt;stdlib.h&gt;<br />#include &quot;allegro5/allegro.h&quot;<br />#include &quot;allegro5/allegro_font.h&quot;<br />#include &quot;allegro5/allegro_ttf.h&quot;<br />#include &quot;allegro5/allegro_image.h&quot;<br />#include &quot;allegro5/allegro_color.h&quot;<br />#include &quot;allegro5/allegro_primitives.h&quot;</p><p>/* The implementation of <span class="source-code">SHOWWORLD</span> type used in this simple text-based world<br /> * visualization code. In this simple case, we only need to keep track of the<br /> * world dimensions and of the function pointer which knows how to read an<br /> * agent from the world data structure.<br /> * */<br />struct showworld {<br />    unsigned int xdim;<br />    unsigned int ydim;<br />    get_agent_info_at aginfo_func;<br />};</p><p>/* Variable for the canvas */<br />ALLEGRO_DISPLAY *display = NULL;</p><p>ALLEGRO_EVENT_QUEUE *event_queue;<br />ALLEGRO_EVENT event;</p><p>ALLEGRO_TIMEOUT timeout;</p><p>ALLEGRO_FONT *font1 = NULL;<br />ALLEGRO_FONT *font2 = NULL;</p><p>/* Variables of the drawings of the agents on the canvas*/<br />ALLEGRO_BITMAP *h_playable = NULL;<br />ALLEGRO_BITMAP *h_ai = NULL;<br />ALLEGRO_BITMAP *z_playable = NULL;<br />ALLEGRO_BITMAP *z_ai = NULL;<br />ALLEGRO_BITMAP *none = NULL;</p><p>int w_init, h_init, w_new, h_new;<br />int reason_w = 1, reason_h = 1;</p><p>/* Create a new display/visualization object for the simulation world.<br /> *<br /> * This function obeys the <span class="source-code">showworld_new<span class="k2">(</span><span class="k2">)</span></span> prototype defined in<br /> * <span class="source-code">showworld.h</span>. */<br />SHOWWORLD *showworld_new(<br />    unsigned int xdim,<br />    unsigned int ydim,<br />    get_agent_info_at aginfo_func) {</p><p>    al_init();<br />    al_init_image_addon();<br />    al_init_font_addon();<br />    al_init_ttf_addon();</p><p>    al_set_new_display_flags(ALLEGRO_WINDOWED);<br />    al_set_new_display_flags(ALLEGRO_RESIZABLE);</p><p>    SHOWWORLD *sw = NULL;<br />    sw = malloc(sizeof(SHOWWORLD));<br />    sw-&gt;xdim = xdim;<br />    sw-&gt;ydim = ydim;<br />    sw-&gt;aginfo_func = aginfo_func;</p><p>    display = al_create_display((sw-&gt;xdim)*32, (sw-&gt;ydim)*32);</p><p>    event_queue = al_create_event_queue();<br />    al_register_event_source(event_queue, al_get_display_event_source(display));<br />    event.type == ALLEGRO_EVENT_DISPLAY_RESIZE;<br />    al_acknowledge_resize(display); //MY MAIN PROBLEM IS HERE, I DONT UNDERSTAND </p><p>    font1 = al_load_font(&quot;uni0553.ttf&quot;,16,0);<br />    font2 = al_load_font(&quot;uni0553.ttf&quot;,16,0);<br />    al_clear_to_color(al_map_rgb(0,0,0));</p><p>    w_init = (int *)((sw-&gt;xdim) * 32);<br />    h_init = (int *)((sw-&gt;ydim) * 32);</p><p>    w_new = al_get_display_width(display);<br />    h_new = al_get_display_height(display);</p><p>    if (w_init &gt; w_new) {<br />      reason_w = w_init/w_new;<br />    } else {<br />      reason_w = w_new/w_init;<br />    }</p><p>    if (h_init &gt; h_new) {<br />      reason_h = h_init/h_new;<br />    } else {<br />      reason_h = h_new/h_init;<br />    }</p><p>    printf(&quot;%d\n&quot;, h_new);<br />  /*  w_init = ((sw-&gt;xdim) * 32);<br />    h_init = ((sw-&gt;ydim) * 32);</p><p>    w_new = al_get_display_width(display);<br />    h_new = al_get_display_width(display);</p><p>    if (w_init &gt; w_new) {<br />      reason_w = w_init/w_new;<br />    } else {<br />      reason_w = w_new/w_init;<br />    }</p><p>    if (h_init &gt; h_new) {<br />      reason_h = h_init/h_new;<br />    } else {<br />      reason_h = h_new/h_init;<br />    }</p><p>    al_resize_display(display, w_init * reason_w, h_init * reason_h ); /*</p><p>    /*  int larg = xdim.innerWidth;<br />      int alt = ydim.innerHeight;<br />      display.setAttribute(&quot;width&quot;, larg);<br />      display.setAttribute(&quot;height&quot;, alt);*/</p><p>      /*if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {<br />        al_destroy_event_queue(event_queue);<br />        al_destroy_display(display);<br />      }*/<br />      /*if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {<br />        al_resize_display(display,(sw-&gt;xdim)+1,(sw-&gt;ydim)+1);<br />        al_get_window_constraints(display,(sw-&gt;xdim),(sw-&gt;ydim),(sw-&gt;xdim)*100,(sw-&gt;xdim)*100);<br />        al_acknowledge_resize(display);<br />      }*/</p><p>      return sw;</p><p>}</p><p>/* Destroy a display/visualization object for the simulation world.<br /> *<br /> * This function obeys the <span class="source-code">showworld_destroy<span class="k2">(</span><span class="k2">)</span></span> prototype defined in<br /> * <span class="source-code">showworld.h</span>. */<br />void showworld_destroy(SHOWWORLD *sw) {</p><p>    al_destroy_bitmap(h_playable);<br />    al_destroy_bitmap(h_ai);<br />    al_destroy_bitmap(z_playable);<br />    al_destroy_bitmap(z_ai);<br />    al_destroy_display(display);<br />    al_destroy_event_queue(event_queue);<br />    free(sw);<br />}</p><p>/* Update the simulation world display/visualization.<br /> *<br /> * This function obeys the <span class="source-code">showworld_update<span class="k2">(</span><span class="k2">)</span></span> prototype defined in<br /> * <span class="source-code">showworld.h</span>. */<br />void showworld_update(SHOWWORLD *sw, void *w) {</p><p>    printf(&quot;%d\n&quot;, h_new);</p><p>    /* Load the respective images of each type */<br />    h_playable = al_load_bitmap(&quot;Human1.png&quot;);<br />    h_ai = al_load_bitmap(&quot;Human2.png&quot;);<br />    z_playable = al_load_bitmap(&quot;Zombie1.png&quot;);<br />    z_ai = al_load_bitmap(&quot;Zombie2.png&quot;);<br />    none = al_load_bitmap(&quot;Bg.png&quot;);</p><p>    /* Cycle through all the rows */<br />    for (unsigned int y = 0; y &lt; sw-&gt;ydim; ++y) {</p><p>        /* Cycle through all the columns for the current row */<br />        for (unsigned int x = 0; x &lt; sw-&gt;xdim; ++x) {</p><p>            /* Get state of the world (in bit packed fashion) using the user<br />               supplied function. */<br />            unsigned int item = sw-&gt;aginfo_func(w, x, y);</p><p>            /* Extract the agent type (2 bits). */<br />            AGENT_TYPE ag_type = item &amp; 0x3;<br />            /* Extract whether the agent is playable (1 bit). */<br />            unsigned char playable = (item &gt;&gt; 2) &amp; 0x1;<br />            /* Extract the agent ID (16 bits). */<br />            unsigned short ag_id = (item &gt;&gt; 3) &amp; 0xFFFF;</p><p>            /* Determine the agent type. */<br />            switch (ag_type) {</p><p>                /* If no agent is present at (x,y), draw Bg. */<br />                case None:<br />                    al_draw_bitmap(none, x*32*reason_w, y*32*reason_h, 0);<br />                    break;</p><p>                /* If human agent present at (x,y) draw Human1 or Human2. */<br />                case Human:<br />                    if (playable) {<br />                        /* Human1 for player-controlled human agent. */<br />                        al_draw_bitmap(h_playable, x*32*reason_w, y*32*reason_h, 0);<br />                    } else {<br />                        /* Human2 for AI-controlled human agent. */<br />                        al_draw_bitmap(h_ai, x*32*reason_w, y*32*reason_h, 0);<br />                    }<br />                    /* Print the agent ID in front of it&#39;s image. */<br />                    al_draw_textf(font1, al_map_rgb(255,255,255), x*32*reason_w, (y*32+10)*reason_h, 0, &quot;%i&quot;, ag_id);<br />                    //printf(&quot;%02X &quot;, ag_id);<br />                    break;</p><p>                /* If zombie agent present at (x,y) draw Zombie1 or Zombie2. */<br />                case Zombie:<br />                    if (playable) {<br />                        /* Zombie1 for player-controlled zombie agent. */<br />                        al_draw_bitmap(z_playable, x*32*reason_w, y*32*reason_h, 0);<br />                    } else {<br />                        /* Zombie2 for AI-controlled zombie agent. */<br />                        al_draw_bitmap(z_ai, x*32*reason_w, y*32*reason_h, 0);<br />                    }<br />                    /* Print the agent ID in front of the image. */<br />                    al_draw_textf(font1, al_map_rgb(200,0,0), x*32*reason_w, (y*32+10)*reason_h, 0, &quot;%i&quot;, ag_id);<br />                    //printf(&quot;%02X &quot;, ag_id);<br />                    break;</p><p>                /* Print &#39;?&#39; if unknown type detected. This should <b>never</b><br />                   happen. */<br />                default:<br />                    al_draw_text(font1, al_map_rgb(255,255,255), x*32*reason_w, y*32*reason_h, 0, &quot;?&quot;);</p><p>            }<br />        }</p><p>        /* Print two newlines after each row. */<br />        //printf(&quot;\n\n&quot;);</p><p>    }<br />    al_resize_display(display, w_init * reason_w, h_init * reason_h );<br />    /* Print a newline after world is shown/updated. */<br />    al_flip_display();<br />    //printf(&quot;\n&quot;);</p><p>}
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DidaFM)</author>
		<pubDate>Fri, 26 Jan 2018 08:34:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>To make it easier to read your post please edit it (there is an icon with a pencil) and wrap the code in &lt;code&gt; tags (XHTML style). <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>I don&#39;t know of a good answer to your question, but that will help somebody that does. I think it&#39;s rare for games to be resizeable in this way precisely for this reason. It&#39;s difficult to scale all of the drawn content to fit the window properly. A robust job might have to detect certain &quot;thresholds&quot; and change the scale of content (e.g., text) to keep it usable. Most games just let you choose a &quot;resolution&quot; and then can either load content specifically made for that resolution, or possibly scale the content dynamically at the setting change to adjust. I think Allegro 4 used to support scaled drawing too so it&#39;s likely Allegro 5 supports it too, but the graphics will necessarily lose quality in the process.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 26 Jan 2018 09:41:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You need to bitwise OR the values in the argument to the function <span class="source-code"><a href="http://www.allegro.cc/manual/al_set_new_display_flags"><span class="a">al_set_new_display_flags</span></a></span> like so :</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_set_new_display_flags"><span class="a">al_set_new_display_flags</span></a><span class="k2">(</span>ALLEGRO_OPENGL <span class="k3">|</span> ALLEGRO_WINDOWED <span class="k3">|</span> ALLEGRO_RESIZABLE<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Also, you don&#39;t want to call <span class="source-code"><a href="http://www.allegro.cc/manual/al_resize_display"><span class="a">al_resize_display</span></a></span> when you get an <a href="http://liballeg.org/a5docs/trunk/events.html#allegro_event_display_resize">ALLEGRO_EVENT_DISPLAY_RESIZE</a> event. You only want to call <span class="source-code"><a href="http://www.allegro.cc/manual/al_acknowledge_resize"><span class="a">al_acknowledge_resize</span></a></span>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 26 Jan 2018 17:17:07 +0000</pubDate>
	</item>
</rss>
