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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_primitives.h"
#include "common.c"
static const int load_total = 100;
static int load_count = 0;
static ALLEGRO_BITMAP *bitmaps[100];
static ALLEGRO_MUTEX *mutex;
static void *loading_thread(ALLEGRO_THREAD *thread, void *arg)
{
ALLEGRO_FONT *font;
ALLEGRO_COLOR text;
font = al_load_font("data/fixed_font.tga", 0, 0);
text = al_map_rgb_f(255, 255, 255);
/* In this example we load mysha.pcx 100 times to simulate loading
* many bitmaps.
*/
load_count = 0;
while (load_count < load_total) {
ALLEGRO_COLOR color;
ALLEGRO_BITMAP *bmp;
color = al_map_rgb(rand() % 256, rand() % 256, rand() % 256);
color.r /= 4;
color.g /= 4;
color.b /= 4;
color.a /= 4;
if (al_get_thread_should_stop(thread))
break;
bmp = al_load_bitmap("data/mysha.pcx");
/* Simulate different contents. */
al_set_target_bitmap(bmp);
al_draw_filled_rectangle(0, 0, 320, 200, color);
al_draw_textf(font, text, 0, 0, 0, "bitmap %d", 1 + load_count);
al_set_target_bitmap(NULL);
/* Allow the main thread to see the completed bitmap. */
al_lock_mutex(mutex);
bitmaps[load_count] = bmp;
load_count++;
al_unlock_mutex(mutex);
/* Simulate that it's slow. */
al_rest(0.05);
}
al_destroy_font(font);
return arg;
}
static void print_bitmap_flags(ALLEGRO_BITMAP *bitmap)
{
ALLEGRO_USTR *ustr = al_ustr_new("");
if (al_get_bitmap_flags(bitmap) & ALLEGRO_VIDEO_BITMAP)
al_ustr_append_cstr(ustr, " VIDEO");
if (al_get_bitmap_flags(bitmap) & ALLEGRO_MEMORY_BITMAP)
al_ustr_append_cstr(ustr, " MEMORY");
if (al_get_bitmap_flags(bitmap) & ALLEGRO_CONVERT_BITMAP)
al_ustr_append_cstr(ustr, " CONVERT");
al_ustr_trim_ws(ustr);
al_ustr_find_replace_cstr(ustr, 0, " ", " | ");
log_printf("%s", al_cstr(ustr));
al_ustr_free(ustr);
}
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display;
ALLEGRO_TIMER *timer;
ALLEGRO_EVENT_QUEUE *queue;
bool redraw = true;
ALLEGRO_FONT *font;
ALLEGRO_BITMAP *spin, *spin2;
int current_bitmap = 0;
int loaded_bitmap = 0;
ALLEGRO_THREAD *thread;
(void)argc;
(void)argv;
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
al_init_image_addon();
al_init_font_addon();
al_init_primitives_addon();
init_platform_specific();
open_log();
al_install_mouse();
al_install_keyboard();
spin = al_load_bitmap("data/cursor.tga");
log_printf("default bitmap without display: %p\n", spin);
al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
spin2 = al_load_bitmap("data/cursor.tga");
log_printf("video bitmap without display: %p\n", spin2);
log_printf("%p before create_display: ", spin);
print_bitmap_flags(spin);
log_printf("\n");
display = al_create_display(64, 64);
if (!display) {
abort_example("Error creating display\n");
}
spin2 = al_load_bitmap("data/cursor.tga");
log_printf("video bitmap with display: %p\n", spin2);
log_printf("%p after create_display: ", spin);
print_bitmap_flags(spin);
log_printf("\n");
log_printf("%p after create_display: ", spin2);
print_bitmap_flags(spin2);
log_printf("\n");
al_destroy_display(display);
log_printf("%p after destroy_display: ", spin);
print_bitmap_flags(spin);
log_printf("\n");
log_printf("%p after destroy_display: ", spin2);
print_bitmap_flags(spin2);
log_printf("\n");
display = al_create_display(640, 480);
log_printf("%p after create_display: ", spin);
print_bitmap_flags(spin);
log_printf("\n");
log_printf("%p after create_display: ", spin2);
print_bitmap_flags(spin2);
log_printf("\n");
font = al_load_font("data/fixed_font.tga", 0, 0);
mutex = al_create_mutex();
thread = al_create_thread(loading_thread, NULL);
al_start_thread(thread);
timer = al_create_timer(1.0 / 30);
queue = al_create_event_queue();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_display_event_source(display));
al_register_event_source(queue, al_get_timer_event_source(timer));
al_start_timer(timer);
while (1) {
ALLEGRO_EVENT event;
al_wait_for_event(queue, &event);
if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
break;
if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
break;
}
if (event.type == ALLEGRO_EVENT_TIMER)
redraw = true;
if (redraw && al_is_event_queue_empty(queue)) {
float x = 20, y = 320;
int i;
ALLEGRO_COLOR color = al_map_rgb_f(0, 0, 0);
float t = al_current_time();
redraw = false;
al_clear_to_color(al_map_rgb_f(0.5, 0.6, 1));
al_draw_textf(font, color, x + 40, y, 0, "Loading %d%%",
100 * load_count / load_total);
al_lock_mutex(mutex);
if (loaded_bitmap < load_count) {
/* This will convert any video bitmaps without a display
* (all the bitmaps being loaded in the loading_thread) to
* video bitmaps we can use in the main thread.
*/
al_convert_bitmap(bitmaps[loaded_bitmap]);
loaded_bitmap++;
}
al_unlock_mutex(mutex);
if (current_bitmap < loaded_bitmap) {
int bw;
al_draw_bitmap(bitmaps[current_bitmap], 0, 0, 0);
if (current_bitmap + 1 < loaded_bitmap)
current_bitmap++;
for (i = 0; i <= current_bitmap; i++) {
bw = al_get_bitmap_width(bitmaps[i]);
al_draw_scaled_rotated_bitmap(bitmaps[i],
0, 0, (i % 20) * 640 / 20, 360 + (i / 20) * 24,
32.0 / bw, 32.0 / bw, 0, 0);
}
}
if (loaded_bitmap < load_total) {
al_draw_scaled_rotated_bitmap(spin,
16, 16, x, y, 1.0, 1.0, t * ALLEGRO_PI * 2, 0);
}
al_flip_display();
}
}
al_join_thread(thread, NULL);
al_destroy_mutex(mutex);
al_destroy_font(font);
al_destroy_display(display);
close_log(true);
return 0;
}
|