Skip to content

Commit 22ce8ac

Browse files
committed
gdrv: blit no more, present render:vScreen directly.
Improved split bitmap handling.
1 parent 625a6e7 commit 22ce8ac

16 files changed

+289
-466
lines changed

SpaceCadetPinball/GroupData.cpp

+31-22
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ EntryData::~EntryData()
1818
zdrv::destroy_zmap(reinterpret_cast<zmap_header_type*>(Buffer));
1919
memory::free(Buffer);
2020
}
21-
if (DerivedBmp)
22-
{
23-
gdrv::destroy_bitmap(DerivedBmp);
24-
memory::free(DerivedBmp);
25-
}
26-
if (DerivedZMap)
27-
{
28-
zdrv::destroy_zmap(DerivedZMap);
29-
memory::free(DerivedZMap);
30-
}
3121
}
3222

3323

@@ -38,30 +28,31 @@ GroupData::GroupData(int groupId)
3828

3929
void GroupData::AddEntry(EntryData* entry)
4030
{
41-
Entries.push_back(entry);
42-
31+
auto addEntry = true;
4332
switch (entry->EntryType)
4433
{
4534
case FieldTypes::GroupName:
4635
GroupName = entry->Buffer;
4736
break;
4837
case FieldTypes::Bitmap8bit:
4938
{
50-
auto bmp = reinterpret_cast<gdrv_bitmap8*>(entry->Buffer);
51-
if (bmp->BitmapType == BitmapTypes::Spliced)
39+
auto srcBmp = reinterpret_cast<gdrv_bitmap8*>(entry->Buffer);
40+
if (srcBmp->BitmapType == BitmapTypes::Spliced)
5241
{
5342
// Get rid of spliced bitmap early on, to simplify render pipeline
54-
auto splitBmp = memory::allocate<gdrv_bitmap8>();
55-
auto splitZMap = memory::allocate<zmap_header_type>();
56-
SplitSplicedBitmap(*bmp, *splitBmp, *splitZMap);
57-
entry->DerivedBmp = splitBmp;
58-
entry->DerivedZMap = splitZMap;
59-
SetBitmap(splitBmp);
60-
SetZMap(splitZMap);
43+
auto bmp = memory::allocate<gdrv_bitmap8>();
44+
auto zMap = memory::allocate<zmap_header_type>();
45+
SplitSplicedBitmap(*srcBmp, *bmp, *zMap);
46+
47+
NeedsSort = true;
48+
addEntry = false;
49+
AddEntry(new EntryData(FieldTypes::Bitmap8bit, reinterpret_cast<char*>(bmp)));
50+
AddEntry(new EntryData(FieldTypes::Bitmap16bit, reinterpret_cast<char*>(zMap)));
51+
delete entry;
6152
}
6253
else
6354
{
64-
SetBitmap(bmp);
55+
SetBitmap(srcBmp);
6556
}
6657
break;
6758
}
@@ -72,6 +63,24 @@ void GroupData::AddEntry(EntryData* entry)
7263
}
7364
default: break;
7465
}
66+
67+
if (addEntry)
68+
Entries.push_back(entry);
69+
}
70+
71+
void GroupData::FinalizeGroup()
72+
{
73+
if (NeedsSort)
74+
{
75+
// Entries within a group are sorted by EntryType, in ascending order.
76+
// Dat files follow this rule, zMaps inserted in the middle break it.
77+
NeedsSort = false;
78+
std::sort(Entries.begin(), Entries.end(), [](const EntryData* lhs, const EntryData* rhs)
79+
{
80+
return lhs->EntryType < rhs->EntryType;
81+
});
82+
Entries.shrink_to_fit();
83+
}
7584
}
7685

7786
gdrv_bitmap8* GroupData::GetBitmap(int resolution) const

SpaceCadetPinball/GroupData.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ enum class FieldTypes : int16_t
4343

4444
struct EntryData
4545
{
46+
EntryData() = default;
47+
48+
EntryData(FieldTypes entryType, char* buffer): EntryType(entryType), FieldSize(-1), Buffer(buffer)
49+
{
50+
}
51+
4652
~EntryData();
4753
FieldTypes EntryType{};
4854
int FieldSize{};
4955
char* Buffer{};
50-
gdrv_bitmap8* DerivedBmp{};
51-
zmap_header_type* DerivedZMap{};
5256
};
5357

5458
class GroupData
@@ -59,6 +63,7 @@ class GroupData
5963

6064
GroupData(int groupId);
6165
void AddEntry(EntryData* entry);
66+
void FinalizeGroup();
6267
const std::vector<EntryData*>& GetEntries() const { return Entries; }
6368
const EntryData* GetEntry(size_t index) const { return Entries[index]; }
6469
size_t EntryCount() const { return Entries.size(); }
@@ -70,6 +75,7 @@ class GroupData
7075
std::vector<EntryData*> Entries;
7176
gdrv_bitmap8* Bitmaps[3]{};
7277
zmap_header_type* ZMaps[3]{};
78+
bool NeedsSort = false;
7379

