Converted memory to direct pointers.

Fixed memory leaks in uninit.
Fixed some of the code analysis warnings.
Enabled /MP build.
Cleaned up the code.
This commit is contained in:
Muzychenko Andrey 2021-02-02 18:29:54 +03:00
parent 49f6132d23
commit d594f5fdb7
26 changed files with 264 additions and 341 deletions

View File

@ -1,3 +1,3 @@
rc /Fo.\DrMem\SpaceCadetPinball.res ".\SpaceCadetPinball\SpaceCadetPinball.rc" rc /Fo.\DrMem\SpaceCadetPinball.res ".\SpaceCadetPinball\SpaceCadetPinball.rc"
cl /Zi /MT /EHsc /O /Ob0 /cgthreads4 /Fo.\DrMem\ /Fe.\DrMem\myapp.exe ".\SpaceCadetPinball\*.cpp" Comctl32.lib Winmm.lib Htmlhelp.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib ".\DrMem\SpaceCadetPinball.res" cl /Zi /MT /MP /EHsc /O /Ob0 /cgthreads4 /Fo.\DrMem\ /Fe.\DrMem\myapp.exe ".\SpaceCadetPinball\*.cpp" Comctl32.lib Winmm.lib Htmlhelp.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib ".\DrMem\SpaceCadetPinball.res"

View File

@ -94,6 +94,7 @@
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<BufferSecurityCheck>true</BufferSecurityCheck> <BufferSecurityCheck>true</BufferSecurityCheck>
<PreprocessToFile>false</PreprocessToFile> <PreprocessToFile>false</PreprocessToFile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@ -110,6 +111,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@ -128,6 +130,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@ -148,6 +151,7 @@
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile> <PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>

View File

@ -18,7 +18,7 @@ TFlagSpinner::TFlagSpinner(TPinballTable* table, int groupIndex) : TCollisionCom
Timer = 0; Timer = 0;
loader::query_visual(groupIndex, 0, &visual); loader::query_visual(groupIndex, 0, &visual);
end.X = *visual.FloatArr; end.X = visual.FloatArr[0];
end.Y = visual.FloatArr[1]; end.Y = visual.FloatArr[1];
start.X = visual.FloatArr[2]; start.X = visual.FloatArr[2];
start.Y = visual.FloatArr[3]; start.Y = visual.FloatArr[3];

View File

@ -439,10 +439,10 @@ int TFlipperEdge::is_ball_inside(float x, float y)
vector_type testPoint{}; vector_type testPoint{};
float dx = RotOrigin.X - x; float dx = RotOrigin.X - x;
float dy = RotOrigin.Y - y; float dy = RotOrigin.Y - y;
if ((A2.X - A1.X) * (y - A1.Y) - (A2.Y - A1.Y) * (x - A1.X) >= 0.0 && if ((A2.X - A1.X) * (y - A1.Y) - (A2.Y - A1.Y) * (x - A1.X) >= 0.0f &&
(B1.X - A2.X) * (y - A2.Y) - (B1.Y - A2.Y) * (x - A2.X) >= 0.0 && (B1.X - A2.X) * (y - A2.Y) - (B1.Y - A2.Y) * (x - A2.X) >= 0.0f &&
(B2.X - B1.X) * (y - B1.Y) - (B2.Y - B1.Y) * (x - B1.X) >= 0.0 && (B2.X - B1.X) * (y - B1.Y) - (B2.Y - B1.Y) * (x - B1.X) >= 0.0f &&
(A1.X - B2.X) * (y - B2.Y) - (A1.Y - B2.Y) * (x - B2.X) >= 0.0 || (A1.X - B2.X) * (y - B2.Y) - (A1.Y - B2.Y) * (x - B2.X) >= 0.0f ||
dy * dy + dx * dx <= CirclebaseRadiusSq || dy * dy + dx * dx <= CirclebaseRadiusSq ||
(T1.Y - y) * (T1.Y - y) + (T1.X - x) * (T1.X - x) < CircleT1RadiusSq) (T1.Y - y) * (T1.Y - y) + (T1.X - x) * (T1.X - x) < CircleT1RadiusSq)
{ {
@ -455,7 +455,7 @@ int TFlipperEdge::is_ball_inside(float x, float y)
testPoint = T1; testPoint = T1;
if (((y - testPoint.Y) * (RotOrigin.X - testPoint.X) - if (((y - testPoint.Y) * (RotOrigin.X - testPoint.X) -
(x - testPoint.X) * (RotOrigin.Y - testPoint.Y)) * flipperLR < 0.0) (x - testPoint.X) * (RotOrigin.Y - testPoint.Y)) * flipperLR < 0.0f)
return 4; return 4;
return 5; return 5;
} }

View File

@ -30,7 +30,7 @@ TKickout::TKickout(TPinballTable* table, int groupIndex, bool someFlag): TCollis
SoftHitSoundId = visual.SoftHitSoundId; SoftHitSoundId = visual.SoftHitSoundId;
HardHitSoundId = visual.Kicker.HardHitSoundId; HardHitSoundId = visual.Kicker.HardHitSoundId;
Circle.Center.X = *visual.FloatArr; Circle.Center.X = visual.FloatArr[0];
Circle.Center.Y = visual.FloatArr[1]; Circle.Center.Y = visual.FloatArr[1];
Circle.RadiusSq = *loader::query_float_attribute(groupIndex, 0, 306) * visual.FloatArr[2]; Circle.RadiusSq = *loader::query_float_attribute(groupIndex, 0, 306) * visual.FloatArr[2];
if (Circle.RadiusSq == 0.0) if (Circle.RadiusSq == 0.0)

View File

