1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "allegro5/allegro.h"
5 #include "allegro5/allegro_font.h"
6 
7 #include "common.c"
8 
main(int argc,char ** argv)9 int main(int argc, char **argv)
10 {
11    ALLEGRO_DISPLAY *display;
12    ALLEGRO_BITMAP *tex1, *tex2;
13    ALLEGRO_TIMER *timer;
14    ALLEGRO_EVENT_QUEUE *queue;
15    bool redraw = true;
16    ALLEGRO_LOCKED_REGION *lock;
17    ALLEGRO_FONT *font;
18    unsigned char *p;
19    int x, y;
20 
21    (void)argc;
22    (void)argv;
23 
24    if (!al_init()) {
25       abort_example("Could not init Allegro.\n");
26    }
27    al_init_font_addon();
28 
29    al_install_mouse();
30    al_install_keyboard();
31 
32    display = al_create_display(640, 480);
33    if (!display) {
34       abort_example("Error creating display\n");
35    }
36 
37    font = al_create_builtin_font();
38 
39    tex1 = al_create_bitmap(8, 8);
40    lock = al_lock_bitmap(tex1, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY);
41    p = lock->data;
42    for (y = 0; y < 8; y++) {
43       unsigned char *lp = p;
44       for (x = 0; x < 8; x++) {
45          if (x == 0 || y == 0 || x == 7 || y == 7) {
46              p[0] = 0;
47              p[1] = 0;
48              p[2] = 0;
49              p[3] = 0;
50          }
51          else {
52              p[0] = 0;
53              p[1] = 255;
54              p[2] = 0;
55              p[3] = 255;
56          }
57          p += 4;
58       }
59       p = lp + lock->pitch;
60    }
61    al_unlock_bitmap(tex1);
62 
63    al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR);
64    tex2 = al_clone_bitmap(tex1);
65 
66    timer = al_create_timer(1.0 / 30);
67    queue = al_create_event_queue();
68    al_register_event_source(queue, al_get_keyboard_event_source());
69    al_register_event_source(queue, al_get_display_event_source(display));
70    al_register_event_source(queue, al_get_timer_event_source(timer));
71    al_start_timer(timer);
72 
73    while (1) {
74       ALLEGRO_EVENT event;
75       al_wait_for_event(queue, &event);
76 
77       if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
78          break;
79       if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
80          if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
81              break;
82          }
83       if (event.type == ALLEGRO_EVENT_TIMER)
84          redraw = true;
85 
86       if (redraw && al_is_event_queue_empty(queue)) {
87          float x = 8, y = 60;
88          float a, t = al_get_time();
89          float th = al_get_font_line_height(font);
90          ALLEGRO_COLOR color = al_map_rgb_f(0, 0, 0);
91          ALLEGRO_COLOR color2 = al_map_rgb_f(1, 0, 0);
92          ALLEGRO_COLOR color3 = al_map_rgb_f(0, 0.5, 0);
93 
94          t /= 10;
95          a = t - floor(t);
96          a *= 2 * ALLEGRO_PI;
97 
98          redraw = false;
99          al_clear_to_color(al_map_rgb_f(0.5, 0.6, 1));
100 
101          al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
102          al_draw_textf(font, color, x, y, 0, "not premultiplied");
103          al_draw_textf(font, color, x, y + th, 0, "no filtering");
104          al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
105          al_draw_scaled_rotated_bitmap(tex1, 4, 4, x + 320, y, 8, 8, a, 0);
106 
107          y += 120;
108 
109          al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
110          al_draw_textf(font, color, x, y, 0, "not premultiplied");
111          al_draw_textf(font, color, x, y + th, 0, "mag linear filtering");
112          al_draw_textf(font, color2, x + 400, y, 0, "wrong dark border");
113          al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
114          al_draw_scaled_rotated_bitmap(tex2, 4, 4, x + 320, y, 8, 8, a, 0);
115 
116          y += 120;
117 
118          al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
119          al_draw_textf(font, color, x, y, 0, "premultiplied alpha");
120          al_draw_textf(font, color, x, y + th, 0, "no filtering");
121          al_draw_textf(font, color, x + 400, y, 0, "no difference");
122          al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
123          al_draw_scaled_rotated_bitmap(tex1, 4, 4, x + 320, y, 8, 8, a, 0);
124 
125          y += 120;
126 
127          al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
128          al_draw_textf(font, color, x, y, 0, "premultiplied alpha");
129          al_draw_textf(font, color, x, y + th, 0, "mag linear filtering");
130          al_draw_textf(font, color3, x + 400, y, 0, "correct color");
131          al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
132          al_draw_scaled_rotated_bitmap(tex2, 4, 4, x + 320, y, 8, 8, a, 0);
133 
134          al_flip_display();
135       }
136    }
137 
138    al_destroy_font(font);
139    al_destroy_bitmap(tex1);
140    al_destroy_bitmap(tex2);
141 
142    return 0;
143 }
144