From 9ffdf66bd0f6d0118d5bec20f9adc0c2698694e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Nie=C3=9Fner?= Date: Sat, 26 May 2018 11:14:12 +0200 Subject: [PATCH] Formatted classes --- src/Assets.h | 11 +- src/BitmapFont.cpp | 61 +++++----- src/BitmapFont.h | 10 +- src/Camera.cpp | 36 +++--- src/Camera.h | 9 +- src/DarkRP2D.cpp | 108 +++++++++-------- src/DarkRP2D.h | 16 ++- src/DesktopLauncher.cpp | 101 +++++++++------- src/DesktopLauncher.h | 8 +- src/Mesh.cpp | 31 +++-- src/Mesh.h | 18 +-- src/Renderer.cpp | 13 +- src/Renderer.h | 11 +- src/Shader.cpp | 45 ++++--- src/Shader.h | 36 +++--- src/Texture.cpp | 11 +- src/Texture.h | 10 +- src/Utility.cpp | 184 ++++++++++++++++------------- src/Utility.h | 66 ++++++----- src/entities/components/Position.h | 3 +- src/input/Command.cpp | 15 ++- src/input/Command.h | 43 ++++--- src/input/InputHandler.cpp | 65 +++++----- src/input/InputHandler.h | 19 +-- src/screens/GameScreen.cpp | 82 ++++++------- src/screens/GameScreen.h | 15 +-- src/screens/Screen.h | 8 +- 27 files changed, 574 insertions(+), 461 deletions(-) diff --git a/src/Assets.h b/src/Assets.h index 3760725..c2d537c 100755 --- a/src/Assets.h +++ b/src/Assets.h @@ -9,15 +9,16 @@ #include "Texture.h" #include "Shader.h" -class Assets { - private: +class Assets +{ + private: std::map textures; std::map shaders; Texture2D loadTextureFromFile(const GLchar *file, GLboolean alpha); - Shader loadShaderFromFile(const GLchar *vShaderFile,const GLchar *fShaderFile, const GLchar *gShaderFile=nullptr); + Shader loadShaderFromFile(const GLchar *vShaderFile, const GLchar *fShaderFile, const GLchar *gShaderFile = nullptr); - public: + public: Assets() {} ~Assets(); @@ -25,8 +26,6 @@ class Assets { Shader getShader(std::string name); Texture2D loadTexture(const GLchar *file, GLboolean alpha, std::string name); Texture2D getTexture(std::string name); - - }; #endif diff --git a/src/BitmapFont.cpp b/src/BitmapFont.cpp index ad46c8b..ab25db7 100644 --- a/src/BitmapFont.cpp +++ b/src/BitmapFont.cpp @@ -6,20 +6,25 @@ #include -BitmapFont::BitmapFont(std::string fontLocation) { +BitmapFont::BitmapFont(std::string fontLocation) +{ FT_Library ft; - if(FT_Init_FreeType(&ft)) { + if (FT_Init_FreeType(&ft)) + { std::cout << "ERROR::FreeType: Could not init FreeType Library" << std::endl; } FT_Face face; - if(FT_New_Face(ft, fontLocation.c_str(), 0, &face)){ + if (FT_New_Face(ft, fontLocation.c_str(), 0, &face)) + { std::cout << "ERROR::FreeType: Failed to load font" << std::endl; } FT_Set_Pixel_Sizes(face, 0, 48); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //Disable byte-alignment restriction - for(GLubyte c = 0; c < 128; c++) { + for (GLubyte c = 0; c < 128; c++) + { //Map bitmaps to chars - if(FT_Load_Char(face, c, FT_LOAD_RENDER)) { + if (FT_Load_Char(face, c, FT_LOAD_RENDER)) + { std::cout << "ERROR::FreeType: Could not load Glyph: " << c << std::endl; continue; } @@ -27,27 +32,25 @@ BitmapFont::BitmapFont(std::string fontLocation) { glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D( - GL_TEXTURE_2D, - 0, - GL_RED, - face->glyph->bitmap.width, - face->glyph->bitmap.rows, - 0, - GL_RED, - GL_UNSIGNED_BYTE, - face->glyph->bitmap.buffer - ); + GL_TEXTURE_2D, + 0, + GL_RED, + face->glyph->bitmap.width, + face->glyph->bitmap.rows, + 0, + GL_RED, + GL_UNSIGNED_BYTE, + face->glyph->bitmap.buffer); // Set texture options glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); Character character = { texture, glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top), - face->glyph->advance.x - }; + face->glyph->advance.x}; characters.insert(std::pair(c, character)); } glPixelStorei(GL_UNPACK_ALIGNMENT, 4); //Reset byte-alignment restriction @@ -57,7 +60,7 @@ BitmapFont::BitmapFont(std::string fontLocation) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - projection = glm::ortho(0.0f,800.0f,0.0f,600.0f); + projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f); glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); @@ -70,17 +73,18 @@ BitmapFont::BitmapFont(std::string fontLocation) { glBindVertexArray(0); } -void BitmapFont::drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color) { +void BitmapFont::drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color) +{ s.Use(); s.SetVector3f("textColor", color, true); - s.SetMatrix4("projection",projection,true); + s.SetMatrix4("projection", projection, true); glActiveTexture(GL_TEXTURE0); glBindVertexArray(VAO); // Iterate through all characters std::string::const_iterator c; - for (c = text.begin(); c != text.end(); c++) + for (c = text.begin(); c != text.end(); c++) { Character ch = characters[*c]; @@ -91,14 +95,13 @@ void BitmapFont::drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLf GLfloat h = ch.size.y * scale; // Update VBO for each character GLfloat vertices[6][4] = { - { xpos, ypos + h, 0.0, 0.0 }, - { xpos, ypos, 0.0, 1.0 }, - { xpos + w, ypos, 1.0, 1.0 }, + {xpos, ypos + h, 0.0, 0.0}, + {xpos, ypos, 0.0, 1.0}, + {xpos + w, ypos, 1.0, 1.0}, - { xpos, ypos + h, 0.0, 0.0 }, - { xpos + w, ypos, 1.0, 1.0 }, - { xpos + w, ypos + h, 1.0, 0.0 } - }; + {xpos, ypos + h, 0.0, 0.0}, + {xpos + w, ypos, 1.0, 1.0}, + {xpos + w, ypos + h, 1.0, 0.0}}; // Render glyph texture over quad glBindTexture(GL_TEXTURE_2D, ch.texID); // Update content of VBO memory diff --git a/src/BitmapFont.h b/src/BitmapFont.h index d9f801a..0f59b5d 100644 --- a/src/BitmapFont.h +++ b/src/BitmapFont.h @@ -8,19 +8,21 @@ #include "Shader.h" -struct Character { +struct Character +{ GLuint texID; glm::ivec2 size; glm::ivec2 bearing; GLuint advance; }; -class BitmapFont { - public: +class BitmapFont +{ + public: BitmapFont(std::string fontLocation); void drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color); - private: + private: std::map characters; GLuint VAO, VBO; glm::mat4 projection; diff --git a/src/Camera.cpp b/src/Camera.cpp index 19bb422..a746cd2 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -2,52 +2,60 @@ #include -void OrtographicCamera::setPosition(glm::vec2 position) { +void OrtographicCamera::setPosition(glm::vec2 position) +{ this->cameraPos = position; } -glm::vec2 OrtographicCamera::getPosition() { +glm::vec2 OrtographicCamera::getPosition() +{ return this->cameraPos; } -void OrtographicCamera::setZoomLevel(float newZoom) { +void OrtographicCamera::setZoomLevel(float newZoom) +{ this->zoom = newZoom; } -void OrtographicCamera::zoomIn(float targetZoom) { +void OrtographicCamera::zoomIn(float targetZoom) +{ zoom -= targetZoom; } -void OrtographicCamera::translate(glm::vec2 target) { +void OrtographicCamera::translate(glm::vec2 target) +{ cameraPos = cameraPos - target; } -void OrtographicCamera::smoothTranslate(glm::vec2 target) { - +void OrtographicCamera::smoothTranslate(glm::vec2 target) +{ } -glm::mat4 OrtographicCamera::getViewMatrix() { +glm::mat4 OrtographicCamera::getViewMatrix() +{ glm::mat4 view(1); - view = glm::translate(view, glm::vec3(-cameraPos,0.f)); + view = glm::translate(view, glm::vec3(-cameraPos, 0.f)); return view; } -glm::mat4 OrtographicCamera::getProjectionMatrix() { +glm::mat4 OrtographicCamera::getProjectionMatrix() +{ //Dies gibt die Größe des kamera auschnittes an die in die welt zeigt (Frustum) int width = 800; int height = 600; int DPI = 2; // 200% windows settings glm::mat4 projection; - projection = glm::ortho(0.0f, (width/DPI)*this->zoom, 0.0f, (height/DPI)*this->zoom); + projection = glm::ortho(0.0f, (width / DPI) * this->zoom, 0.0f, (height / DPI) * this->zoom); return projection; } -glm::mat4 OrtographicCamera::getCombinedMatrix() { +glm::mat4 OrtographicCamera::getCombinedMatrix() +{ glm::mat4 combined; combined = getProjectionMatrix() * getViewMatrix(); return combined; } -void OrtographicCamera::update() { - +void OrtographicCamera::update() +{ } \ No newline at end of file diff --git a/src/Camera.h b/src/Camera.h index 3e72c20..d451c30 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -3,13 +3,14 @@ #include -class OrtographicCamera { - private: +class OrtographicCamera +{ + private: glm::vec2 cameraPos; float zoom; - public: - OrtographicCamera() : cameraPos(glm::vec2(-5.0f,-5.0f)), zoom(0.05f) {} + public: + OrtographicCamera() : cameraPos(glm::vec2(-5.0f, -5.0f)), zoom(0.05f) {} void setPosition(glm::vec2); glm::vec2 getPosition(); diff --git a/src/DarkRP2D.cpp b/src/DarkRP2D.cpp index 407ecf0..fb4f31f 100644 --- a/src/DarkRP2D.cpp +++ b/src/DarkRP2D.cpp @@ -1,8 +1,8 @@ #include "DarkRP2D.h" #ifndef NDEBUG - #include "imgui.h" - #include "imgui_impl_sdl_gl3.h" +#include "imgui.h" +#include "imgui_impl_sdl_gl3.h" #endif #include @@ -10,51 +10,56 @@ #include "screens/GameScreen.h" #include "Utility.h" -DarkRP2D::~DarkRP2D() { +DarkRP2D::~DarkRP2D() +{ std::cout << "Game got destroyed!" << std::endl; - #ifndef NDEBUG - ImGui_ImplSdlGL3_Shutdown(); - ImGui::DestroyContext(); - #endif +#ifndef NDEBUG + ImGui_ImplSdlGL3_Shutdown(); + ImGui::DestroyContext(); +#endif delete gameScreen; delete assets; delete renderer; } -void DarkRP2D::create () { +void DarkRP2D::create() +{ std::cout << "Game got created!" << std::endl; assets = new Assets(); - assets->loadTexture("police_officer.png",true,"police_officer"); - assets->loadShader("vert.shader","frag.shader",nullptr,"defaultShader"); - assets->loadShader("bitmapvert.shader","bitmapfrag.shader",nullptr,"bitmapShader"); + assets->loadTexture("police_officer.png", true, "police_officer"); + assets->loadShader("vert.shader", "frag.shader", nullptr, "defaultShader"); + assets->loadShader("bitmapvert.shader", "bitmapfrag.shader", nullptr, "bitmapShader"); renderer = new Renderer(); font = new BitmapFont("arial.ttf"); - gameScreen = new GameScreen(renderer,assets); + gameScreen = new GameScreen(renderer, assets); gameScreen->show(); - #ifndef NDEBUG - //Setup IMGUI debugging - ImGui::CreateContext(); - ImGui_ImplSdlGL3_Init(this->gWindow); - ImGui::StyleColorsDark(); - #endif +#ifndef NDEBUG + //Setup IMGUI debugging + ImGui::CreateContext(); + ImGui_ImplSdlGL3_Init(this->gWindow); + ImGui::StyleColorsDark(); +#endif } -void DarkRP2D::loop(unsigned int delta) { +void DarkRP2D::loop(unsigned int delta) +{ static unsigned int accumulator = 0, ups = 0, fps = 0, acc = 0; accumulator += delta; acc += delta; - while(accumulator >= 17) { + while (accumulator >= 17) + { accumulator -= 17; update(delta); ups++; } - if(acc >= 1000) { + if (acc >= 1000) + { std::cout << "Updates per second: " << ups << " Frames per Second: " << fps << std::endl; acc -= 1000; ups = 0; @@ -64,47 +69,50 @@ void DarkRP2D::loop(unsigned int delta) { fps++; } -void DarkRP2D::update(unsigned int delta) { +void DarkRP2D::update(unsigned int delta) +{ gameScreen->update(); } -void DarkRP2D::render(unsigned int delta) { +void DarkRP2D::render(unsigned int delta) +{ renderer->clear(); - #ifndef NDEBUG - ImGui_ImplSdlGL3_NewFrame(gWindow); - #endif - +#ifndef NDEBUG + ImGui_ImplSdlGL3_NewFrame(gWindow); +#endif + //texttest Shader bitmapShader = assets->getShader("bitmapShader"); - font->drawText(bitmapShader, "Hallo du noob", 25.0f, 25.0f, 1.f, glm::vec3(0.5f,0.8f,0.2f)); + font->drawText(bitmapShader, "Hallo du noob", 25.0f, 25.0f, 1.f, glm::vec3(0.5f, 0.8f, 0.2f)); gameScreen->render(); - #ifndef NDEBUG - // 1. Show a simple window. - // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug". - static bool ShowOpenGLLogger = false; - { - ImGui::Separator(); - ImGui::Text("\t\t\t\t\tOpenGL Info"); - ImGui::Text("GLEW Version: %s\n", glewGetString(GLEW_VERSION)); - ImGui::Text(" Version: %s\n", glGetString(GL_VERSION)); - ImGui::Text(" Vendor: %s\n", glGetString(GL_VENDOR)); - ImGui::Text(" Renderer: %s\n", glGetString(GL_RENDERER)); - ImGui::Text(" Shading: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); - ImGui::Separator(); - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); - ImGui::Checkbox("Show OpenGL Logger", &ShowOpenGLLogger); - } - if(ShowOpenGLLogger) - Utility::Debug::openGLLogger.Draw("OpenGL Logger"); +#ifndef NDEBUG + // 1. Show a simple window. + // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug". + static bool ShowOpenGLLogger = false; + { + ImGui::Separator(); + ImGui::Text("\t\t\t\t\tOpenGL Info"); + ImGui::Text("GLEW Version: %s\n", glewGetString(GLEW_VERSION)); + ImGui::Text(" Version: %s\n", glGetString(GL_VERSION)); + ImGui::Text(" Vendor: %s\n", glGetString(GL_VENDOR)); + ImGui::Text(" Renderer: %s\n", glGetString(GL_RENDERER)); + ImGui::Text(" Shading: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); + ImGui::Separator(); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + ImGui::Checkbox("Show OpenGL Logger", &ShowOpenGLLogger); + } + if (ShowOpenGLLogger) + Utility::Debug::openGLLogger.Draw("OpenGL Logger"); - ImGui::Render(); - ImGui_ImplSdlGL3_RenderDrawData(ImGui::GetDrawData()); - #endif + ImGui::Render(); + ImGui_ImplSdlGL3_RenderDrawData(ImGui::GetDrawData()); +#endif SDL_GL_SwapWindow(gWindow); } -void DarkRP2D::resize(int width, int height){ +void DarkRP2D::resize(int width, int height) +{ } \ No newline at end of file diff --git a/src/DarkRP2D.h b/src/DarkRP2D.h index 70b48aa..36cb05a 100644 --- a/src/DarkRP2D.h +++ b/src/DarkRP2D.h @@ -5,31 +5,29 @@ class DarkRP2D; #include #include - #include "Assets.h" #include "Renderer.h" #include "input/InputHandler.h" #include "BitmapFont.h" #include "screens/Screen.h" -class DarkRP2D { - private: - SDL_Window* gWindow; +class DarkRP2D +{ + private: + SDL_Window *gWindow; Assets *assets; - Renderer* renderer; + Renderer *renderer; BitmapFont *font; Screen *gameScreen; - public: - + public: DarkRP2D(SDL_Window *gWindow) : gWindow(gWindow) {} ~DarkRP2D(); - void create (); + void create(); void loop(unsigned int delta); void update(unsigned int delta); void render(unsigned int delta); void resize(int width, int height); - }; #endif diff --git a/src/DesktopLauncher.cpp b/src/DesktopLauncher.cpp index 451dbc5..b9407ca 100755 --- a/src/DesktopLauncher.cpp +++ b/src/DesktopLauncher.cpp @@ -4,23 +4,24 @@ #include #include - #include "Desktoplauncher.h" #include "DarkRP2D.h" #include "Utility.h" using namespace std; -static SDL_Window* gWindow = NULL; +static SDL_Window *gWindow = NULL; static SDL_GLContext glcontext; static bool gameQuit = false; void dispose(); bool initSDL(); -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ //Init SDL_Window, SDL_Surface, SDL_Renderer - if(!initSDL()) { + if (!initSDL()) + { dispose(); return 1; } @@ -29,7 +30,8 @@ int main(int argc, char *argv[]) { DarkRP2D resA(gWindow); resA.create(); unsigned int lastTime = SDL_GetTicks(), currentTime, elapsedTime; - while( !gameQuit ) { + while (!gameQuit) + { currentTime = SDL_GetTicks(); elapsedTime = currentTime - lastTime; lastTime = currentTime; @@ -40,87 +42,102 @@ int main(int argc, char *argv[]) { return 0; } -void Game::exit() { +void Game::exit() +{ gameQuit = true; } -namespace Game { - float DDPI, HDPI,VDPI; +namespace Game +{ +float DDPI, HDPI, VDPI; } -bool initSDL() { +bool initSDL() +{ int result = SetProcessDPIAware(); // NEEDED or SDL_GetDisplayDPI returns wrong numbers cout << "Result: " << result << endl; //Init SDL - if(SDL_Init(SDL_INIT_VIDEO) < 0 ) { + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl; return false; - } + } //Init Window uint32_t WindowFlags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL; - gWindow = SDL_CreateWindow( "SDL TEST", SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED, Game::SCREEN_WIDTH, Game::SCREEN_HEIGHT, WindowFlags); - if( gWindow == NULL) { + gWindow = SDL_CreateWindow("SDL TEST", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Game::SCREEN_WIDTH, Game::SCREEN_HEIGHT, WindowFlags); + if (gWindow == NULL) + { cout << "Window could not be created! SDL_Error: " << SDL_GetError() << endl; return false; - } + } //Read in Display DPI - int returnDisplayDPI = SDL_GetDisplayDPI(SDL_GetWindowDisplayIndex(gWindow),&Game::DDPI,&Game::HDPI,&Game::VDPI); - if(returnDisplayDPI != 0) { - cout << "DisplayDPI cannot be loaded! SDL_ERROR: " << SDL_GetError() << endl; + int returnDisplayDPI = SDL_GetDisplayDPI(SDL_GetWindowDisplayIndex(gWindow), &Game::DDPI, &Game::HDPI, &Game::VDPI); + if (returnDisplayDPI != 0) + { + cout << "DisplayDPI cannot be loaded! SDL_ERROR: " << SDL_GetError() << endl; } cout << "DPI: DDPI: " << Game::DDPI << " HDPI: " << Game::HDPI << " VPDI: " << Game::VDPI << endl; //Mask deprectated functions SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - #ifndef NDEBUG - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); - #endif +#ifndef NDEBUG + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); +#endif //Init OpenGL Renderer SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); glcontext = SDL_GL_CreateContext(gWindow); - if(glcontext == NULL) { + if (glcontext == NULL) + { cout << "Could not create OpenGL Context: " << SDL_GetError() << endl; return false; } //Init GLEW glewExperimental = GL_TRUE; GLenum err = glewInit(); - if(GLEW_OK != err) { + if (GLEW_OK != err) + { cout << "Init of Glew failed: " << glewGetErrorString(err) << endl; return false; } - //Init debug callback function - #ifndef NDEBUG - if (GLEW_ARB_debug_output) { +//Init debug callback function +#ifndef NDEBUG + if (GLEW_ARB_debug_output) + { Utility::printInfo(Utility::GLEW, "Supporting ARB debug output!"); } - if (GLEW_AMD_debug_output) { + if (GLEW_AMD_debug_output) + { Utility::printInfo(Utility::GLEW, "Supporting AMD debug output!"); } - if (GLEW_KHR_debug) { + if (GLEW_KHR_debug) + { Utility::printInfo(Utility::GLEW, "Supporting KHR debug output!"); } - - if(glDebugMessageCallback) { + + if (glDebugMessageCallback) + { glEnable(GL_DEBUG_OUTPUT); Utility::printInfo(Utility::OpenGL, "Register OpenGL debug callback"); - glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); - glDebugMessageCallback(Utility::openglCallbackFunction, nullptr); - GLuint unusedIds = 0; - glDebugMessageControl(GL_DONT_CARE, - GL_DONT_CARE, - GL_DONT_CARE, - 0, - &unusedIds, - true); - } else { + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); + glDebugMessageCallback(Utility::openglCallbackFunction, nullptr); + GLuint unusedIds = 0; + glDebugMessageControl(GL_DONT_CARE, + GL_DONT_CARE, + GL_DONT_CARE, + 0, + &unusedIds, + true); + } + else + { Utility::printWarning(Utility::OpenGL, "glDebugMessageCallback not available"); } - #endif +#endif return true; } -void dispose() { +void dispose() +{ SDL_GL_DeleteContext(glcontext); - SDL_DestroyWindow( gWindow ); + SDL_DestroyWindow(gWindow); SDL_Quit(); } diff --git a/src/DesktopLauncher.h b/src/DesktopLauncher.h index 10b72f1..6759612 100644 --- a/src/DesktopLauncher.h +++ b/src/DesktopLauncher.h @@ -3,7 +3,8 @@ #include -namespace Game { +namespace Game +{ const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; @@ -15,8 +16,7 @@ extern float HDPI; //vertical DPI of the display used while starting extern float VDPI; +void exit(); -void exit(); - -} +} // namespace Game #endif \ No newline at end of file diff --git a/src/Mesh.cpp b/src/Mesh.cpp index b395002..50bac4f 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -3,8 +3,8 @@ #include #include - -Mesh::Mesh(std::vector vertices, std::vector indices, Texture2D texture) { +Mesh::Mesh(std::vector vertices, std::vector indices, Texture2D texture) +{ this->texture = texture; this->vertices = vertices; this->indices = indices; @@ -12,22 +12,23 @@ Mesh::Mesh(std::vector vertices, std::vector indices, Te setupMesh(); } -void Mesh::setupMesh() { +void Mesh::setupMesh() +{ //Create VAO & Bind glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); //Create VBO glGenBuffers(1, &VBO); - glBindBuffer(GL_ARRAY_BUFFER,VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex2D), &vertices[0], GL_STATIC_DRAW); //Create EBO glGenBuffers(1, &EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); - + //Set attriPointer and enable glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) 0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *)offsetof(Vertex2D, texCoord)); @@ -36,21 +37,25 @@ void Mesh::setupMesh() { glBindVertexArray(0); } -Mesh::~Mesh() { +Mesh::~Mesh() +{ std::cout << "Deleting Mesh" << std::endl; - glDeleteBuffers(1,&VBO); - glDeleteBuffers(1,&EBO); - glDeleteBuffers(1,&VAO); + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &EBO); + glDeleteBuffers(1, &VAO); } -unsigned int Mesh::getIndicesCount() const { +unsigned int Mesh::getIndicesCount() const +{ return indices.size(); } -void Mesh::bindVAO() const { +void Mesh::bindVAO() const +{ glBindVertexArray(this->VAO); } -void Mesh::bindTexture() const { +void Mesh::bindTexture() const +{ texture.Bind(); } diff --git a/src/Mesh.h b/src/Mesh.h index 40d9e97..439c82d 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -7,24 +7,26 @@ //Aggregate class -> so following construction is available //Vertex2D{glm::vec2(-0.5f,-0.5f), glm::vec2(0.0f,1.0f)} -struct Vertex2D { +struct Vertex2D +{ glm::vec2 position; glm::vec2 texCoord; }; -class Mesh { - private: +class Mesh +{ + private: unsigned int VBO, VAO, EBO; void setupMesh(); - public: + public: /* Mesh Data */ - std::vector vertices; - std::vector indices; - Texture2D texture; + std::vector vertices; + std::vector indices; + Texture2D texture; - Mesh(std::vector vertices, std::vector indices,Texture2D textures); + Mesh(std::vector vertices, std::vector indices, Texture2D textures); ~Mesh(); unsigned int getIndicesCount() const; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index c65bc41..3c743fa 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1,18 +1,21 @@ #include "Renderer.h" #include -Renderer::Renderer() { - glClearColor(1.f,1.f,1.f,1.f); +Renderer::Renderer() +{ + glClearColor(1.f, 1.f, 1.f, 1.f); } -void Renderer::draw(const Mesh& mesh, Shader& shader) { +void Renderer::draw(const Mesh &mesh, Shader &shader) +{ //Bind texture of mesh mesh.bindTexture(); mesh.bindVAO(); //draw Mesh - glDrawElements(GL_TRIANGLES, mesh.getIndicesCount(), GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, mesh.getIndicesCount(), GL_UNSIGNED_INT, 0); } -void Renderer::clear() const { +void Renderer::clear() const +{ glClear(GL_COLOR_BUFFER_BIT); } \ No newline at end of file diff --git a/src/Renderer.h b/src/Renderer.h index c0b9bea..bd0fd39 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -4,11 +4,12 @@ #include "Mesh.h" #include "Shader.h" -class Renderer { - public: - Renderer(); - void draw(const Mesh& mesh, Shader& shader); - void clear() const; +class Renderer +{ + public: + Renderer(); + void draw(const Mesh &mesh, Shader &shader); + void clear() const; }; #endif \ No newline at end of file diff --git a/src/Shader.cpp b/src/Shader.cpp index e018e07..d7d161f 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -2,12 +2,14 @@ #include -Shader &Shader::Use() { +Shader &Shader::Use() +{ glUseProgram(this->ID); return *this; } -void Shader::Compile(const GLchar* vertexSource, const GLchar* fragmentSource, const GLchar* geometrySource) { +void Shader::Compile(const GLchar *vertexSource, const GLchar *fragmentSource, const GLchar *geometrySource) +{ GLuint sVertex, sFragment, gShader; // Vertex Shader sVertex = glCreateShader(GL_VERTEX_SHADER); @@ -42,54 +44,63 @@ void Shader::Compile(const GLchar* vertexSource, const GLchar* fragmentSource, c glDeleteShader(gShader); } -void Shader::SetFloat(const GLchar *name, GLfloat value, GLboolean useShader) { +void Shader::SetFloat(const GLchar *name, GLfloat value, GLboolean useShader) +{ if (useShader) this->Use(); glUniform1f(glGetUniformLocation(this->ID, name), value); } -void Shader::SetInteger(const GLchar *name, GLint value, GLboolean useShader) { +void Shader::SetInteger(const GLchar *name, GLint value, GLboolean useShader) +{ if (useShader) this->Use(); glUniform1i(glGetUniformLocation(this->ID, name), value); } -void Shader::SetVector2f(const GLchar *name, GLfloat x, GLfloat y, GLboolean useShader) { +void Shader::SetVector2f(const GLchar *name, GLfloat x, GLfloat y, GLboolean useShader) +{ if (useShader) this->Use(); glUniform2f(glGetUniformLocation(this->ID, name), x, y); } -void Shader::SetVector2f(const GLchar *name, const glm::vec2 &value, GLboolean useShader) { +void Shader::SetVector2f(const GLchar *name, const glm::vec2 &value, GLboolean useShader) +{ if (useShader) this->Use(); glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y); } -void Shader::SetVector3f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLboolean useShader) { +void Shader::SetVector3f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLboolean useShader) +{ if (useShader) this->Use(); glUniform3f(glGetUniformLocation(this->ID, name), x, y, z); } -void Shader::SetVector3f(const GLchar *name, const glm::vec3 &value, GLboolean useShader) { +void Shader::SetVector3f(const GLchar *name, const glm::vec3 &value, GLboolean useShader) +{ if (useShader) this->Use(); glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z); } -void Shader::SetVector4f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLboolean useShader) { +void Shader::SetVector4f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLboolean useShader) +{ if (useShader) this->Use(); glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w); } -void Shader::SetVector4f(const GLchar *name, const glm::vec4 &value, GLboolean useShader) { +void Shader::SetVector4f(const GLchar *name, const glm::vec4 &value, GLboolean useShader) +{ if (useShader) this->Use(); glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z, value.w); } -void Shader::SetMatrix4(const GLchar *name, const glm::mat4 &matrix, GLboolean useShader) { +void Shader::SetMatrix4(const GLchar *name, const glm::mat4 &matrix, GLboolean useShader) +{ if (useShader) this->Use(); glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, GL_FALSE, glm::value_ptr(matrix)); } - -void Shader::checkCompileErrors(GLuint object, std::string type) { +void Shader::checkCompileErrors(GLuint object, std::string type) +{ GLint success; GLchar infoLog[1024]; if (type != "PROGRAM") @@ -99,8 +110,8 @@ void Shader::checkCompileErrors(GLuint object, std::string type) { { glGetShaderInfoLog(object, 1024, NULL, infoLog); std::cout << "| Shader Error at Compile-time: Type: " << type << "\n" - << infoLog << "\n -- --------------------------------------------------- -- " - << std::endl; + << infoLog << "\n -- --------------------------------------------------- -- " + << std::endl; } } else @@ -110,8 +121,8 @@ void Shader::checkCompileErrors(GLuint object, std::string type) { { glGetProgramInfoLog(object, 1024, NULL, infoLog); std::cout << "| ERROR::Shader: Link-time error: Type: " << type << "\n" - << infoLog << "\n -- --------------------------------------------------- -- " - << std::endl; + << infoLog << "\n -- --------------------------------------------------- -- " + << std::endl; } } } diff --git a/src/Shader.h b/src/Shader.h index acccffd..e7c5f08 100644 --- a/src/Shader.h +++ b/src/Shader.h @@ -7,34 +7,34 @@ #include #include - // General purpsoe shader object. Compiles from file, generates -// compile/link-time error messages and hosts several utility +// compile/link-time error messages and hosts several utility // functions for easy management. class Shader { -public: + public: // State - GLuint ID; + GLuint ID; // Constructor - Shader() { } + Shader() {} // Sets the current shader as active - Shader &Use(); + Shader &Use(); // Compiles the shader from given source code - void Compile(const GLchar *vertexSource, const GLchar *fragmentSource, const GLchar *geometrySource = nullptr); // Note: geometry source code is optional + void Compile(const GLchar *vertexSource, const GLchar *fragmentSource, const GLchar *geometrySource = nullptr); // Note: geometry source code is optional // Utility functions - void SetFloat (const GLchar *name, GLfloat value, GLboolean useShader = false); - void SetInteger (const GLchar *name, GLint value, GLboolean useShader = false); - void SetVector2f (const GLchar *name, GLfloat x, GLfloat y, GLboolean useShader = false); - void SetVector2f (const GLchar *name, const glm::vec2 &value, GLboolean useShader = false); - void SetVector3f (const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLboolean useShader = false); - void SetVector3f (const GLchar *name, const glm::vec3 &value, GLboolean useShader = false); - void SetVector4f (const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLboolean useShader = false); - void SetVector4f (const GLchar *name, const glm::vec4 &value, GLboolean useShader = false); - void SetMatrix4 (const GLchar *name, const glm::mat4 &matrix, GLboolean useShader = false); -private: + void SetFloat(const GLchar *name, GLfloat value, GLboolean useShader = false); + void SetInteger(const GLchar *name, GLint value, GLboolean useShader = false); + void SetVector2f(const GLchar *name, GLfloat x, GLfloat y, GLboolean useShader = false); + void SetVector2f(const GLchar *name, const glm::vec2 &value, GLboolean useShader = false); + void SetVector3f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLboolean useShader = false); + void SetVector3f(const GLchar *name, const glm::vec3 &value, GLboolean useShader = false); + void SetVector4f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLboolean useShader = false); + void SetVector4f(const GLchar *name, const glm::vec4 &value, GLboolean useShader = false); + void SetMatrix4(const GLchar *name, const glm::mat4 &matrix, GLboolean useShader = false); + + private: // Checks if compilation or linking failed and if so, print the error logs - void checkCompileErrors(GLuint object, std::string type); + void checkCompileErrors(GLuint object, std::string type); }; #endif diff --git a/src/Texture.cpp b/src/Texture.cpp index dee9855..74e975f 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -2,14 +2,14 @@ #include "Texture.h" - Texture2D::Texture2D() - : Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Filter_Min(GL_LINEAR_MIPMAP_NEAREST), Filter_Max(GL_NEAREST) + : Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Filter_Min(GL_LINEAR_MIPMAP_NEAREST), Filter_Max(GL_LINEAR) { glGenTextures(1, &this->ID); } -void Texture2D::Generate(GLuint width, GLuint height, unsigned char* data) { +void Texture2D::Generate(GLuint width, GLuint height, unsigned char *data) +{ this->Width = width; this->Height = height; // Create Texture @@ -21,7 +21,7 @@ void Texture2D::Generate(GLuint width, GLuint height, unsigned char* data) { //float aniso = 0.0f; //glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso); //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); - + // Set Texture wrap and filter modes glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, this->Wrap_S); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, this->Wrap_T); @@ -31,6 +31,7 @@ void Texture2D::Generate(GLuint width, GLuint height, unsigned char* data) { glBindTexture(GL_TEXTURE_2D, 0); } -void Texture2D::Bind() const { +void Texture2D::Bind() const +{ glBindTexture(GL_TEXTURE_2D, this->ID); } diff --git a/src/Texture.h b/src/Texture.h index 2d277b9..377632e 100644 --- a/src/Texture.h +++ b/src/Texture.h @@ -7,23 +7,23 @@ // It also hosts utility functions for easy management. class Texture2D { -public: + public: // Holds the ID of the texture object, used for all texture operations to reference to this particlar texture GLuint ID; // Texture image dimensions GLuint Width, Height; // Width and height of loaded image in pixels // Texture Format GLuint Internal_Format; // Format of texture object - GLuint Image_Format; // Format of loaded image + GLuint Image_Format; // Format of loaded image // Texture configuration - GLuint Wrap_S; // Wrapping mode on S axis - GLuint Wrap_T; // Wrapping mode on T axis + GLuint Wrap_S; // Wrapping mode on S axis + GLuint Wrap_T; // Wrapping mode on T axis GLuint Filter_Min; // Filtering mode if texture pixels < screen pixels GLuint Filter_Max; // Filtering mode if texture pixels > screen pixels // Constructor (sets default texture modes) Texture2D(); // Generates texture from image data - void Generate(GLuint width, GLuint height, unsigned char* data); + void Generate(GLuint width, GLuint height, unsigned char *data); // Binds the texture as the current active GL_TEXTURE_2D texture object void Bind() const; }; diff --git a/src/Utility.cpp b/src/Utility.cpp index 76cd5b1..4332ad7 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -2,87 +2,103 @@ #ifndef NDEBUG - namespace Utility{ - namespace Debug { - ImGuiLogger openGLLogger; - } - } +namespace Utility +{ +namespace Debug +{ +ImGuiLogger openGLLogger; +} +} // namespace Utility - void Utility::Debug::ImGuiLogger::Clear() { - Buf.clear(); - LineOffsets.clear(); - } +void Utility::Debug::ImGuiLogger::Clear() +{ + Buf.clear(); + LineOffsets.clear(); +} - void Utility::Debug::ImGuiLogger::AddLog(const char* fmt, ...) IM_FMTARGS(2) { - int old_size = Buf.size(); - va_list args; - va_start(args, fmt); - Buf.appendfv(fmt, args); - va_end(args); - for (int new_size = Buf.size(); old_size < new_size; old_size++) - if (Buf[old_size] == '\n') - LineOffsets.push_back(old_size); - ScrollToBottom = true; - } +void Utility::Debug::ImGuiLogger::AddLog(const char *fmt, ...) IM_FMTARGS(2) +{ + int old_size = Buf.size(); + va_list args; + va_start(args, fmt); + Buf.appendfv(fmt, args); + va_end(args); + for (int new_size = Buf.size(); old_size < new_size; old_size++) + if (Buf[old_size] == '\n') + LineOffsets.push_back(old_size); + ScrollToBottom = true; +} - void Utility::Debug::ImGuiLogger::Draw(const char* title, bool* p_open) { - ImGui::SetNextWindowSize(ImVec2(500,400), ImGuiCond_FirstUseEver); - ImGui::Begin(title, p_open); - if (ImGui::Button("Clear")) Clear(); - ImGui::SameLine(); - bool copy = ImGui::Button("Copy"); - ImGui::SameLine(); - ImGui::Checkbox("Errors", &ShowError); ImGui::SameLine(); - ImGui::Checkbox("Deprecated Behavior", &ShowDeprecatedBehavior); ImGui::SameLine(); - ImGui::Checkbox("Undefined Behavior", &ShowUndefinedBehavior); ImGui::SameLine(); - ImGui::Checkbox("Portability", &ShowPortability); - ImGui::Checkbox("Performance", &ShowPerformance); ImGui::SameLine(); - ImGui::Checkbox("Marker", &ShowMarker); ImGui::SameLine(); - ImGui::Checkbox("Push Group", &ShowPushGroup); ImGui::SameLine(); - ImGui::Checkbox("Pop Group", &ShowPopGroup); ImGui::SameLine(); - ImGui::Checkbox("Other", &ShowOther); +void Utility::Debug::ImGuiLogger::Draw(const char *title, bool *p_open) +{ + ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver); + ImGui::Begin(title, p_open); + if (ImGui::Button("Clear")) + Clear(); + ImGui::SameLine(); + bool copy = ImGui::Button("Copy"); + ImGui::SameLine(); + ImGui::Checkbox("Errors", &ShowError); + ImGui::SameLine(); + ImGui::Checkbox("Deprecated Behavior", &ShowDeprecatedBehavior); + ImGui::SameLine(); + ImGui::Checkbox("Undefined Behavior", &ShowUndefinedBehavior); + ImGui::SameLine(); + ImGui::Checkbox("Portability", &ShowPortability); + ImGui::Checkbox("Performance", &ShowPerformance); + ImGui::SameLine(); + ImGui::Checkbox("Marker", &ShowMarker); + ImGui::SameLine(); + ImGui::Checkbox("Push Group", &ShowPushGroup); + ImGui::SameLine(); + ImGui::Checkbox("Pop Group", &ShowPopGroup); + ImGui::SameLine(); + ImGui::Checkbox("Other", &ShowOther); - Filter.Draw("Filter", -100.0f); - ImGui::Separator(); - ImGui::BeginChild("scrolling", ImVec2(0,0), false, ImGuiWindowFlags_HorizontalScrollbar); - if (copy) ImGui::LogToClipboard(); + Filter.Draw("Filter", -100.0f); + ImGui::Separator(); + ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); + if (copy) + ImGui::LogToClipboard(); - if (Filter.IsActive()) + if (Filter.IsActive()) + { + const char *buf_begin = Buf.begin(); + const char *line = buf_begin; + for (int line_no = 0; line != NULL; line_no++) { - const char* buf_begin = Buf.begin(); - const char* line = buf_begin; - for (int line_no = 0; line != NULL; line_no++) - { - const char* line_end = (line_no < LineOffsets.Size) ? buf_begin + LineOffsets[line_no] : NULL; - if (Filter.PassFilter(line, line_end)) - ImGui::TextUnformatted(line, line_end); - line = line_end && line_end[1] ? line_end + 1 : NULL; - } + const char *line_end = (line_no < LineOffsets.Size) ? buf_begin + LineOffsets[line_no] : NULL; + if (Filter.PassFilter(line, line_end)) + ImGui::TextUnformatted(line, line_end); + line = line_end && line_end[1] ? line_end + 1 : NULL; } - else - { - ImGui::TextUnformatted(Buf.begin()); - } - - if (ScrollToBottom) - ImGui::SetScrollHere(1.0f); - ScrollToBottom = false; - ImGui::EndChild(); - ImGui::End(); } + else + { + ImGui::TextUnformatted(Buf.begin()); + } + + if (ScrollToBottom) + ImGui::SetScrollHere(1.0f); + ScrollToBottom = false; + ImGui::EndChild(); + ImGui::End(); +} #endif //NDEBUG void APIENTRY Utility::openglCallbackFunction(GLenum source, - GLenum type, - GLuint id, - GLenum severity, - GLsizei length, - const GLchar* message, - const void* userParam){ - + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar *message, + const void *userParam) +{ + std::string _severity; - switch (severity){ + switch (severity) + { case GL_DEBUG_SEVERITY_LOW: _severity = "LOW"; break; @@ -97,48 +113,58 @@ void APIENTRY Utility::openglCallbackFunction(GLenum source, break; } - switch (type) { + switch (type) + { case GL_DEBUG_TYPE_ERROR: - if(Utility::Debug::openGLLogger.ShowError) { + if (Utility::Debug::openGLLogger.ShowError) + { Debug::openGLLogger.AddLog("ERROR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); } break; case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: - if(Utility::Debug::openGLLogger.ShowDeprecatedBehavior) { + if (Utility::Debug::openGLLogger.ShowDeprecatedBehavior) + { Debug::openGLLogger.AddLog("DEPRECATED_BEHAVIOR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); } break; case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: - if(Utility::Debug::openGLLogger.ShowUndefinedBehavior) { + if (Utility::Debug::openGLLogger.ShowUndefinedBehavior) + { Debug::openGLLogger.AddLog("UNDEFINED_BEHAVIOR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); } break; case GL_DEBUG_TYPE_PORTABILITY: - if(Utility::Debug::openGLLogger.ShowPortability) { + if (Utility::Debug::openGLLogger.ShowPortability) + { Debug::openGLLogger.AddLog("PORTATBILITY: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); } break; case GL_DEBUG_TYPE_PERFORMANCE: - if(Utility::Debug::openGLLogger.ShowPerformance) { + if (Utility::Debug::openGLLogger.ShowPerformance) + { Debug::openGLLogger.AddLog("PERFORMANCE: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); } break; case GL_DEBUG_TYPE_OTHER: - if(Utility::Debug::openGLLogger.ShowOther) { + if (Utility::Debug::openGLLogger.ShowOther) + { Debug::openGLLogger.AddLog("OTHER: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str()); } break; } } -static const char* Errornames[] = { "OpenGL", "SDL","GLEW", "Other" }; -void Utility::printWarning(ERROR_TYPE type, std::string msg) { +static const char *Errornames[] = {"OpenGL", "SDL", "GLEW", "Other"}; +void Utility::printWarning(ERROR_TYPE type, std::string msg) +{ std::cout << "Warning[" << Errornames[type] << "]: " << msg << std::endl; } -void Utility::printError(ERROR_TYPE type, std::string msg) { +void Utility::printError(ERROR_TYPE type, std::string msg) +{ std::cout << "Error[" << Errornames[type] << "]: " << msg << std::endl; } -void Utility::printInfo(ERROR_TYPE type, std::string msg) { +void Utility::printInfo(ERROR_TYPE type, std::string msg) +{ std::cout << "Info[" << Errornames[type] << "]: " << msg << std::endl; } \ No newline at end of file diff --git a/src/Utility.h b/src/Utility.h index 390fb8c..c77bf5c 100644 --- a/src/Utility.h +++ b/src/Utility.h @@ -7,13 +7,15 @@ #include #ifndef NDEBUG - #include "imgui.h" +#include "imgui.h" #endif -namespace Utility{ +namespace Utility +{ #ifndef NDEBUG -namespace Debug { +namespace Debug +{ // Usage: // static ExampleAppLog my_log; @@ -21,43 +23,47 @@ namespace Debug { // my_log.Draw("title"); struct ImGuiLogger { - ImGuiTextBuffer Buf; - ImGuiTextFilter Filter; - ImVector LineOffsets; // Index to lines offset - bool ShowError; - bool ShowDeprecatedBehavior; - bool ShowUndefinedBehavior; - bool ShowPortability; - bool ShowPerformance; - bool ShowMarker; - bool ShowPushGroup; - bool ShowPopGroup; - bool ShowOther; - bool ScrollToBottom; + ImGuiTextBuffer Buf; + ImGuiTextFilter Filter; + ImVector LineOffsets; // Index to lines offset + bool ShowError; + bool ShowDeprecatedBehavior; + bool ShowUndefinedBehavior; + bool ShowPortability; + bool ShowPerformance; + bool ShowMarker; + bool ShowPushGroup; + bool ShowPopGroup; + bool ShowOther; + bool ScrollToBottom; ImGuiLogger() : ShowError(true) {} - void Clear(); - void AddLog(const char* fmt, ...) IM_FMTARGS(2); - void Draw(const char* title, bool* p_open = NULL); + void Clear(); + void AddLog(const char *fmt, ...) IM_FMTARGS(2); + void Draw(const char *title, bool *p_open = NULL); }; extern ImGuiLogger openGLLogger; -} +} // namespace Debug #endif //NDEBUG -enum ERROR_TYPE { OpenGL = 0, SDL, GLEW, Other}; +enum ERROR_TYPE +{ + OpenGL = 0, + SDL, + GLEW, + Other +}; void APIENTRY openglCallbackFunction(GLenum source, - GLenum type, - GLuint id, - GLenum severity, - GLsizei length, - const GLchar* message, - const void* userParam); - - + GLenum type, + GLuint id, + GLenum severity, + GLsizei length, + const GLchar *message, + const void *userParam); void printWarning(ERROR_TYPE type, std::string msg); @@ -65,5 +71,5 @@ void printError(ERROR_TYPE type, std::string msg); void printInfo(ERROR_TYPE type, std::string msg); -} +} // namespace Utility #endif // UTILITY_H diff --git a/src/entities/components/Position.h b/src/entities/components/Position.h index 0823a62..cfd2247 100644 --- a/src/entities/components/Position.h +++ b/src/entities/components/Position.h @@ -1,7 +1,8 @@ #ifndef POSITION_H #define POSITION_H -struct Position { +struct Position +{ Position(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} float x, y; diff --git a/src/input/Command.cpp b/src/input/Command.cpp index f15c989..c71d8ee 100644 --- a/src/input/Command.cpp +++ b/src/input/Command.cpp @@ -2,22 +2,27 @@ #include "../DesktopLauncher.h" -void QuitCommand::execute(OrtographicCamera& camera) { +void QuitCommand::execute(OrtographicCamera &camera) +{ Game::exit(); } -void WCommand::execute(OrtographicCamera& camera) { +void WCommand::execute(OrtographicCamera &camera) +{ camera.zoomIn(0.01f); } -void ACommand::execute(OrtographicCamera& camera) { +void ACommand::execute(OrtographicCamera &camera) +{ camera.translate(glm::vec2(-0.1f, 0.0f)); } -void SCommand::execute(OrtographicCamera& camera) { +void SCommand::execute(OrtographicCamera &camera) +{ camera.zoomIn(-0.01f); } -void DCommand::execute(OrtographicCamera& camera) { +void DCommand::execute(OrtographicCamera &camera) +{ camera.translate(glm::vec2(0.1f, 0.0f)); } \ No newline at end of file diff --git a/src/input/Command.h b/src/input/Command.h index 0e107b9..1a4b9f0 100644 --- a/src/input/Command.h +++ b/src/input/Command.h @@ -3,33 +3,38 @@ #include "../Camera.h" -class Command { - public: +class Command +{ + public: virtual ~Command() {} - virtual void execute(OrtographicCamera& camera) = 0; + virtual void execute(OrtographicCamera &camera) = 0; }; -class QuitCommand : public Command { - public: - virtual void execute(OrtographicCamera& camera) override; +class QuitCommand : public Command +{ + public: + virtual void execute(OrtographicCamera &camera) override; }; -class WCommand : public Command { - public: - virtual void execute(OrtographicCamera& camera) override; +class WCommand : public Command +{ + public: + virtual void execute(OrtographicCamera &camera) override; }; -class ACommand : public Command { - public: - virtual void execute(OrtographicCamera& camera) override; +class ACommand : public Command +{ + public: + virtual void execute(OrtographicCamera &camera) override; }; -class SCommand : public Command { - public: - virtual void execute(OrtographicCamera& camera) override; +class SCommand : public Command +{ + public: + virtual void execute(OrtographicCamera &camera) override; }; -class DCommand : public Command { - public: - virtual void execute(OrtographicCamera& camera) override; +class DCommand : public Command +{ + public: + virtual void execute(OrtographicCamera &camera) override; }; - #endif diff --git a/src/input/InputHandler.cpp b/src/input/InputHandler.cpp index 6977287..ebad9f7 100644 --- a/src/input/InputHandler.cpp +++ b/src/input/InputHandler.cpp @@ -2,7 +2,8 @@ #include -InputHandler::InputHandler() { +InputHandler::InputHandler() +{ W = new WCommand(); A = new ACommand(); S = new SCommand(); @@ -10,7 +11,8 @@ InputHandler::InputHandler() { quit = new QuitCommand(); } -InputHandler::~InputHandler() { +InputHandler::~InputHandler() +{ //Free all Commands delete W; delete A; @@ -19,38 +21,43 @@ InputHandler::~InputHandler() { delete quit; } -Command* InputHandler::handleInput(SDL_Event *event) { - if(event == NULL) { +Command *InputHandler::handleInput(SDL_Event *event) +{ + if (event == NULL) + { return NULL; } - switch (event->type) { - case SDL_QUIT: - return quit; + switch (event->type) + { + case SDL_QUIT: + return quit; + break; + case SDL_KEYDOWN: + std::cout << "The Key " << SDL_GetKeyName(event->key.keysym.sym) << " has been pressed!" << std::endl; + return NULL; + break; + case SDL_KEYUP: + std::cout << "The Key " << SDL_GetKeyName(event->key.keysym.sym) << " has been released!" << std::endl; + switch (event->key.keysym.sym) + { + case SDLK_w: + return W; break; - case SDL_KEYDOWN: - std::cout << "The Key " << SDL_GetKeyName(event->key.keysym.sym) << " has been pressed!" << std::endl; - return NULL; + case SDLK_a: + return A; break; - case SDL_KEYUP: - std::cout << "The Key " << SDL_GetKeyName(event->key.keysym.sym) << " has been released!" << std::endl; - switch(event->key.keysym.sym) { - case SDLK_w: - return W; - break; - case SDLK_a: - return A; - break; - case SDLK_s: - return S; - break; - case SDLK_d: - return D; - break; - default: break; - } - return NULL; + case SDLK_s: + return S; break; - default: + case SDLK_d: + return D; + break; + default: + break; + } + return NULL; + break; + default: return NULL; break; } diff --git a/src/input/InputHandler.h b/src/input/InputHandler.h index 20b200d..62dffa6 100644 --- a/src/input/InputHandler.h +++ b/src/input/InputHandler.h @@ -4,17 +4,18 @@ #include "Command.h" -class InputHandler { - public: +class InputHandler +{ + public: ~InputHandler(); InputHandler(); - Command* handleInput(SDL_Event *event); + Command *handleInput(SDL_Event *event); - private: - Command* W; - Command* A; - Command* S; - Command* D; - Command* quit; + private: + Command *W; + Command *A; + Command *S; + Command *D; + Command *quit; }; #endif diff --git a/src/screens/GameScreen.cpp b/src/screens/GameScreen.cpp index 9163b45..34e0e79 100644 --- a/src/screens/GameScreen.cpp +++ b/src/screens/GameScreen.cpp @@ -4,76 +4,78 @@ #include -GameScreen::GameScreen(Renderer* renderer, Assets* assets) { - std::cout << "GameScreen got created!" << std::endl; - this->renderer = renderer; - this->assets = assets; +GameScreen::GameScreen(Renderer *renderer, Assets *assets) +{ + std::cout << "GameScreen got created!" << std::endl; + this->renderer = renderer; + this->assets = assets; //Vertex2D can be defined like this, because it an aggregate class. // see https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special - std::vector vertices({ - Vertex2D{glm::vec2(-0.5f,-0.5f), glm::vec2(0.0f,1.0f)}, - Vertex2D{glm::vec2(0.5f,-0.5f), glm::vec2(1.0f,1.0f)}, - Vertex2D{glm::vec2(0.5f,0.5f), glm::vec2(1.0f,0.0f)}, - Vertex2D{glm::vec2(-0.5f,0.5f), glm::vec2(0.0f,0.0f)} - }); - - std::vector indices({ - 0,1,3, - 1,2,3 - }); + std::vector vertices({Vertex2D{glm::vec2(-0.5f, -0.5f), glm::vec2(0.0f, 1.0f)}, + Vertex2D{glm::vec2(0.5f, -0.5f), glm::vec2(1.0f, 1.0f)}, + Vertex2D{glm::vec2(0.5f, 0.5f), glm::vec2(1.0f, 0.0f)}, + Vertex2D{glm::vec2(-0.5f, 0.5f), glm::vec2(0.0f, 0.0f)}}); - obj = new Mesh(vertices,indices,assets->getTexture("police_officer")); + std::vector indices({0, 1, 3, + 1, 2, 3}); + + obj = new Mesh(vertices, indices, assets->getTexture("police_officer")); inputHandler = new InputHandler(); - camera = new OrtographicCamera(); + camera = new OrtographicCamera(); - //Create Entities + //Create Entities //entityx::EntityX ex; //entityx::Entity charakter = ex.entities.create(); //charakter.assign(); } -GameScreen::~GameScreen() { - delete camera; - delete obj; +GameScreen::~GameScreen() +{ + delete camera; + delete obj; delete inputHandler; } -void GameScreen::hide() { - +void GameScreen::hide() +{ } -void GameScreen::pause() { - +void GameScreen::pause() +{ } -void GameScreen::render() { - //Drawing +void GameScreen::render() +{ + //Drawing Shader defaultShader = assets->getShader("defaultShader"); - defaultShader.SetMatrix4("MVP",camera->getCombinedMatrix(),true); - //Draw Mesh - renderer->draw(*obj,defaultShader); + defaultShader.SetMatrix4("MVP", camera->getCombinedMatrix(), true); + //Draw Mesh + renderer->draw(*obj, defaultShader); } -void GameScreen::update() { +void GameScreen::update() +{ SDL_Event event; - while(SDL_PollEvent(&event) != 0) { - Command* command = inputHandler->handleInput(&event); - if (command) { + while (SDL_PollEvent(&event) != 0) + { + Command *command = inputHandler->handleInput(&event); + if (command) + { command->execute(*camera); } } } -void GameScreen::resize() { - +void GameScreen::resize() +{ } -void GameScreen::resume() { - +void GameScreen::resume() +{ } -void GameScreen::show() { - +void GameScreen::show() +{ } diff --git a/src/screens/GameScreen.h b/src/screens/GameScreen.h index 60d1f17..bd1fd04 100644 --- a/src/screens/GameScreen.h +++ b/src/screens/GameScreen.h @@ -8,9 +8,10 @@ #include "../Camera.h" #include "../input/InputHandler.h" -class GameScreen : public Screen { - public: - GameScreen(Renderer* renderer, Assets* assets); +class GameScreen : public Screen +{ + public: + GameScreen(Renderer *renderer, Assets *assets); ~GameScreen(); virtual void hide() override; @@ -21,10 +22,10 @@ class GameScreen : public Screen { virtual void resume() override; virtual void show() override; - private: - Renderer* renderer; - Assets* assets; - InputHandler* inputHandler; + private: + Renderer *renderer; + Assets *assets; + InputHandler *inputHandler; OrtographicCamera *camera; Mesh *obj; diff --git a/src/screens/Screen.h b/src/screens/Screen.h index 3ba7f39..29cd2b2 100644 --- a/src/screens/Screen.h +++ b/src/screens/Screen.h @@ -1,11 +1,12 @@ #ifndef SCREEN_H #define SCREEN_H -class Screen { - protected: +class Screen +{ + protected: Screen() {} - public: + public: virtual ~Screen() {} /* Called when this screen is no longer the current screen for a Game. */ @@ -16,7 +17,6 @@ class Screen { virtual void resize() = 0; virtual void resume() = 0; virtual void show() = 0; - }; #endif \ No newline at end of file