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 + class memory { public: static void init(void (*callback)(void)); - static char* allocate(unsigned int size); + static char* allocate(size_t size); static void free(void* buf); - static char* realloc(void* buf, unsigned int size); - static unsigned int use_total; + template + static T* allocate(size_t count = 1, size_t add = 0) + { + size_t size = sizeof(T) * count + add; + auto buf = static_cast(malloc(size)); + if (!buf) + { + if (critical_allocation && critical_callback) + critical_callback(); + return nullptr; + } + + use_total += size; + alloc_map[buf] = size; + return buf; + } + + template + static T* realloc(T* buf, size_t size) + { + if (!buf) + return reinterpret_cast(allocate(size)); + + auto alloc = alloc_map.find(buf); + if (alloc == alloc_map.end()) + { + assertm(false, "Unknown memory type"); + return buf; + } + + auto newBuf = static_cast(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; +private: static void (*critical_callback)(); + static std::map 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; -} \ No newline at end of file diff --git a/SpaceCadetPinball/objlist_class.h b/SpaceCadetPinball/objlist_class.h index 7b1cafb..b88abba 100644 --- a/SpaceCadetPinball/objlist_class.h +++ b/SpaceCadetPinball/objlist_class.h @@ -8,7 +8,7 @@ class objlist_class public: objlist_class(int sizeInt, int growSize) { - ListPtr = reinterpret_cast(memory::allocate(sizeof(T*) * sizeInt)); + ListPtr = memory::allocate(sizeInt); Count = 0; Size = sizeInt; GrowSize = growSize; @@ -39,7 +39,7 @@ public: if (newSize <= Size) return; - auto newList = reinterpret_cast(memory::realloc(ListPtr, sizeof(T*) * newSize)); + auto newList = memory::realloc(ListPtr, sizeof(T*) * newSize); if (!newList) return; diff --git a/SpaceCadetPinball/partman.cpp b/SpaceCadetPinball/partman.cpp index 7fdf44f..86c9503 100644 --- a/SpaceCadetPinball/partman.cpp +++ b/SpaceCadetPinball/partman.cpp @@ -25,7 +25,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful _lclose(fileHandle); return nullptr; } - auto datFile = (datFileStruct*)memory::allocate(sizeof(datFileStruct)); + auto datFile = memory::allocate(); if (!datFile) { _lclose(fileHandle); @@ -38,7 +38,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful else { int lenOfStr = lstrlenA(header.Description); - auto descriptionBuf = static_cast(memory::allocate(lenOfStr + 1)); + auto descriptionBuf = memory::allocate(lenOfStr + 1); datFile->Description = descriptionBuf; if (!descriptionBuf) { @@ -51,7 +51,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful if (header.Unknown) { - auto unknownBuf = static_cast(memory::allocate(header.Unknown)); + auto unknownBuf = memory::allocate(header.Unknown); if (!unknownBuf) { _lclose(fileHandle); @@ -64,7 +64,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful memory::free(unknownBuf); } - auto groupDataBuf = (datGroupData**)memory::allocate(sizeof(void*) * header.NumberOfGroups); + auto groupDataBuf = memory::allocate(header.NumberOfGroups); datFile->GroupData = groupDataBuf; if (!groupDataBuf) { @@ -79,8 +79,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful { auto entryCount = _lread_char(fileHandle); auto groupDataSize = entryCount <= 0 ? 0 : entryCount - 1; - auto groupData = reinterpret_cast(memory::allocate( - sizeof(datEntryData) * groupDataSize + sizeof(datGroupData))); + auto groupData = memory::allocate(1, sizeof(datEntryData) * groupDataSize); datFile->GroupData[groupIndex] = groupData; if (!groupData) break; @@ -105,7 +104,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful continue; } - auto bmp = reinterpret_cast(memory::allocate(sizeof(gdrv_bitmap8))); + auto bmp = memory::allocate(); entryData->Buffer = reinterpret_cast(bmp); if (!bmp) { @@ -146,7 +145,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful _hread(fileHandle, &zMapHeader, sizeof(dat16BitBmpHeader)); int length = fieldSize - sizeof(dat16BitBmpHeader); - auto zmap = reinterpret_cast(memory::allocate(sizeof(zmap_header_type) + length)); + auto zmap = memory::allocate(1, length); zmap->Width = zMapHeader.Width; zmap->Height = zMapHeader.Height; zmap->Stride = zMapHeader.Stride; @@ -155,7 +154,7 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful } else { - char* entryBuffer = static_cast(memory::allocate(fieldSize)); + char* entryBuffer = memory::allocate(fieldSize); entryData->Buffer = entryBuffer; if (!entryBuffer) { @@ -181,30 +180,23 @@ datFileStruct* partman::load_records(LPCSTR lpFileName, int resolution, bool ful 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]; - if (group) + auto group = datFile->GroupData[groupIndex]; + if (!group) + continue; + + for (auto entryIndex = 0; entryIndex < group->EntryCount; ++entryIndex) { - int entryIndex = 0; - if (group->EntryCount > 0) + auto entry = &group->Entries[entryIndex]; + if (entry->Buffer) { - datEntryData* entry = group->Entries; - do - { - 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); + if (entry->EntryType == datFieldTypes::Bitmap8bit) + gdrv::destroy_bitmap(reinterpret_cast(entry->Buffer)); + memory::free(entry->Buffer); } - memory::free(group); } + memory::free(group); } if (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) { - datGroupData* groupData = datFile->GroupData[groupIndex]; - int entryCount = groupData->EntryCount; - int entryIndex = 0; - if (entryCount <= 0) - return nullptr; - datEntryData* entry = groupData->Entries; - while (true) + auto group = datFile->GroupData[groupIndex]; + for (auto entryIndex = 0; entryIndex < group->EntryCount; ++entryIndex) { - auto entryType = entry->EntryType; - if (entryType == targetEntryType) + auto entry = &group->Entries[entryIndex]; + if (entry->EntryType == targetEntryType) + return entry->Buffer; + if (entry->EntryType > targetEntryType) 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) { - datGroupData* groupData = datFile->GroupData[groupIndex]; - int entryCount = groupData->EntryCount, skipCount = 0, entryIndex = 0; - if (0 < entryCount) + auto group = datFile->GroupData[groupIndex]; + for (auto skipCount = 0, entryIndex = 0; entryIndex < group->EntryCount; ++entryIndex) { - datEntryData* entry = groupData->Entries; - do - { - auto entryType = entry->EntryType; - if (entryType == targetEntryType) - { - if (skipCount == skipFirstN) - { - return entry->Buffer; - } - skipCount++; - } - else - { - if (targetEntryType < entryType) - { - return nullptr; - } - } - entryIndex++; - entry++; - } - while (entryIndex < entryCount); + auto entry = &group->Entries[entryIndex]; + if (entry->EntryType > targetEntryType) + break; + if (entry->EntryType == targetEntryType) + if (skipCount++ == skipFirstN) + return entry->Buffer; } return nullptr; } int partman::field_size_nth(datFileStruct* datFile, int groupIndex, datFieldTypes targetEntryType, int skipFirstN) { - datGroupData* groupData = datFile->GroupData[groupIndex]; - int entryCount = groupData->EntryCount, skipCount = 0, entryIndex = 0; - if (0 < entryCount) + auto group = datFile->GroupData[groupIndex]; + for (auto skipCount = 0, entryIndex = 0; entryIndex < group->EntryCount; ++entryIndex) { - datEntryData* entry = groupData->Entries; - do - { - auto entryType = entry->EntryType; - if (entryType == targetEntryType) - { - if (skipCount == skipFirstN) - { - return entry->FieldSize; - } - skipCount++; - } - else - { - if (targetEntryType < entryType) - { - return 0; - } - } - entryIndex++; - entry++; - } - while (entryIndex < entryCount); + auto entry = &group->Entries[entryIndex]; + if (entry->EntryType > targetEntryType) + return 0; + if (entry->EntryType == targetEntryType) + if (skipCount++ == skipFirstN) + return entry->FieldSize; } 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 trgGroupNameLen = lstrlenA(targetGroupName); - int groupIndex = datFile->NumberOfGroups; - while (true) + auto targetLength = lstrlenA(targetGroupName); + for (int groupIndex = datFile->NumberOfGroups - 1; groupIndex >= 0; --groupIndex) { - if (--groupIndex < 0) - return -1; - char* groupName = field(datFile, groupIndex, datFieldTypes::GroupName); - if (groupName) - { - int index = 0; - bool found = trgGroupNameLen == 0; - 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]) + auto groupName = field(datFile, groupIndex, datFieldTypes::GroupName); + if (!groupName) + continue; + + int index; + for (index = 0; index < targetLength; index++) + if (targetGroupName[index] != groupName[index]) 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* result; - int groupIndex = record_labeled(datFile, lpString); - if (groupIndex < 0) - result = nullptr; - else - result = field(datFile, groupIndex, fieldType); - return result; + auto groupIndex = record_labeled(datFile, lpString); + return groupIndex < 0 ? nullptr : field(datFile, groupIndex, fieldType); } char partman::_lread_char(HFILE hFile) diff --git a/SpaceCadetPinball/pb.cpp b/SpaceCadetPinball/pb.cpp index 1021ca4..db3b576 100644 --- a/SpaceCadetPinball/pb.cpp +++ b/SpaceCadetPinball/pb.cpp @@ -47,7 +47,7 @@ int pb::init() score::load_msg_font("pbmsg_ft"); if (!record_table) - return (int)&record_table->NumberOfGroups + 1; + return 1; auto plt = (PALETTEENTRY*)partman::field_labeled(record_table, "background", datFieldTypes::Palette); gdrv::display_palette(plt); diff --git a/SpaceCadetPinball/render.cpp b/SpaceCadetPinball/render.cpp index b118f4d..8cf21fb 100644 --- a/SpaceCadetPinball/render.cpp +++ b/SpaceCadetPinball/render.cpp @@ -18,9 +18,9 @@ void render::init(gdrv_bitmap8* bmp, float zMin, float zScaler, int width, int h zscaler = zScaler; zmin = zMin; zmax = 4294967300.0f / zScaler + zMin; - sprite_list = reinterpret_cast(memory::allocate(1000 * sizeof(void*))); - dirty_list = reinterpret_cast(memory::allocate(1000 * sizeof(void*))); - ball_list = reinterpret_cast(memory::allocate(20 * sizeof(void*))); + sprite_list = memory::allocate(1000); + dirty_list = memory::allocate(1000); + ball_list = memory::allocate(20); gdrv::create_bitmap(&vscreen, width, height); zdrv::create_zmap(&zscreen, width, height); zdrv::fill(&zscreen, zscreen.Width, zscreen.Height, 0, 0, 0xFFFF); @@ -48,9 +48,9 @@ void render::uninit() { gdrv::destroy_bitmap(&vscreen); 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]); - for (int j = 0; j < many_balls; ++j) + for (auto j = many_balls - 1; j >= 0; --j) remove_ball(ball_list[j]); memory::free(ball_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, int xPosition, int yPosition, rectangle_type* rect) { - auto sprite = (render_sprite_type_struct*)memory::allocate(sizeof(render_sprite_type_struct)); + auto sprite = memory::allocate(); if (!sprite) return nullptr; 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) { - int spriteCount = many_sprites; int index = 0; if (many_sprites > 0) { @@ -287,13 +286,12 @@ void render::remove_sprite(render_sprite_type_struct* sprite) if (++index >= many_sprites) return; } - while (index < spriteCount) + while (index < many_sprites) { sprite_list[index] = sprite_list[index + 1]; - spriteCount = many_sprites; ++index; } - many_sprites = spriteCount - 1; + many_sprites--; if (sprite->SpriteArray) memory::free(sprite->SpriteArray); 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) { - int ballCount = many_balls; int index = 0; if (many_balls > 0) { @@ -311,13 +308,12 @@ void render::remove_ball(struct render_sprite_type_struct* ball) if (++index >= many_balls) return; } - while (index < ballCount) + while (index < many_balls) { ball_list[index] = ball_list[index + 1]; - ballCount = many_balls; ++index; } - many_balls = ballCount - 1; + many_balls--; memory::free(ball); } } @@ -542,7 +538,7 @@ void render::build_occlude_list() if (!curSprite->UnknownFlag && curSprite->BoundingRect.Width != -1) { if (!spriteArr) - spriteArr = reinterpret_cast(memory::allocate(1000 * sizeof(void*))); + spriteArr = memory::allocate(1000); int occludeCount = 0; auto spritePtr2 = sprite_list; for (int i = 0; i < many_sprites; ++i, ++spritePtr2) @@ -560,8 +556,7 @@ void render::build_occlude_list() occludeCount = 0; if (occludeCount) { - curSprite->SpriteArray = reinterpret_cast(memory::realloc( - spriteArr, sizeof(void*) * occludeCount)); + curSprite->SpriteArray = memory::realloc(spriteArr, sizeof(void*) * occludeCount); curSprite->SpriteCount = occludeCount; spriteArr = nullptr; } diff --git a/SpaceCadetPinball/score.cpp b/SpaceCadetPinball/score.cpp index dd2cdf6..b17ec41 100644 --- a/SpaceCadetPinball/score.cpp +++ b/SpaceCadetPinball/score.cpp @@ -16,7 +16,7 @@ int score::init() scoreStruct* score::create(LPCSTR fieldName, gdrv_bitmap8* renderBgBmp) { - auto score = reinterpret_cast(memory::allocate(sizeof(scoreStruct))); + auto score = memory::allocate(); if (!score) return nullptr; score->Score = -9999; @@ -45,7 +45,7 @@ scoreStruct* score::create(LPCSTR fieldName, gdrv_bitmap8* renderBgBmp) scoreStruct* score::dup(scoreStruct* score, int scoreIndex) { - auto result = reinterpret_cast(memory::allocate(sizeof(scoreStruct))); + auto result = memory::allocate(); if (result) memcpy(result, score, sizeof(scoreStruct)); return result; @@ -63,7 +63,7 @@ void score::load_msg_font(LPCSTR lpName) auto rcData = static_cast<__int16*>(LockResource(resGlobal)); - auto fontp = reinterpret_cast(memory::allocate(sizeof(score_msg_font_type))); + auto fontp = memory::allocate(); msg_fontp = fontp; if (!fontp) { @@ -102,7 +102,7 @@ void score::load_msg_font(LPCSTR lpName) if (!width) continue; - auto bmp = reinterpret_cast(memory::allocate(sizeof(gdrv_bitmap8))); + auto bmp = memory::allocate(); msg_fontp->Chars[charInd] = bmp; if (!bmp) { @@ -130,6 +130,7 @@ void score::load_msg_font(LPCSTR lpName) } } + memory::free(tmpCharBur); if (charInd != 128) unload_msg_font(); FreeResource(resGlobal); @@ -147,6 +148,7 @@ void score::unload_msg_font() memory::free(msg_fontp->Chars[i]); } } + memory::free(msg_fontp); msg_fontp = nullptr; } } @@ -191,7 +193,7 @@ void score::set(scoreStruct* score, int value) void score::update(scoreStruct* score) { - char scoreBuf[12]; + char scoreBuf[12]{}; if (score && score->DirtyFlag && score->Score <= 1000000000) { score->DirtyFlag = false; diff --git a/SpaceCadetPinball/splash.cpp b/SpaceCadetPinball/splash.cpp index 97ad0d1..572e9ab 100644 --- a/SpaceCadetPinball/splash.cpp +++ b/SpaceCadetPinball/splash.cpp @@ -6,13 +6,14 @@ #include "pinball.h" HINSTANCE splash::HInstance; +HGDIOBJ splash::OriginalDcBitmap = nullptr; splash_struct* splash::splash_screen(HINSTANCE hInstance, LPCSTR bmpName1, LPCSTR bmpName2) { WNDCLASSA WndClass{}; tagRECT Rect{}; - auto splashStruct = reinterpret_cast(memory::allocate(sizeof(splash_struct))); + auto splashStruct = memory::allocate(); if (!splashStruct) return nullptr; @@ -75,7 +76,7 @@ void splash::splash_bitmap_setup(splash_struct* splashStruct) { if (splashStruct->DrawingContext) { - SelectObject(splashStruct->DrawingContext, bmpHandle1); + OriginalDcBitmap = SelectObject(splashStruct->DrawingContext, bmpHandle1); if ((GetDeviceCaps(splashStruct->DrawingContext, RASTERCAPS) & RC_PALETTE) != 0 || GetDeviceCaps(splashStruct->DrawingContext, NUMCOLORS) >= 256) { @@ -85,6 +86,7 @@ void splash::splash_bitmap_setup(splash_struct* splashStruct) else { bmpHandle2 = LoadBitmapA(HInstance, splashStruct->BmpName2); + splashStruct->Palette = nullptr; } splashStruct->Bitmap = bmpHandle2; @@ -162,11 +164,11 @@ HBITMAP splash::load_title_bitmap(HMODULE hModule, HDC hdc, LPCSTR lpName, UINT return resBmp; } -HPALETTE splash::splash_init_palette(LOGPALETTEx256* plpal) +HPALETTE splash::splash_init_palette(LOGPALETTE* plpal) { plpal->palVersion = 768; plpal->palNumEntries = 256; - auto hPalette = CreatePalette(reinterpret_cast(plpal)); + auto hPalette = CreatePalette(static_cast(plpal)); auto dc = GetDC(GetDesktopWindow()); GetDeviceCaps(dc, RASTERCAPS); if (GetDeviceCaps(dc, SIZEPALETTE) != 256) @@ -243,7 +245,11 @@ void splash::splash_destroy(splash_struct* splashStruct) splashStruct->Palette = nullptr; if (splashStruct->DrawingContext) + { + if (OriginalDcBitmap) + SelectObject(splashStruct->DrawingContext, OriginalDcBitmap); DeleteDC(splashStruct->DrawingContext); + } if (splashStruct->Bitmap) DeleteObject(splashStruct->Bitmap); } diff --git a/SpaceCadetPinball/splash.h b/SpaceCadetPinball/splash.h index e8f7d97..20b718e 100644 --- a/SpaceCadetPinball/splash.h +++ b/SpaceCadetPinball/splash.h @@ -22,12 +22,14 @@ class splash public: static splash_struct* splash_screen(HINSTANCE hInstance, LPCSTR bmpName1, LPCSTR bmpName2); 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 HPALETTE splash_init_palette(LOGPALETTEx256* plpal); + static HBITMAP load_title_bitmap(HMODULE hModule, HDC hdc, LPCSTR lpName, UINT iStart, int iEnd, + HPALETTE* palettePtr); + static HPALETTE splash_init_palette(LOGPALETTE* plpal); static void splash_paint(splash_struct* splashStruct, HDC dc); static void splash_destroy(splash_struct* splashStruct); static void splash_hide(splash_struct* splashStruct); static LRESULT __stdcall splash_message_handler(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); private: static HINSTANCE HInstance; + static HGDIOBJ OriginalDcBitmap; }; diff --git a/SpaceCadetPinball/timer.cpp b/SpaceCadetPinball/timer.cpp index 2420868..ad97b0c 100644 --- a/SpaceCadetPinball/timer.cpp +++ b/SpaceCadetPinball/timer.cpp @@ -13,7 +13,7 @@ timer_struct* timer::TimerBuffer; int timer::init(int count) { - auto buf = (timer_struct*)memory::allocate(sizeof(timer_struct) * count); + auto buf = memory::allocate(count); TimerBuffer = buf; if (!buf) return 1; diff --git a/SpaceCadetPinball/zdrv.cpp b/SpaceCadetPinball/zdrv.cpp index 6ab815b..a269bfd 100644 --- a/SpaceCadetPinball/zdrv.cpp +++ b/SpaceCadetPinball/zdrv.cpp @@ -8,7 +8,7 @@ int zdrv::create_zmap(zmap_header_type* zmap, int width, int height) { int stride = pad(width); zmap->Stride = stride; - auto bmpBuf = (unsigned short*)memory::allocate(2 * height * stride); + auto bmpBuf = memory::allocate(height * stride); zmap->ZPtr1 = bmpBuf; if (!bmpBuf) return -1; @@ -36,22 +36,16 @@ int zdrv::destroy_zmap(zmap_header_type* zmap) 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 zmapPtr = &zmap->ZPtr1[xOff + zmap->Stride * (zmap->Height - height - yOff)]; - - for (int y = height; width > 0 && y > 0; y--) + auto dstPtr = &zmap->ZPtr1[zmap->Stride * (zmap->Height - height - yOff) + xOff]; + for (int y = height; y > 0; --y) { - char widthMod2 = width & 1; - unsigned int widthDiv2 = static_cast(width) >> 1; - memset32(zmapPtr, fillCharInt, widthDiv2); - - auto lastShort = &zmapPtr[2 * widthDiv2]; - for (int i = widthMod2; i; --i) - *lastShort++ = fillChar; - - zmapPtr += zmap->Stride; + for (int x = width; x > 0; --x) + { + *dstPtr++ = fillWord; + } + dstPtr += zmap->Stride - width; } } diff --git a/SpaceCadetPinball/zdrv.h b/SpaceCadetPinball/zdrv.h index b9a544e..6a5040f 100644 --- a/SpaceCadetPinball/zdrv.h +++ b/SpaceCadetPinball/zdrv.h @@ -3,9 +3,9 @@ struct zmap_header_type { - __int16 Width; - __int16 Height; - __int16 Stride; + int Width; + int Height; + int Stride; unsigned __int16* ZPtr1; unsigned __int16* ZPtr2; unsigned __int16 ZBuffer[1]; @@ -17,7 +17,7 @@ public: static int pad(int width); static int create_zmap(zmap_header_type* zmap, int width, int height); 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, zmap_header_type* dstZMap, int dstZMapXOff, int dstZMapYOff, gdrv_bitmap8* srcBmp, int srcBmpXOff, int srcBmpYOff, zmap_header_type* srcZMap, int srcZMapXOff, int srcZMapYOff);