-
Notifications
You must be signed in to change notification settings - Fork 279
/
bitmap_draw.c
253 lines (213 loc) · 7.08 KB
/
bitmap_draw.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
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
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* Bitmap drawing routines.
*
* See LICENSE.txt for copyright information.
*/
#include "allegro5/allegro.h"
#include "allegro5/internal/aintern_bitmap.h"
#include "allegro5/internal/aintern_display.h"
#include "allegro5/internal/aintern_memblit.h"
#include "allegro5/internal/aintern_pixels.h"
static ALLEGRO_COLOR solid_white = {1, 1, 1, 1};
static void _bitmap_drawer(ALLEGRO_BITMAP *bitmap, ALLEGRO_COLOR tint,
float sx, float sy, float sw, float sh, int flags)
{
ALLEGRO_BITMAP *dest = al_get_target_bitmap();
ALLEGRO_DISPLAY *display = _al_get_bitmap_display(dest);
ASSERT(bitmap->parent == NULL);
ASSERT(!(flags & (ALLEGRO_FLIP_HORIZONTAL | ALLEGRO_FLIP_VERTICAL)));
ASSERT(bitmap != dest && bitmap != dest->parent);
/* If destination is memory, do a memory blit */
if (al_get_bitmap_flags(dest) & ALLEGRO_MEMORY_BITMAP ||
_al_pixel_format_is_compressed(al_get_bitmap_format(dest))) {
_al_draw_bitmap_region_memory(bitmap, tint, sx, sy, sw, sh, 0, 0, flags);
}
else {
/* if source is memory or incompatible */
if ((al_get_bitmap_flags(bitmap) & ALLEGRO_MEMORY_BITMAP) ||
(!al_is_compatible_bitmap(bitmap)))
{
if (display && display->vt->draw_memory_bitmap_region) {
display->vt->draw_memory_bitmap_region(display, bitmap,
sx, sy, sw, sh, flags);
}
else {
_al_draw_bitmap_region_memory(bitmap, tint, sx, sy, sw, sh, 0, 0, flags);
}
}
else {
/* Compatible display bitmap, use full acceleration */
bitmap->vt->draw_bitmap_region(bitmap, tint, sx, sy, sw, sh, flags);
}
}
}
static void _draw_tinted_rotated_scaled_bitmap_region(ALLEGRO_BITMAP *bitmap,
ALLEGRO_COLOR tint, float cx, float cy, float angle,
float xscale, float yscale,
float sx, float sy, float sw, float sh, float dx, float dy,
int flags)
{
ALLEGRO_TRANSFORM backup;
ALLEGRO_TRANSFORM t;
ALLEGRO_BITMAP *parent = bitmap;
float const orig_sw = sw;
float const orig_sh = sh;
ASSERT(bitmap);
al_copy_transform(&backup, al_get_current_transform());
al_identity_transform(&t);
if (bitmap->parent) {
parent = bitmap->parent;
sx += bitmap->xofs;
sy += bitmap->yofs;
}
if (sx < 0) {
sw += sx;
al_translate_transform(&t, -sx, 0);
sx = 0;
}
if (sy < 0) {
sh += sy;
al_translate_transform(&t, 0, -sy);
sy = 0;
}
if (sx + sw > parent->w)
sw = parent->w - sx;
if (sy + sh > parent->h)
sh = parent->h - sy;
if (flags & ALLEGRO_FLIP_HORIZONTAL) {
al_scale_transform(&t, -1, 1);
al_translate_transform(&t, orig_sw, 0);
flags &= ~ALLEGRO_FLIP_HORIZONTAL;
}
if (flags & ALLEGRO_FLIP_VERTICAL) {
al_scale_transform(&t, 1, -1);
al_translate_transform(&t, 0, orig_sh);
flags &= ~ALLEGRO_FLIP_VERTICAL;
}
al_translate_transform(&t, -cx, -cy);
al_scale_transform(&t, xscale, yscale);
al_rotate_transform(&t, angle);
al_translate_transform(&t, dx, dy);
al_compose_transform(&t, &backup);
al_use_transform(&t);
_bitmap_drawer(parent, tint, sx, sy, sw, sh, flags);
al_use_transform(&backup);
}
/* Function: al_draw_tinted_bitmap_region
*/
void al_draw_tinted_bitmap_region(ALLEGRO_BITMAP *bitmap,
ALLEGRO_COLOR tint,
float sx, float sy, float sw, float sh, float dx, float dy,
int flags)
{
_draw_tinted_rotated_scaled_bitmap_region(bitmap, tint,
0, 0, 0, 1, 1, sx, sy, sw, sh, dx, dy, flags);
}
/* Function: al_draw_tinted_bitmap
*/
void al_draw_tinted_bitmap(ALLEGRO_BITMAP *bitmap, ALLEGRO_COLOR tint,
float dx, float dy, int flags)
{
ASSERT(bitmap);
al_draw_tinted_bitmap_region(bitmap, tint, 0, 0,
bitmap->w, bitmap->h, dx, dy, flags);
}
/* Function: al_draw_bitmap
*/
void al_draw_bitmap(ALLEGRO_BITMAP *bitmap, float dx, float dy, int flags)
{
al_draw_tinted_bitmap(bitmap, solid_white, dx, dy, flags);
}
/* Function: al_draw_bitmap_region
*/
void al_draw_bitmap_region(ALLEGRO_BITMAP *bitmap,
float sx, float sy, float sw, float sh, float dx, float dy, int flags)
{
al_draw_tinted_bitmap_region(bitmap, solid_white, sx, sy, sw, sh,
dx, dy, flags);
}
/* Function: al_draw_tinted_scaled_bitmap
*/
void al_draw_tinted_scaled_bitmap(ALLEGRO_BITMAP *bitmap,
ALLEGRO_COLOR tint,
float sx, float sy, float sw, float sh,
float dx, float dy, float dw, float dh, int flags)
{
_draw_tinted_rotated_scaled_bitmap_region(bitmap, tint,
0, 0, 0,
dw / sw, dh / sh,
sx, sy, sw, sh, dx, dy, flags);
}
/* Function: al_draw_scaled_bitmap
*/
void al_draw_scaled_bitmap(ALLEGRO_BITMAP *bitmap,
float sx, float sy, float sw, float sh,
float dx, float dy, float dw, float dh, int flags)
{
al_draw_tinted_scaled_bitmap(bitmap, solid_white, sx, sy, sw, sh,
dx, dy, dw, dh, flags);
}
/* Function: al_draw_tinted_rotated_bitmap
*
* angle is specified in radians and moves clockwise
* on the screen.
*/
void al_draw_tinted_rotated_bitmap(ALLEGRO_BITMAP *bitmap,
ALLEGRO_COLOR tint,
float cx, float cy, float dx, float dy, float angle, int flags)
{
al_draw_tinted_scaled_rotated_bitmap(bitmap, tint, cx, cy, dx, dy,
1, 1, angle, flags);
}
/* Function: al_draw_rotated_bitmap
*/
void al_draw_rotated_bitmap(ALLEGRO_BITMAP *bitmap,
float cx, float cy, float dx, float dy, float angle, int flags)
{
al_draw_tinted_rotated_bitmap(bitmap, solid_white, cx, cy, dx, dy,
angle, flags);
}
/* Function: al_draw_tinted_scaled_rotated_bitmap
*/
void al_draw_tinted_scaled_rotated_bitmap(ALLEGRO_BITMAP *bitmap,
ALLEGRO_COLOR tint,
float cx, float cy, float dx, float dy, float xscale, float yscale,
float angle, int flags)
{
_draw_tinted_rotated_scaled_bitmap_region(bitmap, tint,
cx, cy, angle,
xscale, yscale,
0, 0, bitmap->w, bitmap->h, dx, dy, flags);
}
/* Function: al_draw_tinted_scaled_rotated_bitmap_region
*/
void al_draw_tinted_scaled_rotated_bitmap_region(ALLEGRO_BITMAP *bitmap,
float sx, float sy, float sw, float sh,
ALLEGRO_COLOR tint,
float cx, float cy, float dx, float dy, float xscale, float yscale,
float angle, int flags)
{
_draw_tinted_rotated_scaled_bitmap_region(bitmap, tint,
cx, cy, angle,
xscale, yscale,
sx, sy, sw, sh, dx, dy, flags);
}
/* Function: al_draw_scaled_rotated_bitmap
*/
void al_draw_scaled_rotated_bitmap(ALLEGRO_BITMAP *bitmap,
float cx, float cy, float dx, float dy, float xscale, float yscale,
float angle, int flags)
{
al_draw_tinted_scaled_rotated_bitmap(bitmap, solid_white,
cx, cy, dx, dy, xscale, yscale, angle, flags);
}
/* vim: set ts=8 sts=3 sw=3 et: */