File: ex_membmp.c

package info (click to toggle)
allegro5 2%3A5.2.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,820 kB
  • sloc: ansic: 109,795; cpp: 12,976; objc: 4,592; java: 2,845; python: 2,595; javascript: 1,238; sh: 1,008; makefile: 40; xml: 27; pascal: 24
file content (126 lines) | stat: -rw-r--r-- 3,286 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>

#include "allegro5/allegro.h"
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_image.h"

#include "common.c"

static void print(ALLEGRO_FONT *myfont, char *message, int x, int y)
{
   al_draw_text(myfont, al_map_rgb(0, 0, 0), x+2, y+2, 0, message);
   al_draw_text(myfont, al_map_rgb(255, 255, 255), x, y, 0, message);
}

static bool test(ALLEGRO_BITMAP *bitmap, ALLEGRO_FONT *font, char *message)
{
   ALLEGRO_EVENT_QUEUE *queue;
   ALLEGRO_EVENT event;
   double start_time;
   long frames = 0;
   double fps = 0;
   char second_line[100];
   bool quit = false;

   queue = al_create_event_queue();
   al_register_event_source(queue, al_get_keyboard_event_source());

   start_time = al_get_time();

   for (;;) {
      if (al_get_next_event(queue, &event)) {
         if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
            if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) {
               break;
            }
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
               quit = true;
               break;
            }
         }
      }

      al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);

      /* Clear the backbuffer with red so we can tell if the bitmap does not
       * cover the entire backbuffer.
       */
      al_clear_to_color(al_map_rgb(255, 0, 0));

      al_draw_scaled_bitmap(bitmap, 0, 0,
         al_get_bitmap_width(bitmap),
         al_get_bitmap_height(bitmap),
         0, 0,
         al_get_bitmap_width(al_get_target_bitmap()),
         al_get_bitmap_height(al_get_target_bitmap()),
         0);
      
      al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);

      /* Note this makes the memory buffer case much slower due to repeated
       * locking of the backbuffer.  Officially you can't use al_lock_bitmap
       * to solve the problem either.
       */
      print(font, message, 0, 0);
      sprintf(second_line, "%.1f FPS", fps);
      print(font, second_line, 0, al_get_font_line_height(font)+5);

      al_flip_display();

      frames++;
      fps = (double)frames / (al_get_time() - start_time);
   }

   al_destroy_event_queue(queue);

   return quit;
}

int main(int argc, char **argv)
{
   ALLEGRO_DISPLAY *display;
   ALLEGRO_FONT *accelfont;
   ALLEGRO_FONT *memfont;
   ALLEGRO_BITMAP *accelbmp;
   ALLEGRO_BITMAP *membmp;

   (void)argc;
   (void)argv;

   if (!al_init()) {
      abort_example("Could not init Allegro.\n");
   }

   al_install_keyboard();
   al_init_image_addon();
   al_init_font_addon();
   init_platform_specific();

   display = al_create_display(640, 400);
   if (!display) {
      abort_example("Error creating display\n");
   }

   accelfont = al_load_font("data/font.tga", 0, 0);
   if (!accelfont) {
      abort_example("font.tga not found\n");
   }
   accelbmp = al_load_bitmap("data/mysha.pcx");
   if (!accelbmp) {
      abort_example("mysha.pcx not found\n");
   }

   al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);

   memfont = al_load_font("data/font.tga", 0, 0);
   membmp = al_load_bitmap("data/mysha.pcx");

   for (;;) {
      if (test(membmp, memfont, "Memory bitmap (press SPACE key)"))
         break;
      if (test(accelbmp, accelfont, "Accelerated bitmap (press SPACE key)"))
         break;
   }

   return 0;
}