forked from iscle/SpaceCadetPinball
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgdrv.cpp
301 lines (260 loc) · 6.59 KB
/
gdrv.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
#include "pch.h"
#include "gdrv.h"
#include "memory.h"
#include "winmain.h"
SDL_Texture* gdrv::vScreenTex = nullptr;
char* gdrv::vScreenPixels = nullptr;
int gdrv::vScreenWidth, gdrv::vScreenHeight;
ColorRgba gdrv::current_palette[256]{};
SDL_Rect gdrv::DestinationRect{};
int gdrv::init(int width, int height)
{
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
vScreenTex = SDL_CreateTexture
(
winmain::Renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
width, height
);
vScreenPixels = memory::allocate(width * height * 4);
vScreenWidth = width;
vScreenHeight = height;
return 0;
}
int gdrv::uninit()
{
SDL_DestroyTexture(vScreenTex);
memory::free(vScreenPixels);
return 0;
}
void gdrv::get_focus()
{
}
int gdrv::create_bitmap(gdrv_bitmap8* bmp, int width, int height)
{
bmp->Width = width;
bmp->Stride = width;
if (width % 4)
bmp->Stride = 4 - width % 4 + width;
bmp->Height = height;
bmp->BitmapType = BitmapTypes::DibBitmap;
bmp->BmpBufPtr1 = memory::allocate(bmp->Height * bmp->Stride);
return 0;
}
int gdrv::create_raw_bitmap(gdrv_bitmap8* bmp, int width, int height, int flag)
{
bmp->Width = width;
bmp->Stride = width;
if (flag && width % 4)
bmp->Stride = width - width % 4 + 4;
unsigned int sizeInBytes = height * bmp->Stride;
bmp->Height = height;
bmp->BitmapType = BitmapTypes::RawBitmap;
char* buf = memory::allocate(sizeInBytes);
bmp->BmpBufPtr1 = buf;
if (!buf)
return -1;
return 0;
}
int gdrv::create_spliced_bitmap(gdrv_bitmap8* bmp, int width, int height, int size)
{
bmp->Width = width;
bmp->Stride = width;
bmp->BitmapType = BitmapTypes::Spliced;
bmp->Height = height;
char* buf = memory::allocate(size);
bmp->BmpBufPtr1 = buf;
if (!buf)
return -1;
return 0;
}
int gdrv::display_palette(ColorRgba* plt)
{
const uint32_t sysPaletteColors[]
{
0x00000000,
0x00000080,
0x00008000,
0x00008080,
0x00800000,
0x00800080,
0x00808000,
0x00C0C0C0,
0x00C0DCC0,
0x00F0CAA6
};
memcpy(current_palette, sysPaletteColors, sizeof sysPaletteColors);
for (int i = 0; i < 256; i++)
{
current_palette[i].rgba.peFlags = 0;
}
auto pltSrc = &plt[10];
auto pltDst = ¤t_palette[10];
for (int index = 236; index > 0; --index)
{
if (plt)
{
pltDst->rgba.peRed = pltSrc->rgba.peRed;
pltDst->rgba.peGreen = pltSrc->rgba.peGreen;
pltDst->rgba.peBlue = pltSrc->rgba.peBlue;
}
pltDst->rgba.peFlags = 4;
pltSrc++;
pltDst++;
}
current_palette[255].rgba.peBlue = -1;
current_palette[255].rgba.peGreen = -1;
current_palette[255].rgba.peRed = -1;
return 0;
}
int gdrv::destroy_bitmap(gdrv_bitmap8* bmp)
{
if (!bmp)
return -1;
if (bmp->BitmapType != BitmapTypes::None)
{
memory::free(bmp->BmpBufPtr1);
}
memset(bmp, 0, sizeof(gdrv_bitmap8));
return 0;
}
void gdrv::start_blit_sequence()
{
}
void gdrv::end_blit_sequence()
{
}
void gdrv::blit(gdrv_bitmap8* bmp, int xSrc, int ySrc, int xDest, int yDest, int width, int height)
{
StretchDIBitsScaled(
xSrc,
ySrc ,
xDest,
yDest,
width,
height,
bmp
);
}
void gdrv::blat(gdrv_bitmap8* bmp, int xDest, int yDest)
{
StretchDIBitsScaled(
0,
0,
xDest,
yDest,
bmp->Width,
bmp->Height,
bmp
);
}
void gdrv::fill_bitmap(gdrv_bitmap8* bmp, int width, int height, int xOff, int yOff, char fillChar)
{
int bmpHeight = bmp->Height;
if (bmpHeight < 0)
bmpHeight = -bmpHeight;
char* bmpPtr = &bmp->BmpBufPtr1[bmp->Width * (bmpHeight - height - yOff) + xOff];
for (; height > 0; --height)
{
if (width > 0)
memset(bmpPtr, fillChar, width);
bmpPtr += bmp->Stride;
}
}
void gdrv::copy_bitmap(gdrv_bitmap8* dstBmp, int width, int height, int xOff, int yOff, gdrv_bitmap8* srcBmp,
int srcXOff, int srcYOff)
{
int dstHeight = abs(dstBmp->Height);
int srcHeight = abs(srcBmp->Height);
char* srcPtr = &srcBmp->BmpBufPtr1[srcBmp->Stride * (srcHeight - height - srcYOff) + srcXOff];
char* dstPtr = &dstBmp->BmpBufPtr1[dstBmp->Stride * (dstHeight - height - yOff) + xOff];
for (int y = height; y > 0; --y)
{
for (int x = width; x > 0; --x)
*dstPtr++ = *srcPtr++;
srcPtr += srcBmp->Stride - width;
dstPtr += dstBmp->Stride - width;
}
}
void gdrv::copy_bitmap_w_transparency(gdrv_bitmap8* dstBmp, int width, int height, int xOff, int yOff,
gdrv_bitmap8* srcBmp, int srcXOff, int srcYOff)
{
int dstHeight = abs(dstBmp->Height);
int srcHeight = abs(srcBmp->Height);
char* srcPtr = &srcBmp->BmpBufPtr1[srcBmp->Stride * (srcHeight - height - srcYOff) + srcXOff];
char* dstPtr = &dstBmp->BmpBufPtr1[dstBmp->Stride * (dstHeight - height - yOff) + xOff];
for (int y = height; y > 0; --y)
{
for (int x = width; x > 0; --x)
{
if (*srcPtr)
*dstPtr = *srcPtr;
++srcPtr;
++dstPtr;
}
srcPtr += srcBmp->Stride - width;
dstPtr += dstBmp->Stride - width;
}
}
void gdrv::grtext_draw_ttext_in_box(LPCSTR text, int xOff, int yOff, int width, int height, int a6)
{
}
int gdrv::StretchDIBitsScaled(int xSrc, int ySrc, int xDst, int yDst,
int width, int height, gdrv_bitmap8* bmp)
{
// Y is inverted, X normal left to right
ySrc = std::max(0, std::min(bmp->Height - height, bmp->Height)) - ySrc;
yDst = std::max(0, std::min(vScreenHeight - height, vScreenHeight)) - yDst;
// Negative dst == positive src offset
if (xDst < 0)
{
xSrc -= xDst;
xDst = 0;
}
if (yDst < 0)
{
ySrc -= yDst;
yDst = 0;
}
// Clamp out of bounds rectangles
xSrc = std::max(0, std::min(xSrc, bmp->Width));
ySrc = std::max(0, std::min(ySrc, bmp->Height));
if (xSrc + width > bmp->Width)
width = bmp->Width - xSrc;
if (ySrc + height > bmp->Height)
height = bmp->Height - ySrc;
xDst = std::max(0, std::min(xDst, vScreenWidth));
yDst = std::max(0, std::min(yDst, vScreenHeight));
if (xDst + width > vScreenWidth)
width = vScreenWidth - xDst;
if (yDst + height > vScreenHeight)
height = vScreenHeight - yDst;
auto srcPtr = reinterpret_cast<uint8_t*>(&bmp->BmpBufPtr1[bmp->Stride * ySrc + xSrc]);
auto dstPtr = &reinterpret_cast<uint32_t*>(vScreenPixels)[vScreenWidth * yDst + xDst];
for (int y = height; y > 0; --y)
{
for (int x = width; x > 0; --x)
{
*dstPtr++ = current_palette[*srcPtr++].Color;
}
srcPtr += bmp->Stride - width;
dstPtr += vScreenWidth - width;
}
return 0;
}
void gdrv::BlitScreen()
{
unsigned char* lockedPixels = nullptr;
int pitch = 0;
SDL_LockTexture
(
vScreenTex,
nullptr,
reinterpret_cast<void**>(&lockedPixels),
&pitch
);
std::memcpy(lockedPixels, vScreenPixels, vScreenWidth * vScreenHeight * 4);
SDL_UnlockTexture(vScreenTex);
SDL_RenderCopyEx(winmain::Renderer, vScreenTex, nullptr, &DestinationRect, 0, nullptr, SDL_FLIP_VERTICAL);
}