/
ex_premulalpha.c
143 lines (118 loc) · 4.39 KB
/
ex_premulalpha.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_font.h"
#include "common.c"
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display;
ALLEGRO_BITMAP *tex1, *tex2;
ALLEGRO_TIMER *timer;
ALLEGRO_EVENT_QUEUE *queue;
bool redraw = true;
ALLEGRO_LOCKED_REGION *lock;
ALLEGRO_FONT *font;
unsigned char *p;
int x, y;
(void)argc;
(void)argv;
if (!al_init()) {
abort_example("Could not init Allegro.\n");
}
al_init_font_addon();
al_install_mouse();
al_install_keyboard();
display = al_create_display(640, 480);
if (!display) {
abort_example("Error creating display\n");
}
font = al_create_builtin_font();
tex1 = al_create_bitmap(8, 8);
lock = al_lock_bitmap(tex1, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY);
p = lock->data;
for (y = 0; y < 8; y++) {
unsigned char *lp = p;
for (x = 0; x < 8; x++) {
if (x == 0 || y == 0 || x == 7 || y == 7) {
p[0] = 0;
p[1] = 0;
p[2] = 0;
p[3] = 0;
}
else {
p[0] = 0;
p[1] = 255;
p[2] = 0;
p[3] = 255;
}
p += 4;
}
p = lp + lock->pitch;
}
al_unlock_bitmap(tex1);
al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR);
tex2 = al_clone_bitmap(tex1);
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 = 8, y = 60;
float a, t = al_get_time();
float th = al_get_font_line_height(font);
ALLEGRO_COLOR color = al_map_rgb_f(0, 0, 0);
ALLEGRO_COLOR color2 = al_map_rgb_f(1, 0, 0);
ALLEGRO_COLOR color3 = al_map_rgb_f(0, 0.5, 0);
t /= 10;
a = t - floor(t);
a *= 2 * ALLEGRO_PI;
redraw = false;
al_clear_to_color(al_map_rgb_f(0.5, 0.6, 1));
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_draw_textf(font, color, x, y, 0, "not premultiplied");
al_draw_textf(font, color, x, y + th, 0, "no filtering");
al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
al_draw_scaled_rotated_bitmap(tex1, 4, 4, x + 320, y, 8, 8, a, 0);
y += 120;
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_draw_textf(font, color, x, y, 0, "not premultiplied");
al_draw_textf(font, color, x, y + th, 0, "mag linear filtering");
al_draw_textf(font, color2, x + 400, y, 0, "wrong dark border");
al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
al_draw_scaled_rotated_bitmap(tex2, 4, 4, x + 320, y, 8, 8, a, 0);
y += 120;
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_draw_textf(font, color, x, y, 0, "premultiplied alpha");
al_draw_textf(font, color, x, y + th, 0, "no filtering");
al_draw_textf(font, color, x + 400, y, 0, "no difference");
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_draw_scaled_rotated_bitmap(tex1, 4, 4, x + 320, y, 8, 8, a, 0);
y += 120;
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_draw_textf(font, color, x, y, 0, "premultiplied alpha");
al_draw_textf(font, color, x, y + th, 0, "mag linear filtering");
al_draw_textf(font, color3, x + 400, y, 0, "correct color");
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_draw_scaled_rotated_bitmap(tex2, 4, 4, x + 320, y, 8, 8, a, 0);
al_flip_display();
}
}
al_destroy_font(font);
al_destroy_bitmap(tex1);
al_destroy_bitmap(tex2);
return 0;
}