From cfe26918923934e219d3644ac44f82926bcb259d Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Fri, 27 May 2022 13:54:36 +0300 Subject: [PATCH] Optimized SDL_RenderDrawCircle. Change mouse warping strategy in hidden test cheat. --- SpaceCadetPinball/DebugOverlay.cpp | 36 ++++++++++++++++++++---------- SpaceCadetPinball/midi.cpp | 5 +++-- SpaceCadetPinball/winmain.cpp | 19 ++++++++++++---- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/SpaceCadetPinball/DebugOverlay.cpp b/SpaceCadetPinball/DebugOverlay.cpp index af6dfed..fda5598 100644 --- a/SpaceCadetPinball/DebugOverlay.cpp +++ b/SpaceCadetPinball/DebugOverlay.cpp @@ -21,6 +21,8 @@ gdrv_bitmap8* DebugOverlay::dbScreen = nullptr; static int SDL_RenderDrawCircle(SDL_Renderer* renderer, int x, int y, int radius) { + SDL_Point points[256]; + int pointCount = 0; int offsetx, offsety, d; int status; @@ -29,21 +31,28 @@ static int SDL_RenderDrawCircle(SDL_Renderer* renderer, int x, int y, int radius d = radius - 1; status = 0; - while (offsety >= offsetx) { - status += SDL_RenderDrawPoint(renderer, x + offsetx, y + offsety); - status += SDL_RenderDrawPoint(renderer, x + offsety, y + offsetx); - status += SDL_RenderDrawPoint(renderer, x - offsetx, y + offsety); - status += SDL_RenderDrawPoint(renderer, x - offsety, y + offsetx); - status += SDL_RenderDrawPoint(renderer, x + offsetx, y - offsety); - status += SDL_RenderDrawPoint(renderer, x + offsety, y - offsetx); - status += SDL_RenderDrawPoint(renderer, x - offsetx, y - offsety); - status += SDL_RenderDrawPoint(renderer, x - offsety, y - offsetx); + while (offsety >= offsetx) + { + if (pointCount + 8 > 256) + { + status = SDL_RenderDrawPoints(renderer, points, pointCount); + pointCount = 0; - if (status < 0) { - status = -1; - break; + if (status < 0) { + status = -1; + break; + } } + points[pointCount++] = { x + offsetx, y + offsety }; + points[pointCount++] = { x + offsety, y + offsetx }; + points[pointCount++] = { x - offsetx, y + offsety }; + points[pointCount++] = { x - offsety, y + offsetx }; + points[pointCount++] = { x + offsetx, y - offsety }; + points[pointCount++] = { x + offsety, y - offsetx }; + points[pointCount++] = { x - offsetx, y - offsety }; + points[pointCount++] = { x - offsety, y - offsetx }; + if (d >= 2 * offsetx) { d -= 2 * offsetx + 1; offsetx += 1; @@ -59,6 +68,9 @@ static int SDL_RenderDrawCircle(SDL_Renderer* renderer, int x, int y, int radius } } + if (pointCount > 0) + status = SDL_RenderDrawPoints(renderer, points, pointCount); + return status; } diff --git a/SpaceCadetPinball/midi.cpp b/SpaceCadetPinball/midi.cpp index af6b800..3e51f91 100644 --- a/SpaceCadetPinball/midi.cpp +++ b/SpaceCadetPinball/midi.cpp @@ -227,7 +227,8 @@ std::vector* midi::MdsToMidi(std::string file) fseek(fileHandle, 0, SEEK_END); auto fileSize = static_cast(ftell(fileHandle)); - auto fileBuf = reinterpret_cast(new uint8_t [fileSize]); + auto buffer = new uint8_t[fileSize]; + auto fileBuf = reinterpret_cast(buffer); fseek(fileHandle, 0, SEEK_SET); fread(fileBuf, 1, fileSize, fileHandle); fclose(fileHandle); @@ -371,7 +372,7 @@ std::vector* midi::MdsToMidi(std::string file) } while (false); - delete[] fileBuf; + delete[] buffer; if (returnCode && midiOut) { delete midiOut; diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index 1d733c5..2fe85c3 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -211,11 +211,22 @@ int winmain::WinMain(LPCSTR lpCmdLine) float dy = static_cast(y - last_mouse_y) / static_cast(h); pb::ballset(dx, dy); - SDL_WarpMouseInWindow(window, last_mouse_x, last_mouse_y); + // Original creates continuous mouse movement with mouse capture. + // Alternative solution: mouse warp at window edges. + int xMod = 0, yMod = 0; + if (x == 0 || x >= w - 1) + xMod = w - 2; + if (y == 0 || y >= h - 1) + yMod = h - 2; + if (xMod != 0 || yMod != 0) + { + // Mouse warp does not work over remote desktop or in some VMs + x = abs(x - xMod); y = abs(y - yMod); + SDL_WarpMouseInWindow(window, x, y); + } - // Mouse warp does not work over remote desktop or in some VMs - //last_mouse_x = x; - //last_mouse_y = y; + last_mouse_x = x; + last_mouse_y = y; } if (!single_step && !no_time_loss) {