From d465e0bc34230ea01951c2606a96af6c163a037e Mon Sep 17 00:00:00 2001 From: Furkan Mudanyali Date: Wed, 31 May 2023 02:56:13 +0300 Subject: [PATCH] add input handling, move vertexstrip struct to graphics, fix broken rendering of the gears --- src/game/game.cpp | 1 + src/graphics/graphics.cpp | 7 +++++-- src/graphics/graphics.h | 11 ++++++++++- src/input/input.cpp | 5 +++++ src/input/input.h | 2 ++ src/scene/gears/gears.cpp | 23 +++++++++++++++++++++-- src/scene/gears/gears.h | 9 ++------- 7 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/game/game.cpp b/src/game/game.cpp index 274fcae..6186fbd 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -18,6 +18,7 @@ void Game::loop() { while (!this->done && !this->scenes.empty()) { this->pWindow->updateDimensions(); this->done = this->pInput->pollEvent(); + this->pInput->pollKeys(); if (this->scenes.top()->destroy) { delete this->scenes.top(); diff --git a/src/graphics/graphics.cpp b/src/graphics/graphics.cpp index 54c1cc5..2dd6b80 100644 --- a/src/graphics/graphics.cpp +++ b/src/graphics/graphics.cpp @@ -102,7 +102,8 @@ void Graphics::enable(int cap) { void Graphics::drawArrays(GLuint& vertexBufObj, int mode, - int count, + int stripCount, + VertexStrip* strips, int attrBindingIdx, int attrCount, int attrOffset, @@ -121,7 +122,9 @@ void Graphics::drawArrays(GLuint& vertexBufObj, glBindVertexBuffer(attrBindingIdx, vertexBufObj, 0, attrCount * attrSize); /* Draw the triangle strips that comprise the gear */ - glDrawArrays(mode, 0, count); + for (int n = 0; n < stripCount; ++n) { + glDrawArrays(mode, strips[n].first, strips[n].count); + } /* Disable the attributes */ for (int i = attrCount - 1; i >= 0; --i) { diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index de86404..b10446c 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -15,6 +15,14 @@ class Game; class Window; +// Struct describing the vertices in triangle strip +using VertexStrip = struct VertexStrip { + // First vertex in the strip + GLint first; + // Number of consecutive verices in the strip after the first + GLint count; +}; + class Graphics { Game* pGame; SDL_GLContext context = nullptr; @@ -38,7 +46,8 @@ class Graphics { static void drawArrays(GLuint& vertexBufObj, int mode, - int count, + int stripCount, + VertexStrip* strips, int attrBindingIdx, int attrCount, int attrOffset, diff --git a/src/input/input.cpp b/src/input/input.cpp index 00bb96b..2cd0754 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -3,6 +3,7 @@ * All rights reserved */ +#include #include #include @@ -21,4 +22,8 @@ bool Input::pollEvent() { event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(this->game->pWindow->window); +} + +void Input::pollKeys() { + this->keys = SDL_GetKeyboardState(nullptr); } \ No newline at end of file diff --git a/src/input/input.h b/src/input/input.h index eb6377c..f9cd396 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -18,7 +18,9 @@ class Input { public: Input(Game*); ~Input() = default; + const Uint8* keys; bool pollEvent(); + void pollKeys(); }; #endif \ No newline at end of file diff --git a/src/scene/gears/gears.cpp b/src/scene/gears/gears.cpp index 6ebb877..369e99c 100644 --- a/src/scene/gears/gears.cpp +++ b/src/scene/gears/gears.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include #include @@ -266,8 +268,8 @@ void GearsScene::drawGear(Gear* gear, Graphics::setUniformValue((GLint)materialColorLoc, color); // Draw the triangle strips that comprise the gear - Graphics::drawArrays(gear->vertexBufObj, GL_TRIANGLE_STRIP, gear->nVertices, - 0, 2, 0, sizeof(GLfloat) * 3); + Graphics::drawArrays(gear->vertexBufObj, GL_TRIANGLE_STRIP, gear->nStrips, + gear->strips, 0, 2, 0, sizeof(GLfloat) * 3); } void GearsScene::reshape() { @@ -317,6 +319,22 @@ void GearsScene::idle() { } } +void GearsScene::keypress() { + static const Uint8* keys = pGame->pInput->keys; + if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) { + viewRotation[1] += 5.0F; + } + if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) { + viewRotation[1] -= 5.0F; + } + if (keys[SDL_SCANCODE_UP] || keys[SDL_SCANCODE_W]) { + viewRotation[0] += 5.0F; + } + if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S]) { + viewRotation[0] -= 5.0F; + } +} + void GearsScene::draw() { const static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0}; const static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0}; @@ -344,5 +362,6 @@ void GearsScene::draw() { blue); reshape(); + keypress(); idle(); } \ No newline at end of file diff --git a/src/scene/gears/gears.h b/src/scene/gears/gears.h index 0a0d06f..2cdd54c 100644 --- a/src/scene/gears/gears.h +++ b/src/scene/gears/gears.h @@ -11,13 +11,7 @@ #define VERTICES_PER_TOOTH 34 #define GEAR_VERTEX_STRIDE 6 -// Class describing the vertices in triangle strip -using VertexStrip = struct { - // First vertex in the strip - GLint first; - // Number of consecutive verices in the strip after the first - GLint count; -}; +struct VertexStrip; // Each vertex consists of GEAR_VERTEX_STRIDE GLfloat attributes using GearVertex = GLfloat[GEAR_VERTEX_STRIDE]; @@ -75,6 +69,7 @@ class GearsScene : public Scene { void idle(); void reshape(); + void keypress(); // The direction of the directional light for the scene const GLfloat lightSourcePos[4] = {5.0, 5.0, 10.0, 1.0};