From ac31c3bbf564dc09c34fc40569b12fe726b39023 Mon Sep 17 00:00:00 2001 From: maka70vv <25.makarovv@gmail.com> Date: Sun, 2 Mar 2025 13:40:12 +0600 Subject: [PATCH] update rendering aictive objects as unordered_map --- ActiveObject.h | 28 +++++++++------ BoneAnimatedModel.cpp | 6 ++-- GameObjectManager.cpp | 29 +++++++++------- GameObjectManager.h | 1 + RenderSystem.cpp | 80 +++++++++++++++++++++---------------------- RenderSystem.h | 1 + 6 files changed, 78 insertions(+), 67 deletions(-) diff --git a/ActiveObject.h b/ActiveObject.h index 47725eb..ae94cf7 100644 --- a/ActiveObject.h +++ b/ActiveObject.h @@ -1,6 +1,8 @@ #pragma once #include "TextureManager.h" #include "Math.h" +#include + #include namespace ZL { @@ -20,15 +22,17 @@ struct ActiveObject { class ActiveObjectManager { public: + std::unordered_map activeObjectsEntities; + // Добавить или обновить объект в контейнере void addActiveObject(const ActiveObject& object) { - activeObjects[object.name] = object; + activeObjectsEntities[object.name] = object; } // Найти объект по имени (возвращает указатель, nullptr — если не найден) ActiveObject* findByName(const std::string& name) { - auto it = activeObjects.find(name); - if (it != activeObjects.end()) { + auto it = activeObjectsEntities.find(name); + if (it != activeObjectsEntities.end()) { return &it->second; } return nullptr; @@ -36,10 +40,11 @@ class ActiveObjectManager { // Найти все объекты с нужным значением highlighted // (возвращает список указателей на найденные объекты) - std::vector findByHighlighted(bool highlighted) { - std::vector result; - result.reserve(activeObjects.size()); - for (auto& [key, object] : activeObjects) { + // ActiveObject.h + std::vector findByHighlighted(bool highlighted) const { + std::vector result; + result.reserve(activeObjectsEntities.size()); + for (const auto& [key, object] : activeObjectsEntities) { // const auto& if (object.highlighted == highlighted) { result.push_back(&object); } @@ -47,9 +52,10 @@ class ActiveObjectManager { return result; } - private: - // Хранение объектов по ключу name - std::unordered_map activeObjects; - }; + + void removeByName(const std::string& name) { + activeObjectsEntities.erase(name); + } + }; } diff --git a/BoneAnimatedModel.cpp b/BoneAnimatedModel.cpp index a8863d0..f2b52d7 100644 --- a/BoneAnimatedModel.cpp +++ b/BoneAnimatedModel.cpp @@ -279,7 +279,7 @@ namespace ZL { std::getline(f, tempLine); //Group: 'Bone', Weight: 0.9929084181785583 if (std::regex_search(tempLine, match, pattern_bone_weight)) { - // ( ) + // ��������� ����� (��� �������) std::string word = match.str(1); double weight = std::stod(match.str(2)); @@ -527,12 +527,12 @@ namespace ZL if (abs(finalPos.v[0] - originalPos.v[0]) > 1 || abs(finalPos.v[1] - originalPos.v[1]) > 1 || abs(finalPos.v[2] - originalPos.v[2]) > 1) { - std::cout << "Hello!" << std::endl; +// std::cout << "Hello!" << std::endl; } if (!vMoved) { - std::cout << "Hello!" << std::endl; +// std::cout << "Hello!" << std::endl; } mesh.PositionData[i].v[0] = finalPos.v[0]; diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index 1948b38..907c585 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -60,6 +60,7 @@ void GameObjectManager::initialize() { room_1.sound_name = "Symphony No.6 (1st movement).ogg"; room_1.roomLogic = createRoom1Logic(); rooms.push_back(room_1); + aoMgr.addActiveObject(ao1); Room room_2; room_2.roomTexture = std::make_shared(CreateTextureDataFromBmp24("./background.bmp")); @@ -113,14 +114,16 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { switch_room(1); } else if (event.type == SDL_MOUSEBUTTONDOWN) { - for (size_t i = 0; i < activeObjects.size(); ++i) { - auto& ao = activeObjects[i]; - if (ao.highlighted) { - AddItemToInventory(ao.name, ao.activeObjectTexturePtr); - activeObjects.erase(activeObjects.begin() + i); - // Можно выйти из цикла, если объект удален, чтобы избежать ошибок индексации. - break; + const auto highlightedObjects = aoMgr.findByHighlighted(true); + + for (auto* ao : highlightedObjects) { + if (!ao) { + continue; } + + AddItemToInventory(ao->name, ao->activeObjectTexturePtr); + + aoMgr.removeByName(ao->name); } // bx.Interpolate(animationCounter); // animationCounter += 2; @@ -218,16 +221,16 @@ void GameObjectManager::updateScene(size_t ms) { Environment::characterPos.v[1] = -Environment::cameraShift.v[1]; Environment::characterPos.v[2] = -Environment::cameraShift.v[2]; - for (auto& ao : activeObjects) { + for (auto& [key, obj] : aoMgr.activeObjectsEntities) { 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) + pow(Environment::characterPos.v[0] - obj.objectPos.v[0], 2) + + pow(Environment::characterPos.v[1] - obj.objectPos.v[1], 2) + + pow(Environment::characterPos.v[2] - obj.objectPos.v[2], 2) ); - ao.highlighted = (dist < 50.f); - + obj.highlighted = (dist < 50.f); } + if (rooms[current_room_index].roomLogic) { rooms[current_room_index].roomLogic(*this, ms); } diff --git a/GameObjectManager.h b/GameObjectManager.h index fdad22b..1aa019b 100644 --- a/GameObjectManager.h +++ b/GameObjectManager.h @@ -51,6 +51,7 @@ public: static const float INVENTORY_ICON_SIZE; static const float INVENTORY_MARGIN; + ActiveObjectManager aoMgr; private: //int animationCounter = 0; diff --git a/RenderSystem.cpp b/RenderSystem.cpp index dd0cc01..80b5093 100644 --- a/RenderSystem.cpp +++ b/RenderSystem.cpp @@ -32,7 +32,7 @@ void RenderSystem::drawScene(GameObjectManager& gameObjects) { renderer.EnableVertexAttribArray(vTexCoordName); */ drawWorld(gameObjects); - //drawUI(gameObjects); + drawUI(gameObjects); /*renderer.DisableVertexAttribArray(vPositionName); renderer.DisableVertexAttribArray(vTexCoordName); @@ -161,59 +161,59 @@ void RenderSystem::drawWorld(GameObjectManager& gameObjects) { } void RenderSystem::drawUI(const GameObjectManager& gameObjects) { - renderer.PushProjectionMatrix(static_cast(Environment::width), + // Устанавливаем нужный шейдер для UI (например, "default") + renderer.shaderManager.PushShader("default"); + + renderer.PushProjectionMatrix(static_cast(Environment::width), static_cast(Environment::height)); renderer.PushMatrix(); renderer.LoadIdentity(); - // Draw highlighted objects UI - for (const auto& ao : gameObjects.activeObjects) { - if (ao.highlighted) { - if (ao.activeObjectScreenTexturePtr){ - int screenX, screenY; - worldToScreenCoordinates(ao.objectPos, currentProjectionModelView, - 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(); - } else {} - } +// Draw hidden UI + for (const auto* ao : gameObjects.aoMgr.findByHighlighted(true)) { + std::cout << ao -> name << std::endl; + std::cout << "Draw" << std::endl; + if (ao->activeObjectScreenTexturePtr) { + std::cout << "Found activeObjectScreenTexturePtr" << std::endl; + int screenX, screenY; + worldToScreenCoordinates(ao->objectPos, currentProjectionModelView, + Environment::width, Environment::height, screenX, screenY); + renderer.PushMatrix(); + renderer.TranslateMatrix(Vector3f{100, 100, 0}); + glBindTexture(GL_TEXTURE_2D, ao->activeObjectScreenTexturePtr->getTexID()); + renderer.DrawVertexRenderStruct(ao->activeObjectScreenMeshMutable); + renderer.PopMatrix(); + } } -const auto& inventoryMap = ZL::ReturnInventory(); + const auto& inventoryMap = ZL::ReturnInventory(); + int i = 0; + for (const auto& [name, item] : inventoryMap) { + renderer.PushMatrix(); -// Заводим счётчик i, чтобы вычислять позицию иконки -int i = 0; + float xPos = Environment::width + - gameObjects.INVENTORY_MARGIN + - gameObjects.INVENTORY_ICON_SIZE; + float yPos = gameObjects.INVENTORY_MARGIN + + i * (gameObjects.INVENTORY_ICON_SIZE + + gameObjects.INVENTORY_MARGIN); -// Итерируемся по всем предметам, -for (const auto& [name, item] : inventoryMap) { - 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}); - - // item.texture->getTexID() – сам текстурный ID - glBindTexture(GL_TEXTURE_2D, item.texture->getTexID()); - renderer.DrawVertexRenderStruct(gameObjects.inventoryIconMeshMutable); - - renderer.PopMatrix(); - - i++; -} + renderer.TranslateMatrix(Vector3f{xPos, yPos, 0.0f}); + glBindTexture(GL_TEXTURE_2D, item.texture->getTexID()); + renderer.DrawVertexRenderStruct(gameObjects.inventoryIconMeshMutable); + renderer.PopMatrix(); + i++; + } renderer.PopMatrix(); renderer.PopProjectionMatrix(); + + // Снимаем шейдер, тем самым балансируем стек + renderer.shaderManager.PopShader(); } + void RenderSystem::worldToScreenCoordinates(Vector3f objectPos, Matrix4f projectionModelView, int screenWidth, int screenHeight, diff --git a/RenderSystem.h b/RenderSystem.h index 02ad0d5..5729320 100644 --- a/RenderSystem.h +++ b/RenderSystem.h @@ -1,5 +1,6 @@ #pragma once #include "Renderer.h" +#include "ActiveObject.h" #include "GameObjectManager.h" #include