1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "allegro5/allegro.h"
4 #include "allegro5/allegro_image.h"
5 #include "allegro5/allegro_font.h"
6
7 #include "common.c"
8
9 typedef struct BITMAP_TYPE {
10 ALLEGRO_BITMAP* bmp;
11 ALLEGRO_BITMAP* clone;
12 ALLEGRO_BITMAP* decomp;
13 ALLEGRO_BITMAP* lock_clone;
14 ALLEGRO_PIXEL_FORMAT format;
15 const char* name;
16 } BITMAP_TYPE;
17
main(int argc,char ** argv)18 int main(int argc, char **argv)
19 {
20 const char *filename;
21 ALLEGRO_DISPLAY *display;
22 ALLEGRO_TIMER *timer;
23 ALLEGRO_EVENT_QUEUE *queue;
24 ALLEGRO_FONT *font;
25 ALLEGRO_BITMAP *bkg;
26 bool redraw = true;
27 int ii;
28 int cur_bitmap = 0;
29 bool compare = false;
30 #define NUM_BITMAPS 4
31 BITMAP_TYPE bitmaps[NUM_BITMAPS] = {
32 {NULL, NULL, NULL, NULL, ALLEGRO_PIXEL_FORMAT_ANY, "Uncompressed"},
33 {NULL, NULL, NULL, NULL, ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT1, "DXT1"},
34 {NULL, NULL, NULL, NULL, ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT3, "DXT3"},
35 {NULL, NULL, NULL, NULL, ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT5, "DXT5"},
36 };
37
38 if (argc > 1) {
39 filename = argv[1];
40 }
41 else {
42 filename = "data/mysha.pcx";
43 }
44
45 if (!al_init()) {
46 abort_example("Could not init Allegro.\n");
47 }
48
49 open_log();
50
51 if (argc > 2) {
52 al_set_new_display_adapter(atoi(argv[2]));
53 }
54
55 al_init_image_addon();
56 al_init_font_addon();
57 al_install_keyboard();
58
59 display = al_create_display(640, 480);
60 if (!display) {
61 abort_example("Error creating display\n");
62 }
63
64 for (ii = 0; ii < NUM_BITMAPS; ii++) {
65 double t0, t1;
66 al_set_new_bitmap_format(bitmaps[ii].format);
67
68 /* Load */
69 t0 = al_get_time();
70 bitmaps[ii].bmp = al_load_bitmap(filename);
71 t1 = al_get_time();
72
73 if (!bitmaps[ii].bmp) {
74 abort_example("%s not found or failed to load\n", filename);
75 }
76 log_printf("%s load time: %f sec\n", bitmaps[ii].name, t1 - t0);
77
78 /* Clone */
79 t0 = al_get_time();
80 bitmaps[ii].clone = al_clone_bitmap(bitmaps[ii].bmp);
81 t1 = al_get_time();
82
83 if (!bitmaps[ii].clone) {
84 abort_example("Couldn't clone %s\n", bitmaps[ii].name);
85 }
86 log_printf("%s clone time: %f sec\n", bitmaps[ii].name, t1 - t0);
87
88 /* Decompress */
89 al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY);
90 t0 = al_get_time();
91 bitmaps[ii].decomp = al_clone_bitmap(bitmaps[ii].bmp);
92 t1 = al_get_time();
93
94 if (!bitmaps[ii].decomp) {
95 abort_example("Couldn't decompress %s\n", bitmaps[ii].name);
96 }
97 log_printf("%s decompress time: %f sec\n", bitmaps[ii].name, t1 - t0);
98
99 /* RW lock */
100 al_set_new_bitmap_format(bitmaps[ii].format);
101 bitmaps[ii].lock_clone = al_clone_bitmap(bitmaps[ii].bmp);
102
103 if (!bitmaps[ii].lock_clone) {
104 abort_example("Couldn't clone %s\n", bitmaps[ii].name);
105 }
106
107 if (al_get_bitmap_width(bitmaps[ii].bmp) > 128
108 && al_get_bitmap_height(bitmaps[ii].bmp) > 128) {
109 int bitmap_format = al_get_bitmap_format(bitmaps[ii].bmp);
110 int block_width = al_get_pixel_block_width(bitmap_format);
111 int block_height = al_get_pixel_block_height(bitmap_format);
112
113 /* Lock and unlock it, hopefully causing a no-op operation */
114 al_lock_bitmap_region_blocked(bitmaps[ii].lock_clone,
115 16 / block_width, 16 / block_height, 64 / block_width,
116 64 / block_height, ALLEGRO_LOCK_READWRITE);
117
118 al_unlock_bitmap(bitmaps[ii].lock_clone);
119 }
120 }
121
122 bkg = al_load_bitmap("data/bkg.png");
123 if (!bkg) {
124 abort_example("data/bkg.png not found or failed to load\n");
125 }
126
127 al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY);
128 font = al_create_builtin_font();
129 timer = al_create_timer(1.0 / 30);
130 queue = al_create_event_queue();
131 al_register_event_source(queue, al_get_display_event_source(display));
132 al_register_event_source(queue, al_get_keyboard_event_source());
133 al_register_event_source(queue, al_get_timer_event_source(timer));
134 al_start_timer(timer);
135
136 while (1) {
137 ALLEGRO_EVENT event;
138 al_wait_for_event(queue, &event);
139 switch (event.type) {
140 case ALLEGRO_EVENT_DISPLAY_CLOSE:
141 goto EXIT;
142 case ALLEGRO_EVENT_TIMER:
143 redraw = true;
144 break;
145 case ALLEGRO_EVENT_KEY_DOWN:
146 switch (event.keyboard.keycode) {
147 case ALLEGRO_KEY_LEFT:
148 cur_bitmap = (cur_bitmap - 1 + NUM_BITMAPS) % NUM_BITMAPS;
149 break;
150 case ALLEGRO_KEY_RIGHT:
151 cur_bitmap = (cur_bitmap + 1) % NUM_BITMAPS;
152 break;
153 case ALLEGRO_KEY_SPACE:
154 compare = true;
155 break;
156 case ALLEGRO_KEY_ESCAPE:
157 goto EXIT;
158 }
159 break;
160 case ALLEGRO_EVENT_KEY_UP:
161 if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) {
162 compare = false;
163 }
164 break;
165 }
166 if (redraw && al_is_event_queue_empty(queue)) {
167 int w = al_get_bitmap_width(bitmaps[cur_bitmap].bmp);
168 int h = al_get_bitmap_height(bitmaps[cur_bitmap].bmp);
169 int idx = compare ? 0 : cur_bitmap;
170 redraw = false;
171 al_clear_to_color(al_map_rgb_f(0, 0, 0));
172 al_draw_bitmap(bkg, 0, 0, 0);
173 al_draw_textf(font, al_map_rgb_f(1, 1, 1), 5, 5, ALLEGRO_ALIGN_LEFT,
174 "SPACE to compare. Arrows to switch. Format: %s", bitmaps[idx].name);
175 al_draw_bitmap(bitmaps[idx].bmp, 0, 20, 0);
176 al_draw_bitmap(bitmaps[idx].clone, w, 20, 0);
177 al_draw_bitmap(bitmaps[idx].decomp, 0, 20 + h, 0);
178 al_draw_bitmap(bitmaps[idx].lock_clone, w, 20 + h, 0);
179 al_flip_display();
180 }
181 }
182 EXIT:
183
184 al_destroy_bitmap(bkg);
185 for (ii = 0; ii < NUM_BITMAPS; ii++) {
186 al_destroy_bitmap(bitmaps[ii].bmp);
187 }
188
189 close_log(false);
190
191 return 0;
192 }
193
194 /* vim: set sts=4 sw=4 et: */
195