From 1fd1829076c6a86ad66dd3de9591324a5542cab2 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Wed, 9 Oct 2019 10:03:31 +0100 Subject: [PATCH] cleaned some stuff up a little --- src/game.cpp | 6 ++++-- src/renderer/camera.cpp | 28 +++++++--------------------- src/renderer/camera.hpp | 2 +- src/renderer/renderer.cpp | 3 +++ src/renderer/texture.cpp | 3 ++- src/world/block.cpp | 13 ------------- src/world/block.hpp | 10 ---------- 7 files changed, 17 insertions(+), 48 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 3d418b8..ecb89ae 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -101,17 +101,19 @@ void Game::Setup(int w, int h) { void Game::Input(SDL_Event* e) { + Uint8* state = (Uint8*)SDL_GetKeyboardState(NULL); + while (SDL_PollEvent(e)) { m_activeCamera->HandleMouse(*e); if (e->type == SDL_KEYDOWN) - if (SDL_GetKeyboardState(NULL)[SDL_SCANCODE_ESCAPE]) + if (state[SDL_SCANCODE_ESCAPE]) IsDisplayOpen = false; if (e->type == SDL_QUIT) IsDisplayOpen = false; } - m_activeCamera->MoveCamera(); + m_activeCamera->MoveCamera(state); } diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index 93e60c3..973cc70 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -49,13 +49,11 @@ void Camera::HandleMouse(SDL_Event e) { MouseMoved(mouseDelta); } -void Camera::MoveCamera() { +void Camera::MoveCamera(Uint8* state) { float dx = 0; float dz = 0; float dy = 0; - const Uint8* state = SDL_GetKeyboardState(NULL); - // Rotate by camera direction glm::mat2 rotate { cos(yaw), -sin(yaw), @@ -65,46 +63,34 @@ void Camera::MoveCamera() { glm::vec2 f(0.0, 1.0); f = f * rotate; - if (state[SDL_SCANCODE_W]) - { + if (state[SDL_SCANCODE_W]) { dz -= f.y; dx -= f.x; } - if (state[SDL_SCANCODE_S]) - { + if (state[SDL_SCANCODE_S]) { dz += f.y; dx += f.x; } - if (state[SDL_SCANCODE_A]) - { + if (state[SDL_SCANCODE_A]) { dz += f.x; dx += -f.y; } - if (state[SDL_SCANCODE_D]) - { + if (state[SDL_SCANCODE_D]) { dz -= f.x; dx -= -f.y; } - if (state[SDL_SCANCODE_SPACE]) - { + if (state[SDL_SCANCODE_SPACE]) { dy += 1; } - if (state[SDL_SCANCODE_LSHIFT]) - { + if (state[SDL_SCANCODE_LSHIFT]) { dy -= 1; } - // if (state[SDL_SCANCODE_Z]) - - // if (state[SDL_SCANCODE_LSHIFT]) // get current view matrix glm::mat4 mat = GetViewMatrix(); glm::vec3 forward(mat[0][2], mat[1][2], mat[2][2]); glm::vec3 strafe(mat[0][0], mat[1][0], mat[2][0]); - - - // forward vector must be negative to look forward. // read :http://in2gpu.com/2015/05/17/view-matrix/ eyeVector.x += dx * CameraSpeed; diff --git a/src/renderer/camera.hpp b/src/renderer/camera.hpp index 3e38883..5e8d28b 100644 --- a/src/renderer/camera.hpp +++ b/src/renderer/camera.hpp @@ -12,7 +12,7 @@ public: glm::vec3 GetPos(); void HandleMouse(SDL_Event e); - void MoveCamera(); + void MoveCamera(Uint8* state); void MouseMoved(glm::vec2 mouseDelta); float MouseSensitivity = 0.1f; diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index de7235d..63cb9ef 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -12,8 +12,11 @@ Renderer::Renderer() { void Renderer::Render(std::shared_ptr world, std::shared_ptr camera) { glBindTexture(GL_TEXTURE_2D_ARRAY, world->TextureID); + for (int i = 0; i < world->Faces.size(); i++) { + world->Faces[i]->Render(camera, world->Shaders["Basic"]); + } } diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp index f7ad280..1282167 100644 --- a/src/renderer/texture.cpp +++ b/src/renderer/texture.cpp @@ -28,11 +28,12 @@ GLuint Texture::LoadTextures(std::vector> textures) int cR = 0; unsigned char* texture = stbi_load(path.c_str(), &xR, &yR, &cR, STBI_rgb_alpha); - logger << LOGGER_INFO << "Texture at '" << path << "' Loaded..." << LOGGER_ENDL; memcpy(texels + (i * x * y * 4), texture, x * y * 4); stbi_image_free(texture); + logger << LOGGER_INFO << "Texture at '" << path << "' Loaded..." << LOGGER_ENDL; + } diff --git a/src/world/block.cpp b/src/world/block.cpp index dbd7bfa..cbbcb27 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -3,16 +3,3 @@ #include "../config.hpp" // Texture winding order - top, bottom, left, right, front, back - -// std::vector> TextureIdsAndPaths { -// {1, "dirt.png"}, -// {2, "grass_side.png"}, -// {3, "grass_top.png"} -// }; - - - -std::vector> BlockAtlas() { - - -} diff --git a/src/world/block.hpp b/src/world/block.hpp index 38fe7dd..b4ab8ac 100644 --- a/src/world/block.hpp +++ b/src/world/block.hpp @@ -3,20 +3,10 @@ #include "../common.hpp" -struct Block { - std::string Name; - int id; - -public: - Block(); -}; - static std::vector> TextureIdsAndPaths { {0, "dirt.png"}, {1, "grass_side.png"}, {2, "grass_top.png"} }; - - #endif