diff --git a/Environment.cpp b/Environment.cpp index e8fb822..f30535c 100644 --- a/Environment.cpp +++ b/Environment.cpp @@ -1,5 +1,10 @@ #include "Environment.h" +#include "RenderSystem.h" +#include "Utils.h" +#include "Inventory.h" +#include + namespace ZL { int Environment::windowHeaderHeight = 0; @@ -15,6 +20,8 @@ bool Environment::downPressed = false; Vector3f Environment::cameraShift = {0, 0, 0}; Vector3f Environment::characterPos = {0, 0, 0}; +float Environment::cameraPhi = 0.f; +float Environment::cameraAlpha = 0.3*M_PI / 2.0; float Environment::violaCurrentIdleFrame = 0.f; int Environment::violaLastIdleFrame = -1; @@ -24,4 +31,13 @@ int Environment::violaLastWalkFrame = 0; int Environment::violaCurrentAnimation = 0; float Environment::violaAngleAroundY = 0.f; +bool Environment::settings_inverseVertical = true; + +SDL_Window* Environment::window = nullptr; + +float Environment::cameraDefaultVerticalShift = -150.f; +bool Environment::showMouse = false; + +bool Environment::exitGameLoop = false; + } // namespace ZL diff --git a/Environment.h b/Environment.h index ce8bf5a..1a52c35 100644 --- a/Environment.h +++ b/Environment.h @@ -1,5 +1,9 @@ #pragma once #include "Math.h" +#ifdef __linux__ +#include +#endif +#include "OpenGlExtensions.h" namespace ZL { @@ -17,6 +21,8 @@ public: static Vector3f cameraShift; static Vector3f characterPos; + static float cameraPhi; + static float cameraAlpha; //Viola static float violaCurrentIdleFrame; @@ -29,6 +35,15 @@ public: static float violaAngleAroundY; + static bool settings_inverseVertical; + + static SDL_Window* window; + + static float cameraDefaultVerticalShift; + + static bool showMouse; + static bool exitGameLoop; + }; diff --git a/Game.cpp b/Game.cpp index 89ab637..54c7e8c 100755 --- a/Game.cpp +++ b/Game.cpp @@ -12,7 +12,6 @@ namespace ZL Game::Game() : window(nullptr) , glContext(nullptr) - , exitGameLoop(false) , newTickCount(0) , lastTickCount(0) , renderer(renderSystem.getRenderer()) // Инициализация ссылки на renderer @@ -41,6 +40,8 @@ void Game::setup() { window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Environment::width, Environment::height, SDL_WINDOW_OPENGL); + + Environment::window = window; glContext = SDL_GL_CreateContext(window); @@ -103,7 +104,7 @@ void Game::update() { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { - exitGameLoop = true; + Environment::exitGameLoop = true; } gameObjects.handleEvent(event); diff --git a/Game.h b/Game.h index 2c803cd..70e6680 100755 --- a/Game.h +++ b/Game.h @@ -17,7 +17,7 @@ public: void update(); void render(); - bool shouldExit() const { return exitGameLoop; } + bool shouldExit() const { return Environment::exitGameLoop; } private: void processTickCount(); @@ -29,7 +29,6 @@ private: GameObjectManager gameObjects; Renderer& renderer; // Ссылка на renderer из RenderSystem - bool exitGameLoop; size_t newTickCount; size_t lastTickCount; diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index 76a32a9..6d8fe8c 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -105,6 +105,9 @@ void GameObjectManager::initialize() { inventoryIconMeshMutable.RefreshVBO(); roomTexturePtr = rooms[current_room_index].roomTexture; + + //SDL_ShowCursor(SDL_DISABLE); + SDL_SetRelativeMouseMode(SDL_TRUE); } void GameObjectManager::switch_room(int index){ @@ -160,6 +163,25 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { } else if (event.type == SDL_KEYDOWN) { switch (event.key.keysym.sym) { + case SDLK_SPACE: + Environment::showMouse = !Environment::showMouse; + + if (Environment::showMouse) + { + SDL_SetRelativeMouseMode(SDL_FALSE); + } + else + { + SDL_SetRelativeMouseMode(SDL_TRUE); + lastMouseX = 0; + lastMouseY = 0; + } + break; + + case SDLK_ESCAPE: + case SDLK_q: + Environment::exitGameLoop = true; + break; case SDLK_LEFT: case SDLK_a: Environment::leftPressed = true; @@ -264,15 +286,60 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { } } if (event.type == SDL_MOUSEMOTION) { - // Сохраняем позицию мыши для последующей проверки - lastMouseX = event.motion.x; - lastMouseY = event.motion.y; + + if (Environment::showMouse == false) + { + int mouseX, mouseY; + SDL_GetRelativeMouseState(&mouseX, &mouseY); + + float diffX = 0.01f * mouseX; + + float diffY = 0.01f * mouseY; + + Environment::cameraPhi += diffX; + + if (Environment::settings_inverseVertical) + { + Environment::cameraAlpha -= diffY; + } + else + { + Environment::cameraAlpha += diffY; + } + if (Environment::cameraAlpha < 0.1 * M_PI / 2.0) + { + Environment::cameraAlpha = 0.1 * M_PI / 2.0; + } + else if (Environment::cameraAlpha > 0.9 * M_PI / 2.0) + { + Environment::cameraAlpha = 0.9 * M_PI / 2.0; + } + + } + else + { + lastMouseX = event.motion.x; + lastMouseY = event.motion.y; + } } } void GameObjectManager::updateScene(size_t ms) { const float SPEED = 0.1f; + + Vector2f directionVector = { 0.f, SPEED }; //x and z + + // Вычисляем новые координаты вектора + float x_new = directionVector.v[0] * cos(Environment::cameraPhi) - directionVector.v[1] * sin(Environment::cameraPhi); + float y_new = directionVector.v[0] * sin(Environment::cameraPhi) + directionVector.v[1] * cos(Environment::cameraPhi); + + // Обновляем вектор + directionVector.v[0] = x_new; + directionVector.v[1] = y_new; + + //Only forward is allowed + /* if (Environment::leftPressed) { Environment::cameraShift.v[0] += SPEED * ms; } @@ -284,6 +351,11 @@ void GameObjectManager::updateScene(size_t ms) { } if (Environment::downPressed) { Environment::cameraShift.v[2] -= SPEED * ms; + }*/ + + if (Environment::upPressed) { + Environment::cameraShift.v[0] += directionVector.v[0] * ms; + Environment::cameraShift.v[2] += directionVector.v[1] * ms; } Environment::characterPos.v[0] = -Environment::cameraShift.v[0]; @@ -335,7 +407,6 @@ void GameObjectManager::updateScene(size_t ms) { Environment::violaLastWalkFrame = int(Environment::violaCurrentWalkFrame); } } - } bool GameObjectManager::isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const { diff --git a/RenderSystem.cpp b/RenderSystem.cpp index b4a7eb4..40a3188 100644 --- a/RenderSystem.cpp +++ b/RenderSystem.cpp @@ -62,13 +62,20 @@ void RenderSystem::drawViola(GameObjectManager& gameObjects) renderer.LoadIdentity(); renderer.TranslateMatrix({ 0,0, -100 * Environment::zoom }); - float t = 0.3; - renderer.RotateMatrix(QuatFromRotateAroundX(t * M_PI / 2.0)); - renderer.ScaleMatrix(10); + renderer.RotateMatrix(QuatFromRotateAroundX(Environment::cameraAlpha)); + //renderer.RotateMatrix(QuatFromRotateAroundY(Environment::cameraPhi)); + //Go a little bit up to make camera at the position of Viola + renderer.TranslateMatrix({ 0, Environment::cameraDefaultVerticalShift, 0 }); + + + //Viola stuff + renderer.ScaleMatrix(10); renderer.RotateMatrix(QuatFromRotateAroundX(-M_PI / 2.0)); + + if (Environment::violaCurrentAnimation == 0) { gameObjects.violaIdleModelMutable.AssignFrom(gameObjects.violaIdleModel.mesh); @@ -130,11 +137,14 @@ void RenderSystem::drawWorld(GameObjectManager& gameObjects) { renderer.LoadIdentity(); renderer.TranslateMatrix({ 0,0, -100 * Environment::zoom }); - float t = 0.3; - renderer.RotateMatrix(QuatFromRotateAroundX(t * M_PI / 2.0)); + renderer.RotateMatrix(QuatFromRotateAroundX(Environment::cameraAlpha)); + renderer.RotateMatrix(QuatFromRotateAroundY(Environment::cameraPhi)); renderer.TranslateMatrix(Environment::cameraShift); + //Go a little bit up to make camera at the position of Viola + renderer.TranslateMatrix({ 0, Environment::cameraDefaultVerticalShift, 0 }); + // Draw active objects for (const auto& ao : gameObjects.activeObjects) { renderer.PushMatrix(); diff --git a/ZeptoLabTest1.vcxproj b/ZeptoLabTest1.vcxproj index c086b41..a764aae 100755 --- a/ZeptoLabTest1.vcxproj +++ b/ZeptoLabTest1.vcxproj @@ -117,6 +117,7 @@ _DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true C:\Work\SDL2-2.28.3\include;C:\Work\Projects\lpng1645\build\install\include;C:\Work\OpenAL 1.1 SDK\include;C:\Work\Projects\libogg\include;C:\Work\Projects\vorbis\include + stdcpp17 Console @@ -158,6 +159,7 @@ + @@ -180,6 +182,7 @@ + diff --git a/ZeptoLabTest1.vcxproj.filters b/ZeptoLabTest1.vcxproj.filters index 14f9358..d1b2895 100755 --- a/ZeptoLabTest1.vcxproj.filters +++ b/ZeptoLabTest1.vcxproj.filters @@ -72,6 +72,9 @@ Source Files + + Source Files + @@ -134,5 +137,8 @@ Header Files + + Header Files + \ No newline at end of file