diff --git a/src/Environment.cpp b/src/Environment.cpp index 09ab631..99a13f5 100644 --- a/src/Environment.cpp +++ b/src/Environment.cpp @@ -13,8 +13,8 @@ namespace ZL { int Environment::windowHeaderHeight = 0; -int Environment::width = 0; -int Environment::height = 0; +int Environment::width = CONST_DEFAULT_WIDTH; +int Environment::height = CONST_DEFAULT_HEIGHT; float Environment::zoom = DEFAULT_ZOOM; @@ -49,10 +49,10 @@ void Environment::computeProjectionDimensions() if (width >= height) { // Landscape: fix height to 720, scale width to preserve aspect projectionHeight = refShortSide; - projectionWidth = refShortSide * aspect; + projectionWidth = refShortSide * aspect; } else { // Portrait: fix width to 720, scale height to preserve aspect - projectionWidth = refShortSide; + projectionWidth = refShortSide; projectionHeight = refShortSide / aspect; } } diff --git a/src/Environment.h b/src/Environment.h index 00fd608..8eac9ca 100644 --- a/src/Environment.h +++ b/src/Environment.h @@ -14,6 +14,8 @@ namespace ZL { class Environment { public: + static constexpr int CONST_DEFAULT_WIDTH = 1280; + static constexpr int CONST_DEFAULT_HEIGHT = 720; static int windowHeaderHeight; static int width; static int height; diff --git a/src/Game.cpp b/src/Game.cpp index e8536db..a7fa628 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -86,6 +86,8 @@ namespace ZL //glContext = SDL_GL_CreateContext(ZL::Environment::window); //glContext = in_glContext; + Environment::width = Environment::CONST_DEFAULT_WIDTH; + Environment::height = Environment::CONST_DEFAULT_HEIGHT; Environment::computeProjectionDimensions(); ZL::BindOpenGlFunctions(); diff --git a/src/GameConstants.cpp b/src/GameConstants.cpp index 6771660..1b617aa 100644 --- a/src/GameConstants.cpp +++ b/src/GameConstants.cpp @@ -1,4 +1,4 @@ -#include "GameConstants.h" +#include "GameConstants.h" namespace ZL { diff --git a/src/GameConstants.h b/src/GameConstants.h index a19c0c8..2d5ae0b 100644 --- a/src/GameConstants.h +++ b/src/GameConstants.h @@ -1,4 +1,4 @@ -#pragma once +#pragma once #include "render/Renderer.h" namespace ZL diff --git a/src/Space.cpp b/src/Space.cpp index 7864fb6..80b69e2 100644 --- a/src/Space.cpp +++ b/src/Space.cpp @@ -25,6 +25,8 @@ #include "network/LocalClient.h" #endif +#include "GameConstants.h" + namespace ZL { @@ -418,12 +420,10 @@ namespace ZL void Space::drawCubemap(float skyPercent) { - static const std::string defaultShaderName = "default"; - static const std::string envShaderName = "env_sky"; - static const std::string textureUniformName = "Texture"; + static const std::string envSkyShaderName = "env_sky"; static const std::string skyPercentUniformName = "skyPercent"; - renderer.shaderManager.PushShader(envShaderName); + renderer.shaderManager.PushShader(envSkyShaderName); renderer.RenderUniform1i(textureUniformName, 0); renderer.RenderUniform1f(skyPercentUniformName, skyPercent); @@ -480,10 +480,6 @@ namespace ZL void Space::drawShip() { - static const std::string defaultShaderName = "default"; - static const std::string envShaderName = "env"; - static const std::string textureUniformName = "Texture"; - renderer.shaderManager.PushShader(defaultShaderName); renderer.RenderUniform1i(textureUniformName, 0); @@ -511,7 +507,7 @@ namespace ZL glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - renderer.shaderManager.PushShader("default"); + renderer.shaderManager.PushShader(defaultShaderName); renderer.RenderUniform1i(textureUniformName, 0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); @@ -522,7 +518,6 @@ namespace ZL renderer.TranslateMatrix(-Environment::shipState.position); for (const auto& p : projectiles) { if (p && p->isActive()) { - //p->draw(renderer); p->projectileEmitter.draw(renderer, Environment::zoom, Environment::width, Environment::height); } } @@ -531,8 +526,6 @@ namespace ZL renderer.shaderManager.PopShader(); - //projectileEmitter.draw(renderer, Environment::zoom, Environment::width, Environment::height); - if (shipAlive) { renderer.PushMatrix(); renderer.TranslateMatrix({ 0, 0, 16 }); @@ -550,9 +543,6 @@ namespace ZL explosionEmitter.draw(renderer, Environment::zoom, Environment::width, Environment::height, false); } - //glBindTexture(GL_TEXTURE_2D, basePlatformTexture->getTexID()); - //renderer.DrawVertexRenderStruct(basePlatform); - glDisable(GL_BLEND); renderer.PopMatrix(); renderer.PopProjectionMatrix(); @@ -562,10 +552,6 @@ namespace ZL void Space::drawBoxes() { - static const std::string defaultShaderName = "default"; - static const std::string envShaderName = "env"; - static const std::string textureUniformName = "Texture"; - renderer.shaderManager.PushShader(defaultShaderName); renderer.RenderUniform1i(textureUniformName, 0); renderer.PushPerspectiveProjectionMatrix(1.0 / 1.5, @@ -586,7 +572,6 @@ namespace ZL renderer.RotateMatrix(boxCoordsArr[i].m); glBindTexture(GL_TEXTURE_2D, boxTexture->getTexID()); - //glBindTexture(GL_TEXTURE_2D, rockTexture->getTexID()); renderer.DrawVertexRenderStruct(boxRenderArr[i]); renderer.PopMatrix(); @@ -597,10 +582,6 @@ namespace ZL } void Space::drawScene() { - static const std::string defaultShaderName = "default"; - static const std::string envShaderName = "env"; - static const std::string textureUniformName = "Texture"; - glClearColor(0.0f, 1.0f, 0.0f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); @@ -643,9 +624,6 @@ namespace ZL } void Space::drawRemoteShips() { - static const std::string defaultShaderName = "default"; - static const std::string textureUniformName = "Texture"; - renderer.shaderManager.PushShader(defaultShaderName); renderer.RenderUniform1i(textureUniformName, 0); @@ -689,7 +667,6 @@ namespace ZL renderer.LoadIdentity(); renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom }); - //renderer.TranslateMatrix({ 0, -6.f, 0 }); //Ship camera offset renderer.RotateMatrix(Environment::inverseShipMatrix); renderer.TranslateMatrix(-Environment::shipState.position); diff --git a/src/main.cpp b/src/main.cpp index 6dda7cb..94809bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,10 +68,6 @@ static void applyResize(int logicalW, int logicalH) { SDL_SetWindowSize(ZL::Environment::window, physicalW, physicalH); } - // Обновляем ваши внутренние переменные окружения - ZL::Environment::width = physicalW; - ZL::Environment::height = physicalH; - // Пушим событие, чтобы движок пересчитал матрицы проекции SDL_Event e = {}; e.type = SDL_WINDOWEVENT; @@ -240,68 +236,6 @@ int main(int argc, char *argv[]) { ZL::Environment::width = CONST_WIDTH; ZL::Environment::height = CONST_HEIGHT; - - -/*#ifdef EMSCRIPTEN - if (SDL_Init(SDL_INIT_VIDEO) != 0) { - std::cerr << "SDL_Init failed: " << SDL_GetError() << std::endl; - return 1; - } - - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); - - SDL_Window* win = SDL_CreateWindow("Space Ship Game", - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - CONST_WIDTH, CONST_HEIGHT, - SDL_WINDOW_OPENGL); - - if (!win) { - std::cerr << "SDL_CreateWindow failed: " << SDL_GetError() << std::endl; - return 1; - } - - SDL_GLContext glContext = SDL_GL_CreateContext(win); - if (!glContext) { - std::cerr << "SDL_GL_CreateContext failed: " << SDL_GetError() << std::endl; - return 1; - } - - // Привязка контекста к окну — важно! - SDL_GL_MakeCurrent(win, glContext); - - ZL::Environment::window = win; - - g_game = new ZL::Game(); - g_game->setup(); - - // Re-create the game object when the WebGL context is lost and restored - // (this happens e.g. when the user toggles fullscreen in the browser). - emscripten_set_webglcontextlost_callback("#canvas", nullptr, EM_TRUE, onWebGLContextLost); - emscripten_set_webglcontextrestored_callback("#canvas", nullptr, EM_TRUE, onWebGLContextRestored); - - // Keep Environment::width/height in sync when the canvas is resized. - emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_FALSE, onWindowResized); - emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, EM_FALSE, onFullscreenChanged); - - // 2. ИНИЦИАЛИЗАЦИЯ РАЗМЕРОВ: - // Получаем реальные размеры окна браузера на момент запуска - int canvasW = EM_ASM_INT({ return window.innerWidth; }); - int canvasH = EM_ASM_INT({ return window.innerHeight; }); - - // Вызываем вашу функцию — она сама применит DPR, выставит физический размер - // канваса и отправит SDL_WINDOWEVENT_RESIZED для настройки проекции. - applyResize(canvasW, canvasH); - - // 3. Создаем игру и вызываем setup (теперь проекция уже будет знать верный size) - g_game = new ZL::Game(); - g_game->setup(); - - SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "0"); - - emscripten_set_main_loop(MainLoop, 0, 1); -#else*/ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { SDL_Log("SDL init failed: %s", SDL_GetError()); return 1; @@ -327,8 +261,6 @@ int main(int argc, char *argv[]) { game.update(); SDL_Delay(2); } -//#endif - } catch (const std::exception& e) {