From 8a38dee56283c12bf4ab27fcd4107ff9da4b050c Mon Sep 17 00:00:00 2001 From: iProgramInCpp Date: Thu, 17 Aug 2023 17:49:52 +0300 Subject: [PATCH] * A couple of fixes for SDL/Emscripten build. --- .../emscripten/AppPlatform_emscripten.cpp | 6 +-- platforms/sdl/main.cpp | 48 +++++++++++++------ source/AppPlatform.hpp | 2 +- source/Minecraft.cpp | 10 +++- source/NinecraftApp.cpp | 2 +- 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/platforms/emscripten/AppPlatform_emscripten.cpp b/platforms/emscripten/AppPlatform_emscripten.cpp index bbb22ce..cdd1d67 100644 --- a/platforms/emscripten/AppPlatform_emscripten.cpp +++ b/platforms/emscripten/AppPlatform_emscripten.cpp @@ -20,9 +20,9 @@ Texture AppPlatform_emscripten::loadTexture(const std::string& path, bool b) char *data = emscripten_get_preloaded_image_data(("/" + realPath).c_str(), &out.m_width, &out.m_height); if (data != NULL) { - size_t data_size = out.m_width * out.m_height * 4; - out.m_pixels = (uint32_t *) new unsigned char[data_size]; - memcpy(out.m_pixels, data, data_size); + size_t data_size = out.m_width * out.m_height; + out.m_pixels = new uint32_t[data_size]; + memcpy(out.m_pixels, data, data_size * sizeof(uint32_t)); free(data); return out; } diff --git a/platforms/sdl/main.cpp b/platforms/sdl/main.cpp index 0161f1e..9057905 100644 --- a/platforms/sdl/main.cpp +++ b/platforms/sdl/main.cpp @@ -7,10 +7,10 @@ #include "App.hpp" #ifdef __EMSCRIPTEN__ #include "../emscripten/AppPlatform_emscripten.hpp" -#define APP_PLATFORM_TYPE AppPlatform_emscripten +typedef AppPlatform_emscripten UsedAppPlatform #else #include "AppPlatform_sdl.hpp" -#define APP_PLATFORM_TYPE AppPlatform_sdl +typedef AppPlatform_sdl UsedAppPlatform; #endif #include "NinecraftApp.hpp" @@ -46,7 +46,7 @@ void LogMsgNoCR(const char* fmt, ...) va_end(lst); } -APP_PLATFORM_TYPE *g_AppPlatform; +UsedAppPlatform *g_pAppPlatform; NinecraftApp *g_pApp; SDL_Window *window = NULL; @@ -84,16 +84,16 @@ static void handle_events() { if (event.key.keysym.sym == SDLK_F2) { - if (event.key.state == SDL_PRESSED && g_AppPlatform != nullptr) + if (event.key.state == SDL_PRESSED && g_pAppPlatform != nullptr) { - g_AppPlatform->saveScreenshot("", -1, -1); + g_pAppPlatform->saveScreenshot("", -1, -1); } break; } Keyboard::feed(event.key.state == SDL_PRESSED ? 1 : 0, translate_sdl_key_to_mcpe(event.key.keysym.sym)); if (event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT) { - g_AppPlatform->setShiftPressed(event.key.state == SDL_PRESSED, event.key.keysym.sym == SDLK_LSHIFT); + g_pAppPlatform->setShiftPressed(event.key.state == SDL_PRESSED, event.key.keysym.sym == SDLK_LSHIFT); } break; } @@ -111,7 +111,7 @@ static void handle_events() float y = event.motion.y * scale; Mouse::setX(x); Mouse::setY(y); Mouse::feed(0, 0, x, y); - g_AppPlatform->setMouseDiff(event.motion.xrel * scale, event.motion.yrel * scale); + g_pAppPlatform->setMouseDiff(event.motion.xrel * scale, event.motion.yrel * scale); break; } case SDL_MOUSEWHEEL: @@ -199,7 +199,7 @@ static EM_BOOL main_loop(double time, void *user_data) if (g_pApp->wantToQuit()) { delete g_pApp; - delete g_AppPlatform; + delete g_pAppPlatform; teardown(); // Stop Looping return EM_FALSE; @@ -214,10 +214,30 @@ static EM_BOOL main_loop(double time, void *user_data) extern bool g_bIsMenuBackgroundAvailable; // client/gui/Screen.cpp extern bool g_bAreCloudsAvailable; // client/renderer/LevelRenderer.cpp +#ifdef __EMSCRIPTEN__ +bool DoesAssetExist(const std::string & fileName) +{ + std::string realPath = g_pAppPlatform->getAssetPath(fileName); + int width = 0, height = 0; + char *data = emscripten_get_preloaded_image_data(("/" + realPath).c_str(), &width, &height); + if (data == NULL) + return false; + + free(data); + return true; +} +#endif + void CheckOptionalTextureAvailability() { - //g_bIsMenuBackgroundAvailable = XPL_ACCESS("assets/gui/background/panorama_0.png", 0) == 0; - //g_bAreCloudsAvailable = XPL_ACCESS("assets/environment/clouds.png", 0) == 0; +#ifdef __EMSCRIPTEN__ + g_bIsMenuBackgroundAvailable = DoesAssetExist("gui/background/panorama_0.png"); + g_bAreCloudsAvailable = DoesAssetExist("environment/clouds.png"); +#else + // access works just fine on linux and friends + g_bIsMenuBackgroundAvailable = XPL_ACCESS("assets/gui/background/panorama_0.png", 0) == 0; + g_bAreCloudsAvailable = XPL_ACCESS("assets/environment/clouds.png", 0) == 0; +#endif } // Main @@ -248,8 +268,6 @@ int main(int argc, char *argv[]) Minecraft::height = std::stoi(argv[2]); #endif - CheckOptionalTextureAvailability(); - // Create Window 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); @@ -297,9 +315,11 @@ int main(int argc, char *argv[]) // Start MCPE g_pApp = new NinecraftApp; g_pApp->m_externalStorageDir = storagePath; - g_AppPlatform = new APP_PLATFORM_TYPE(g_pApp->m_externalStorageDir, window); - g_pApp->m_pPlatform = g_AppPlatform; + g_pAppPlatform = new UsedAppPlatform(g_pApp->m_externalStorageDir, window); + g_pApp->m_pPlatform = g_pAppPlatform; g_pApp->init(); + + CheckOptionalTextureAvailability(); // Set Size resize(); diff --git a/source/AppPlatform.hpp b/source/AppPlatform.hpp index 8801b55..170541c 100644 --- a/source/AppPlatform.hpp +++ b/source/AppPlatform.hpp @@ -56,7 +56,7 @@ public: virtual std::string getPatchData(); #endif -protected: +public: virtual std::string getAssetPath(const std::string& path) const; private: diff --git a/source/Minecraft.cpp b/source/Minecraft.cpp index 121647d..66987da 100644 --- a/source/Minecraft.cpp +++ b/source/Minecraft.cpp @@ -557,13 +557,19 @@ void Minecraft::sendMessage(const std::string& message) if (isOnlineClient()) { // send the server a message packet - m_pRakNetInstance->send(new MessagePacket(message)); + if (m_pRakNetInstance) + m_pRakNetInstance->send(new MessagePacket(message)); + else + m_gui.addMessage("You aren't actually playing multiplayer!"); } else { // fake the server having received a packet MessagePacket mp(message); - m_pNetEventCallback->handle(m_pRakNetInstance->m_pRakPeerInterface->GetMyGUID(), &mp); + if (m_pNetEventCallback && m_pRakNetInstance) + m_pNetEventCallback->handle(m_pRakNetInstance->m_pRakPeerInterface->GetMyGUID(), &mp); + else + m_gui.addMessage("You aren't hosting a multiplayer server!"); } } diff --git a/source/NinecraftApp.cpp b/source/NinecraftApp.cpp index c2bd369..d460453 100644 --- a/source/NinecraftApp.cpp +++ b/source/NinecraftApp.cpp @@ -50,7 +50,7 @@ void NinecraftApp::initGLStates() glDepthFunc(GL_LEQUAL); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.1f); - glCullFace(GL_NONE); + glCullFace(GL_BACK); glEnable(GL_TEXTURE_2D); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); }