/
ex_blend2.cpp
388 lines (341 loc) · 10.5 KB
/
ex_blend2.cpp
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Example program for the Allegro library, by Peter Wang.
*
* Compare software blending routines with hardware blending.
*/
#include <string>
#include "allegro5/allegro.h"
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_image.h"
#include <allegro5/allegro_primitives.h>
#include "common.c"
#include "nihgui.hpp"
ALLEGRO_BITMAP *allegro;
ALLEGRO_BITMAP *mysha;
ALLEGRO_BITMAP *allegro_bmp;
ALLEGRO_BITMAP *mysha_bmp;
ALLEGRO_BITMAP *target;
ALLEGRO_BITMAP *target_bmp;
class Prog {
private:
Dialog d;
Label memory_label;
Label texture_label;
Label source_label;
Label destination_label;
List source_image;
List destination_image;
List draw_mode;
Label operation_label[6];
List operations[6];
Label rgba_label[3];
HSlider r[3];
HSlider g[3];
HSlider b[3];
HSlider a[3];
public:
Prog(const Theme & theme, ALLEGRO_DISPLAY *display);
void run();
private:
void blending_test(bool memory);
void draw_samples();
void draw_bitmap(const std::string &, const std::string &, bool, bool);
};
Prog::Prog(const Theme & theme, ALLEGRO_DISPLAY *display) :
d(Dialog(theme, display, 20, 40)),
memory_label(Label("Memory")),
texture_label(Label("Texture")),
source_label(Label("Source", false)),
destination_label(Label("Destination", false)),
source_image(List(0)),
destination_image(List(1)),
draw_mode(List(0))
{
d.add(memory_label, 9, 0, 10, 2);
d.add(texture_label, 0, 0, 10, 2);
d.add(source_label, 1, 15, 6, 2);
d.add(destination_label, 7, 15, 6, 2);
List *images[] = {&source_image, &destination_image, &draw_mode};
for (int i = 0; i < 3; i++) {
List & image = *images[i];
if (i < 2) {
image.append_item("Mysha");
image.append_item("Allegro");
image.append_item("Mysha (tinted)");
image.append_item("Allegro (tinted)");
image.append_item("Color");
}
else {
image.append_item("original");
image.append_item("scaled");
image.append_item("rotated");
}
d.add(image, 1 + i * 6, 16, 4, 6);
}
for (int i = 0; i < 4; i++) {
operation_label[i] = Label(i % 2 == 0 ? "Color" : "Alpha", false);
d.add(operation_label[i], 1 + i * 3, 23, 3, 2);
List &l = operations[i];
l.append_item("ONE");
l.append_item("ZERO");
l.append_item("ALPHA");
l.append_item("INVERSE");
l.append_item("SRC_COLOR");
l.append_item("DEST_COLOR");
l.append_item("INV_SRC_COLOR");
l.append_item("INV_DEST_COLOR");
l.append_item("CONST_COLOR");
l.append_item("INV_CONST_COLOR");
d.add(l, 1 + i * 3, 24, 3, 10);
}
for (int i = 4; i < 6; i++) {
operation_label[i] = Label(i == 4 ? "Blend op" : "Alpha op", false);
d.add(operation_label[i], 1 + i * 3, 23, 3, 2);
List &l = operations[i];
l.append_item("ADD");
l.append_item("SRC_MINUS_DEST");
l.append_item("DEST_MINUS_SRC");
d.add(l, 1 + i * 3, 24, 3, 6);
}
rgba_label[0] = Label("Source tint/color RGBA");
rgba_label[1] = Label("Dest tint/color RGBA");
rgba_label[2] = Label("Const color RGBA");
d.add(rgba_label[0], 1, 34, 5, 1);
d.add(rgba_label[1], 7, 34, 5, 1);
d.add(rgba_label[2], 13, 34, 5, 1);
for (int i = 0; i < 3; i++) {
r[i] = HSlider(255, 255);
g[i] = HSlider(255, 255);
b[i] = HSlider(255, 255);
a[i] = HSlider(255, 255);
d.add(r[i], 1 + i * 6, 35, 5, 1);
d.add(g[i], 1 + i * 6, 36, 5, 1);
d.add(b[i], 1 + i * 6, 37, 5, 1);
d.add(a[i], 1 + i * 6, 38, 5, 1);
}
}
void Prog::run()
{
d.prepare();
while (!d.is_quit_requested()) {
if (d.is_draw_requested()) {
al_clear_to_color(al_map_rgb(128, 128, 128));
draw_samples();
d.draw();
al_flip_display();
}
d.run_step(true);
}
}
int str_to_blend_mode(const std::string & str)
{
if (str == "ZERO")
return ALLEGRO_ZERO;
if (str == "ONE")
return ALLEGRO_ONE;
if (str == "SRC_COLOR")
return ALLEGRO_SRC_COLOR;
if (str == "DEST_COLOR")
return ALLEGRO_DEST_COLOR;
if (str == "INV_SRC_COLOR")
return ALLEGRO_INVERSE_SRC_COLOR;
if (str == "INV_DEST_COLOR")
return ALLEGRO_INVERSE_DEST_COLOR;
if (str == "ALPHA")
return ALLEGRO_ALPHA;
if (str == "INVERSE")
return ALLEGRO_INVERSE_ALPHA;
if (str == "ADD")
return ALLEGRO_ADD;
if (str == "SRC_MINUS_DEST")
return ALLEGRO_SRC_MINUS_DEST;
if (str == "DEST_MINUS_SRC")
return ALLEGRO_DEST_MINUS_SRC;
if (str == "CONST_COLOR")
return ALLEGRO_CONST_COLOR;
if (str == "INV_CONST_COLOR")
return ALLEGRO_INVERSE_CONST_COLOR;
ALLEGRO_ASSERT(false);
return ALLEGRO_ONE;
}
void draw_background(int x, int y)
{
ALLEGRO_COLOR c[] = {
al_map_rgba(0x66, 0x66, 0x66, 0xff),
al_map_rgba(0x99, 0x99, 0x99, 0xff)
};
for (int i = 0; i < 320 / 16; i++) {
for (int j = 0; j < 200 / 16; j++) {
al_draw_filled_rectangle(x + i * 16, y + j * 16,
x + i * 16 + 16, y + j * 16 + 16,
c[(i + j) & 1]);
}
}
}
static ALLEGRO_COLOR makecol(int r, int g, int b, int a)
{
/* Premultiply alpha. */
float rf = (float)r / 255.0f;
float gf = (float)g / 255.0f;
float bf = (float)b / 255.0f;
float af = (float)a / 255.0f;
return al_map_rgba_f(rf*af, gf*af, bf*af, af);
}
static bool contains(const std::string & haystack, const std::string & needle)
{
return haystack.find(needle) != std::string::npos;
}
void Prog::draw_bitmap(const std::string & str,
const std::string &how,
bool memory,
bool destination)
{
int i = destination ? 1 : 0;
int rv = r[i].get_cur_value();
int gv = g[i].get_cur_value();
int bv = b[i].get_cur_value();
int av = a[i].get_cur_value();
ALLEGRO_COLOR color = makecol(rv, gv, bv, av);
ALLEGRO_BITMAP *bmp;
if (contains(str, "Mysha"))
bmp = (memory ? mysha_bmp : mysha);
else
bmp = (memory ? allegro_bmp : allegro);
if (how == "original") {
if (str == "Color")
al_draw_filled_rectangle(0, 0, 320, 200, color);
else if (contains(str, "tint"))
al_draw_tinted_bitmap(bmp, color, 0, 0, 0);
else
al_draw_bitmap(bmp, 0, 0, 0);
}
else if (how == "scaled") {
int w = al_get_bitmap_width(bmp);
int h = al_get_bitmap_height(bmp);
float s = 200.0 / h * 0.9;
if (str == "Color") {
al_draw_filled_rectangle(10, 10, 300, 180, color);
}
else if (contains(str, "tint")) {
al_draw_tinted_scaled_bitmap(bmp, color, 0, 0, w, h,
160 - w * s / 2, 100 - h * s / 2, w * s, h * s, 0);
}
else {
al_draw_scaled_bitmap(bmp, 0, 0, w, h,
160 - w * s / 2, 100 - h * s / 2, w * s, h * s, 0);
}
}
else if (how == "rotated") {
if (str == "Color") {
al_draw_filled_circle(160, 100, 100, color);
}
else if (contains(str, "tint")) {
al_draw_tinted_rotated_bitmap(bmp, color, 160, 100,
160, 100, ALLEGRO_PI / 8, 0);
}
else {
al_draw_rotated_bitmap(bmp, 160, 100,
160, 100, ALLEGRO_PI / 8, 0);
}
}
}
void Prog::blending_test(bool memory)
{
ALLEGRO_COLOR transparency = al_map_rgba_f(0, 0, 0, 0);
int op = str_to_blend_mode(operations[4].get_selected_item_text());
int aop = str_to_blend_mode(operations[5].get_selected_item_text());
int src = str_to_blend_mode(operations[0].get_selected_item_text());
int asrc = str_to_blend_mode(operations[1].get_selected_item_text());
int dst = str_to_blend_mode(operations[2].get_selected_item_text());
int adst = str_to_blend_mode(operations[3].get_selected_item_text());
int rv = r[2].get_cur_value();
int gv = g[2].get_cur_value();
int bv = b[2].get_cur_value();
int av = a[2].get_cur_value();
ALLEGRO_COLOR color = makecol(rv, gv, bv, av);
/* Initialize with destination. */
al_clear_to_color(transparency); // Just in case.
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
draw_bitmap(destination_image.get_selected_item_text(),
"original", memory, true);
/* Now draw the blended source over it. */
al_set_separate_blender(op, src, dst, aop, asrc, adst);
al_set_blend_color(color);
draw_bitmap(source_image.get_selected_item_text(),
draw_mode.get_selected_item_text(), memory, false);
}
void Prog::draw_samples()
{
ALLEGRO_STATE state;
al_store_state(&state, ALLEGRO_STATE_TARGET_BITMAP | ALLEGRO_STATE_BLENDER);
/* Draw a background, in case our target bitmap will end up with
* alpha in it.
*/
draw_background(40, 20);
draw_background(400, 20);
/* Test standard blending. */
al_set_target_bitmap(target);
blending_test(false);
/* Test memory blending. */
al_set_target_bitmap(target_bmp);
blending_test(true);
/* Display results. */
al_restore_state(&state);
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
al_draw_bitmap(target, 40, 20, 0);
al_draw_bitmap(target_bmp, 400, 20, 0);
al_restore_state(&state);
}
int main(int argc, char *argv[])
{
ALLEGRO_DISPLAY *display;
ALLEGRO_FONT *font;
(void)argc;
(void)argv;
if (!al_init()) {
abort_example("Could not init Allegro\n");
}
al_init_primitives_addon();
al_install_keyboard();
al_install_mouse();
al_init_font_addon();
al_init_image_addon();
init_platform_specific();
al_set_new_display_flags(ALLEGRO_GENERATE_EXPOSE_EVENTS);
display = al_create_display(800, 600);
if (!display) {
abort_example("Unable to create display\n");
}
font = al_load_font("data/fixed_font.tga", 0, 0);
if (!font) {
abort_example("Failed to load data/fixed_font.tga\n");
}
allegro = al_load_bitmap("data/allegro.pcx");
if (!allegro) {
abort_example("Failed to load data/allegro.pcx\n");
}
mysha = al_load_bitmap("data/mysha256x256.png");
if (!mysha) {
abort_example("Failed to load data/mysha256x256.png\n");
}
target = al_create_bitmap(320, 200);
al_add_new_bitmap_flag(ALLEGRO_MEMORY_BITMAP);
allegro_bmp = al_clone_bitmap(allegro);
mysha_bmp = al_clone_bitmap(mysha);
target_bmp = al_clone_bitmap(target);
/* Don't remove these braces. */
{
Theme theme(font);
Prog prog(theme, display);
prog.run();
}
al_destroy_bitmap(allegro);
al_destroy_bitmap(allegro_bmp);
al_destroy_bitmap(mysha);
al_destroy_bitmap(mysha_bmp);
al_destroy_bitmap(target);
al_destroy_bitmap(target_bmp);
al_destroy_font(font);
return 0;
}
/* vim: set sts=3 sw=3 et: */