Window resizing
This commit is contained in:
43
src/game.cpp
43
src/game.cpp
@@ -54,14 +54,21 @@ void Game::Setup(int w, int h) {
|
|||||||
m_window = SDL_CreateWindow("Minecraft 1.14.2",
|
m_window = SDL_CreateWindow("Minecraft 1.14.2",
|
||||||
SDL_WINDOWPOS_CENTERED,
|
SDL_WINDOWPOS_CENTERED,
|
||||||
SDL_WINDOWPOS_CENTERED, w, h,
|
SDL_WINDOWPOS_CENTERED, w, h,
|
||||||
SDL_WINDOW_OPENGL);
|
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||||
|
|
||||||
// Create GL context
|
// Create GL context
|
||||||
*m_logger << LOGGER_INFO << "Creating OpenGL context" << LOGGER_ENDL;
|
*m_logger << LOGGER_INFO << "Creating OpenGL context" << LOGGER_ENDL;
|
||||||
m_glContext = SDL_GL_CreateContext(m_window);
|
m_glContext = SDL_GL_CreateContext(m_window);
|
||||||
|
|
||||||
SDL_WarpMouseInWindow(m_window, w / 2, h / 2);
|
if (IsMouseActive) {
|
||||||
SDL_SetRelativeMouseMode(SDL_TRUE);
|
|
||||||
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
SDL_SetRelativeMouseMode(SDL_FALSE);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Set VSYNC swap interval
|
// Set VSYNC swap interval
|
||||||
SDL_GL_SetSwapInterval(1);
|
SDL_GL_SetSwapInterval(1);
|
||||||
@@ -92,8 +99,8 @@ void Game::Setup(int w, int h) {
|
|||||||
Texture texture;
|
Texture texture;
|
||||||
m_world->TextureID = texture.LoadTextures(BlockDictionary->Textures);
|
m_world->TextureID = texture.LoadTextures(BlockDictionary->Textures);
|
||||||
|
|
||||||
for (int x = 0; x < 2; x++)
|
for (int x = 0; x < 1; x++)
|
||||||
for (int y = 0; y < 2; y++) {
|
for (int y = 0; y < 1; y++) {
|
||||||
|
|
||||||
m_world->Chunks.push_back(std::make_shared<Chunk>(x, y));
|
m_world->Chunks.push_back(std::make_shared<Chunk>(x, y));
|
||||||
|
|
||||||
@@ -111,16 +118,34 @@ void Game::Input(SDL_Event* e) {
|
|||||||
|
|
||||||
while (SDL_PollEvent(e)) {
|
while (SDL_PollEvent(e)) {
|
||||||
|
|
||||||
m_activeCamera->HandleMouse(*e);
|
|
||||||
|
|
||||||
switch (e->type) {
|
switch (e->type) {
|
||||||
|
|
||||||
case SDL_KEYDOWN:
|
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);
|
m_activeCamera->MoveCamera(state);
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ public:
|
|||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
bool IsDisplayOpen = false;
|
bool IsDisplayOpen = false;
|
||||||
|
bool IsMouseActive = true;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SDL_Window* m_window = nullptr;
|
SDL_Window* m_window = nullptr;
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ Camera::Camera(int w, int h) {
|
|||||||
|
|
||||||
eyeVector = {};
|
eyeVector = {};
|
||||||
viewMatrix = {};
|
viewMatrix = {};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::UpdateView() {
|
void Camera::UpdateView() {
|
||||||
|
|
||||||
// roll can be removed
|
// roll can be removed
|
||||||
glm::mat4 matRoll = glm::mat4(1.0f); //identity matrix;
|
glm::mat4 matRoll = glm::mat4(1.0f); //identity matrix;
|
||||||
glm::mat4 matPitch = 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);
|
translate = glm::translate(translate, -eyeVector);
|
||||||
|
|
||||||
viewMatrix = rotate * translate;
|
viewMatrix = rotate * translate;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 Camera::GetViewMatrix() {
|
glm::mat4 Camera::GetViewMatrix() {
|
||||||
|
|
||||||
return viewMatrix;
|
return viewMatrix;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 Camera::GetProjectionMatrix() {
|
glm::mat4 Camera::GetProjectionMatrix() {
|
||||||
|
|
||||||
return projMatrix;
|
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() {
|
glm::vec3 Camera::GetPos() {
|
||||||
|
|
||||||
return eyeVector;
|
return eyeVector;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::HandleMouse(SDL_Event e) {
|
void Camera::HandleMouse(SDL_Event e) {
|
||||||
|
|
||||||
if (e.type != SDL_MOUSEMOTION)
|
if (e.type != SDL_MOUSEMOTION)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -54,9 +70,11 @@ void Camera::HandleMouse(SDL_Event e) {
|
|||||||
glm::vec2 mouseDelta{ mouseDX, mouseDY };
|
glm::vec2 mouseDelta{ mouseDX, mouseDY };
|
||||||
|
|
||||||
MouseMoved(mouseDelta);
|
MouseMoved(mouseDelta);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::MoveCamera(Uint8* state) {
|
void Camera::MoveCamera(Uint8* state) {
|
||||||
|
|
||||||
float dx = 0;
|
float dx = 0;
|
||||||
float dz = 0;
|
float dz = 0;
|
||||||
float dy = 0;
|
float dy = 0;
|
||||||
@@ -105,9 +123,11 @@ void Camera::MoveCamera(Uint8* state) {
|
|||||||
eyeVector.y += dy * CameraSpeed;
|
eyeVector.y += dy * CameraSpeed;
|
||||||
// update the view matrix
|
// update the view matrix
|
||||||
UpdateView();
|
UpdateView();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::MouseMoved(glm::vec2 mouseDelta) {
|
void Camera::MouseMoved(glm::vec2 mouseDelta) {
|
||||||
|
|
||||||
// note that yaw and pitch must be converted to radians.
|
// note that yaw and pitch must be converted to radians.
|
||||||
// this is done in UpdateView() by glm::rotate
|
// this is done in UpdateView() by glm::rotate
|
||||||
yaw += MouseSensitivity * (mouseDelta.x/100);
|
yaw += MouseSensitivity * (mouseDelta.x/100);
|
||||||
@@ -115,5 +135,6 @@ void Camera::MouseMoved(glm::vec2 mouseDelta) {
|
|||||||
pitch = glm::clamp<float>(pitch, -M_PI/2, M_PI/2);
|
pitch = glm::clamp<float>(pitch, -M_PI/2, M_PI/2);
|
||||||
|
|
||||||
UpdateView();
|
UpdateView();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ public:
|
|||||||
|
|
||||||
glm::mat4 GetViewMatrix();
|
glm::mat4 GetViewMatrix();
|
||||||
glm::mat4 GetProjectionMatrix();
|
glm::mat4 GetProjectionMatrix();
|
||||||
|
void UpdateProjection(int width, int height);
|
||||||
|
|
||||||
glm::vec3 GetPos();
|
glm::vec3 GetPos();
|
||||||
|
|
||||||
void HandleMouse(SDL_Event e);
|
void HandleMouse(SDL_Event e);
|
||||||
|
|||||||
Reference in New Issue
Block a user