From d594f5fdb7a5927c2d82654e614a760dce7a3b77 Mon Sep 17 00:00:00 2001
From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com>
Date: Tue, 2 Feb 2021 18:29:54 +0300
Subject: [PATCH] Converted memory to direct pointers. Fixed memory leaks in
uninit. Fixed some of the code analysis warnings. Enabled /MP build. Cleaned
up the code.
---
CompileForDrMemory.bat | 2 +-
SpaceCadetPinball/SpaceCadetPinball.vcxproj | 4 +
SpaceCadetPinball/TFlagSpinner.cpp | 2 +-
SpaceCadetPinball/TFlipperEdge.cpp | 10 +-
SpaceCadetPinball/TKickout.cpp | 2 +-
SpaceCadetPinball/TLightBargraph.cpp | 2 +-
SpaceCadetPinball/TPinballTable.cpp | 2 +-
SpaceCadetPinball/control.cpp | 12 +-
SpaceCadetPinball/control.h | 2 +-
SpaceCadetPinball/gdrv.cpp | 91 ++++------
SpaceCadetPinball/gdrv.h | 11 +-
SpaceCadetPinball/high_score.cpp | 29 ++-
SpaceCadetPinball/high_score.h | 2 +-
SpaceCadetPinball/loader.cpp | 27 ++-
SpaceCadetPinball/memory.cpp | 56 ++----
SpaceCadetPinball/memory.h | 62 +++++--
SpaceCadetPinball/objlist_class.h | 4 +-
SpaceCadetPinball/partman.cpp | 188 ++++++--------------
SpaceCadetPinball/pb.cpp | 2 +-
SpaceCadetPinball/render.cpp | 29 ++-
SpaceCadetPinball/score.cpp | 12 +-
SpaceCadetPinball/splash.cpp | 14 +-
SpaceCadetPinball/splash.h | 6 +-
SpaceCadetPinball/timer.cpp | 2 +-
SpaceCadetPinball/zdrv.cpp | 24 +--
SpaceCadetPinball/zdrv.h | 8 +-
26 files changed, 264 insertions(+), 341 deletions(-)
diff --git a/CompileForDrMemory.bat b/CompileForDrMemory.bat
index bcbdb8c..421476e 100644
--- a/CompileForDrMemory.bat
+++ b/CompileForDrMemory.bat
@@ -1,3 +1,3 @@
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"
\ No newline at end of file
+ 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"
\ No newline at end of file
diff --git a/SpaceCadetPinball/SpaceCadetPinball.vcxproj b/SpaceCadetPinball/SpaceCadetPinball.vcxproj
index 647c3a0..cf961cf 100644
--- a/SpaceCadetPinball/SpaceCadetPinball.vcxproj
+++ b/SpaceCadetPinball/SpaceCadetPinball.vcxproj
@@ -94,6 +94,7 @@
pch.h
true
false
+ true
Console
@@ -110,6 +111,7 @@
_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
true
pch.h
+ true
Console
@@ -128,6 +130,7 @@
WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
true
pch.h
+ true
Windows
@@ -148,6 +151,7 @@
NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
true
pch.h
+ true
Windows
diff --git a/SpaceCadetPinball/TFlagSpinner.cpp b/SpaceCadetPinball/TFlagSpinner.cpp
index 1ec42d8..c075a8a 100644
--- a/SpaceCadetPinball/TFlagSpinner.cpp
+++ b/SpaceCadetPinball/TFlagSpinner.cpp
@@ -18,7 +18,7 @@ TFlagSpinner::TFlagSpinner(TPinballTable* table, int groupIndex) : TCollisionCom
Timer = 0;
loader::query_visual(groupIndex, 0, &visual);
- end.X = *visual.FloatArr;
+ end.X = visual.FloatArr[0];
end.Y = visual.FloatArr[1];
start.X = visual.FloatArr[2];
start.Y = visual.FloatArr[3];
diff --git a/SpaceCadetPinball/TFlipperEdge.cpp b/SpaceCadetPinball/TFlipperEdge.cpp
index cfe87c6..5a71a20 100644
--- a/SpaceCadetPinball/TFlipperEdge.cpp
+++ b/SpaceCadetPinball/TFlipperEdge.cpp
@@ -439,10 +439,10 @@ int TFlipperEdge::is_ball_inside(float x, float y)
vector_type testPoint{};
float dx = RotOrigin.X - x;
float dy = RotOrigin.Y - y;
- if ((A2.X - A1.X) * (y - A1.Y) - (A2.Y - A1.Y) * (x - A1.X) >= 0.0 &&
- (B1.X - A2.X) * (y - A2.Y) - (B1.Y - A2.Y) * (x - A2.X) >= 0.0 &&
- (B2.X - B1.X) * (y - B1.Y) - (B2.Y - B1.Y) * (x - B1.X) >= 0.0 &&
- (A1.X - B2.X) * (y - B2.Y) - (A1.Y - B2.Y) * (x - B2.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.0f &&
+ (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.0f ||
dy * dy + dx * dx <= CirclebaseRadiusSq ||
(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;
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 5;
}
diff --git a/SpaceCadetPinball/TKickout.cpp b/SpaceCadetPinball/TKickout.cpp
index 26c7d83..01ebbdc 100644
--- a/SpaceCadetPinball/TKickout.cpp
+++ b/SpaceCadetPinball/TKickout.cpp
@@ -30,7 +30,7 @@ TKickout::TKickout(TPinballTable* table, int groupIndex, bool someFlag): TCollis
SoftHitSoundId = visual.SoftHitSoundId;
HardHitSoundId = visual.Kicker.HardHitSoundId;
- Circle.Center.X = *visual.FloatArr;
+ Circle.Center.X = visual.FloatArr[0];
Circle.Center.Y = visual.FloatArr[1];
Circle.RadiusSq = *loader::query_float_attribute(groupIndex, 0, 306) * visual.FloatArr[2];
if (Circle.RadiusSq == 0.0)
diff --git a/SpaceCadetPinball/TLightBargraph.cpp b/SpaceCadetPinball/TLightBargraph.cpp
index 0ad1a43..f2d3699 100644
--- a/SpaceCadetPinball/TLightBargraph.cpp
+++ b/SpaceCadetPinball/TLightBargraph.cpp
@@ -19,7 +19,7 @@ TLightBargraph::TLightBargraph(TPinballTable* table, int groupIndex) : TLightGro
if (floatArr)
{
int count = 2 * List->GetCount();
- TimerTimeArray = reinterpret_cast(memory::allocate(count * sizeof(float)));
+ TimerTimeArray = memory::allocate(count);
if (TimerTimeArray)
{
for (int i = 0; i < count; ++floatArr)
diff --git a/SpaceCadetPinball/TPinballTable.cpp b/SpaceCadetPinball/TPinballTable.cpp
index 3fa290a..7c3d660 100644
--- a/SpaceCadetPinball/TPinballTable.cpp
+++ b/SpaceCadetPinball/TPinballTable.cpp
@@ -242,7 +242,7 @@ TPinballComponent* TPinballTable::find_component(LPCSTR componentName)
TPinballComponent* TPinballTable::find_component(int groupIndex)
{
- char Buffer[40];
+ char Buffer[40]{};
int objCount = ComponentList->GetCount();
if (objCount > 0)
{
diff --git a/SpaceCadetPinball/control.cpp b/SpaceCadetPinball/control.cpp
index d44cbe1..4484e98 100644
--- a/SpaceCadetPinball/control.cpp
+++ b/SpaceCadetPinball/control.cpp
@@ -616,17 +616,15 @@ TPinballComponent* control::make_component_link(component_tag_base* tag)
void control::handler(int code, TPinballComponent* cmp)
{
component_control* control = cmp->Control;
- int scoreInd = 0;
+
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]);
- ++scoreInd;
}
- while (scoreInd < control->ScoreCount);
}
control->ControlFunc(code, cmp);
}
@@ -3019,7 +3017,7 @@ void control::GameoverController(int code, TPinballComponent* caller)
int missionMsg = control_mission_text_box_tag.Component->MessageField;
if (missionMsg & 0x100)
{
- int playerId = missionMsg & 0xF;
+ int playerId = missionMsg % 4;
int playerScore = TableG->PlayerScores[playerId].ScoreStruct->Score;
auto nextPlayerId = playerId + 1;
if (playerScore >= 0)
@@ -3056,7 +3054,7 @@ void control::GameoverController(int code, TPinballComponent* caller)
if (missionMsg & 0x200)
{
- int highscoreId = missionMsg & 0xF;
+ int highscoreId = missionMsg % 4;
int highScore = pb::highscore_table[highscoreId].Score;
auto nextHidhscoreId = highscoreId + 1;
if (highScore > 0)
diff --git a/SpaceCadetPinball/control.h b/SpaceCadetPinball/control.h
index e00e237..138ed95 100644
--- a/SpaceCadetPinball/control.h
+++ b/SpaceCadetPinball/control.h
@@ -26,7 +26,7 @@ struct component_tag : component_tag_base
static_assert(std::is_base_of::value, "T must inherit from TPinballComponent");
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);
}
diff --git a/SpaceCadetPinball/gdrv.cpp b/SpaceCadetPinball/gdrv.cpp
index 47bc550..c4f9196 100644
--- a/SpaceCadetPinball/gdrv.cpp
+++ b/SpaceCadetPinball/gdrv.cpp
@@ -7,7 +7,6 @@
HPALETTE gdrv::palette_handle = nullptr;
HINSTANCE gdrv::hinst;
HWND gdrv::hwnd;
-LOGPALETTEx256 gdrv::current_palette{};
int gdrv::sequence_handle;
HDC gdrv::sequence_hdc;
int gdrv::use_wing = 0;
@@ -18,10 +17,12 @@ int gdrv::grtext_red = -1;
int gdrv::init(HINSTANCE hInst, HWND hWnd)
{
+ LOGPALETTEx256 current_palette{};
+
hinst = hInst;
hwnd = hWnd;
if (!palette_handle)
- palette_handle = CreatePalette((LOGPALETTE*)¤t_palette);
+ palette_handle = CreatePalette(¤t_palette);
return 0;
}
@@ -39,15 +40,15 @@ void gdrv::get_focus()
BITMAPINFO* gdrv::DibCreate(__int16 bpp, int width, int height)
{
- auto sizeBytes = height * ((width * bpp / 8 + 3) & 0xFFFFFFFC);
- auto buf = GlobalAlloc(0x42u, sizeBytes + 1064);
+ auto sizeBytes = height * (width * bpp / 8 + 3 & 0xFFFFFFFC);
+ auto buf = GlobalAlloc(GHND, sizeBytes + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
auto dib = static_cast(GlobalLock(buf));
if (!dib)
return nullptr;
dib->bmiHeader.biSizeImage = sizeBytes;
dib->bmiHeader.biWidth = width;
- dib->bmiHeader.biSize = 40;
+ dib->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
dib->bmiHeader.biHeight = height;
dib->bmiHeader.biPlanes = 1;
dib->bmiHeader.biBitCount = bpp;
@@ -65,25 +66,15 @@ BITMAPINFO* gdrv::DibCreate(__int16 bpp, int width, int height)
dib->bmiHeader.biClrUsed = 256;
}
- int index = 0;
- for (auto i = (int*)dib->bmiColors; index < static_cast(dib->bmiHeader.biClrUsed) / 16; ++index)
+
+ uint32_t paletteColors[]
{
- *i++ = 0;
- *i++ = 0x800000;
- *i++ = 0x8000;
- *i++ = 8421376;
- *i++ = 128;
- *i++ = 8388736;
- *i++ = 32896;
- *i++ = 12632256;
- *i++ = 8421504;
- *i++ = 16711680;
- *i++ = 65280;
- *i++ = 16776960;
- *i++ = 255;
- *i++ = 16711935;
- *i++ = 0xFFFF;
- *i++ = 0xFFFFFF;
+ 0, 0x800000, 0x8000, 8421376, 128, 8388736, 32896, 12632256,
+ 8421504, 16711680, 65280, 16776960, 255, 16711935, 0xFFFF, 0xFFFFFF,
+ };
+ for (auto index = 0u; index < dib->bmiHeader.biClrUsed; index += 16)
+ {
+ memcpy(&dib->bmiColors[index], paletteColors, sizeof paletteColors);
}
return dib;
}
@@ -108,31 +99,24 @@ void gdrv::DibSetUsage(BITMAPINFO* dib, HPALETTE hpal, int someFlag)
{
if (someFlag && someFlag <= 2)
{
- auto pltPtr = (short*)((char*)dib + dib->bmiHeader.biSize);
- for (int i = 0; i < numOfColors; ++i)
+ auto pltPtr = reinterpret_cast(dib->bmiColors);
+ for (auto i = 0; i < numOfColors; ++i)
{
*pltPtr++ = i;
}
}
else
{
- assertm(false, "Entered bad code");
- char* dibPtr = (char*)dib + dib->bmiHeader.biSize;
if (numOfColors >= 256)
numOfColors = 256;
GetPaletteEntries(hpal, 0, numOfColors, pPalEntries);
- int index = 0;
- char* dibPtr2 = dibPtr + 1;
- do
+ for (auto index = 0; index < numOfColors; index++)
{
- char v9 = pPalEntries[index++].peRed;
- dibPtr2[1] = v9;
- *dibPtr2 = dibPtr2[(char*)pPalEntries - dibPtr];
- *(dibPtr2 - 1) = dibPtr2[&pPalEntries[0].peGreen - (unsigned char*)dibPtr];
- dibPtr2[2] = 0;
- dibPtr2 += 4;
+ dib->bmiColors[index].rgbRed = pPalEntries[index].peRed;
+ dib->bmiColors[index].rgbGreen = pPalEntries[index].peGreen;
+ dib->bmiColors[index].rgbBlue = pPalEntries[index].peBlue;
+ dib->bmiColors[index].rgbReserved = 0;
}
- while (index < numOfColors);
}
}
}
@@ -156,7 +140,7 @@ int gdrv::create_bitmap_dib(gdrv_bitmap8* bmp, int width, int height)
if (dib->bmiHeader.biCompression == 3)
bmpBufPtr = (char*)&dib->bmiHeader.biPlanes + dib->bmiHeader.biSize;
else
- bmpBufPtr = (char*)&dib->bmiHeader.biSize + 4 * dib->bmiHeader.biClrUsed + dib->bmiHeader.biSize;
+ bmpBufPtr = reinterpret_cast(&dib->bmiColors[dib->bmiHeader.biClrUsed]);
bmp->BmpBufPtr1 = bmpBufPtr;
bmp->BmpBufPtr2 = bmpBufPtr;
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)
{
+ LOGPALETTEx256 current_palette{};
+
if (palette_handle)
DeleteObject(palette_handle);
- palette_handle = CreatePalette((LOGPALETTE*)¤t_palette);
+ palette_handle = CreatePalette(¤t_palette);
auto windowHandle = GetDesktopWindow();
auto dc = winmain::_GetDC(windowHandle);
SetSystemPaletteUse(dc, 2u);
SetSystemPaletteUse(dc, 1u);
- auto pltHandle = SelectPalette(dc, palette_handle, 0);
+ auto originalPalette = SelectPalette(dc, palette_handle, 0);
RealizePalette(dc);
- SelectPalette(dc, pltHandle, 0);
- GetSystemPaletteEntries(dc, 0, 0x100u, current_palette.palPalEntry);
+ SelectPalette(dc, originalPalette, 0);
+ GetSystemPaletteEntries(dc, 0, 256, current_palette.palPalEntry);
for (int i = 0; i < 256; i++)
{
current_palette.palPalEntry[i].peFlags = 0;
@@ -234,15 +220,15 @@ int gdrv::display_palette(PALETTEENTRY* plt)
pltDst++;
}
- if (!(GetDeviceCaps(dc, 38) & 0x100))
+ if (!(GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE))
{
current_palette.palPalEntry[255].peBlue = -1;
current_palette.palPalEntry[255].peGreen = -1;
current_palette.palPalEntry[255].peRed = -1;
}
- ResizePalette(palette_handle, 0x100u);
- SetPaletteEntries(palette_handle, 0, 0x100u, current_palette.palPalEntry);
+ ResizePalette(palette_handle, 256);
+ SetPaletteEntries(palette_handle, 0, 256, current_palette.palPalEntry);
windowHandle = GetDesktopWindow();
ReleaseDC(windowHandle, dc);
return 0;
@@ -355,16 +341,11 @@ void gdrv::fill_bitmap(gdrv_bitmap8* bmp, int width, int height, int xOff, int y
if (bmpHeight < 0)
bmpHeight = -bmpHeight;
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);
- bmpPtr += bmp->Stride;
- --height;
- }
- while (height);
+ if (width > 0)
+ memset(bmpPtr, fillChar, width);
+ bmpPtr += bmp->Stride;
}
}
@@ -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);
}
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);
SetBkMode(dc, prevMode);
SetTextColor(dc, color);
diff --git a/SpaceCadetPinball/gdrv.h b/SpaceCadetPinball/gdrv.h
index 16befb5..8cf5416 100644
--- a/SpaceCadetPinball/gdrv.h
+++ b/SpaceCadetPinball/gdrv.h
@@ -22,14 +22,14 @@ struct gdrv_bitmap8
int YPosition;
};
-struct LOGPALETTEx256
+struct LOGPALETTEx256 : LOGPALETTE
{
- WORD palVersion;
- WORD palNumEntries;
- PALETTEENTRY palPalEntry[256];
+ PALETTEENTRY palPalEntry2[256 - 1];
- LOGPALETTEx256() : palVersion(0x300), palNumEntries(256), palPalEntry{}
+ LOGPALETTEx256() : palPalEntry2{}
{
+ palVersion = 0x300;
+ palNumEntries = 256;
}
};
@@ -38,7 +38,6 @@ class gdrv
{
public:
static HPALETTE palette_handle;
- static LOGPALETTEx256 current_palette;
static int sequence_handle;
static HDC sequence_hdc;
static int use_wing;
diff --git a/SpaceCadetPinball/high_score.cpp b/SpaceCadetPinball/high_score.cpp
index e30c5ab..2ad5e29 100644
--- a/SpaceCadetPinball/high_score.cpp
+++ b/SpaceCadetPinball/high_score.cpp
@@ -9,7 +9,7 @@
int high_score::dlg_enter_name;
int high_score::dlg_score;
-int high_score::position;
+int high_score::dlg_position;
LPCSTR high_score::default_name;
high_score_struct* high_score::dlg_hst;
@@ -48,11 +48,10 @@ int high_score::read(high_score_struct* table, int* ptrToSmth)
if (!buf1)
return 1;
char* buf2 = memory::allocate(300u);
- int position = 0;
- high_score_struct* tablePtr = table;
- const CHAR* optPath = pinball::get_rc_string(166, 0);
- do
+ auto optPath = pinball::get_rc_string(166, 0);
+ for (auto position = 0; position < 5; ++position)
{
+ auto tablePtr = &table[position];
_itoa_s(position, Buffer, 10);
lstrcatA(Buffer, ".Name");
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;
- ++position;
- ++tablePtr;
}
- while (position < 5);
+
scramble_number_string(scoreSum, buf1);
options::get_string(optPath, "Verification", buf2, pinball::WindowName, 300);
if (lstrcmpA(buf1, buf2))
@@ -88,9 +85,8 @@ int high_score::write(high_score_struct* table, int* ptrToSmth)
CHAR* buf = memory::allocate(300u);
if (!buf)
return 1;
- int position = 0;
const CHAR* optPath = pinball::get_rc_string(166, 0);
- do
+ for (auto position = 0; position < 5; ++position)
{
_itoa_s(position, Buffer, 10);
lstrcatA(Buffer, ".Name");
@@ -106,7 +102,6 @@ int high_score::write(high_score_struct* table, int* ptrToSmth)
++position;
++tablePtr;
}
- while (position < 5);
scramble_number_string(scoreSum, buf);
options::set_string(optPath, "Verification", 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)
{
- position = pos;
+ dlg_position = pos;
dlg_score = score;
dlg_hst = table;
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 (position == -1)
+ if (dlg_position == -1)
{
dlg_enter_name = 0;
return 1;
}
- HWND nameTextBox = GetDlgItem(hWnd, position + DLG_HIGHSCORES_EditName1);
+ HWND nameTextBox = GetDlgItem(hWnd, dlg_position + DLG_HIGHSCORES_EditName1);
ShowWindow(nameTextBox, 5);
EnableWindow(nameTextBox, 1);
SetFocus(nameTextBox);
@@ -246,9 +241,9 @@ INT_PTR high_score::HighScore(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
break;
}
- GetDlgItemTextA(hWnd, position + DLG_HIGHSCORES_EditName1, name, 32);
+ GetDlgItemTextA(hWnd, dlg_position + DLG_HIGHSCORES_EditName1, name, 32);
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;
case DLG_HIGHSCORES_Cancel:
break;
@@ -281,7 +276,7 @@ void high_score::show_high_scores(HWND hDlg, high_score_struct* table)
int nextPosition = 0;
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);
nextPosition = 1;
diff --git a/SpaceCadetPinball/high_score.h b/SpaceCadetPinball/high_score.h
index a136fd6..842226e 100644
--- a/SpaceCadetPinball/high_score.h
+++ b/SpaceCadetPinball/high_score.h
@@ -26,7 +26,7 @@ public:
private :
static int dlg_enter_name;
static int dlg_score;
- static int position;
+ static int dlg_position;
static LPCSTR default_name;
static high_score_struct* dlg_hst;
static winhelp_entry help[21];
diff --git a/SpaceCadetPinball/loader.cpp b/SpaceCadetPinball/loader.cpp
index 0debf70..5e8dcac 100644
--- a/SpaceCadetPinball/loader.cpp
+++ b/SpaceCadetPinball/loader.cpp
@@ -47,20 +47,19 @@ soundListStruct loader::sound_list[65];
int loader::error(int errorCode, int captionCode)
{
- int curCode = loader_errors[0].Code;
+ auto curCode = loader_errors;
const char *errorText = nullptr, *errorCaption = nullptr;
- int index = 0, index2 = 0;
- if (loader_errors[0].Code >= 0)
- do
- {
- if (errorCode == curCode)
- errorText = loader_errors[index2].Message;
- if (captionCode == curCode)
- errorCaption = loader_errors[index2].Message;
- index2 = ++index;
- curCode = loader_errors[index].Code;
- }
- while (curCode >= 0);
+ auto index = 0;
+ while (curCode->Code >= 0)
+ {
+ if (errorCode == curCode->Code)
+ errorText = curCode->Message;
+ if (captionCode == curCode->Code)
+ errorCaption = curCode->Message;
+ curCode++;
+ index++;
+ }
+
if (!errorText)
errorText = loader_errors[index].Message;
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)
{
auto floatArr = reinterpret_cast(partman::field_nth(loader_table, stateId,
- datFieldTypes::FloatArray,skipIndex));
+ datFieldTypes::FloatArray, skipIndex));
if (!floatArr)
break;
if (static_cast<__int16>(floor(*floatArr)) == firstValue)
diff --git a/SpaceCadetPinball/memory.cpp b/SpaceCadetPinball/memory.cpp
index 3d7838a..2963b9c 100644
--- a/SpaceCadetPinball/memory.cpp
+++ b/SpaceCadetPinball/memory.cpp
@@ -1,58 +1,40 @@
#include "pch.h"
#include "memory.h"
-unsigned int memory::use_total;
+size_t memory::use_total;
int memory::critical_allocation;
void (*memory::critical_callback)();
+std::map memory::alloc_map{};
void memory::init(void (*callback)())
{
critical_callback = callback;
}
-char* memory::allocate(unsigned int size)
+char* memory::allocate(size_t size)
{
- char* buf = static_cast(malloc(size + 4));
- if (buf)
+ auto buf = static_cast(malloc(size));
+ if (!buf)
{
- *(unsigned int*)buf = size << 8;
- use_total += size + 4;
- *buf = size >= 0xFFDC ? -91 : 90;
- return buf + 4;
+ if (critical_allocation && critical_callback)
+ critical_callback();
+ return nullptr;
}
- if (critical_allocation && critical_callback)
- critical_callback();
- return nullptr;
+
+ use_total += size;
+ alloc_map[buf] = size;
+ return buf;
}
void memory::free(void* buf)
{
- unsigned int* bufStart = static_cast(buf) - 1;
- use_total -= (*bufStart >> 8) + 4;
- 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(buf) - 4;
- use_total -= *(unsigned int*)bufStart >> 8;
- if (*bufStart != 90 && *bufStart != -91 ||
- (bufStart = static_cast(std::realloc(bufStart, size + 4))) != nullptr)
+ auto alloc = alloc_map.find(buf);
+ if (alloc == alloc_map.end())
{
- char bufType = *bufStart;
- *(unsigned int*)bufStart = size << 8;
- use_total += size;
- *bufStart = bufType;
- return bufStart + 4;
+ assertm(false, "Unknown memory type");
+ return;
}
- if (critical_allocation && critical_callback)
- critical_callback();
- return nullptr;
+
+ use_total -= alloc->second;
+ std::free(alloc->first);
}
diff --git a/SpaceCadetPinball/memory.h b/SpaceCadetPinball/memory.h
index e42d53c..5c63ad5 100644
--- a/SpaceCadetPinball/memory.h
+++ b/SpaceCadetPinball/memory.h
@@ -1,22 +1,60 @@
#pragma once
+#include