7480
static void SplitSplicedBitmap(const gdrv_bitmap8& srcBmp, gdrv_bitmap8& bmp, zmap_header_type& zMap);
7581

SpaceCadetPinball/TTextBox.cpp

+2-27
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,7 @@ void TTextBox::Clear()
8585
OffsetX,
8686
OffsetY);
8787
else
88-
gdrv::fill_bitmap(&render::vscreen, Width, Height, OffsetX, OffsetY, 0);
89-
gdrv::blit(
90-
&render::vscreen,
91-
OffsetX,
92-
OffsetY,
93-
OffsetX + render::vscreen.XPosition,
94-
OffsetY + render::vscreen.YPosition,
95-
Width,
96-
Height);
88+
gdrv::fill_bitmap(&render::vscreen, Width, Height, OffsetX, OffsetY, 0);
9789
if (Timer)
9890
{
9991
if (Timer != -1)
@@ -198,14 +190,6 @@ void TTextBox::Draw()
198190
auto font = Font;
199191
if (!font)
200192
{
201-
gdrv::blit(
202-
&render::vscreen,
203-
OffsetX,
204-
OffsetY,
205-
OffsetX + render::vscreen.XPosition,
206-
OffsetY + render::vscreen.YPosition,
207-
Width,
208-
Height);
209193
gdrv::grtext_draw_ttext_in_box(
210194
Message1->Text,
211195
render::vscreen.XPosition + OffsetX,
@@ -275,14 +259,5 @@ void TTextBox::Draw()
275259
if ((*text & 0x7F) == '\n')
276260
++text;
277261
}
278-
}
279-
280-
gdrv::blit(
281-
&render::vscreen,
282-
OffsetX,
283-
OffsetY,
284-
OffsetX + render::vscreen.XPosition,
285-
OffsetY + render::vscreen.YPosition,
286-
Width,
287-
Height);
262+
}
288263
}

SpaceCadetPinball/fullscrn.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void fullscrn::window_size_changed()
121121
OffsetY = static_cast<int>(floor((height - res->TableHeight * ScaleY) / 2));
122122
}
123123

