From a08478d527b6d5b65e733ad4bae17159e64a02b1 Mon Sep 17 00:00:00 2001 From: iProgramInCpp Date: Thu, 17 Aug 2023 11:37:33 +0300 Subject: [PATCH] * Slight refactor to the resize code. --- platforms/sdl/main.cpp | 30 ++++++++++++++-------- platforms/windows/main.cpp | 2 ++ source/Minecraft.cpp | 51 ++++++++++++++++++-------------------- source/Minecraft.hpp | 20 +++------------ source/client/gui/Gui.cpp | 4 +-- 5 files changed, 52 insertions(+), 55 deletions(-) diff --git a/platforms/sdl/main.cpp b/platforms/sdl/main.cpp index 3b33b4c..e550aa9 100644 --- a/platforms/sdl/main.cpp +++ b/platforms/sdl/main.cpp @@ -23,6 +23,8 @@ #define EM_FALSE false #endif +static float g_fPointToPixelScale = 1.0f; + void LogMsg(const char* fmt, ...) { va_list lst; @@ -98,13 +100,13 @@ static void handle_events() case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { - const float scale = Minecraft::getDrawScale(); + float scale = g_fPointToPixelScale; Mouse::feed(event.button.button == SDL_BUTTON_LEFT ? 1 : 2, event.button.state == SDL_PRESSED ? 1 : 0, event.button.x * scale, event.button.y * scale); break; } case SDL_MOUSEMOTION: { - const float scale = Minecraft::getDrawScale(); + float scale = g_fPointToPixelScale; float x = event.motion.x * scale; float y = event.motion.y * scale; Mouse::setX(x); Mouse::setY(y); @@ -114,7 +116,7 @@ static void handle_events() } case SDL_MOUSEWHEEL: { - Mouse::feed(3, event.wheel.y * Minecraft::getDrawScale(), Mouse::getX(), Mouse::getY()); + Mouse::feed(3, event.wheel.y * g_fPointToPixelScale, Mouse::getX(), Mouse::getY()); break; } case SDL_TEXTINPUT: @@ -157,10 +159,18 @@ static void resize() SDL_GetWindowSize(window, &windowWidth, &windowHeight); - Minecraft::setDisplayProperties(drawWidth, drawHeight, windowWidth, windowHeight); - + // recalculate the point to pixel scale. + // This currently assumes that the aspect ratio is the same. + g_fPointToPixelScale = float(drawWidth) / float(windowWidth); + + // Update the scale multiplier. We use the same value, because we pass to `sizeUpdate`, the window width/height. + // They will be multiplied by the GUI scale multiplier, becoming the drawwidth and drawheight, times the decided on GUI scale. + Minecraft::setGuiScaleMultiplier(g_fPointToPixelScale); + + // give it an update. + // As said before, internally, this multiplies by the GUI scale multiplier if (g_pApp) - g_pApp->sizeUpdate(Minecraft::width, Minecraft::height); + g_pApp->sizeUpdate(windowWidth, windowHeight); } // Main Loop @@ -238,11 +248,11 @@ int main(int argc, char *argv[]) CheckOptionalTextureAvailability(); // Create Window - window = SDL_CreateWindow("ReMinecraftPE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Minecraft::width, Minecraft::height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | - SDL_WINDOW_ALLOW_HIGHDPI); + int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; + window = SDL_CreateWindow("ReMinecraftPE", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Minecraft::width, Minecraft::height, flags); if (!window) { - LOGE("Unable To Create SDL Window\n"); + LOGE("Unable to create SDL window\n"); exit(EXIT_FAILURE); } @@ -253,7 +263,7 @@ int main(int argc, char *argv[]) context = SDL_GL_CreateContext(window); if (!context) { - LOGE("Unable To Create OpenGL Context\n"); + LOGE("Unable to create OpenGL context\n"); exit(EXIT_FAILURE); } diff --git a/platforms/windows/main.cpp b/platforms/windows/main.cpp index 6e7f2ee..768e82e 100644 --- a/platforms/windows/main.cpp +++ b/platforms/windows/main.cpp @@ -155,6 +155,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam) Minecraft::width = width; Minecraft::height = height; + Minecraft::setGuiScaleMultiplier(1.0f); // assume no meddling with the DPI stuff g_AppPlatform.setScreenSize(width, height); @@ -264,6 +265,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLin // initialize the app g_pApp->init(); + g_pApp->sizeUpdate(Minecraft::width, Minecraft::height); while (!g_pApp->wantToQuit()) { diff --git a/source/Minecraft.cpp b/source/Minecraft.cpp index f9d8ace..121647d 100644 --- a/source/Minecraft.cpp +++ b/source/Minecraft.cpp @@ -31,9 +31,7 @@ int Minecraft::width = C_DEFAULT_SCREEN_WIDTH; int Minecraft::height = C_DEFAULT_SCREEN_HEIGHT; -int Minecraft::_windowWidth = width; -int Minecraft::_windowHeight = height; -float Minecraft::_drawScale = 1.0f; +float Minecraft::guiScaleMultiplier = 1.0f; bool Minecraft::useAmbientOcclusion = false; int Minecraft::customDebugId = 0; @@ -226,6 +224,11 @@ bool Minecraft::isOnlineClient() return m_pLevel->m_bIsMultiplayer; } +void Minecraft::setGuiScaleMultiplier(float f) +{ + guiScaleMultiplier = f; +} + void Minecraft::handleMouseDown(int type, bool b) { if (!m_pGameMode->field_8 && (type != 1 || this->field_DA4 <= 0)) @@ -884,30 +887,35 @@ void Minecraft::prepareLevel(const std::string& unused) void Minecraft::sizeUpdate(int newWidth, int newHeight) { - // re-calculate the GUI scale. - Gui::InvGuiScale = getBestScaleForThisScreenSize(newWidth, newHeight); + Gui::InvGuiScale = getBestScaleForThisScreenSize(newWidth, newHeight) / guiScaleMultiplier; if (m_pScreen) - m_pScreen->setSize(int(newWidth * Gui::InvGuiScale), int(newHeight * Gui::InvGuiScale)); + m_pScreen->setSize(int(Minecraft::width * Gui::InvGuiScale), int(Minecraft::height * Gui::InvGuiScale)); } float Minecraft::getBestScaleForThisScreenSize(int width, int height) { - // phones only -#if !defined(_WIN32) && !defined(USE_SDL2) - if (width > 1000) + if (height > 1800) return 1.0f / 4.0f; - if (width > 800) - return 1.0f / 3.0f; -#else - if (width > 1000) - return 1.0f / 3.0f; -#endif + // phones only +#if !defined(_WIN32) && !defined(USE_SDL2) + if (height > 600) + return 1.0f / 4.0f; - if (width > 400) + if (height > 400) + return 1.0f / 3.0f; + + if (height > 300) return 1.0f / 2.0f; +#else + if (height > 1000) + return 1.0f / 3.0f; + + if (height > 400) + return 1.0f / 2.0f; +#endif return 1.0f; } @@ -971,17 +979,6 @@ void* Minecraft::prepareLevel_tspawn(void* ptr) return nullptr; } -void Minecraft::setDisplayProperties( - int drawWidth, int drawHeight, - int windowWidth, int windowHeight) -{ - width = drawWidth; height = drawHeight; - _windowWidth = windowWidth; _windowHeight = windowHeight; - // Calculates the scale for the resolution at which the scene is drawn. - // This assumes the aspect ratio is the same between the window and the actual drawing process. - _drawScale = width / _windowWidth; -} - void Minecraft::pauseGame() { if (m_pScreen) return; diff --git a/source/Minecraft.hpp b/source/Minecraft.hpp index 920ac50..32dcfbc 100644 --- a/source/Minecraft.hpp +++ b/source/Minecraft.hpp @@ -60,10 +60,11 @@ public: void respawnPlayer(Player* player); std::string getVersionString(); - virtual void onGraphicsReset(); virtual void update() override; virtual void init() override; + virtual void onGraphicsReset(); virtual void sizeUpdate(int newWidth, int newHeight) override; + virtual int getFpsIntlCounter(); float getBestScaleForThisScreenSize(int width, int height); void generateLevel(const std::string& unused, Level* pLevel); @@ -72,28 +73,15 @@ public: bool isOnline(); bool isOnlineClient(); static void* prepareLevel_tspawn(void* pMinecraft); - static void setDisplayProperties( - int drawWidth, int drawHeight, - int windowWidth, int windowHeight); - static const int getWindowWidth() { return _windowWidth; } - static const int getWindowHeight() { return _windowHeight; } - static const int getDrawWidth() { return width; } - static const int getDrawHeight() { return height; } - static const float getDrawScale() { return _drawScale; } const char* getProgressMessage(); LevelStorageSource* getLevelSource(); - ItemInstance* getSelectedItem(); - virtual int getFpsIntlCounter(); + static void setGuiScaleMultiplier(float f); -private: - static int _windowWidth, _windowHeight; - //static int _drawWidth, _drawHeight; - static float _drawScale; public: - // DEPRECATED: Use getDrawWidth() & getDrawHeight() instead + static float guiScaleMultiplier; static int width, height; static bool useAmbientOcclusion; static const char* progressMessages[]; diff --git a/source/client/gui/Gui.cpp b/source/client/gui/Gui.cpp index 7b88f0f..3771cd3 100644 --- a/source/client/gui/Gui.cpp +++ b/source/client/gui/Gui.cpp @@ -148,8 +148,8 @@ void Gui::render(float f, bool bHaveScreen, int mouseX, int mouseY) field_4 = -90.0f; - int width = Minecraft::getDrawWidth() * InvGuiScale, - height = Minecraft::getDrawHeight() * InvGuiScale; + int width = Minecraft::width * InvGuiScale, + height = Minecraft::height * InvGuiScale; #ifdef ENH_TRANSPARENT_HOTBAR glEnable(GL_BLEND);