diff --git a/src/game.cpp b/src/game.cpp index 2121617..ae19751 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -54,14 +54,21 @@ void Game::Setup(int w, int h) { m_window = SDL_CreateWindow("Minecraft 1.14.2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, - SDL_WINDOW_OPENGL); + SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); // Create GL context *m_logger << LOGGER_INFO << "Creating OpenGL context" << LOGGER_ENDL; m_glContext = SDL_GL_CreateContext(m_window); - SDL_WarpMouseInWindow(m_window, w / 2, h / 2); - SDL_SetRelativeMouseMode(SDL_TRUE); + if (IsMouseActive) { + + SDL_SetRelativeMouseMode(SDL_TRUE); + + } else { + + SDL_SetRelativeMouseMode(SDL_FALSE); + + } // Set VSYNC swap interval SDL_GL_SetSwapInterval(1); @@ -92,8 +99,8 @@ void Game::Setup(int w, int h) { Texture texture; m_world->TextureID = texture.LoadTextures(BlockDictionary->Textures); - for (int x = 0; x < 2; x++) - for (int y = 0; y < 2; y++) { + for (int x = 0; x < 1; x++) + for (int y = 0; y < 1; y++) { m_world->Chunks.push_back(std::make_shared(x, y)); @@ -111,16 +118,34 @@ void Game::Input(SDL_Event* e) { while (SDL_PollEvent(e)) { - m_activeCamera->HandleMouse(*e); switch (e->type) { case SDL_KEYDOWN: { - if (state[SDL_SCANCODE_ESCAPE]) { + if (e->key.keysym.sym == SDLK_ESCAPE) { - IsDisplayOpen = false; + IsMouseActive = !IsMouseActive; + + if (IsMouseActive) + SDL_SetRelativeMouseMode(SDL_TRUE); + else + SDL_SetRelativeMouseMode(SDL_FALSE); + + } + + break; + + } + + case SDL_WINDOWEVENT: + { + + if (e->window.event == SDL_WINDOWEVENT_RESIZED) { + + m_activeCamera->UpdateProjection(e->window.data1, e->window.data2); + glViewport(0, 0, e->window.data1, e->window.data2); } @@ -139,6 +164,8 @@ void Game::Input(SDL_Event* e) { } + if (IsMouseActive) m_activeCamera->HandleMouse(*e); + } m_activeCamera->MoveCamera(state); diff --git a/src/game.hpp b/src/game.hpp index f415445..18f50ad 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -35,6 +35,7 @@ public: void Run(); bool IsDisplayOpen = false; + bool IsMouseActive = true; private: SDL_Window* m_window = nullptr; diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index 0da4d09..b26eb04 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -10,9 +10,11 @@ Camera::Camera(int w, int h) { eyeVector = {}; viewMatrix = {}; + } void Camera::UpdateView() { + // roll can be removed glm::mat4 matRoll = glm::mat4(1.0f); //identity matrix; glm::mat4 matPitch = glm::mat4(1.0f);//identity matrix @@ -29,21 +31,35 @@ void Camera::UpdateView() { translate = glm::translate(translate, -eyeVector); viewMatrix = rotate * translate; + } glm::mat4 Camera::GetViewMatrix() { + return viewMatrix; + } glm::mat4 Camera::GetProjectionMatrix() { + return projMatrix; + +} + +void Camera::UpdateProjection(int width, int height) { + + projMatrix = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1000.0f); + } glm::vec3 Camera::GetPos() { + return eyeVector; + } void Camera::HandleMouse(SDL_Event e) { + if (e.type != SDL_MOUSEMOTION) return; @@ -54,9 +70,11 @@ void Camera::HandleMouse(SDL_Event e) { glm::vec2 mouseDelta{ mouseDX, mouseDY }; MouseMoved(mouseDelta); + } void Camera::MoveCamera(Uint8* state) { + float dx = 0; float dz = 0; float dy = 0; @@ -105,9 +123,11 @@ void Camera::MoveCamera(Uint8* state) { eyeVector.y += dy * CameraSpeed; // update the view matrix UpdateView(); + } void Camera::MouseMoved(glm::vec2 mouseDelta) { + // note that yaw and pitch must be converted to radians. // this is done in UpdateView() by glm::rotate yaw += MouseSensitivity * (mouseDelta.x/100); @@ -115,5 +135,6 @@ void Camera::MouseMoved(glm::vec2 mouseDelta) { pitch = glm::clamp(pitch, -M_PI/2, M_PI/2); UpdateView(); + } diff --git a/src/renderer/camera.hpp b/src/renderer/camera.hpp index f5131ed..39f41d4 100644 --- a/src/renderer/camera.hpp +++ b/src/renderer/camera.hpp @@ -11,6 +11,8 @@ public: glm::mat4 GetViewMatrix(); glm::mat4 GetProjectionMatrix(); + void UpdateProjection(int width, int height); + glm::vec3 GetPos(); void HandleMouse(SDL_Event e);