124-
gdrv::DestinationRect = SDL_Rect
124+
render::DestinationRect = SDL_Rect
125125
{
126126
OffsetX, OffsetY,
127127
width - OffsetX * 2, height - OffsetY * 2

SpaceCadetPinball/gdrv.cpp

+16-136
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,7 @@
99
#include "score.h"
1010
#include "winmain.h"
1111

12-
SDL_Texture* gdrv::vScreenTex = nullptr;
13-
ColorRgba* gdrv::vScreenPixels = nullptr;
14-
int gdrv::vScreenWidth, gdrv::vScreenHeight;
1512
ColorRgba gdrv::current_palette[256]{};
16-
SDL_Rect gdrv::DestinationRect{};
17-
18-
int gdrv::init(int width, int height)
19-
{
20-
{
21-
UsingSdlHint hint{SDL_HINT_RENDER_SCALE_QUALITY, options::Options.LinearFiltering ? "linear" : "nearest"};
22-
vScreenTex = SDL_CreateTexture
23-
(
24-
winmain::Renderer,
25-
SDL_PIXELFORMAT_ARGB8888,
26-
SDL_TEXTUREACCESS_STREAMING,
27-
width, height
28-
);
29-
}
30-
vScreenWidth = width;
31-
vScreenHeight = height;
32-
vScreenPixels = new ColorRgba[width * height];
33-
34-
return 0;
35-
}
36-
37-
int gdrv::uninit()
38-
{
39-
SDL_DestroyTexture(vScreenTex);
40-
delete[] vScreenPixels;
41-
return 0;
42-
}
43-
44-
void gdrv::get_focus()
45-
{
46-
}
4713

4814
int gdrv::create_bitmap(gdrv_bitmap8* bmp, int width, int height, int stride, bool indexed)
4915
{
@@ -122,23 +88,23 @@ int gdrv::display_palette(ColorRgba* plt)
12288
{
12389
const uint32_t sysPaletteColors[]
12490
{
125-
0x00000000, // Color 0: transparent
126-
0x00000080,
127-
0x00008000,
128-
0x00008080,
129-
0x00800000,
130-
0x00800080,
131-
0x00808000,
132-
0x00C0C0C0,
133-
0x00C0DCC0,
134-
0x00F0CAA6
91+
0xff000000, // Color 0: transparent
92+
0xff000080,
93+
0xff008000,
94+
0xff008080,
95+
0xff800000,
96+
0xff800080,
97+
0xff808000,
98+
0xffC0C0C0,
99+
0xffC0DCC0,
100+
0xffF0CAA6
135101
};
136102

137103
memcpy(current_palette, sysPaletteColors, sizeof sysPaletteColors);
138104

139105
for (int i = 0; i < 256; i++)
140106
{
141-
current_palette[i].rgba.peFlags = 0;
107+
current_palette[i].rgba.Alpha = 0;
142108
}
143109

144110
auto pltSrc = &plt[10];
@@ -147,18 +113,16 @@ int gdrv::display_palette(ColorRgba* plt)
147113
{
148114
if (plt)
149115
{
150-
pltDst->rgba.peRed = pltSrc->rgba.peRed;
151-
pltDst->rgba.peGreen = pltSrc->rgba.peGreen;
152-
pltDst->rgba.peBlue = pltSrc->rgba.peBlue;
116+
pltDst->rgba.Blue = pltSrc->rgba.Blue;
117+
pltDst->rgba.Green = pltSrc->rgba.Green;
118+
pltDst->rgba.Red = pltSrc->rgba.Red;
153119
}
154-
pltDst->rgba.peFlags = 4;
120+
pltDst->rgba.Alpha = 0xFF;
155121
pltSrc++;
156122
pltDst++;
157123
}
158124

159-
current_palette[255].rgba.peBlue = -1;
160-
current_palette[255].rgba.peGreen = -1;
161-
current_palette[255].rgba.peRed = -1;
125+
current_palette[255].Color = 0xffFFFFFF;
162126

163127
score::ApplyPalette();
164128
for (const auto group : pb::record_table->Groups)
@@ -194,32 +158,6 @@ int gdrv::destroy_bitmap(gdrv_bitmap8* bmp)
194158
return 0;
195159
}
196160

197-
void gdrv::blit(gdrv_bitmap8* bmp, int xSrc, int ySrc, int xDest, int yDest, int width, int height)
198-
{
199-
StretchDIBitsScaled(
200-
xSrc,
201-
ySrc,
202-
xDest,
203-
yDest,
204-
width,
205-
height,
206-
bmp
207-
);
208-
}
209-
210-
void gdrv::blat(gdrv_bitmap8* bmp, int xDest, int yDest)
211-
{
212-
StretchDIBitsScaled(
213-
0,
214-
0,
215-
xDest,
216-
yDest,
217-
bmp->Width,
218-
bmp->Height,
219-
bmp
220-
);
221-
}
222-
223161
void gdrv::fill_bitmap(gdrv_bitmap8* bmp, int width, int height, int xOff, int yOff, uint8_t fillChar)
224162
{
225163
auto color = current_palette[fillChar];
@@ -272,64 +210,6 @@ void gdrv::grtext_draw_ttext_in_box(LPCSTR text, int xOff, int yOff, int width,
272210
{
273211
}
274212

275-
int gdrv::StretchDIBitsScaled(int xSrc, int ySrc, int xDst, int yDst,
276-
int width, int height, gdrv_bitmap8* bmp)
277-
{
278-
// Negative dst == positive src offset
279-
if (xDst < 0)
280-
{
281-
xSrc -= xDst;
282-
xDst = 0;
283-
}
284-
if (yDst < 0)
285-
{
286-
ySrc -= yDst;
287-
yDst = 0;
288-
}
289-
290-
// Clamp out of bounds rectangles
291-
xSrc = std::max(0, std::min(xSrc, bmp->Width));
292-
ySrc = std::max(0, std::min(ySrc, bmp->Height));
293-
if (xSrc + width > bmp->Width)
294-
width = bmp->Width - xSrc;
295-
if (ySrc + height > bmp->Height)
296-
height = bmp->Height - ySrc;
297-
298-
xDst = std::max(0, std::min(xDst, vScreenWidth));
299-
yDst = std::max(0, std::min(yDst, vScreenHeight));
300-
if (xDst + width > vScreenWidth)
301-
width = vScreenWidth - xDst;
302-
if (yDst + height > vScreenHeight)
303-
height = vScreenHeight - yDst;
304-
305-
auto srcPtr = &bmp->BmpBufPtr1[bmp->Stride * ySrc + xSrc];
306-
auto dstPtr = &vScreenPixels[vScreenWidth * yDst + xDst];
307-
for (int y = height; y > 0; --y)
308-
{
309-
std::memcpy(dstPtr, srcPtr, width * sizeof(ColorRgba));
310-
srcPtr += bmp->Stride;
311-
dstPtr += vScreenWidth;
312-
}
313-
314-
return 0;
315-
}
316-
317-
void gdrv::BlitScreen()
318-
{
319-
int pitch = 0;
320-
void* lockedPixels;
321-
SDL_LockTexture
322-
(
323-
vScreenTex,
324-
nullptr,
325-
&lockedPixels,
326-
&pitch
327-
);
328-
std::memcpy(lockedPixels, vScreenPixels, vScreenWidth * vScreenHeight * sizeof(ColorRgba));
329-
SDL_UnlockTexture(vScreenTex);
330-
SDL_RenderCopy(winmain::Renderer, vScreenTex, nullptr, &DestinationRect);
331-
}
332-
333213
void gdrv::ApplyPalette(gdrv_bitmap8& bmp)
334214
{
335215
if (bmp.BitmapType == BitmapTypes::None)

0 commit comments

Comments
 (0)