Change mouse logic

This commit is contained in:
Vladislav Khorev 2025-03-02 12:13:04 +03:00
parent a8a3b04b09
commit aac2e84ad6
8 changed files with 134 additions and 13 deletions

View File

@ -1,5 +1,10 @@
#include "Environment.h"
#include "RenderSystem.h"
#include "Utils.h"
#include "Inventory.h"
#include <GL/gl.h>
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

View File

@ -1,5 +1,9 @@
#pragma once
#include "Math.h"
#ifdef __linux__
#include <SDL2/SDL.h>
#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;
};

View File

@ -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);

3
Game.h
View File

@ -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;

View File

@ -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 {

View File

@ -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();

View File

@ -117,6 +117,7 @@
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>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</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -158,6 +159,7 @@
<ClCompile Include="ObjLoader.cpp" />
<ClCompile Include="OpenGlExtensions.cpp" />
<ClCompile Include="Physics.cpp" />
<ClCompile Include="QuestScripts.cpp" />
<ClCompile Include="Renderer.cpp" />
<ClCompile Include="RenderSystem.cpp" />
<ClCompile Include="ShaderManager.cpp" />
@ -180,6 +182,7 @@
<ClInclude Include="ObjLoader.h" />
<ClInclude Include="OpenGlExtensions.h" />
<ClInclude Include="Physics.h" />
<ClInclude Include="QuestScripts.h" />
<ClInclude Include="Renderer.h" />
<ClInclude Include="RenderSystem.h" />
<ClInclude Include="ShaderManager.h" />

View File

@ -72,6 +72,9 @@
<ClCompile Include="RenderSystem.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="QuestScripts.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="TextureManager.h">
@ -134,5 +137,8 @@
<ClInclude Include="RenderSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="QuestScripts.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>