Allegro 5 Scaling Methods

This example demonstrates the effect of different scaling methods on FPS and image quality. An area with a resolution of 320 x 240 is scaled up to a display of 640 x 480 using different techniques. The scaling method can be changed by pressing the spacebar.

Source code:


#include <allegro5/allegro.h> // required for the use of Allegro's core-API
#include <allegro5/allegro_font.h> // required for displaying text
#include <allegro5/allegro_image.h> // required for loading image files
#include <allegro5/allegro_primitives.h> // required for using drawing primitives

ALLEGRO_KEYBOARD_STATE keyboard;
ALLEGRO_DISPLAY* display;
ALLEGRO_FONT* font;
ALLEGRO_BITMAP* buffer1;
ALLEGRO_BITMAP* buffer2;
ALLEGRO_BITMAP* image;

double fpstime=0;
int fps=0;
int frames=0;
int mode=0; // scaling mode: 0 -> regular double buffer, 1 -> double buffer with content preserving disabled, 2 -> transformation
bool space=0;

void toggle_mode()
{
   if (al_key_down(&keyboard,ALLEGRO_KEY_SPACE))
   {
      if (!space)
      {
         if (mode==2)
         {
            // reset transformation
            ALLEGRO_TRANSFORM transform;
            al_identity_transform(&transform);
            al_use_transform(&transform);
         }

         if (mode<2) mode++;
         else        mode=0;

         if (mode==2)
         {
            // set transformation
            ALLEGRO_TRANSFORM transform;
            al_identity_transform(&transform);
            al_scale_transform(&transform,2,2);
            al_use_transform(&transform);
         }
      }
      space=1;
   }
   else space=0;
}

void calculate_fps()
{
   if (al_get_time()-fpstime>=1)
   {
      fps=int(frames/(al_get_time()-fpstime));
      fpstime=al_get_time();
      frames=0;
   }
}

void draw()
{
   if (mode==0)      al_set_target_bitmap(buffer1);
   else if (mode==1) al_set_target_bitmap(buffer2);
   else              al_set_target_backbuffer(display);

   al_clear_to_color(al_map_rgb(0,0,0));
   al_draw_filled_circle(160,100,50,al_map_rgb(192,0,0));
   al_draw_filled_circle(120,160,50,al_map_rgb(0,192,0));
   al_draw_filled_circle(200,160,50,al_map_rgb(0,0,192));
   al_draw_bitmap(image,10,180,0);
   al_draw_rotated_bitmap(image,93,29,160,120,0.785,0);
   al_draw_textf(font,al_map_rgb(255,255,255),10,20,0,"FPS: %i",fps);

   if (mode==0)
   {
      al_draw_textf(font,al_map_rgb(255,255,255),10,10,0,"REGULAR BUFFER");
      al_set_target_backbuffer(display);
      al_draw_scaled_bitmap(buffer1,0,0,320,240,0,0,640,480,0);
   }
   else if (mode==1)
   {
      al_draw_textf(font,al_map_rgb(255,255,255),10,10,0,"BUFFER WITHOUT CONTENT PRESERVING");
      al_set_target_backbuffer(display);
      al_draw_scaled_bitmap(buffer2,0,0,320,240,0,0,640,480,0);
   }
   else al_draw_textf(font,al_map_rgb(255,255,255),10,10,0,"TRANSFORMATION");

   al_flip_display();
}

int main(int argc,char** argv)
{
   al_init(); // init core-API
   al_init_font_addon(); // init font addon
   al_init_image_addon(); // init image addon
   al_init_primitives_addon(); // init primitives addon
   al_install_keyboard(); // init keyboard

   display=al_create_display(640,480); // create window with a size of 640 x 480
   font=al_create_builtin_font(); // use Allegro's built-in font
   image=al_load_bitmap("awiki_gimp.png"); // load image file
   buffer1=al_create_bitmap(320,240); // create regular double buffer with texture preserving enabled
   al_add_new_bitmap_flag(ALLEGRO_NO_PRESERVE_TEXTURE); // add flag to avoid texture preserving
   buffer2=al_create_bitmap(320,240); // create double buffer with texture preserving disabled

   while (!al_key_down(&keyboard,ALLEGRO_KEY_ESCAPE))
   {
      al_get_keyboard_state(&keyboard);

      toggle_mode();

      calculate_fps();

      draw();

      frames++;
   }

   al_destroy_display(display);
   al_destroy_font(font);
   al_destroy_bitmap(buffer1);
   al_destroy_bitmap(buffer2);
   al_destroy_bitmap(image);

   return 0;
}

The Allegro logo that is used as an image file can be found here.

Output:

Scaling Method 1 Scaling Method 2 Scaling Method 3