forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblit.c
209 lines (187 loc) · 7.56 KB
/
blit.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
#include "global.h"
#include "blit.h"
void BlitBitmapRect4BitWithoutColorKey(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height)
{
BlitBitmapRect4Bit(src, dst, srcX, srcY, dstX, dstY, width, height, 0xFF);
}
void BlitBitmapRect4Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey)
{
s32 xEnd;
s32 yEnd;
s32 multiplierSrcY;
s32 multiplierDstY;
s32 loopSrcY, loopDstY;
s32 loopSrcX, loopDstX;
const u8 *pixelsSrc;
u8 *pixelsDst;
s32 toOrr;
s32 toAnd;
s32 toShift;
if (dst->width - dstX < width)
xEnd = (dst->width - dstX) + srcX;
else
xEnd = srcX + width;
if (dst->height - dstY < height)
yEnd = (dst->height - dstY) + srcY;
else
yEnd = height + srcY;
multiplierSrcY = (src->width + (src->width & 7)) >> 3;
multiplierDstY = (dst->width + (dst->width & 7)) >> 3;
if (colorKey == 0xFF)
{
for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++)
{
for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++)
{
pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1B);
pixelsDst = dst->pixels + ((loopDstX >> 1) & 3) + ((loopDstX >> 3) << 5) + (((loopDstY >> 3) * multiplierDstY) << 5) + ((u32)(loopDstY << 0x1d) >> 0x1B);
toOrr = ((*pixelsSrc >> ((loopSrcX & 1) << 2)) & 0xF);
toShift = ((loopDstX & 1) << 2);
toOrr <<= toShift;
toAnd = 0xF0 >> (toShift);
*pixelsDst = toOrr | (*pixelsDst & toAnd);
}
}
}
else
{
for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++)
{
for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++)
{
pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1B);
pixelsDst = dst->pixels + ((loopDstX >> 1) & 3) + ((loopDstX >> 3) << 5) + (((loopDstY >> 3) * multiplierDstY) << 5) + ((u32)(loopDstY << 0x1d) >> 0x1B);
toOrr = ((*pixelsSrc >> ((loopSrcX & 1) << 2)) & 0xF);
if (toOrr != colorKey)
{
toShift = ((loopDstX & 1) << 2);
toOrr <<= toShift;
toAnd = 0xF0 >> (toShift);
*pixelsDst = toOrr | (*pixelsDst & toAnd);
}
}
}
}
}
void FillBitmapRect4Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue)
{
s32 xEnd;
s32 yEnd;
s32 multiplierY;
s32 loopX, loopY;
u8 toOrr1, toOrr2;
xEnd = x + width;
if (xEnd > surface->width)
xEnd = surface->width;
yEnd = y + height;
if (yEnd > surface->height)
yEnd = surface->height;
multiplierY = (surface->width + (surface->width & 7)) >> 3;
toOrr1 = fillValue << 4;
toOrr2 = fillValue & 0xF;
for (loopY = y; loopY < yEnd; loopY++)
{
for (loopX = x; loopX < xEnd; loopX++)
{
u8 *pixels = surface->pixels + ((loopX >> 1) & 3) + ((loopX >> 3) << 5) + (((loopY >> 3) * multiplierY) << 5) + ((u32)(loopY << 0x1d) >> 0x1B);
if ((loopX << 0x1F) != 0)
*pixels = toOrr1 | (*pixels & 0xF);
else
*pixels = toOrr2 | (*pixels & 0xF0);
}
}
}
void BlitBitmapRect4BitTo8Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey, u8 paletteOffset)
{
s32 palOffsetBits;
s32 xEnd;
s32 yEnd;
s32 multiplierSrcY;
s32 multiplierDstY;
s32 loopSrcY, loopDstY;
s32 loopSrcX, loopDstX;
const u8 *pixelsSrc;
u8 *pixelsDst;
s32 colorKeyBits;
palOffsetBits = (u32)(paletteOffset << 0x1C) >> 0x18;
colorKeyBits = (u32)(colorKey << 0x1C) >> 0x18;
if (dst->width - dstX < width)
xEnd = (dst->width - dstX) + srcX;
else
xEnd = width + srcX;
if (dst->height - dstY < height)
yEnd = (srcY + dst->height) - dstY;
else
yEnd = srcY + height;
multiplierSrcY = (src->width + (src->width & 7)) >> 3;
multiplierDstY = (dst->width + (dst->width & 7)) >> 3;
if (colorKey == 0xFF)
{
for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++)
{
pixelsSrc = src->pixels + ((srcX >> 1) & 3) + ((srcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b);
for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++)
{
pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a);
if (loopSrcX & 1)
{
*pixelsDst = palOffsetBits + (*pixelsSrc >> 4);
}
else
{
pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b);
*pixelsDst = palOffsetBits + (*pixelsSrc & 0xF);
}
}
}
}
else
{
for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++)
{
pixelsSrc = src->pixels + ((srcX >> 1) & 3) + ((srcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b);
for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++)
{
if (loopSrcX & 1)
{
if ((*pixelsSrc & 0xF0) != colorKeyBits)
{
pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a);
*pixelsDst = palOffsetBits + (*pixelsSrc >> 4);
}
}
else
{
pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b);
if ((*pixelsSrc & 0xF) != colorKey)
{
pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a);
*pixelsDst = palOffsetBits + (*pixelsSrc & 0xF);
}
}
}
}
}
}
void FillBitmapRect8Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue)
{
s32 xEnd;
s32 yEnd;
s32 multiplierY;
s32 loopX, loopY;
xEnd = x + width;
if (xEnd > surface->width)
xEnd = surface->width;
yEnd = y + height;
if (yEnd > surface->height)
yEnd = surface->height;
multiplierY = (surface->width + (surface->width & 7)) >> 3;
for (loopY = y; loopY < yEnd; loopY++)
{
for (loopX = x; loopX < xEnd; loopX++)
{
u8 *pixels = surface->pixels + (loopX & 7) + ((loopX >> 3) << 6) + (((loopY >> 3) * multiplierY) << 6) + ((u32)(loopY << 0x1d) >> 0x1a);
*pixels = fillValue;
}
}
}