-
Notifications
You must be signed in to change notification settings - Fork 0
/
DGLBLIT.C
executable file
·204 lines (174 loc) · 5.67 KB
/
DGLBLIT.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
#include "dglcmn.h"
#include "dglrect.h"
#include "dglblit.h"
#include "dglclip.h"
static bool clip_blit(
const RECT *dest_clip_region,
RECT *src_blit_region,
int *dest_x,
int *dest_y
) {
int dest_clip_right = rect_right(dest_clip_region);
int dest_clip_bottom = rect_bottom(dest_clip_region);
int offset;
// off the left edge?
if (*dest_x < dest_clip_region->x) {
// completely off the left edge?
if ((*dest_x + src_blit_region->width - 1) < dest_clip_region->x)
return false;
offset = src_blit_region->x - *dest_x;
src_blit_region->x += offset;
src_blit_region->width -= offset;
*dest_x = dest_clip_region->x;
}
// off the right edge?
if (*dest_x > (dest_clip_region->width - src_blit_region->width)) {
// completely off the right edge?
if (*dest_x > dest_clip_right)
return false;
offset = *dest_x + src_blit_region->width - dest_clip_region->width;
src_blit_region->width -= offset;
}
// off the top edge?
if (*dest_y < dest_clip_region->y) {
// completely off the top edge?
if ((*dest_y + src_blit_region->height - 1) < dest_clip_region->y)
return false;
offset = dest_clip_region->y - *dest_y;
src_blit_region->y += offset;
src_blit_region->height -= offset;
*dest_y = dest_clip_region->y;
}
// off the bottom edge?
if (*dest_y > (dest_clip_region->height - src_blit_region->height)) {
// completely off the bottom edge?
if (*dest_y > dest_clip_bottom)
return false;
offset = *dest_y + src_blit_region->height - dest_clip_region->height;
src_blit_region->height -= offset;
}
return true;
}
void blit_region(
const SURFACE *src,
SURFACE *dest,
int src_x,
int src_y,
int src_width,
int src_height,
int dest_x,
int dest_y
) {
RECT src_region = rect(src_x, src_y, src_width, src_height);
bool on_screen = clip_blit(&dest->clip_region, &src_region, &dest_x, &dest_y);
if (!on_screen)
return;
blit_region_f(
src, dest,
src_region.x, src_region.y,
src_region.width, src_region.height,
dest_x, dest_y
);
}
void blit_region_f(
const SURFACE *src,
SURFACE *dest,
int src_x,
int src_y,
int src_width,
int src_height,
int dest_x,
int dest_y
) {
const uint8 *psrc;
uint8 *pdest;
int lines;
int src_y_inc = src->width - src_width;
int dest_y_inc = dest->width - src_width;
int width_4, width_remainder;
psrc = (const uint8*)surface_pointer(src, src_x, src_y);
pdest = (uint8*)surface_pointer(dest, dest_x, dest_y);
lines = src_height;
width_4 = src_width / 4;
width_remainder = src_width & 3;
if (width_4 && !width_remainder) {
// width is a multiple of 4 (no remainder)
lowlevel_blit_4(width_4, lines, pdest, psrc, dest_y_inc, src_y_inc);
} else if (width_4 && width_remainder) {
// width is >= 4 and there is a remainder ( <= 3 )
lowlevel_blit_4r(width_4, lines, width_remainder, pdest, psrc, dest_y_inc, src_y_inc);
} else {
// width is <= 3
lowlevel_blit_r(width_remainder, lines, pdest, psrc, dest_y_inc, src_y_inc);
}
}
void blit_sprite_region(
const SURFACE *src,
SURFACE *dest,
int src_x,
int src_y,
int src_width,
int src_height,
int dest_x,
int dest_y
) {
RECT src_region = rect(src_x, src_y, src_width, src_height);
bool on_screen = clip_blit(&dest->clip_region, &src_region, &dest_x, &dest_y);
if (!on_screen)
return;
blit_sprite_region_f(
src, dest,
src_region.x, src_region.y,
src_region.width, src_region.height,
dest_x, dest_y
);
}
void blit_sprite_region_f(
const SURFACE *src,
SURFACE *dest,
int src_x,
int src_y,
int src_width,
int src_height,
int dest_x,
int dest_y
) {
const uint8 *psrc;
uint8 *pdest;
uint8 pixel;
int src_y_inc, dest_y_inc;
int width, width_4, width_8, width_remainder;
int lines_left;
int x;
psrc = (const uint8*)surface_pointer(src, src_x, src_y);
src_y_inc = src->width;
pdest = (uint8*)surface_pointer(dest, dest_x, dest_y);
dest_y_inc = dest->width;
width = src_width;
lines_left = src_height;
src_y_inc -= width;
dest_y_inc -= width;
width_4 = width / 4;
width_remainder = width & 3;
if (width_4 && !width_remainder) {
if ((width_4 & 1) == 0) {
// width is actually an even multiple of 8!
lowlevel_blit_sprite_8(width_4 / 2, lines_left, pdest, psrc, dest_y_inc, src_y_inc);
} else {
// width is a multiple of 4 (no remainder)
lowlevel_blit_sprite_4(width_4, lines_left, pdest, psrc, dest_y_inc, src_y_inc);
}
} else if (width_4 && width_remainder) {
if ((width_4 & 1) == 0) {
// width is _mostly_ made up of an even multiple of 8,
// plus a small remainder
lowlevel_blit_sprite_8r(width_4 / 2, lines_left, pdest, psrc, width_remainder, dest_y_inc, src_y_inc);
} else {
// width is >= 4 and there is a remainder
lowlevel_blit_sprite_4r(width_4, lines_left, pdest, psrc, width_remainder, dest_y_inc, src_y_inc);
}
} else {
// width is <= 3
lowlevel_blit_sprite_r(width_remainder, lines_left, pdest, psrc, dest_y_inc, src_y_inc);
}
}