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
|
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <stdlib.h>
#include <math.h>
#include "common.c"
#define FPS 60
static struct Example {
ALLEGRO_DISPLAY *display;
ALLEGRO_FONT *font;
ALLEGRO_BITMAP *bitmaps[2][9];
ALLEGRO_COLOR bg, fg, info;
int bitmap;
int ticks;
} example;
static int const filter_flags[6] = {
0,
ALLEGRO_MIN_LINEAR,
ALLEGRO_MIPMAP,
ALLEGRO_MIN_LINEAR | ALLEGRO_MIPMAP,
0,
ALLEGRO_MAG_LINEAR
};
static char const *filter_text[4] = {
"nearest", "linear",
"nearest mipmap", "linear mipmap"
};
static void update(void)
{
example.ticks++;
}
static void redraw(void)
{
int w = al_get_display_width(example.display);
int h = al_get_display_height(example.display);
int i;
al_clear_to_color(example.bg);
for (i = 0; i < 6; i++) {
float x = (i / 2) * w / 3 + w / 6;
float y = (i % 2) * h / 2 + h / 4;
ALLEGRO_BITMAP *bmp = example.bitmaps[example.bitmap][i];
float bw = al_get_bitmap_width(bmp);
float bh = al_get_bitmap_height(bmp);
float t = 1 - 2 * fabs((example.ticks % (FPS * 16)) / 16.0 / FPS - 0.5);
float scale;
float angle = example.ticks * ALLEGRO_PI * 2 / FPS / 8;
if (i < 4)
scale = 1 - t * 0.9;
else
scale = 1 + t * 9;
al_draw_textf(example.font, example.fg, x, y - 64 - 14,
ALLEGRO_ALIGN_CENTRE, "%s", filter_text[i % 4]);
al_set_clipping_rectangle(x - 64, y - 64, 128, 128);
al_draw_scaled_rotated_bitmap(bmp, bw / 2, bh / 2,
x, y, scale, scale, angle, 0);
al_set_clipping_rectangle(0, 0, w, h);
}
al_draw_textf(example.font, example.info, w / 2, h - 14,
ALLEGRO_ALIGN_CENTRE, "press space to change");
}
int main(int argc, char **argv)
{
ALLEGRO_TIMER *timer;
ALLEGRO_EVENT_QUEUE *queue;
int w = 640, h = 480;
bool done = false;
bool need_redraw = true;
int i;
ALLEGRO_BITMAP *mysha;
(void)argc;
(void)argv;
if (!al_init()) {
abort_example("Failed to init Allegro.\n");
}
if (!al_init_image_addon()) {
abort_example("Failed to init IIO addon.\n");
}
al_init_font_addon();
init_platform_specific();
example.display = al_create_display(w, h);
if (!example.display) {
abort_example("Error creating display.\n");
}
if (!al_install_keyboard()) {
abort_example("Error installing keyboard.\n");
}
if (!al_install_mouse()) {
abort_example("Error installing mouse.\n");
}
example.font = al_load_font("data/fixed_font.tga", 0, 0);
if (!example.font) {
abort_example("Error loading data/fixed_font.tga\n");
}
mysha = al_load_bitmap("data/mysha256x256.png");
if (!mysha) {
abort_example("Error loading data/mysha256x256.png\n");
}
for (i = 0; i < 6; i++) {
ALLEGRO_LOCKED_REGION *lock;
int x, y;
/* Only power-of-two bitmaps can have mipmaps. */
al_set_new_bitmap_flags(filter_flags[i]);
example.bitmaps[0][i] = al_create_bitmap(1024, 1024);
example.bitmaps[1][i] = al_clone_bitmap(mysha);
lock = al_lock_bitmap(example.bitmaps[0][i],
ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY);
for (y = 0; y < 1024; y++) {
unsigned char *row = (unsigned char *)lock->data + lock->pitch * y;
unsigned char *ptr = row;
for (x = 0; x < 1024; x++) {
int c = 0;
if (((x >> 2) & 1) ^ ((y >> 2) & 1)) c = 255;
*(ptr++) = c;
*(ptr++) = c;
*(ptr++) = c;
*(ptr++) = 255;
}
}
al_unlock_bitmap(example.bitmaps[0][i]);
}
example.bg = al_map_rgb_f(0, 0, 0);
example.fg = al_map_rgb_f(1, 1, 1);
example.info = al_map_rgb_f(0.5, 0.5, 1);
timer = al_create_timer(1.0 / FPS);
queue = al_create_event_queue();
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
al_register_event_source(queue, al_get_timer_event_source(timer));
al_register_event_source(queue, al_get_display_event_source(example.display));
al_start_timer(timer);
while (!done) {
ALLEGRO_EVENT event;
if (need_redraw && al_is_event_queue_empty(queue)) {
redraw();
al_flip_display();
need_redraw = false;
}
al_wait_for_event(queue, &event);
switch (event.type) {
case ALLEGRO_EVENT_KEY_DOWN:
if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
done = true;
if (event.keyboard.keycode == ALLEGRO_KEY_SPACE)
example.bitmap = (example.bitmap + 1) % 2;
break;
case ALLEGRO_EVENT_DISPLAY_CLOSE:
done = true;
break;
case ALLEGRO_EVENT_TIMER:
update();
need_redraw = true;
break;
case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
example.bitmap = (example.bitmap + 1) % 2;
break;
}
}
for (i = 0; i < 6; i++) {
al_destroy_bitmap(example.bitmaps[0][i]);
al_destroy_bitmap(example.bitmaps[1][i]);
}
return 0;
}
/* vim: set sts=3 sw=3 et: */
|