From 666bfc1b377783cae57e4fac793bbd8db2b62d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D1=8C=D0=B1=D0=B5=D1=80=D1=82=20=D0=93=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=B5=D0=B2?= Date: Sat, 1 Mar 2025 19:16:26 +0600 Subject: [PATCH 1/5] added sh file for start in linux --- GameWorld.cpp | 0 GameWorld.h | 0 InputManager.cpp | 0 InputManager.h | 0 start.sh | 7 +++++++ 5 files changed, 7 insertions(+) create mode 100644 GameWorld.cpp create mode 100644 GameWorld.h create mode 100644 InputManager.cpp create mode 100644 InputManager.h create mode 100755 start.sh diff --git a/GameWorld.cpp b/GameWorld.cpp new file mode 100644 index 0000000..e69de29 diff --git a/GameWorld.h b/GameWorld.h new file mode 100644 index 0000000..e69de29 diff --git a/InputManager.cpp b/InputManager.cpp new file mode 100644 index 0000000..e69de29 diff --git a/InputManager.h b/InputManager.h new file mode 100644 index 0000000..e69de29 diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..7b779b7 --- /dev/null +++ b/start.sh @@ -0,0 +1,7 @@ +g++ Game.cpp main.cpp Math.cpp OpenGlExtensions.cpp Physics.cpp Renderer.cpp ShaderManager.cpp TextureManager.cpp Utils.cpp BoneAnimatedModel.cpp ObjLoader.cpp cmakeaudioplayer/src/AudioPlayer.cpp TextModel.cpp Inventory.cpp -o sdl_app -O2 -std=c++17 \ +-I cmakeaudioplayer/include \ +$(pkg-config --cflags --libs sdl2 gl) \ +$(pkg-config --cflags --libs vorbis vorbisfile ogg) \ +-lopenal + +./sdl_app \ No newline at end of file From e2af375aa021b78a5f8764e980969f9f66da3f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D1=8C=D0=B1=D0=B5=D1=80=D1=82=20=D0=93=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=B5=D0=B2?= Date: Sat, 1 Mar 2025 19:18:42 +0600 Subject: [PATCH 2/5] starting refactoring main.cpp --- GameWorld.cpp | 23 +++++++++++++++++++++++ GameWorld.h | 18 ++++++++++++++++++ InputManager.cpp | 27 +++++++++++++++++++++++++++ InputManager.h | 17 +++++++++++++++++ main.cpp | 8 ++++++-- 5 files changed, 91 insertions(+), 2 deletions(-) diff --git a/GameWorld.cpp b/GameWorld.cpp index e69de29..352154d 100644 --- a/GameWorld.cpp +++ b/GameWorld.cpp @@ -0,0 +1,23 @@ +#include "GameWorld.h" +#include "InputManager.h" + +namespace ZL { + GameWorld& GameWorld::getInstance() { + static GameWorld instance; + return instance; + } + + void GameWorld::update(float deltaTime) { + auto& input = InputManager::getInstance(); + + // Update game state based on input + if (input.isKeyPressed(SDL_SCANCODE_W)) { + // Move forward + } + // ... handle other game logic ... + } + + void GameWorld::addObject(const Vector3f& position) { + mGameObjects.push_back(position); + } +} diff --git a/GameWorld.h b/GameWorld.h index e69de29..475acd9 100644 --- a/GameWorld.h +++ b/GameWorld.h @@ -0,0 +1,18 @@ +#pragma once +#include "Vector3f.h" +#include +#include + +namespace ZL { + class GameWorld { + public: + static GameWorld& getInstance(); + void update(float deltaTime); + void addObject(const Vector3f& position); + // ...other game world related methods... + + private: + GameWorld() = default; + std::vector mGameObjects; + }; +} diff --git a/InputManager.cpp b/InputManager.cpp index e69de29..0addbde 100644 --- a/InputManager.cpp +++ b/InputManager.cpp @@ -0,0 +1,27 @@ +#include "InputManager.h" + +namespace ZL { + InputManager& InputManager::getInstance() { + static InputManager instance; + return instance; + } + + void InputManager::processInput() { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + mShouldQuit = true; + } + else if (event.type == SDL_KEYDOWN) { + mKeys[event.key.keysym.scancode] = true; + } + else if (event.type == SDL_KEYUP) { + mKeys[event.key.keysym.scancode] = false; + } + } + } + + bool InputManager::isKeyPressed(SDL_Scancode key) const { + return mKeys[key]; + } +} diff --git a/InputManager.h b/InputManager.h index e69de29..035ff06 100644 --- a/InputManager.h +++ b/InputManager.h @@ -0,0 +1,17 @@ +#pragma once +#include + +namespace ZL { + class InputManager { + public: + static InputManager& getInstance(); + void processInput(); + bool isKeyPressed(SDL_Scancode key) const; + bool shouldQuit() const { return mShouldQuit; } + + private: + InputManager() = default; + bool mKeys[SDL_NUM_SCANCODES] = {false}; + bool mShouldQuit = false; + }; +} diff --git a/main.cpp b/main.cpp index 89566be..6de058a 100755 --- a/main.cpp +++ b/main.cpp @@ -413,7 +413,9 @@ namespace ZL // std::cout << "Before removal:\n"; ZL::PrintInventory(); - + 21s  +    ~/g/ZeptoLabTest1    Albert ⇣2 +5  git commit -m "added sh file for start in linux" + [Albert 666bfc1] added sh file for start in linux // Удаляем "Cone" из инвентаря // ZL::RemoveItemFromInventory("Cone"); @@ -534,7 +536,9 @@ namespace ZL case SDLK_w: Env::upPressed = false; break; - case SDLK_DOWN: + case SDLK_DOWN: 21s  +    ~/g/ZeptoLabTest1    Albert ⇣2 +5  git commit -m "added sh file for start in linux" + [Albert 666bfc1] added sh file for start in linux case SDLK_s: Env::downPressed = false; break; From b6684f7929fdaaefdea214160413f6e35b090867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D1=8C=D0=B1=D0=B5=D1=80=D1=82=20=D0=93=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=B5=D0=B2?= Date: Sat, 1 Mar 2025 19:22:19 +0600 Subject: [PATCH 3/5] refactored --- main.cpp | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index 6de058a..a0fb1c5 100755 --- a/main.cpp +++ b/main.cpp @@ -411,16 +411,7 @@ namespace ZL ZL::AddItemToInventory("RoomCeramics", roomTexturePtr); ZL::AddItemToInventory("Cone", coneTexturePtr); - // std::cout << "Before removal:\n"; ZL::PrintInventory(); - 21s  -    ~/g/ZeptoLabTest1    Albert ⇣2 +5  git commit -m "added sh file for start in linux" - [Albert 666bfc1] added sh file for start in linux - // Удаляем "Cone" из инвентаря - // ZL::RemoveItemFromInventory("Cone"); - - // std::cout << "\nAfter removal:\n"; - // ZL::PrintInventory(); // Initialize audio player and start background music GameObjects::audioPlayer = std::make_unique(); @@ -536,9 +527,7 @@ namespace ZL case SDLK_w: Env::upPressed = false; break; - case SDLK_DOWN: 21s  -    ~/g/ZeptoLabTest1    Albert ⇣2 +5  git commit -m "added sh file for start in linux" - [Albert 666bfc1] added sh file for start in linux + case SDLK_DOWN: case SDLK_s: Env::downPressed = false; break; From dbffcf5f041ef006ce8ce9b7980a094b934947b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D1=8C=D0=B1=D0=B5=D1=80=D1=82=20=D0=93=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=B5=D0=B2?= Date: Sat, 1 Mar 2025 20:53:11 +0600 Subject: [PATCH 4/5] refacored some parts of main.sh --- ActiveObject.h | 17 ++++++++++++ Environment.cpp | 14 ++++++++++ Environment.h | 18 +++++++++++++ Game.h | 39 ++++++++++++++++++++------- GameObjectManager.cpp | 12 +++++++++ GameObjectManager.h | 41 +++++++++++++++++++++++++++++ main.cpp | 61 +++++++++++-------------------------------- start.sh | 14 ++++++---- 8 files changed, 156 insertions(+), 60 deletions(-) create mode 100644 ActiveObject.h create mode 100644 Environment.cpp create mode 100644 Environment.h create mode 100644 GameObjectManager.cpp create mode 100644 GameObjectManager.h diff --git a/ActiveObject.h b/ActiveObject.h new file mode 100644 index 0000000..bc3dc94 --- /dev/null +++ b/ActiveObject.h @@ -0,0 +1,17 @@ +#pragma once +#include "TextureManager.h" +#include "Math.h" +#include + +struct ActiveObject { + std::shared_ptr activeObjectTexturePtr; + ZL::VertexDataStruct activeObjectMesh; + ZL::VertexRenderStruct activeObjectMeshMutable; + + std::shared_ptr activeObjectScreenTexturePtr; + ZL::VertexDataStruct activeObjectScreenMesh; + ZL::VertexRenderStruct activeObjectScreenMeshMutable; + + ZL::Vector3f objectPos; + bool highlighted = false; +}; diff --git a/Environment.cpp b/Environment.cpp new file mode 100644 index 0000000..0bb9b23 --- /dev/null +++ b/Environment.cpp @@ -0,0 +1,14 @@ +#include "Environment.h" + +int Environment::windowHeaderHeight = 0; +int Environment::width = 0; +int Environment::height = 0; +float Environment::zoom = 10.0f; + +bool Environment::leftPressed = false; +bool Environment::rightPressed = false; +bool Environment::upPressed = false; +bool Environment::downPressed = false; + +Vector3f Environment::cameraShift = {0, 0, 0}; +Vector3f Environment::characterPos = {0, 0, 0}; diff --git a/Environment.h b/Environment.h new file mode 100644 index 0000000..f0dc366 --- /dev/null +++ b/Environment.h @@ -0,0 +1,18 @@ +#pragma once +#include "Math.h" + +class Environment { +public: + static int windowHeaderHeight; + static int width; + static int height; + static float zoom; + + static bool leftPressed; + static bool rightPressed; + static bool upPressed; + static bool downPressed; + + static ZL::Vector3f cameraShift; + static ZL::Vector3f characterPos; +}; diff --git a/Game.h b/Game.h index a76b369..66c4804 100755 --- a/Game.h +++ b/Game.h @@ -1,14 +1,35 @@ #pragma once -#include "Math.h" -#include "Physics.h" -#include "TextureManager.h" +#include +#include "GameObjectManager.h" #include "Renderer.h" -#include "AnimatedModel.h" -#include "BoneAnimatedModel.h" -#include +#include "Environment.h" -namespace ZL -{ +class Game { +public: + Game(); + ~Game(); + + void setup(); + void run(); + void update(); + void render(); + + bool shouldExit() const { return exitGameLoop; } -} \ No newline at end of file +private: + void processTickCount(); + void drawScene(); + + SDL_Window* window; + SDL_GLContext glContext; + ZL::Renderer renderer; + GameObjectManager gameObjects; + + bool exitGameLoop; + size_t newTickCount; + size_t lastTickCount; + + static const size_t CONST_TIMER_INTERVAL = 10; + static const size_t CONST_MAX_TIME_INTERVAL = 1000; +}; \ No newline at end of file diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp new file mode 100644 index 0000000..5577188 --- /dev/null +++ b/GameObjectManager.cpp @@ -0,0 +1,12 @@ +#include "GameObjectManager.h" + +const float GameObjectManager::INVENTORY_ICON_SIZE = 32.0f; +const float GameObjectManager::INVENTORY_MARGIN = 10.0f; + +void GameObjectManager::initialize() { + // Implementation coming from main.cpp setup() +} + +void GameObjectManager::update() { + // Implementation coming from main.cpp update() +} diff --git a/GameObjectManager.h b/GameObjectManager.h new file mode 100644 index 0000000..1e56e1f --- /dev/null +++ b/GameObjectManager.h @@ -0,0 +1,41 @@ +#pragma once +#include "TextureManager.h" +#include "BoneAnimatedModel.h" +#include "cmakeaudioplayer/include/AudioPlayer.hpp" +#include +#include +#include "ActiveObject.h" + +class GameObjectManager { +public: + void initialize(); + void update(); + + std::shared_ptr testObjTexturePtr; + std::shared_ptr roomTexturePtr; + std::shared_ptr coneTexturePtr; + + ZL::VertexDataStruct colorCubeMesh; + ZL::VertexRenderStruct colorCubeMeshMutable; + + ZL::VertexDataStruct testObjMesh; + ZL::VertexRenderStruct testObjMeshMutable; + + ZL::BoneSystem bx; + ZL::VertexRenderStruct bxMutable; + + ZL::VertexDataStruct textMesh; + ZL::VertexRenderStruct textMeshMutable; + + ZL::VertexDataStruct coneMesh; + ZL::VertexRenderStruct coneMeshMutable; + + std::vector activeObjects; + std::unique_ptr audioPlayer; + + ZL::VertexDataStruct inventoryIconMesh; + ZL::VertexRenderStruct inventoryIconMeshMutable; + + static const float INVENTORY_ICON_SIZE; + static const float INVENTORY_MARGIN; +}; diff --git a/main.cpp b/main.cpp index a0fb1c5..e13e50b 100755 --- a/main.cpp +++ b/main.cpp @@ -540,57 +540,26 @@ namespace ZL }; -int main(int argc, char* argv[]) -{ +#include "Game.h" - constexpr int CONST_WIDTH = 1280; - constexpr int CONST_HEIGHT = 720; +int main(int argc, char* argv[]) { + constexpr int CONST_WIDTH = 1280; + constexpr int CONST_HEIGHT = 720; - ZL::Env::width = CONST_WIDTH; - ZL::Env::height = CONST_HEIGHT; + Environment::width = CONST_WIDTH; + Environment::height = CONST_HEIGHT; + + Game game; + game.setup(); #ifdef EMSCRIPTEN - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_Renderer* renderer = NULL; - SDL_CreateWindowAndRenderer(CONST_WIDTH, CONST_HEIGHT, SDL_WINDOW_OPENGL, &ZL::window, &renderer); + emscripten_set_main_loop([]() { game.update(); }, 0, 1); #else - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { - SDL_Log("Failed to initialize SDL: %s", SDL_GetError()); - return 1; - } - - - // Use a core profile setup. - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - ZL::window = SDL_CreateWindow("Jumping Bird", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, CONST_WIDTH, CONST_HEIGHT, SDL_WINDOW_OPENGL); -#endif - //todo - ZL::Env::windowHeaderHeight = 0; - - ZL::gl_context = SDL_GL_CreateContext(ZL::window); - - ZL::CheckGlError(); - - ZL::setup(); -#ifdef EMSCRIPTEN - // register update as callback - emscripten_set_main_loop(ZL::update, 0, 1); -#else - while (!ZL::ExitGameLoop) { - - ZL::update(); - SDL_Delay(2); - - } - SDL_GL_DeleteContext(ZL::gl_context); - SDL_DestroyWindow(ZL::window); - SDL_Quit(); - - exit(0); + while (!game.shouldExit()) { + game.update(); + SDL_Delay(2); + } #endif + return 0; } diff --git a/start.sh b/start.sh index 7b779b7..65dd930 100755 --- a/start.sh +++ b/start.sh @@ -1,7 +1,11 @@ -g++ Game.cpp main.cpp Math.cpp OpenGlExtensions.cpp Physics.cpp Renderer.cpp ShaderManager.cpp TextureManager.cpp Utils.cpp BoneAnimatedModel.cpp ObjLoader.cpp cmakeaudioplayer/src/AudioPlayer.cpp TextModel.cpp Inventory.cpp -o sdl_app -O2 -std=c++17 \ --I cmakeaudioplayer/include \ -$(pkg-config --cflags --libs sdl2 gl) \ -$(pkg-config --cflags --libs vorbis vorbisfile ogg) \ --lopenal +g++ Game.cpp main.cpp Math.cpp OpenGlExtensions.cpp Physics.cpp Renderer.cpp \ + ShaderManager.cpp TextureManager.cpp Utils.cpp BoneAnimatedModel.cpp \ + ObjLoader.cpp cmakeaudioplayer/src/AudioPlayer.cpp TextModel.cpp \ + Inventory.cpp Environment.cpp GameObjectManager.cpp \ + -o sdl_app -O2 -std=c++17 \ + -I cmakeaudioplayer/include \ + $(pkg-config --cflags --libs sdl2 gl) \ + $(pkg-config --cflags --libs vorbis vorbisfile ogg) \ + -lopenal ./sdl_app \ No newline at end of file From b1954fd3346ab778fb4e9ec618a80eada3c3e076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D1=8C=D0=B1=D0=B5=D1=80=D1=82=20=D0=93=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=B5=D0=B2?= Date: Sat, 1 Mar 2025 21:13:53 +0600 Subject: [PATCH 5/5] refacored some parts of main.cpp --- Environment.cpp | 4 + Environment.h | 8 +- Game.cpp | 200 +++++++++++++++++++++++++++++++++++++++++- Game.h | 12 ++- GameObjectManager.cpp | 173 +++++++++++++++++++++++++++++++++++- GameObjectManager.h | 10 +++ main.cpp | 9 +- 7 files changed, 405 insertions(+), 11 deletions(-) diff --git a/Environment.cpp b/Environment.cpp index 0bb9b23..31d6782 100644 --- a/Environment.cpp +++ b/Environment.cpp @@ -1,5 +1,7 @@ #include "Environment.h" +namespace ZL { + int Environment::windowHeaderHeight = 0; int Environment::width = 0; int Environment::height = 0; @@ -12,3 +14,5 @@ bool Environment::downPressed = false; Vector3f Environment::cameraShift = {0, 0, 0}; Vector3f Environment::characterPos = {0, 0, 0}; + +} // namespace ZL diff --git a/Environment.h b/Environment.h index f0dc366..30c554d 100644 --- a/Environment.h +++ b/Environment.h @@ -1,6 +1,8 @@ #pragma once #include "Math.h" +namespace ZL { + class Environment { public: static int windowHeaderHeight; @@ -13,6 +15,8 @@ public: static bool upPressed; static bool downPressed; - static ZL::Vector3f cameraShift; - static ZL::Vector3f characterPos; + static Vector3f cameraShift; + static Vector3f characterPos; }; + +} // namespace ZL diff --git a/Game.cpp b/Game.cpp index 4b1f617..655150d 100755 --- a/Game.cpp +++ b/Game.cpp @@ -1,8 +1,206 @@ #include "Game.h" #include "AnimatedModel.h" #include "BoneAnimatedModel.h" +#include "Utils.h" +#include "Inventory.h" // Add this include +#include +#include namespace ZL { -} \ No newline at end of file +void Game::worldToScreenCoordinates(Vector3f objectPos, + Matrix4f projectionModelView, + int screenWidth, int screenHeight, + int& screenX, int& screenY) { + + Vector4f inx = { objectPos.v[0], objectPos.v[1], objectPos.v[2], 1.0f}; + Vector4f clipCoords = MultMatrixVector(projectionModelView, inx); + + float ndcX = clipCoords.v[0] / clipCoords.v[3]; + float ndcY = clipCoords.v[1] / clipCoords.v[3]; + + screenX = (int)((ndcX + 1.0f) * 0.5f * screenWidth); + screenY = (int)((1.0f + ndcY) * 0.5f * screenHeight); +} + +Game::Game() : window(nullptr), glContext(nullptr), exitGameLoop(false), + newTickCount(0), lastTickCount(0) { +} + +Game::~Game() { + if (glContext) { + SDL_GL_DeleteContext(glContext); + } + if (window) { + SDL_DestroyWindow(window); + } + SDL_Quit(); +} + +void Game::setup() { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { + SDL_Log("Failed to initialize SDL: %s", SDL_GetError()); + return; + } + + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + + window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + Environment::width, Environment::height, SDL_WINDOW_OPENGL); + + glContext = SDL_GL_CreateContext(window); + + ZL::BindOpenGlFunctions(); + ZL::CheckGlError(); + + // Initialize renderer + renderer.shaderManager.AddShaderFromFiles("default", "./default.vertex", "./default.fragment"); + renderer.shaderManager.AddShaderFromFiles("defaultColor", "./defaultColor.vertex", "./defaultColor.fragment"); + renderer.InitOpenGL(); + + // Initialize game objects + gameObjects.initialize(); +} + +void Game::drawScene() { + static const std::string defaultShaderName = "default"; + static const std::string vPositionName = "vPosition"; + static const std::string vTexCoordName = "vTexCoord"; + static const std::string textureUniformName = "Texture"; + + glClearColor(0.3f, 0.3f, 0.3f, 1.0f); + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + + glViewport(0, 0, Environment::width, Environment::height); + + renderer.shaderManager.PushShader(defaultShaderName); + renderer.RenderUniform1i(textureUniformName, 0); + + renderer.EnableVertexAttribArray(vPositionName); + renderer.EnableVertexAttribArray(vTexCoordName); + + // 3D Scene rendering + { + renderer.PushPerspectiveProjectionMatrix(1.0 / 1.5, + static_cast(Environment::width) / static_cast(Environment::height), + 50, 10000); + renderer.PushMatrix(); + + renderer.LoadIdentity(); + renderer.TranslateMatrix({ 0,0, -100 * Environment::zoom }); + + float t = 0.3; + renderer.RotateMatrix(QuatFromRotateAroundX(t * M_PI / 2.0)); + + // Draw cone + glBindTexture(GL_TEXTURE_2D, gameObjects.coneTexturePtr->getTexID()); + renderer.DrawVertexRenderStruct(gameObjects.coneMeshMutable); + + renderer.TranslateMatrix(Environment::cameraShift); + + // Draw active objects + for (auto& ao : gameObjects.activeObjects) { + renderer.PushMatrix(); + renderer.TranslateMatrix(ao.objectPos); + glBindTexture(GL_TEXTURE_2D, ao.activeObjectTexturePtr->getTexID()); + renderer.DrawVertexRenderStruct(ao.activeObjectMeshMutable); + renderer.PopMatrix(); + } + + // Draw room + glBindTexture(GL_TEXTURE_2D, gameObjects.roomTexturePtr->getTexID()); + renderer.DrawVertexRenderStruct(gameObjects.textMeshMutable); + + auto latestProjectionModelView = renderer.GetProjectionModelViewMatrix(); + + renderer.PopMatrix(); + renderer.PopProjectionMatrix(); + + // 2D UI rendering + renderer.PushProjectionMatrix(static_cast(Environment::width), + static_cast(Environment::height)); + renderer.PushMatrix(); + renderer.LoadIdentity(); + + // Draw highlighted objects UI + for (auto& ao : gameObjects.activeObjects) { + if (ao.highlighted) { + int screenX, screenY; + worldToScreenCoordinates(ao.objectPos, latestProjectionModelView, + Environment::width, Environment::height, screenX, screenY); + renderer.PushMatrix(); + renderer.TranslateMatrix(Vector3f{screenX + 0.f, screenY + 0.f, 0.0f}); + glBindTexture(GL_TEXTURE_2D, ao.activeObjectScreenTexturePtr->getTexID()); + renderer.DrawVertexRenderStruct(ao.activeObjectScreenMeshMutable); + renderer.PopMatrix(); + } + } + + // Draw inventory + const auto& inventory = ZL::ReturnInventory(); + for (size_t i = 0; i < inventory.size(); ++i) { + renderer.PushMatrix(); + float xPos = Environment::width - gameObjects.INVENTORY_MARGIN - gameObjects.INVENTORY_ICON_SIZE; + float yPos = gameObjects.INVENTORY_MARGIN + i * (gameObjects.INVENTORY_ICON_SIZE + gameObjects.INVENTORY_MARGIN); + renderer.TranslateMatrix(Vector3f{xPos, yPos, 0.0f}); + glBindTexture(GL_TEXTURE_2D, inventory[i].texture->getTexID()); + renderer.DrawVertexRenderStruct(gameObjects.inventoryIconMeshMutable); + renderer.PopMatrix(); + } + + renderer.PopMatrix(); + renderer.PopProjectionMatrix(); + } + + renderer.DisableVertexAttribArray(vPositionName); + renderer.DisableVertexAttribArray(vTexCoordName); + renderer.shaderManager.PopShader(); + + CheckGlError(); +} + +void Game::processTickCount() { + if (lastTickCount == 0) { + lastTickCount = SDL_GetTicks64(); + return; + } + + newTickCount = SDL_GetTicks64(); + if (newTickCount - lastTickCount > CONST_TIMER_INTERVAL) { + size_t delta = (newTickCount - lastTickCount > CONST_MAX_TIME_INTERVAL) ? + CONST_MAX_TIME_INTERVAL : newTickCount - lastTickCount; + + gameObjects.update(); + + lastTickCount = newTickCount; + } +} + +void Game::render() { + SDL_GL_MakeCurrent(window, glContext); + ZL::CheckGlError(); + + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + drawScene(); + processTickCount(); + + SDL_GL_SwapWindow(window); +} + +void Game::update() { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + exitGameLoop = true; + } + gameObjects.handleEvent(event); + } + render(); +} + +} // namespace ZL \ No newline at end of file diff --git a/Game.h b/Game.h index 66c4804..73898d1 100755 --- a/Game.h +++ b/Game.h @@ -5,6 +5,8 @@ #include "Renderer.h" #include "Environment.h" +namespace ZL { + class Game { public: Game(); @@ -20,10 +22,14 @@ public: private: void processTickCount(); void drawScene(); + void worldToScreenCoordinates(Vector3f objectPos, + Matrix4f projectionModelView, + int screenWidth, int screenHeight, + int& screenX, int& screenY); SDL_Window* window; SDL_GLContext glContext; - ZL::Renderer renderer; + Renderer renderer; GameObjectManager gameObjects; bool exitGameLoop; @@ -32,4 +38,6 @@ private: static const size_t CONST_TIMER_INTERVAL = 10; static const size_t CONST_MAX_TIME_INTERVAL = 1000; -}; \ No newline at end of file +}; + +} // namespace ZL \ No newline at end of file diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index 5577188..87426b6 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -1,12 +1,181 @@ #include "GameObjectManager.h" +#include "Environment.h" +#include "ObjLoader.h" +#include "Inventory.h" +#include "TextModel.h" // Add this include for LoadFromTextFile + +namespace ZL { const float GameObjectManager::INVENTORY_ICON_SIZE = 32.0f; const float GameObjectManager::INVENTORY_MARGIN = 10.0f; void GameObjectManager::initialize() { - // Implementation coming from main.cpp setup() + // Load textures + roomTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")); + coneTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./conus.bmp")); + + // Load models + colorCubeMesh = CreateCube3D(5.0); + colorCubeMeshMutable.data = CreateCube3D(5.0); + colorCubeMeshMutable.RefreshVBO(); + + testObjMesh = LoadFromObjFile("./chair_01.obj"); + testObjMesh.Scale(10); + testObjMesh.SwapZandY(); + testObjMeshMutable.data = testObjMesh; + testObjMeshMutable.RefreshVBO(); + + textMesh = ZL::LoadFromTextFile("./mesh001.txt"); // Add ZL:: namespace + coneMesh = ZL::LoadFromTextFile("./cone001.txt"); // Add ZL:: namespace + coneMesh.Scale(200); + + textMeshMutable.AssignFrom(textMesh); + textMeshMutable.RefreshVBO(); + coneMeshMutable.AssignFrom(coneMesh); + coneMeshMutable.RefreshVBO(); + + // Load bone animations + bx.LoadFromFile("mesh_armature_and_animation_data.txt"); + + // Create active object + ActiveObject ao1; + ao1.activeObjectMesh = ZL::LoadFromTextFile("./book001.txt"); // Add ZL:: namespace + ao1.activeObjectMesh.Scale(4); + ao1.activeObjectMeshMutable.AssignFrom(ao1.activeObjectMesh); + ao1.activeObjectMeshMutable.RefreshVBO(); + ao1.objectPos = Vector3f{50, 0, -300}; + ao1.activeObjectTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./book03.bmp")); + ao1.activeObjectScreenTexturePtr = std::make_shared(CreateTextureDataFromBmp24("./aoscreen01.bmp")); + ao1.activeObjectScreenMesh = CreateRect2D({ 0.f, 0.f }, { 64.f, 64.f }, 0.5); + ao1.activeObjectScreenMeshMutable.AssignFrom(ao1.activeObjectScreenMesh); + ao1.activeObjectScreenMeshMutable.RefreshVBO(); + activeObjects.push_back(ao1); + + // Initialize audio + audioPlayer = std::make_unique(); + if (audioPlayer) { + audioPlayer->playMusic("Symphony No.6 (1st movement).ogg"); + } + + // Initialize inventory + inventoryIconMesh = CreateRect2D( + {0.0f, 0.0f}, + {INVENTORY_ICON_SIZE/2, INVENTORY_ICON_SIZE/2}, + 0.5f + ); + inventoryIconMeshMutable.AssignFrom(inventoryIconMesh); + inventoryIconMeshMutable.RefreshVBO(); + + // Add test items to inventory + auto testRoomTexture = std::make_shared(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")); + auto testConeTexture = std::make_shared(CreateTextureDataFromBmp24("./conus.bmp")); + AddItemToInventory("RoomCeramics", testRoomTexture); + AddItemToInventory("Cone", testConeTexture); } void GameObjectManager::update() { - // Implementation coming from main.cpp update() + updateScene(16); // Добавим фиксированный timestep для обновления сцены } + +void GameObjectManager::handleEvent(const SDL_Event& event) { + if (event.type == SDL_MOUSEBUTTONDOWN) { + bx.Interpolate(animationCounter); + animationCounter += 2; + } + else if (event.type == SDL_MOUSEWHEEL) { + static const float zoomstep = 1.0f; + if (event.wheel.y > 0) { + Environment::zoom -= zoomstep; + } + else if (event.wheel.y < 0) { + Environment::zoom += zoomstep; + } + if (Environment::zoom < zoomstep) { + Environment::zoom = zoomstep; + } + } + else if (event.type == SDL_KEYDOWN) { + switch (event.key.keysym.sym) { + case SDLK_LEFT: + case SDLK_a: + Environment::leftPressed = true; + if (audioPlayer) { + audioPlayer->playSound("Звук-Идут-по-земле.ogg"); + } + break; + case SDLK_RIGHT: + case SDLK_d: + Environment::rightPressed = true; + if (audioPlayer) { + audioPlayer->playSound("Звук-Идут-по-земле.ogg"); + } + break; + case SDLK_UP: + case SDLK_w: + Environment::upPressed = true; + if (audioPlayer) { + audioPlayer->playSound("Звук-Идут-по-земле.ogg"); + } + break; + case SDLK_DOWN: + case SDLK_s: + Environment::downPressed = true; + if (audioPlayer) { + audioPlayer->playSound("Звук-Идут-по-земле.ogg"); + } + break; + // ...handle other keys... + } + } + else if (event.type == SDL_KEYUP) { + switch (event.key.keysym.sym) { + case SDLK_LEFT: + case SDLK_a: + Environment::leftPressed = false; + break; + case SDLK_RIGHT: + case SDLK_d: + Environment::rightPressed = false; + break; + case SDLK_UP: + case SDLK_w: + Environment::upPressed = false; + break; + case SDLK_DOWN: + case SDLK_s: + Environment::downPressed = false; + break; + } + } +} + +void GameObjectManager::updateScene(size_t ms) { + const float SPEED = 0.1f; + if (Environment::leftPressed) { + Environment::cameraShift.v[0] += SPEED * ms; + } + if (Environment::rightPressed) { + Environment::cameraShift.v[0] -= SPEED * ms; + } + if (Environment::upPressed) { + Environment::cameraShift.v[2] += SPEED * ms; + } + if (Environment::downPressed) { + Environment::cameraShift.v[2] -= SPEED * ms; + } + + Environment::characterPos.v[0] = -Environment::cameraShift.v[0]; + Environment::characterPos.v[1] = -Environment::cameraShift.v[1]; + Environment::characterPos.v[2] = -Environment::cameraShift.v[2]; + + for (auto& ao : activeObjects) { + float dist = sqrtf( + pow(Environment::characterPos.v[0] - ao.objectPos.v[0], 2) + + pow(Environment::characterPos.v[1] - ao.objectPos.v[1], 2) + + pow(Environment::characterPos.v[2] - ao.objectPos.v[2], 2) + ); + ao.highlighted = (dist < 50.f); + } +} + +} // namespace ZL diff --git a/GameObjectManager.h b/GameObjectManager.h index 1e56e1f..e998d50 100644 --- a/GameObjectManager.h +++ b/GameObjectManager.h @@ -5,11 +5,16 @@ #include #include #include "ActiveObject.h" +#include + +namespace ZL { class GameObjectManager { public: void initialize(); void update(); + void handleEvent(const SDL_Event& event); + void updateScene(size_t ms); std::shared_ptr testObjTexturePtr; std::shared_ptr roomTexturePtr; @@ -38,4 +43,9 @@ public: static const float INVENTORY_ICON_SIZE; static const float INVENTORY_MARGIN; + +private: + int animationCounter = 0; }; + +} // namespace ZL diff --git a/main.cpp b/main.cpp index e13e50b..4aa257e 100755 --- a/main.cpp +++ b/main.cpp @@ -541,19 +541,20 @@ namespace ZL }; #include "Game.h" +#include "Environment.h" int main(int argc, char* argv[]) { constexpr int CONST_WIDTH = 1280; constexpr int CONST_HEIGHT = 720; - Environment::width = CONST_WIDTH; - Environment::height = CONST_HEIGHT; + ZL::Environment::width = CONST_WIDTH; + ZL::Environment::height = CONST_HEIGHT; - Game game; + ZL::Game game; game.setup(); #ifdef EMSCRIPTEN - emscripten_set_main_loop([]() { game.update(); }, 0, 1); + emscripten_set_main_loop([](){ game.update(); }, 0, 1); #else while (!game.shouldExit()) { game.update();