1 #include <stdio.h>
2 
3 #include "allegro5/allegro.h"
4 #include "allegro5/allegro_font.h"
5 #include "allegro5/allegro_image.h"
6 
7 #include "common.c"
8 
print(ALLEGRO_FONT * myfont,char * message,int x,int y)9 static void print(ALLEGRO_FONT *myfont, char *message, int x, int y)
10 {
11    al_draw_text(myfont, al_map_rgb(0, 0, 0), x+2, y+2, 0, message);
12    al_draw_text(myfont, al_map_rgb(255, 255, 255), x, y, 0, message);
13 }
14 
test(ALLEGRO_BITMAP * bitmap,ALLEGRO_FONT * font,char * message)15 static bool test(ALLEGRO_BITMAP *bitmap, ALLEGRO_FONT *font, char *message)
16 {
17    ALLEGRO_EVENT_QUEUE *queue;
18    ALLEGRO_EVENT event;
19    double start_time;
20    long frames = 0;
21    double fps = 0;
22    char second_line[100];
23    bool quit = false;
24 
25    queue = al_create_event_queue();
26    al_register_event_source(queue, al_get_keyboard_event_source());
27 
28    start_time = al_get_time();
29 
30    for (;;) {
31       if (al_get_next_event(queue, &event)) {
32          if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
33             if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) {
34                break;
35             }
36             if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
37                quit = true;
38                break;
39             }
40          }
41       }
42 
43       al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
44 
45       /* Clear the backbuffer with red so we can tell if the bitmap does not
46        * cover the entire backbuffer.
47        */
48       al_clear_to_color(al_map_rgb(255, 0, 0));
49 
50       al_draw_scaled_bitmap(bitmap, 0, 0,
51          al_get_bitmap_width(bitmap),
52          al_get_bitmap_height(bitmap),
53          0, 0,
54          al_get_bitmap_width(al_get_target_bitmap()),
55          al_get_bitmap_height(al_get_target_bitmap()),
56          0);
57 
58       al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
59 
60       /* Note this makes the memory buffer case much slower due to repeated
61        * locking of the backbuffer.  Officially you can't use al_lock_bitmap
62        * to solve the problem either.
63        */
64       print(font, message, 0, 0);
65       sprintf(second_line, "%.1f FPS", fps);
66       print(font, second_line, 0, al_get_font_line_height(font)+5);
67 
68       al_flip_display();
69 
70       frames++;
71       fps = (double)frames / (al_get_time() - start_time);
72    }
73 
74    al_destroy_event_queue(queue);
75 
76    return quit;
77 }
78 
main(int argc,char ** argv)79 int main(int argc, char **argv)
80 {
81    ALLEGRO_DISPLAY *display;
82    ALLEGRO_FONT *accelfont;
83    ALLEGRO_FONT *memfont;
84    ALLEGRO_BITMAP *accelbmp;
85    ALLEGRO_BITMAP *membmp;
86 
87    (void)argc;
88    (void)argv;
89 
90    if (!al_init()) {
91       abort_example("Could not init Allegro.\n");
92    }
93 
94    al_install_keyboard();
95    al_init_image_addon();
96    al_init_font_addon();
97    init_platform_specific();
98 
99    display = al_create_display(640, 400);
100    if (!display) {
101       abort_example("Error creating display\n");
102    }
103 
104    accelfont = al_load_font("data/font.tga", 0, 0);
105    if (!accelfont) {
106       abort_example("font.tga not found\n");
107    }
108    accelbmp = al_load_bitmap("data/mysha.pcx");
109    if (!accelbmp) {
110       abort_example("mysha.pcx not found\n");
111    }
112 
113    al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
114 
115    memfont = al_load_font("data/font.tga", 0, 0);
116    membmp = al_load_bitmap("data/mysha.pcx");
117 
118    for (;;) {
119       if (test(membmp, memfont, "Memory bitmap (press SPACE key)"))
120          break;
121       if (test(accelbmp, accelfont, "Accelerated bitmap (press SPACE key)"))
122          break;
123    }
124 
125    return 0;
126 }
127