From 2d0c7ec166c660fe924387fdb916970bc272ee34 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 22:14:33 +0600 Subject: [PATCH] added mouse handling --- GameObjectManager.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++ GameObjectManager.h | 8 ++++++++ RenderSystem.cpp | 4 ++++ RenderSystem.h | 2 ++ 4 files changed, 61 insertions(+) diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index 87426b6..d669ab5 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -147,6 +147,11 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { break; } } + if (event.type == SDL_MOUSEMOTION) { + // Сохраняем позицию мыши для последующей проверки + lastMouseX = event.motion.x; + lastMouseY = event.motion.y; + } } void GameObjectManager::updateScene(size_t ms) { @@ -178,4 +183,46 @@ void GameObjectManager::updateScene(size_t ms) { } } +bool GameObjectManager::isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const { + // Простая проверка попадания точки в квадрат 64x64 вокруг центра объекта + const int objectSize = 32; // Половина размера области выделения + return (screenX >= objectScreenX - objectSize && + screenX <= objectScreenX + objectSize && + screenY >= objectScreenY - objectSize && + screenY <= objectScreenY + objectSize); +} + +void GameObjectManager::checkMouseIntersection(int mouseX, int mouseY, const Matrix4f& projectionModelView) { + for (auto& ao : activeObjects) { + int screenX, screenY; + worldToScreenCoordinates(ao.objectPos, projectionModelView, + Environment::width, Environment::height, screenX, screenY); + + if (isPointInObject(mouseX, mouseY, screenX, screenY)) { + std::cout << "Mouse over object at screen coordinates: " + << screenX << ", " << screenY + << " (world pos: " + << ao.objectPos.v[0] << ", " + << ao.objectPos.v[1] << ", " + << ao.objectPos.v[2] << ")" + << std::endl; + } + } +} + +void GameObjectManager::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); +} + } // namespace ZL diff --git a/GameObjectManager.h b/GameObjectManager.h index e998d50..8f32892 100644 --- a/GameObjectManager.h +++ b/GameObjectManager.h @@ -15,6 +15,7 @@ public: void update(); void handleEvent(const SDL_Event& event); void updateScene(size_t ms); + void checkMouseIntersection(int mouseX, int mouseY, const Matrix4f& projectionModelView); // Добавляем новый метод std::shared_ptr testObjTexturePtr; std::shared_ptr roomTexturePtr; @@ -46,6 +47,13 @@ public: private: int animationCounter = 0; + int lastMouseX = 0; // Добавляем переменные для хранения позиции мыши + int lastMouseY = 0; + bool isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const; + void worldToScreenCoordinates(Vector3f objectPos, // Добавляем метод + Matrix4f projectionModelView, + int screenWidth, int screenHeight, + int& screenX, int& screenY); }; } // namespace ZL diff --git a/RenderSystem.cpp b/RenderSystem.cpp index 8290d5b..aa5c38e 100644 --- a/RenderSystem.cpp +++ b/RenderSystem.cpp @@ -71,6 +71,10 @@ void RenderSystem::drawWorld(const GameObjectManager& gameObjects) { renderer.DrawVertexRenderStruct(gameObjects.textMeshMutable); Matrix4f latestProjectionModelView = renderer.GetProjectionModelViewMatrix(); + + // Проверяем пересечение с мышью после расчета всех матриц + const_cast(gameObjects).checkMouseIntersection( + lastMouseX, lastMouseY, latestProjectionModelView); renderer.PopMatrix(); renderer.PopProjectionMatrix(); diff --git a/RenderSystem.h b/RenderSystem.h index 951bcbd..26d12b7 100644 --- a/RenderSystem.h +++ b/RenderSystem.h @@ -24,6 +24,8 @@ private: Renderer renderer; ShaderManager shaderManager; Matrix4f currentProjectionModelView; // Добавлено для хранения матрицы между drawWorld и drawUI + int lastMouseX = 0; + int lastMouseY = 0; }; } // namespace ZL