@ -19,7 +19,7 @@ TLightBargraph::TLightBargraph(TPinballTable* table, int groupIndex) : TLightGro
if (floatArr) if (floatArr)
{ {
int count = 2 * List->GetCount(); int count = 2 * List->GetCount();
TimerTimeArray = reinterpret_cast<float*>(memory::allocate(count * sizeof(float))); TimerTimeArray = memory::allocate<float>(count);
if (TimerTimeArray) if (TimerTimeArray)
{ {
for (int i = 0; i < count; ++floatArr) for (int i = 0; i < count; ++floatArr)

View File

@ -242,7 +242,7 @@ TPinballComponent* TPinballTable::find_component(LPCSTR componentName)
TPinballComponent* TPinballTable::find_component(int groupIndex) TPinballComponent* TPinballTable::find_component(int groupIndex)
{ {
char Buffer[40]; char Buffer[40]{};
int objCount = ComponentList->GetCount(); int objCount = ComponentList->GetCount();
if (objCount > 0) if (objCount > 0)
{ {

View File

@ -616,17 +616,15 @@ TPinballComponent* control::make_component_link(component_tag_base* tag)
void control::handler(int code, TPinballComponent* cmp) void control::handler(int code, TPinballComponent* cmp)
{ {
component_control* control = cmp->Control; component_control* control = cmp->Control;
int scoreInd = 0;
if (control) if (control)
{ {
if (code == 1019 && control->ScoreCount > 0) if (code == 1019)
{ {
do for (auto scoreInd = 0; scoreInd < control->ScoreCount; ++scoreInd)
{ {
cmp->put_scoring(scoreInd, control->Scores[scoreInd]); cmp->put_scoring(scoreInd, control->Scores[scoreInd]);
++scoreInd;
} }
while (scoreInd < control->ScoreCount);
} }
control->ControlFunc(code, cmp); control->ControlFunc(code, cmp);
} }
@ -3019,7 +3017,7 @@ void control::GameoverController(int code, TPinballComponent* caller)
int missionMsg = control_mission_text_box_tag.Component->MessageField; int missionMsg = control_mission_text_box_tag.Component->MessageField;
if (missionMsg & 0x100) if (missionMsg & 0x100)
{ {
int playerId = missionMsg & 0xF; int playerId = missionMsg % 4;
int playerScore = TableG->PlayerScores[playerId].ScoreStruct->Score; int playerScore = TableG->PlayerScores[playerId].ScoreStruct->Score;
auto nextPlayerId = playerId + 1; auto nextPlayerId = playerId + 1;
if (playerScore >= 0) if (playerScore >= 0)
@ -3056,7 +3054,7 @@ void control::GameoverController(int code, TPinballComponent* caller)
if (missionMsg & 0x200) if (missionMsg & 0x200)
{ {
int highscoreId = missionMsg & 0xF; int highscoreId = missionMsg % 4;
int highScore = pb::highscore_table[highscoreId].Score; int highScore = pb::highscore_table[highscoreId].Score;
auto nextHidhscoreId = highscoreId + 1; auto nextHidhscoreId = highscoreId + 1;
if (highScore > 0) if (highScore > 0)

View File

@ -26,7 +26,7 @@ struct component_tag : component_tag_base
static_assert(std::is_base_of<TPinballComponent, T>::value, "T must inherit from TPinballComponent"); static_assert(std::is_base_of<TPinballComponent, T>::value, "T must inherit from TPinballComponent");
T* Component; T* Component;
component_tag(LPCSTR name, TPinballComponent* component): component_tag_base(name) component_tag(LPCSTR name, TPinballComponent* component): component_tag_base(name), Component(nullptr)
{ {
component_tag::SetComponent(component); component_tag::SetComponent(component);
} }

View File

@ -7,7 +7,6 @@
HPALETTE gdrv::palette_handle = nullptr; HPALETTE gdrv::palette_handle = nullptr;
HINSTANCE gdrv::hinst; HINSTANCE gdrv::hinst;
HWND gdrv::hwnd; HWND gdrv::hwnd;
LOGPALETTEx256 gdrv::current_palette{};
int gdrv::sequence_handle; int gdrv::sequence_handle;
HDC gdrv::sequence_hdc; HDC gdrv::sequence_hdc;
int gdrv::use_wing = 0; int gdrv::use_wing = 0;
@ -18,10 +17,12 @@ int gdrv::grtext_red = -1;
int gdrv::init(HINSTANCE hInst, HWND hWnd) int gdrv::init(HINSTANCE hInst, HWND hWnd)
{ {
LOGPALETTEx256 current_palette{};
hinst = hInst; hinst = hInst;
hwnd = hWnd; hwnd = hWnd;
if (!palette_handle) if (!palette_handle)
palette_handle = CreatePalette((LOGPALETTE*)&current_palette); palette_handle = CreatePalette(&current_palette);
return 0; return 0;
} }
@ -39,15 +40,15 @@ void gdrv::get_focus()
BITMAPINFO* gdrv::DibCreate(__int16 bpp, int width, int height) BITMAPINFO* gdrv::DibCreate(__int16 bpp, int width, int height)
{ {
auto sizeBytes = height * ((width * bpp / 8 + 3) & 0xFFFFFFFC); auto sizeBytes = height * (width * bpp / 8 + 3 & 0xFFFFFFFC);
auto buf = GlobalAlloc(0x42u, sizeBytes + 1064); auto buf = GlobalAlloc(GHND, sizeBytes + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
auto dib = static_cast<BITMAPINFO*>(GlobalLock(buf)); auto dib = static_cast<BITMAPINFO*>(GlobalLock(buf));
if (!dib) if (!dib)
return nullptr; return nullptr;
dib->bmiHeader.biSizeImage = sizeBytes; dib->bmiHeader.biSizeImage = sizeBytes;
dib->bmiHeader.biWidth = width; dib->bmiHeader.biWidth = width;
dib->bmiHeader.biSize = 40; dib->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
dib->bmiHeader.biHeight = height; dib->bmiHeader.biHeight = height;
dib->bmiHeader.biPlanes = 1; dib->bmiHeader.biPlanes = 1;
dib->bmiHeader.biBitCount = bpp; dib->bmiHeader.biBitCount = bpp;
@ -65,25 +66,15 @@ BITMAPINFO* gdrv::DibCreate(__int16 bpp, int width, int height)
dib->bmiHeader.biClrUsed = 256; dib->bmiHeader.biClrUsed = 256;
} }
int index = 0;
for (auto i = (int*)dib->bmiColors; index < static_cast<signed int>(dib->bmiHeader.biClrUsed) / 16; ++index) uint32_t paletteColors[]
{ {
*i++ = 0; 0, 0x800000, 0x8000, 8421376, 128, 8388736, 32896, 12632256,
*i++ = 0x800000; 8421504, 16711680, 65280, 16776960, 255, 16711935, 0xFFFF, 0xFFFFFF,
*i++ = 0x8000; };
*i++ = 8421376; for (auto index = 0u; index < dib->bmiHeader.biClrUsed; index += 16)
*i++ = 128; {
*i++ = 8388736; memcpy(&dib->bmiColors[index], paletteColors, sizeof paletteColors);
*i++ = 32896;
*i++ = 12632256;
*i++ = 8421504;
*i++ = 16711680;
*i++ = 65280;
*i++ = 16776960;
*i++ = 255;
*i++ = 16711935;
*i++ = 0xFFFF;
*i++ = 0xFFFFFF;
} }
return dib; return dib;
} }
@ -108,31 +99,24 @@ void gdrv::DibSetUsage(BITMAPINFO* dib, HPALETTE hpal, int someFlag)
{ {
if (someFlag && someFlag <= 2) if (someFlag && someFlag <= 2)
{ {
auto pltPtr = (short*)((char*)dib + dib->bmiHeader.biSize); auto pltPtr = reinterpret_cast<short*>(dib->bmiColors);
for (int i = 0; i < numOfColors; ++i) for (auto i = 0; i < numOfColors; ++i)
{ {
*pltPtr++ = i; *pltPtr++ = i;
} }
} }
else else
{ {
assertm(false, "Entered bad code");
char* dibPtr = (char*)dib + dib->bmiHeader.biSize;
if (numOfColors >= 256) if (numOfColors >= 256)
numOfColors = 256; numOfColors = 256;
GetPaletteEntries(hpal, 0, numOfColors, pPalEntries); GetPaletteEntries(hpal, 0, numOfColors, pPalEntries);
int index = 0; for (auto index = 0; index < numOfColors; index++)
char* dibPtr2 = dibPtr + 1;
do
{ {
char v9 = pPalEntries[index++].peRed; dib->bmiColors[index].rgbRed = pPalEntries[index].peRed;
dibPtr2[1] = v9; dib->bmiColors[index].rgbGreen = pPalEntries[index].peGreen;
*dibPtr2 = dibPtr2[(char*)pPalEntries - dibPtr]; dib->bmiColors[index].rgbBlue = pPalEntries[index].peBlue;
*(dibPtr2 - 1) = dibPtr2[&pPalEntries[0].peGreen - (unsigned char*)dibPtr]; dib->bmiColors[index].rgbReserved = 0;
dibPtr2[2] = 0;
dibPtr2 += 4;
} }
while (index < numOfColors);
} }
} }
} }
@ -156,7 +140,7 @@ int gdrv::create_bitmap_dib(gdrv_bitmap8* bmp, int width, int height)
if (dib->bmiHeader.biCompression == 3) if (dib->bmiHeader.biCompression == 3)
bmpBufPtr = (char*)&dib->bmiHeader.biPlanes + dib->bmiHeader.biSize; bmpBufPtr = (char*)&dib->bmiHeader.biPlanes + dib->bmiHeader.biSize;
else else
bmpBufPtr = (char*)&dib->bmiHeader.biSize + 4 * dib->bmiHeader.biClrUsed + dib->bmiHeader.biSize; bmpBufPtr = reinterpret_cast<char*>(&dib->bmiColors[dib->bmiHeader.biClrUsed]);
bmp->BmpBufPtr1 = bmpBufPtr; bmp->BmpBufPtr1 = bmpBufPtr;
bmp->BmpBufPtr2 = bmpBufPtr; bmp->BmpBufPtr2 = bmpBufPtr;
return 0; return 0;
@ -203,17 +187,19 @@ int gdrv::create_spliced_bitmap(gdrv_bitmap8* bmp, int width, int height, int si
int gdrv::display_palette(PALETTEENTRY* plt) int gdrv::display_palette(PALETTEENTRY* plt)
{ {
LOGPALETTEx256 current_palette{};
if (palette_handle) if (palette_handle)
DeleteObject(palette_handle); DeleteObject(palette_handle);
palette_handle = CreatePalette((LOGPALETTE*)&current_palette); palette_handle = CreatePalette(&current_palette);
auto windowHandle = GetDesktopWindow(); auto windowHandle = GetDesktopWindow();
auto dc = winmain::_GetDC(windowHandle); auto dc = winmain::_GetDC(windowHandle);
SetSystemPaletteUse(dc, 2u); SetSystemPaletteUse(dc, 2u);
SetSystemPaletteUse(dc, 1u); SetSystemPaletteUse(dc, 1u);
auto pltHandle = SelectPalette(dc, palette_handle, 0); auto originalPalette = SelectPalette(dc, palette_handle, 0);
RealizePalette(dc); RealizePalette(dc);
SelectPalette(dc, pltHandle, 0); SelectPalette(dc, originalPalette, 0);
GetSystemPaletteEntries(dc, 0, 0x100u, current_palette.palPalEntry); GetSystemPaletteEntries(dc, 0, 256, current_palette.palPalEntry);
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
{ {
current_palette.palPalEntry[i].peFlags = 0; current_palette.palPalEntry[i].peFlags = 0;
@ -234,15 +220,15 @@ int gdrv::display_palette(PALETTEENTRY* plt)
pltDst++; pltDst++;
} }
if (!(GetDeviceCaps(dc, 38) & 0x100)) if (!(GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE))
{ {
current_palette.palPalEntry[255].peBlue = -1; current_palette.palPalEntry[255].peBlue = -1;
current_palette.palPalEntry[255].peGreen = -1; current_palette.palPalEntry[255].peGreen = -1;
current_palette.palPalEntry[255].peRed = -1; current_palette.palPalEntry[255].peRed = -1;
} }
ResizePalette(palette_handle, 0x100u); ResizePalette(palette_handle, 256);
SetPaletteEntries(palette_handle, 0, 0x100u, current_palette.palPalEntry); SetPaletteEntries(palette_handle, 0, 256, current_palette.palPalEntry);
windowHandle = GetDesktopWindow(); windowHandle = GetDesktopWindow();
ReleaseDC(windowHandle, dc); ReleaseDC(windowHandle, dc);
return 0; return 0;
@ -355,16 +341,11 @@ void gdrv::fill_bitmap(gdrv_bitmap8* bmp, int width, int height, int xOff, int y
if (bmpHeight < 0) if (bmpHeight < 0)
bmpHeight = -bmpHeight; bmpHeight = -bmpHeight;
char* bmpPtr = &bmp->BmpBufPtr1[bmp->Width * (bmpHeight - height - yOff) + xOff]; char* bmpPtr = &bmp->BmpBufPtr1[bmp->Width * (bmpHeight - height - yOff) + xOff];
if (height > 0) for (; height > 0; --height)
{ {
do if (width > 0)
{ memset(bmpPtr, fillChar, width);
if (width > 0) bmpPtr += bmp->Stride;
memset(bmpPtr, fillChar, width);
bmpPtr += bmp->Stride;
--height;
}
while (height);
} }
} }
@ -429,7 +410,7 @@ void gdrv::grtext_draw_ttext_in_box(LPCSTR text, int xOff, int yOff, int width,
sscanf_s(fontColor, "%d %d %d", &grtext_red, &grtext_green, &grtext_blue); sscanf_s(fontColor, "%d %d %d", &grtext_red, &grtext_green, &grtext_blue);
} }
int prevMode = SetBkMode(dc, 1); int prevMode = SetBkMode(dc, 1);
COLORREF color = SetTextColor(dc, (grtext_red) | (grtext_green << 8) | (grtext_blue << 16)); COLORREF color = SetTextColor(dc, grtext_red | grtext_green << 8 | grtext_blue << 16);
DrawTextA(dc, text, lstrlenA(text), &rc, 0x810u); DrawTextA(dc, text, lstrlenA(text), &rc, 0x810u);
SetBkMode(dc, prevMode); SetBkMode(dc, prevMode);
SetTextColor(dc, color); SetTextColor(dc, color);

View File

@ -22,14 +22,14 @@ struct gdrv_bitmap8
int YPosition; int YPosition;
}; };
struct LOGPALETTEx256 struct LOGPALETTEx256 : LOGPALETTE
{ {
WORD palVersion; PALETTEENTRY palPalEntry2[256 - 1];
WORD palNumEntries;
PALETTEENTRY palPalEntry[256];
LOGPALETTEx256() : palVersion(0x300), palNumEntries(256), palPalEntry{} LOGPALETTEx256() : palPalEntry2{}
{ {
palVersion = 0x300;
palNumEntries = 256;
} }
}; };
@ -38,7 +38,6 @@ class gdrv
{ {
public: public:
static HPALETTE palette_handle; static HPALETTE palette_handle;
static LOGPALETTEx256 current_palette;
static int sequence_handle; static int sequence_handle;
static HDC sequence_hdc; static HDC sequence_hdc;
static int use_wing; static int use_wing;

View File

@ -9,7 +9,7 @@
int high_score::dlg_enter_name; int high_score::dlg_enter_name;
int high_score::dlg_score; int high_score::dlg_score;
int high_score::position; int high_score::dlg_position;
LPCSTR high_score::default_name; LPCSTR high_score::default_name;
high_score_struct* high_score::dlg_hst; high_score_struct* high_score::dlg_hst;
@ -48,11 +48,10 @@ int high_score::read(high_score_struct* table, int* ptrToSmth)
if (!buf1) if (!buf1)
return 1; return 1;
char* buf2 = memory::allocate(300u); char* buf2 = memory::allocate(300u);
int position = 0; auto optPath = pinball::get_rc_string(166, 0);
high_score_struct* tablePtr = table; for (auto position = 0; position < 5; ++position)
const CHAR* optPath = pinball::get_rc_string(166, 0);
do
{ {
auto tablePtr = &table[position];
_itoa_s(position, Buffer, 10); _itoa_s(position, Buffer, 10);
lstrcatA(Buffer, ".Name"); lstrcatA(Buffer, ".Name");
options::get_string(optPath, Buffer, buf1, pinball::WindowName, 32); options::get_string(optPath, Buffer, buf1, pinball::WindowName, 32);
@ -66,10 +65,8 @@ int high_score::read(high_score_struct* table, int* ptrToSmth)
{ {
} }
scoreSum += tablePtr->Score; scoreSum += tablePtr->Score;
++position;
++tablePtr;
} }
while (position < 5);
scramble_number_string(scoreSum, buf1); scramble_number_string(scoreSum, buf1);
options::get_string(optPath, "Verification", buf2, pinball::WindowName, 300); options::get_string(optPath, "Verification", buf2, pinball::WindowName, 300);
if (lstrcmpA(buf1, buf2)) if (lstrcmpA(buf1, buf2))
@ -88,9 +85,8 @@ int high_score::write(high_score_struct* table, int* ptrToSmth)
CHAR* buf = memory::allocate(300u); CHAR* buf = memory::allocate(300u);
if (!buf) if (!buf)
return 1; return 1;
int position = 0;
const CHAR* optPath = pinball::get_rc_string(166, 0); const CHAR* optPath = pinball::get_rc_string(166, 0);
do for (auto position = 0; position < 5; ++position)
{ {
_itoa_s(position, Buffer, 10); _itoa_s(position, Buffer, 10);
lstrcatA(Buffer, ".Name"); lstrcatA(Buffer, ".Name");
@ -106,7 +102,6 @@ int high_score::write(high_score_struct* table, int* ptrToSmth)
++position; ++position;
++tablePtr; ++tablePtr;
} }
while (position < 5);
scramble_number_string(scoreSum, buf); scramble_number_string(scoreSum, buf);
options::set_string(optPath, "Verification", buf); options::set_string(optPath, "Verification", buf);
memory::free(buf); memory::free(buf);
@ -177,7 +172,7 @@ void high_score::show_high_score_dialog(high_score_struct* table)
void high_score::show_and_set_high_score_dialog(high_score_struct* table, int score, int pos, LPCSTR defaultName) void high_score::show_and_set_high_score_dialog(high_score_struct* table, int score, int pos, LPCSTR defaultName)
{ {
position = pos; dlg_position = pos;
dlg_score = score; dlg_score = score;
dlg_hst = table; dlg_hst = table;
dlg_enter_name = 1; dlg_enter_name = 1;
@ -214,12 +209,12 @@ INT_PTR high_score::HighScore(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
} }
if (dlg_enter_name == 1) if (dlg_enter_name == 1)
{ {
if (position == -1) if (dlg_position == -1)
{ {
dlg_enter_name = 0; dlg_enter_name = 0;
return 1; return 1;
} }
HWND nameTextBox = GetDlgItem(hWnd, position + DLG_HIGHSCORES_EditName1); HWND nameTextBox = GetDlgItem(hWnd, dlg_position + DLG_HIGHSCORES_EditName1);
ShowWindow(nameTextBox, 5); ShowWindow(nameTextBox, 5);
EnableWindow(nameTextBox, 1); EnableWindow(nameTextBox, 1);
SetFocus(nameTextBox); SetFocus(nameTextBox);
@ -246,9 +241,9 @@ INT_PTR high_score::HighScore(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ {
break; break;
} }
GetDlgItemTextA(hWnd, position + DLG_HIGHSCORES_EditName1, name, 32); GetDlgItemTextA(hWnd, dlg_position + DLG_HIGHSCORES_EditName1, name, 32);
name[31] = 0; name[31] = 0;
place_new_score_into(dlg_hst, dlg_score, name, position); place_new_score_into(dlg_hst, dlg_score, name, dlg_position);
break; break;
case DLG_HIGHSCORES_Cancel: case DLG_HIGHSCORES_Cancel:
break; break;
@ -281,7 +276,7 @@ void high_score::show_high_scores(HWND hDlg, high_score_struct* table)
int nextPosition = 0; int nextPosition = 0;
for (int i = 0; i < 5; ++i) for (int i = 0; i < 5; ++i)
{ {
if (dlg_enter_name == 1 && position == i) if (dlg_enter_name == 1 && dlg_position == i)
{ {
hsdlg_show_score(hDlg, " ", dlg_score, i); hsdlg_show_score(hDlg, " ", dlg_score, i);
nextPosition = 1; nextPosition = 1;

View File

@ -26,7 +26,7 @@ public:
private : private :
static int dlg_enter_name; static int dlg_enter_name;
static int dlg_score; static int dlg_score;
static int position; static int dlg_position;
static LPCSTR default_name; static LPCSTR default_name;
static high_score_struct* dlg_hst; static high_score_struct* dlg_hst;
static winhelp_entry help[21]; static winhelp_entry help[21];

View File

@ -47,20 +47,19 @@ soundListStruct loader::sound_list[65];
int loader::error(int errorCode, int captionCode) int loader::error(int errorCode, int captionCode)
{ {
int curCode = loader_errors[0].Code; auto curCode = loader_errors;
const char *errorText = nullptr, *errorCaption = nullptr; const char *errorText = nullptr, *errorCaption = nullptr;
int index = 0, index2 = 0; auto index = 0;
if (loader_errors[0].Code >= 0) while (curCode->Code >= 0)
do {
{ if (errorCode == curCode->Code)
if (errorCode == curCode) errorText = curCode->Message;
errorText = loader_errors[index2].Message; if (captionCode == curCode->Code)
if (captionCode == curCode) errorCaption = curCode->Message;
errorCaption = loader_errors[index2].Message; curCode++;
index2 = ++index; index++;
curCode = loader_errors[index].Code; }
}
while (curCode >= 0);
if (!errorText) if (!errorText)
errorText = loader_errors[index].Message; errorText = loader_errors[index].Message;
MessageBoxA(nullptr, errorText, errorCaption, 0x2000u); MessageBoxA(nullptr, errorText, errorCaption, 0x2000u);
@ -260,7 +259,7 @@ float loader::query_float_attribute(int groupIndex, int groupIndexOffset, int fi
for (auto skipIndex = 0;; ++skipIndex) for (auto skipIndex = 0;; ++skipIndex)
{ {
auto floatArr = reinterpret_cast<float*>(partman::field_nth(loader_table, stateId, auto floatArr = reinterpret_cast<float*>(partman::field_nth(loader_table, stateId,
datFieldTypes::FloatArray,skipIndex)); datFieldTypes::FloatArray, skipIndex));
if (!floatArr) if (!floatArr)
break; break;
if (static_cast<__int16>(floor(*floatArr)) == firstValue) if (static_cast<__int16>(floor(*floatArr)) == firstValue)

View File

@ -1,58 +1,40 @@
#include "pch.h" #include "pch.h"
#include "memory.h" #include "memory.h"
unsigned int memory::use_total; size_t memory::use_total;
int memory::critical_allocation; int memory::critical_allocation;
void (*memory::critical_callback)(); void (*memory::critical_callback)();
std::map<void*, size_t> memory::alloc_map{};
void memory::init(void (*callback)()) void memory::init(void (*callback)())
{ {
critical_callback = callback; critical_callback = callback;
} }
char* memory::allocate(unsigned int size) char* memory::allocate(size_t size)
{ {
char* buf = static_cast<char*>(malloc(size + 4)); auto buf = static_cast<char*>(malloc(size));
if (buf) if (!buf)
{ {
*(unsigned int*)buf = size << 8; if (critical_allocation && critical_callback)
use_total += size + 4; critical_callback();
*buf = size >= 0xFFDC ? -91 : 90; return nullptr;
return buf + 4;
} }
if (critical_allocation && critical_callback)
critical_callback(); use_total += size;
return nullptr; alloc_map[buf] = size;
return buf;
} }
void memory::free(void* buf) void memory::free(void* buf)
{ {
unsigned int* bufStart = static_cast<unsigned int*>(buf) - 1; auto alloc = alloc_map.find(buf);
use_total -= (*bufStart >> 8) + 4; if (alloc == alloc_map.end())
char firstChar = *(char*)bufStart;
if (firstChar == 90 || firstChar == -91)
std::free(bufStart);
else
assertm(false, "Unknown memory type");
}
char* memory::realloc(void* buf, unsigned int size)
{
if (!buf)
return allocate(size);
char* bufStart = static_cast<char*>(buf) - 4;
use_total -= *(unsigned int*)bufStart >> 8;
if (*bufStart != 90 && *bufStart != -91 ||
(bufStart = static_cast<char*>(std::realloc(bufStart, size + 4))) != nullptr)
{ {
char bufType = *bufStart; assertm(false, "Unknown memory type");
*(unsigned int*)bufStart = size << 8; return;
use_total += size;
*bufStart = bufType;
return bufStart + 4;
} }
if (critical_allocation && critical_callback)
critical_callback(); use_total -= alloc->second;
return nullptr; std::free(alloc->first);
} }

View File

@ -1,22 +1,60 @@
#pragma once #pragma once
#include <map>
class memory class memory
{ {
public: public:
static void init(void (*callback)(void)); static void init(void (*callback)(void));
static char* allocate(unsigned int size); static char* allocate(size_t size);
static void free(void* buf); static void free(void* buf);
static char* realloc(void* buf, unsigned int size);
static unsigned int use_total; template <typename T>
static T* allocate(size_t count = 1, size_t add = 0)
{
size_t size = sizeof(T) * count + add;
auto buf = static_cast<T*>(malloc(size));
if (!buf)
{
if (critical_allocation && critical_callback)
critical_callback();
return nullptr;
}
use_total += size;
alloc_map[buf] = size;
return buf;
}
template <typename T>
static T* realloc(T* buf, size_t size)
{
if (!buf)
return reinterpret_cast<T*>(allocate(size));
auto alloc = alloc_map.find(buf);
if (alloc == alloc_map.end())
{
assertm(false, "Unknown memory type");
return buf;
}
auto newBuf = static_cast<T*>(std::realloc(alloc->first, size));
if (!newBuf)
{
if (critical_allocation && critical_callback)
critical_callback();
return nullptr;
}
use_total += size - alloc->second;
alloc_map.erase(alloc);
alloc_map[newBuf] = size;
return newBuf;
}
static size_t use_total;
static int critical_allocation; static int critical_allocation;
private:
static void (*critical_callback)(); static void (*critical_callback)();
static std::map<void*, size_t> alloc_map;
}; };
// Fill memory block with an integer value
inline void memset32(void* ptr, unsigned int value, int count)
{
auto p = (unsigned int*)ptr;
for (int i = 0; i < count; i++)
*p++ = value;
}

View File

@ -8,7 +8,7 @@ class objlist_class
public: public:
objlist_class(int sizeInt, int growSize) objlist_class(int sizeInt, int growSize)
{ {
ListPtr = reinterpret_cast<T**>(memory::allocate(sizeof(T*) * sizeInt)); ListPtr = memory::allocate<T*>(sizeInt);
Count = 0; Count = 0;
Size = sizeInt; Size = sizeInt;
GrowSize = growSize; GrowSize = growSize;
@ -39,7 +39,7 @@ public:
if (newSize <= Size) if (newSize <= Size)
return; return;
auto newList = reinterpret_cast<T**>(memory::realloc(ListPtr, sizeof(T*) * newSize)); auto newList = memory::realloc(ListPtr, sizeof(T*) * newSize);
if (!newList) if (!newList)
return; return;

View File

@ -25,7 +25,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
_lclose(fileHandle); _lclose(fileHandle);
return nullptr; return nullptr;
} }
auto datFile = (datFileStruct*)memory::allocate(sizeof(datFileStruct)); auto datFile = memory::allocate<datFileStruct>();
if (!datFile) if (!datFile)
{ {
_lclose(fileHandle); _lclose(fileHandle);
@ -38,7 +38,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
else else
{ {
int lenOfStr = lstrlenA(header.Description); int lenOfStr = lstrlenA(header.Description);
auto descriptionBuf = static_cast<char*>(memory::allocate(lenOfStr + 1)); auto descriptionBuf = memory::allocate(lenOfStr + 1);
datFile->Description = descriptionBuf; datFile->Description = descriptionBuf;
if (!descriptionBuf) if (!descriptionBuf)
{ {
@ -51,7 +51,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
if (header.Unknown) if (header.Unknown)
{ {
auto unknownBuf = static_cast<char*>(memory::allocate(header.Unknown)); auto unknownBuf = memory::allocate(header.Unknown);
if (!unknownBuf) if (!unknownBuf)
{ {
_lclose(fileHandle); _lclose(fileHandle);
@ -64,7 +64,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
memory::free(unknownBuf); memory::free(unknownBuf);
} }
auto groupDataBuf = (datGroupData**)memory::allocate(sizeof(void*) * header.NumberOfGroups); auto groupDataBuf = memory::allocate<datGroupData*>(header.NumberOfGroups);
datFile->GroupData = groupDataBuf; datFile->GroupData = groupDataBuf;
if (!groupDataBuf) if (!groupDataBuf)
{ {
@ -79,8 +79,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
{ {
auto entryCount = _lread_char(fileHandle); auto entryCount = _lread_char(fileHandle);
auto groupDataSize = entryCount <= 0 ? 0 : entryCount - 1; auto groupDataSize = entryCount <= 0 ? 0 : entryCount - 1;
auto groupData = reinterpret_cast<datGroupData*>(memory::allocate( auto groupData = memory::allocate<datGroupData>(1, sizeof(datEntryData) * groupDataSize);
sizeof(datEntryData) * groupDataSize + sizeof(datGroupData)));
datFile->GroupData[groupIndex] = groupData; datFile->GroupData[groupIndex] = groupData;
if (!groupData) if (!groupData)
break; break;
@ -105,7 +104,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
continue; continue;
} }
auto bmp = reinterpret_cast<gdrv_bitmap8*>(memory::allocate(sizeof(gdrv_bitmap8))); auto bmp = memory::allocate<gdrv_bitmap8>();
entryData->Buffer = reinterpret_cast<char*>(bmp); entryData->Buffer = reinterpret_cast<char*>(bmp);
if (!bmp) if (!bmp)
{ {
@ -146,7 +145,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
_hread(fileHandle, &zMapHeader, sizeof(dat16BitBmpHeader)); _hread(fileHandle, &zMapHeader, sizeof(dat16BitBmpHeader));
int length = fieldSize - sizeof(dat16BitBmpHeader); int length = fieldSize - sizeof(dat16BitBmpHeader);
auto zmap = reinterpret_cast<zmap_header_type*>(memory::allocate(sizeof(zmap_header_type) + length)); auto zmap = memory::allocate<zmap_header_type>(1, length);
zmap->Width = zMapHeader.Width; zmap->Width = zMapHeader.Width;
zmap->Height = zMapHeader.Height; zmap->Height = zMapHeader.Height;
zmap->Stride = zMapHeader.Stride; zmap->Stride = zMapHeader.Stride;
@ -155,7 +154,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
} }
else else
{ {
char* entryBuffer = static_cast<char*>(memory::allocate(fieldSize)); char* entryBuffer = memory::allocate(fieldSize);
entryData->Buffer = entryBuffer; entryData->Buffer = entryBuffer;
if (!entryBuffer) if (!entryBuffer)
{ {
@ -181,30 +180,23 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful
void partman::unload_records(datFileStruct* datFile) void partman::unload_records(datFileStruct* datFile)
{ {
for (int groupIndex = 0; groupIndex < datFile->NumberOfGroups; ++groupIndex) for (auto groupIndex = 0; groupIndex < datFile->NumberOfGroups; ++groupIndex)
{ {
datGroupData* group = datFile->GroupData[groupIndex]; auto group = datFile->GroupData[groupIndex];
if (group) if (!group)
continue;
for (auto entryIndex = 0; entryIndex < group->EntryCount; ++entryIndex)
{ {
int entryIndex = 0; auto entry = &group->Entries[entryIndex];
if (group->EntryCount > 0) if (entry->Buffer)
{ {
datEntryData* entry = group->Entries; if (entry->EntryType == datFieldTypes::Bitmap8bit)
do gdrv::destroy_bitmap(reinterpret_cast<gdrv_bitmap8*>(entry->Buffer));
{ memory::free(entry->Buffer);
if (entry->Buffer)
{
if (entry->EntryType == datFieldTypes::Bitmap8bit)
gdrv::destroy_bitmap((gdrv_bitmap8*)entry->Buffer);
memory::free(entry->Buffer);
}
++entryIndex;
++entry;
}
while (entryIndex < group->EntryCount);
} }
memory::free(group);
} }
memory::free(group);
} }
if (datFile->Description) if (datFile->Description)
memory::free(datFile->Description); memory::free(datFile->Description);
@ -214,91 +206,45 @@ void partman::unload_records(datFileStruct* datFile)
char* partman::field(datFileStruct* datFile, int groupIndex, datFieldTypes targetEntryType) char* partman::field(datFileStruct* datFile, int groupIndex, datFieldTypes targetEntryType)
{ {
datGroupData* groupData = datFile->GroupData[groupIndex]; auto group = datFile->GroupData[groupIndex];
int entryCount = groupData->EntryCount; for (auto entryIndex = 0; entryIndex < group->EntryCount; ++entryIndex)
int entryIndex = 0;
if (entryCount <= 0)
return nullptr;
datEntryData* entry = groupData->Entries;
while (true)
{ {
auto entryType = entry->EntryType; auto entry = &group->Entries[entryIndex];
if (entryType == targetEntryType) if (entry->EntryType == targetEntryType)
return entry->Buffer;
if (entry->EntryType > targetEntryType)
break; break;
if (entryType > targetEntryType)
return nullptr;
++entryIndex;
++entry;
if (entryIndex < entryCount)
continue;
return nullptr;
} }
return entry->Buffer; return nullptr;
} }
char* partman::field_nth(datFileStruct* datFile, int groupIndex, datFieldTypes targetEntryType, int skipFirstN) char* partman::field_nth(datFileStruct* datFile, int groupIndex, datFieldTypes targetEntryType, int skipFirstN)
{ {
datGroupData* groupData = datFile->GroupData[groupIndex]; auto group = datFile->GroupData[groupIndex];
int entryCount = groupData->EntryCount, skipCount = 0, entryIndex = 0; for (auto skipCount = 0, entryIndex = 0; entryIndex < group->EntryCount; ++entryIndex)
if (0 < entryCount)
{ {
datEntryData* entry = groupData->Entries; auto entry = &group->Entries[entryIndex];
do if (entry->EntryType > targetEntryType)
{ break;
auto entryType = entry->EntryType; if (entry->EntryType == targetEntryType)
if (entryType == targetEntryType) if (skipCount++ == skipFirstN)
{ return entry->Buffer;
if (skipCount == skipFirstN)
{
return entry->Buffer;
}
skipCount++;
}
else
{
if (targetEntryType < entryType)
{
return nullptr;
}
}
entryIndex++;
entry++;
}
while (entryIndex < entryCount);
} }
return nullptr; return nullptr;
} }
int partman::field_size_nth(datFileStruct* datFile, int groupIndex, datFieldTypes targetEntryType, int skipFirstN) int partman::field_size_nth(datFileStruct* datFile, int groupIndex, datFieldTypes targetEntryType, int skipFirstN)
{ {
datGroupData* groupData = datFile->GroupData[groupIndex]; auto group = datFile->GroupData[groupIndex];
int entryCount = groupData->EntryCount, skipCount = 0, entryIndex = 0; for (auto skipCount = 0, entryIndex = 0; entryIndex < group->EntryCount; ++entryIndex)
if (0 < entryCount)
{ {
datEntryData* entry = groupData->Entries; auto entry = &group->Entries[entryIndex];
do if (entry->EntryType > targetEntryType)
{ return 0;
auto entryType = entry->EntryType; if (entry->EntryType == targetEntryType)
if (entryType == targetEntryType) if (skipCount++ == skipFirstN)
{ return entry->FieldSize;
if (skipCount == skipFirstN)
{
return entry->FieldSize;
}
skipCount++;
}
else
{
if (targetEntryType < entryType)
{
return 0;
}
}
entryIndex++;
entry++;
}
while (entryIndex < entryCount);
} }
return 0; return 0;
} }
@ -310,46 +256,28 @@ int partman::field_size(datFileStruct* datFile, int groupIndex, datFieldTypes ta
int partman::record_labeled(datFileStruct* datFile, LPCSTR targetGroupName) int partman::record_labeled(datFileStruct* datFile, LPCSTR targetGroupName)
{ {
int trgGroupNameLen = lstrlenA(targetGroupName); auto targetLength = lstrlenA(targetGroupName);
int groupIndex = datFile->NumberOfGroups; for (int groupIndex = datFile->NumberOfGroups - 1; groupIndex >= 0; --groupIndex)
while (true)
{ {
if (--groupIndex < 0) auto groupName = field(datFile, groupIndex, datFieldTypes::GroupName);
return -1; if (!groupName)
char* groupName = field(datFile, groupIndex, datFieldTypes::GroupName); continue;
if (groupName)
{ int index;
int index = 0; for (index = 0; index < targetLength; index++)
bool found = trgGroupNameLen == 0; if (targetGroupName[index] != groupName[index])
if (trgGroupNameLen > 0)
{
LPCSTR targetNamePtr = targetGroupName;
do
{
if (*targetNamePtr != targetNamePtr[groupName - targetGroupName])
break;
++index;
++targetNamePtr;
}
while (index < trgGroupNameLen);
found = index == trgGroupNameLen;
}
if (found && !targetGroupName[index] && !groupName[index])
break; break;
} if (index == targetLength && !targetGroupName[index] && !groupName[index])
return groupIndex;
} }
return groupIndex;
return -1;
} }
char* partman::field_labeled(datFileStruct* datFile, LPCSTR lpString, datFieldTypes fieldType) char* partman::field_labeled(datFileStruct* datFile, LPCSTR lpString, datFieldTypes fieldType)
{ {
char* result; auto groupIndex = record_labeled(datFile, lpString);
int groupIndex = record_labeled(datFile, lpString); return groupIndex < 0 ? nullptr : field(datFile, groupIndex, fieldType);
if (groupIndex < 0)
result = nullptr;
else
result = field(datFile, groupIndex, fieldType);
return result;
} }
char partman::_lread_char(HFILE hFile) char partman::_lread_char(HFILE hFile)

View File

@ -47,7 +47,7 @@ int pb::init()
score::load_msg_font("pbmsg_ft"); score::load_msg_font("pbmsg_ft");
if (!record_table) if (!record_table)
return (int)&record_table->NumberOfGroups + 1; return 1;
auto plt = (PALETTEENTRY*)partman::field_labeled(record_table, "background", datFieldTypes::Palette); auto plt = (PALETTEENTRY*)partman::field_labeled(record_table, "background", datFieldTypes::Palette);
gdrv::display_palette(plt); gdrv::display_palette(plt);

View File

@ -18,9 +18,9 @@ void render::init(gdrv_bitmap8* bmp, float zMin, float zScaler, int width, int h
zscaler = zScaler; zscaler = zScaler;
zmin = zMin; zmin = zMin;
zmax = 4294967300.0f / zScaler + zMin; zmax = 4294967300.0f / zScaler + zMin;
sprite_list = reinterpret_cast<render_sprite_type_struct**>(memory::allocate(1000 * sizeof(void*))); sprite_list = memory::allocate<render_sprite_type_struct*>(1000);
dirty_list = reinterpret_cast<render_sprite_type_struct**>(memory::allocate(1000 * sizeof(void*))); dirty_list = memory::allocate<render_sprite_type_struct*>(1000);
ball_list = reinterpret_cast<render_sprite_type_struct**>(memory::allocate(20 * sizeof(void*))); ball_list = memory::allocate<render_sprite_type_struct*>(20);
gdrv::create_bitmap(&vscreen, width, height); gdrv::create_bitmap(&vscreen, width, height);
zdrv::create_zmap(&zscreen, width, height); zdrv::create_zmap(&zscreen, width, height);
zdrv::fill(&zscreen, zscreen.Width, zscreen.Height, 0, 0, 0xFFFF); zdrv::fill(&zscreen, zscreen.Width, zscreen.Height, 0, 0, 0xFFFF);
@ -48,9 +48,9 @@ void render::uninit()
{ {
gdrv::destroy_bitmap(&vscreen); gdrv::destroy_bitmap(&vscreen);
zdrv::destroy_zmap(&zscreen); zdrv::destroy_zmap(&zscreen);
for (int i = 0; i < many_sprites; ++i) for (auto i = many_sprites - 1; i >= 0; --i)
remove_sprite(sprite_list[i]); remove_sprite(sprite_list[i]);
for (int j = 0; j < many_balls; ++j) for (auto j = many_balls - 1; j >= 0; --j)
remove_ball(ball_list[j]); remove_ball(ball_list[j]);
memory::free(ball_list); memory::free(ball_list);
memory::free(dirty_list); memory::free(dirty_list);
@ -221,7 +221,7 @@ void render::sprite_modified(render_sprite_type_struct* sprite)
render_sprite_type_struct* render::create_sprite(VisualType visualType, gdrv_bitmap8* bmp, zmap_header_type* zMap, render_sprite_type_struct* render::create_sprite(VisualType visualType, gdrv_bitmap8* bmp, zmap_header_type* zMap,
int xPosition, int yPosition, rectangle_type* rect) int xPosition, int yPosition, rectangle_type* rect)
{ {
auto sprite = (render_sprite_type_struct*)memory::allocate(sizeof(render_sprite_type_struct)); auto sprite = memory::allocate<render_sprite_type_struct>();
if (!sprite) if (!sprite)
return nullptr; return nullptr;
sprite->BmpRect.YPosition = yPosition; sprite->BmpRect.YPosition = yPosition;
@ -278,7 +278,6 @@ render_sprite_type_struct* render::create_sprite(VisualType visualType, gdrv_bit
void render::remove_sprite(render_sprite_type_struct* sprite) void render::remove_sprite(render_sprite_type_struct* sprite)
{ {
int spriteCount = many_sprites;
int index = 0; int index = 0;
if (many_sprites > 0) if (many_sprites > 0)
{ {
@ -287,13 +286,12 @@ void render::remove_sprite(render_sprite_type_struct* sprite)
if (++index >= many_sprites) if (++index >= many_sprites)
return; return;
} }
while (index < spriteCount) while (index < many_sprites)
{ {
sprite_list[index] = sprite_list[index + 1]; sprite_list[index] = sprite_list[index + 1];
spriteCount = many_sprites;
++index; ++index;
} }
many_sprites = spriteCount - 1; many_sprites--;
if (sprite->SpriteArray) if (sprite->SpriteArray)
memory::free(sprite->SpriteArray); memory::free(sprite->SpriteArray);
memory::free(sprite); memory::free(sprite);
@ -302,7 +300,6 @@ void render::remove_sprite(render_sprite_type_struct* sprite)
void render::remove_ball(struct render_sprite_type_struct* ball) void render::remove_ball(struct render_sprite_type_struct* ball)
{ {
int ballCount = many_balls;
int index = 0; int index = 0;
if (many_balls > 0) if (many_balls > 0)
{ {
@ -311,13 +308,12 @@ void render::remove_ball(struct render_sprite_type_struct* ball)
if (++index >= many_balls) if (++index >= many_balls)
return; return;
} }
while (index < ballCount) while (index < many_balls)
{ {
ball_list[index] = ball_list[index + 1]; ball_list[index] = ball_list[index + 1];
ballCount = many_balls;
++index; ++index;
} }
many_balls = ballCount - 1; many_balls--;
memory::free(ball); memory::free(ball);
} }
} }
@ -542,7 +538,7 @@ void render::build_occlude_list()
if (!curSprite->UnknownFlag && curSprite->BoundingRect.Width != -1) if (!curSprite->UnknownFlag && curSprite->BoundingRect.Width != -1)
{ {
if (!spriteArr) if (!spriteArr)
spriteArr = reinterpret_cast<render_sprite_type_struct**>(memory::allocate(1000 * sizeof(void*))); spriteArr = memory::allocate<render_sprite_type_struct*>(1000);
int occludeCount = 0; int occludeCount = 0;
auto spritePtr2 = sprite_list; auto spritePtr2 = sprite_list;
for (int i = 0; i < many_sprites; ++i, ++spritePtr2) for (int i = 0; i < many_sprites; ++i, ++spritePtr2)
@ -560,8 +556,7 @@ void render::build_occlude_list()
occludeCount = 0; occludeCount = 0;
if (occludeCount) if (occludeCount)
{ {
curSprite->SpriteArray = reinterpret_cast<render_sprite_type_struct**>(memory::realloc( curSprite->SpriteArray = memory::realloc(spriteArr, sizeof(void*) * occludeCount);
spriteArr, sizeof(void*) * occludeCount));
curSprite->SpriteCount = occludeCount; curSprite->SpriteCount = occludeCount;
spriteArr = nullptr; spriteArr = nullptr;
} }

View File

@ -16,7 +16,7 @@ int score::init()
scoreStruct* score::create(LPCSTR fieldName, gdrv_bitmap8* renderBgBmp) scoreStruct* score::create(LPCSTR fieldName, gdrv_bitmap8* renderBgBmp)
{ {
auto score = reinterpret_cast<scoreStruct*>(memory::allocate(sizeof(scoreStruct))); auto score = memory::allocate<scoreStruct>();
if (!score) if (!score)
return nullptr; return nullptr;
score->Score = -9999; score->Score = -9999;
@ -45,7 +45,7 @@ scoreStruct* score::create(LPCSTR fieldName, gdrv_bitmap8* renderBgBmp)
scoreStruct* score::dup(scoreStruct* score, int scoreIndex) scoreStruct* score::dup(scoreStruct* score, int scoreIndex)
{ {
auto result = reinterpret_cast<scoreStruct*>(memory::allocate(sizeof(scoreStruct))); auto result = memory::allocate<scoreStruct>();
if (result) if (result)
memcpy(result, score, sizeof(scoreStruct)); memcpy(result, score, sizeof(scoreStruct));
return result; return result;
@ -63,7 +63,7 @@ void score::load_msg_font(LPCSTR lpName)
auto rcData = static_cast<__int16*>(LockResource(resGlobal)); auto rcData = static_cast<__int16*>(LockResource(resGlobal));
auto fontp = reinterpret_cast<score_msg_font_type*>(memory::allocate(sizeof(score_msg_font_type))); auto fontp = memory::allocate<score_msg_font_type>();
msg_fontp = fontp; msg_fontp = fontp;
if (!fontp) if (!fontp)
{ {
@ -102,7 +102,7 @@ void score::load_msg_font(LPCSTR lpName)
if (!width) if (!width)
continue; continue;
auto bmp = reinterpret_cast<gdrv_bitmap8*>(memory::allocate(sizeof(gdrv_bitmap8))); auto bmp = memory::allocate<gdrv_bitmap8>();
msg_fontp->Chars[charInd] = bmp; msg_fontp->Chars[charInd] = bmp;
if (!bmp) if (!bmp)
{ {
@ -130,6 +130,7 @@ void score::load_msg_font(LPCSTR lpName)
} }
} }
memory::free(tmpCharBur);
if (charInd != 128) if (charInd != 128)
unload_msg_font(); unload_msg_font();
FreeResource(resGlobal); FreeResource(resGlobal);
@ -147,6 +148,7 @@ void score::unload_msg_font()
memory::free(msg_fontp->Chars[i]); memory::free(msg_fontp->Chars[i]);
} }
} }
memory::free(msg_fontp);
msg_fontp = nullptr; msg_fontp = nullptr;
} }
} }
@ -191,7 +193,7 @@ void score::set(scoreStruct* score, int value)
void score::update(scoreStruct* score) void score::update(scoreStruct* score)
{ {
char scoreBuf[12]; char scoreBuf[12]{};
if (score && score->DirtyFlag && score->Score <= 1000000000) if (score && score->DirtyFlag && score->Score <= 1000000000)
{ {
score->DirtyFlag = false; score->DirtyFlag = false;

View File

@ -6,13 +6,14 @@
#include "pinball.h" #include "pinball.h"
HINSTANCE splash::HInstance; HINSTANCE splash::HInstance;
HGDIOBJ splash::OriginalDcBitmap = nullptr;
splash_struct* splash::splash_screen(HINSTANCE hInstance, LPCSTR bmpName1, LPCSTR bmpName2) splash_struct* splash::splash_screen(HINSTANCE hInstance, LPCSTR bmpName1, LPCSTR bmpName2)
{ {
WNDCLASSA WndClass{}; WNDCLASSA WndClass{};
tagRECT Rect{}; tagRECT Rect{};
auto splashStruct = reinterpret_cast<splash_struct*>(memory::allocate(sizeof(splash_struct))); auto splashStruct = memory::allocate<splash_struct>();
if (!splashStruct) if (!splashStruct)
return nullptr; return nullptr;
@ -75,7 +76,7 @@ void splash::splash_bitmap_setup(splash_struct* splashStruct)
{ {
if (splashStruct->DrawingContext) if (splashStruct->DrawingContext)
{ {
SelectObject(splashStruct->DrawingContext, bmpHandle1); OriginalDcBitmap = SelectObject(splashStruct->DrawingContext, bmpHandle1);
if ((GetDeviceCaps(splashStruct->DrawingContext, RASTERCAPS) & RC_PALETTE) != 0 if ((GetDeviceCaps(splashStruct->DrawingContext, RASTERCAPS) & RC_PALETTE) != 0
|| GetDeviceCaps(splashStruct->DrawingContext, NUMCOLORS) >= 256) || GetDeviceCaps(splashStruct->DrawingContext, NUMCOLORS) >= 256)
{ {
@ -85,6 +86,7 @@ void splash::splash_bitmap_setup(splash_struct* splashStruct)
else else
{ {
bmpHandle2 = LoadBitmapA(HInstance, splashStruct->BmpName2); bmpHandle2 = LoadBitmapA(HInstance, splashStruct->BmpName2);
splashStruct->Palette = nullptr;
} }
splashStruct->Bitmap = bmpHandle2; splashStruct->Bitmap = bmpHandle2;
@ -162,11 +164,11 @@ HBITMAP splash::load_title_bitmap(HMODULE hModule, HDC hdc, LPCSTR lpName, UINT
return resBmp; return resBmp;
} }
HPALETTE splash::splash_init_palette(LOGPALETTEx256* plpal) HPALETTE splash::splash_init_palette(LOGPALETTE* plpal)
{ {
plpal->palVersion = 768; plpal->palVersion = 768;
plpal->palNumEntries = 256; plpal->palNumEntries = 256;
auto hPalette = CreatePalette(reinterpret_cast<const LOGPALETTE*>(plpal)); auto hPalette = CreatePalette(static_cast<const LOGPALETTE*>(plpal));
auto dc = GetDC(GetDesktopWindow()); auto dc = GetDC(GetDesktopWindow());
GetDeviceCaps(dc, RASTERCAPS); GetDeviceCaps(dc, RASTERCAPS);
if (GetDeviceCaps(dc, SIZEPALETTE) != 256) if (GetDeviceCaps(dc, SIZEPALETTE) != 256)
@ -243,7 +245,11 @@ void splash::splash_destroy(splash_struct* splashStruct)
splashStruct->Palette = nullptr; splashStruct->Palette = nullptr;
if (splashStruct->DrawingContext) if (splashStruct->DrawingContext)
{
if (OriginalDcBitmap)
SelectObject(splashStruct->DrawingContext, OriginalDcBitmap);
DeleteDC(splashStruct->DrawingContext); DeleteDC(splashStruct->DrawingContext);
}
if (splashStruct->Bitmap) if (splashStruct->Bitmap)
DeleteObject(splashStruct->Bitmap); DeleteObject(splashStruct->Bitmap);
} }

View File

@ -22,12 +22,14 @@ class splash
public: public:
static splash_struct* splash_screen(HINSTANCE hInstance, LPCSTR bmpName1, LPCSTR bmpName2); static splash_struct* splash_screen(HINSTANCE hInstance, LPCSTR bmpName1, LPCSTR bmpName2);
static void splash_bitmap_setup(splash_struct* splashStruct); static void splash_bitmap_setup(splash_struct* splashStruct);
static HBITMAP load_title_bitmap(HMODULE hModule, HDC hdc, LPCSTR lpName, UINT iStart, int iEnd, HPALETTE* palettePtr); static HBITMAP load_title_bitmap(HMODULE hModule, HDC hdc, LPCSTR lpName, UINT iStart, int iEnd,
static HPALETTE splash_init_palette(LOGPALETTEx256* plpal); HPALETTE* palettePtr);
static HPALETTE splash_init_palette(LOGPALETTE* plpal);
static void splash_paint(splash_struct* splashStruct, HDC dc); static void splash_paint(splash_struct* splashStruct, HDC dc);
static void splash_destroy(splash_struct* splashStruct); static void splash_destroy(splash_struct* splashStruct);
static void splash_hide(splash_struct* splashStruct); static void splash_hide(splash_struct* splashStruct);
static LRESULT __stdcall splash_message_handler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); static LRESULT __stdcall splash_message_handler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
private: private:
static HINSTANCE HInstance; static HINSTANCE HInstance;
static HGDIOBJ OriginalDcBitmap;
}; };

View File

@ -13,7 +13,7 @@ timer_struct* timer::TimerBuffer;
int timer::init(int count) int timer::init(int count)
{ {
auto buf = (timer_struct*)memory::allocate(sizeof(timer_struct) * count); auto buf = memory::allocate<timer_struct>(count);
TimerBuffer = buf; TimerBuffer = buf;
if (!buf) if (!buf)
return 1; return 1;

View File

@ -8,7 +8,7 @@ int zdrv::create_zmap(zmap_header_type* zmap, int width, int height)
{ {
int stride = pad(width); int stride = pad(width);
zmap->Stride = stride; zmap->Stride = stride;
auto bmpBuf = (unsigned short*)memory::allocate(2 * height * stride); auto bmpBuf = memory::allocate<unsigned short>(height * stride);
zmap->ZPtr1 = bmpBuf; zmap->ZPtr1 = bmpBuf;
if (!bmpBuf) if (!bmpBuf)
return -1; return -1;
@ -36,22 +36,16 @@ int zdrv::destroy_zmap(zmap_header_type* zmap)
return 0; return 0;
} }
void zdrv::fill(zmap_header_type* zmap, int width, int height, int xOff, int yOff, unsigned __int16 fillChar) void zdrv::fill(zmap_header_type* zmap, int width, int height, int xOff, int yOff, unsigned __int16 fillWord)
{ {
int fillCharInt = fillChar | (fillChar << 16); auto dstPtr = &zmap->ZPtr1[zmap->Stride * (zmap->Height - height - yOff) + xOff];
auto zmapPtr = &zmap->ZPtr1[xOff + zmap->Stride * (zmap->Height - height - yOff)]; for (int y = height; y > 0; --y)
for (int y = height; width > 0 && y > 0; y--)
{ {
char widthMod2 = width & 1; for (int x = width; x > 0; --x)
unsigned int widthDiv2 = static_cast<unsigned int>(width) >> 1; {
memset32(zmapPtr, fillCharInt, widthDiv2); *dstPtr++ = fillWord;
}
auto lastShort = &zmapPtr[2 * widthDiv2]; dstPtr += zmap->Stride - width;
for (int i = widthMod2; i; --i)
*lastShort++ = fillChar;
zmapPtr += zmap->Stride;
} }
} }

View File

@ -3,9 +3,9 @@
struct zmap_header_type struct zmap_header_type
{ {
__int16 Width; int Width;
__int16 Height; int Height;
__int16 Stride; int Stride;
unsigned __int16* ZPtr1; unsigned __int16* ZPtr1;
unsigned __int16* ZPtr2; unsigned __int16* ZPtr2;
unsigned __int16 ZBuffer[1]; unsigned __int16 ZBuffer[1];
@ -17,7 +17,7 @@ public:
static int pad(int width); static int pad(int width);
static int create_zmap(zmap_header_type* zmap, int width, int height); static int create_zmap(zmap_header_type* zmap, int width, int height);
static int destroy_zmap(zmap_header_type* zmap); static int destroy_zmap(zmap_header_type* zmap);
static void fill(zmap_header_type* zmap, int width, int height, int xOff, int yOff, unsigned __int16 fillChar); static void fill(zmap_header_type* zmap, int width, int height, int xOff, int yOff, unsigned __int16 fillWord);
static void paint(int width, int height, gdrv_bitmap8* dstBmp, int dstBmpXOff, int dstBmpYOff, static void paint(int width, int height, gdrv_bitmap8* dstBmp, int dstBmpXOff, int dstBmpYOff,
zmap_header_type* dstZMap, int dstZMapXOff, int dstZMapYOff, gdrv_bitmap8* srcBmp, int srcBmpXOff, zmap_header_type* dstZMap, int dstZMapXOff, int dstZMapYOff, gdrv_bitmap8* srcBmp, int srcBmpXOff,
int srcBmpYOff, zmap_header_type* srcZMap, int srcZMapXOff, int srcZMapYOff); int srcBmpYOff, zmap_header_type* srcZMap, int srcZMapXOff, int srcZMapYOff);