Formatted classes

This commit is contained in:
Julian Nießner
2018-05-26 11:14:12 +02:00
parent ae5fa3f2aa
commit 9ffdf66bd0
27 changed files with 574 additions and 461 deletions

View File

@@ -9,7 +9,8 @@
#include "Texture.h" #include "Texture.h"
#include "Shader.h" #include "Shader.h"
class Assets { class Assets
{
private: private:
std::map<std::string, Texture2D> textures; std::map<std::string, Texture2D> textures;
std::map<std::string, Shader> shaders; std::map<std::string, Shader> shaders;
@@ -25,8 +26,6 @@ class Assets {
Shader getShader(std::string name); Shader getShader(std::string name);
Texture2D loadTexture(const GLchar *file, GLboolean alpha, std::string name); Texture2D loadTexture(const GLchar *file, GLboolean alpha, std::string name);
Texture2D getTexture(std::string name); Texture2D getTexture(std::string name);
}; };
#endif #endif

View File

@@ -6,20 +6,25 @@
#include <iostream> #include <iostream>
BitmapFont::BitmapFont(std::string fontLocation) { BitmapFont::BitmapFont(std::string fontLocation)
{
FT_Library ft; FT_Library ft;
if(FT_Init_FreeType(&ft)) { if (FT_Init_FreeType(&ft))
{
std::cout << "ERROR::FreeType: Could not init FreeType Library" << std::endl; std::cout << "ERROR::FreeType: Could not init FreeType Library" << std::endl;
} }
FT_Face face; 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; std::cout << "ERROR::FreeType: Failed to load font" << std::endl;
} }
FT_Set_Pixel_Sizes(face, 0, 48); FT_Set_Pixel_Sizes(face, 0, 48);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //Disable byte-alignment restriction 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 //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; std::cout << "ERROR::FreeType: Could not load Glyph: " << c << std::endl;
continue; continue;
} }
@@ -35,8 +40,7 @@ BitmapFont::BitmapFont(std::string fontLocation) {
0, 0,
GL_RED, GL_RED,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer face->glyph->bitmap.buffer);
);
// Set texture options // Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 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_WRAP_T, GL_CLAMP_TO_EDGE);
@@ -46,8 +50,7 @@ BitmapFont::BitmapFont(std::string fontLocation) {
texture, texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top), glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x face->glyph->advance.x};
};
characters.insert(std::pair<GLchar, Character>(c, character)); characters.insert(std::pair<GLchar, Character>(c, character));
} }
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); //Reset byte-alignment restriction glPixelStorei(GL_UNPACK_ALIGNMENT, 4); //Reset byte-alignment restriction
@@ -70,7 +73,8 @@ BitmapFont::BitmapFont(std::string fontLocation) {
glBindVertexArray(0); 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.Use();
s.SetVector3f("textColor", color, true); s.SetVector3f("textColor", color, true);
s.SetMatrix4("projection", projection, true); s.SetMatrix4("projection", projection, true);
@@ -97,8 +101,7 @@ void BitmapFont::drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLf
{xpos, ypos + h, 0.0, 0.0}, {xpos, ypos + h, 0.0, 0.0},
{xpos + w, ypos, 1.0, 1.0}, {xpos + w, ypos, 1.0, 1.0},
{ xpos + w, ypos + h, 1.0, 0.0 } {xpos + w, ypos + h, 1.0, 0.0}};
};
// Render glyph texture over quad // Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.texID); glBindTexture(GL_TEXTURE_2D, ch.texID);
// Update content of VBO memory // Update content of VBO memory

View File

@@ -8,14 +8,16 @@
#include "Shader.h" #include "Shader.h"
struct Character { struct Character
{
GLuint texID; GLuint texID;
glm::ivec2 size; glm::ivec2 size;
glm::ivec2 bearing; glm::ivec2 bearing;
GLuint advance; GLuint advance;
}; };
class BitmapFont { class BitmapFont
{
public: public:
BitmapFont(std::string fontLocation); BitmapFont(std::string fontLocation);
void drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color); void drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color);

View File

@@ -2,37 +2,44 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
void OrtographicCamera::setPosition(glm::vec2 position) { void OrtographicCamera::setPosition(glm::vec2 position)
{
this->cameraPos = position; this->cameraPos = position;
} }
glm::vec2 OrtographicCamera::getPosition() { glm::vec2 OrtographicCamera::getPosition()
{
return this->cameraPos; return this->cameraPos;
} }
void OrtographicCamera::setZoomLevel(float newZoom) { void OrtographicCamera::setZoomLevel(float newZoom)
{
this->zoom = newZoom; this->zoom = newZoom;
} }
void OrtographicCamera::zoomIn(float targetZoom) { void OrtographicCamera::zoomIn(float targetZoom)
{
zoom -= targetZoom; zoom -= targetZoom;
} }
void OrtographicCamera::translate(glm::vec2 target) { void OrtographicCamera::translate(glm::vec2 target)
{
cameraPos = cameraPos - 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); glm::mat4 view(1);
view = glm::translate(view, glm::vec3(-cameraPos, 0.f)); view = glm::translate(view, glm::vec3(-cameraPos, 0.f));
return view; 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) //Dies gibt die Größe des kamera auschnittes an die in die welt zeigt (Frustum)
int width = 800; int width = 800;
int height = 600; int height = 600;
@@ -42,12 +49,13 @@ glm::mat4 OrtographicCamera::getProjectionMatrix() {
return projection; return projection;
} }
glm::mat4 OrtographicCamera::getCombinedMatrix() { glm::mat4 OrtographicCamera::getCombinedMatrix()
{
glm::mat4 combined; glm::mat4 combined;
combined = getProjectionMatrix() * getViewMatrix(); combined = getProjectionMatrix() * getViewMatrix();
return combined; return combined;
} }
void OrtographicCamera::update() { void OrtographicCamera::update()
{
} }

View File

@@ -3,7 +3,8 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
class OrtographicCamera { class OrtographicCamera
{
private: private:
glm::vec2 cameraPos; glm::vec2 cameraPos;
float zoom; float zoom;

View File

@@ -10,7 +10,8 @@
#include "screens/GameScreen.h" #include "screens/GameScreen.h"
#include "Utility.h" #include "Utility.h"
DarkRP2D::~DarkRP2D() { DarkRP2D::~DarkRP2D()
{
std::cout << "Game got destroyed!" << std::endl; std::cout << "Game got destroyed!" << std::endl;
#ifndef NDEBUG #ifndef NDEBUG
ImGui_ImplSdlGL3_Shutdown(); ImGui_ImplSdlGL3_Shutdown();
@@ -21,7 +22,8 @@ DarkRP2D::~DarkRP2D() {
delete renderer; delete renderer;
} }
void DarkRP2D::create () { void DarkRP2D::create()
{
std::cout << "Game got created!" << std::endl; std::cout << "Game got created!" << std::endl;
assets = new Assets(); assets = new Assets();
assets->loadTexture("police_officer.png", true, "police_officer"); assets->loadTexture("police_officer.png", true, "police_officer");
@@ -43,18 +45,21 @@ void DarkRP2D::create () {
#endif #endif
} }
void DarkRP2D::loop(unsigned int delta) { void DarkRP2D::loop(unsigned int delta)
{
static unsigned int accumulator = 0, ups = 0, fps = 0, acc = 0; static unsigned int accumulator = 0, ups = 0, fps = 0, acc = 0;
accumulator += delta; accumulator += delta;
acc += delta; acc += delta;
while(accumulator >= 17) { while (accumulator >= 17)
{
accumulator -= 17; accumulator -= 17;
update(delta); update(delta);
ups++; ups++;
} }
if(acc >= 1000) { if (acc >= 1000)
{
std::cout << "Updates per second: " << ups << " Frames per Second: " << fps << std::endl; std::cout << "Updates per second: " << ups << " Frames per Second: " << fps << std::endl;
acc -= 1000; acc -= 1000;
ups = 0; ups = 0;
@@ -64,11 +69,13 @@ void DarkRP2D::loop(unsigned int delta) {
fps++; fps++;
} }
void DarkRP2D::update(unsigned int delta) { void DarkRP2D::update(unsigned int delta)
{
gameScreen->update(); gameScreen->update();
} }
void DarkRP2D::render(unsigned int delta) { void DarkRP2D::render(unsigned int delta)
{
renderer->clear(); renderer->clear();
#ifndef NDEBUG #ifndef NDEBUG
ImGui_ImplSdlGL3_NewFrame(gWindow); ImGui_ImplSdlGL3_NewFrame(gWindow);
@@ -106,5 +113,6 @@ void DarkRP2D::render(unsigned int delta) {
SDL_GL_SwapWindow(gWindow); SDL_GL_SwapWindow(gWindow);
} }
void DarkRP2D::resize(int width, int height){ void DarkRP2D::resize(int width, int height)
{
} }

View File

@@ -5,14 +5,14 @@ class DarkRP2D;
#include <SDL.h> #include <SDL.h>
#include <vector> #include <vector>
#include "Assets.h" #include "Assets.h"
#include "Renderer.h" #include "Renderer.h"
#include "input/InputHandler.h" #include "input/InputHandler.h"
#include "BitmapFont.h" #include "BitmapFont.h"
#include "screens/Screen.h" #include "screens/Screen.h"
class DarkRP2D { class DarkRP2D
{
private: private:
SDL_Window *gWindow; SDL_Window *gWindow;
Assets *assets; Assets *assets;
@@ -21,7 +21,6 @@ class DarkRP2D {
Screen *gameScreen; Screen *gameScreen;
public: public:
DarkRP2D(SDL_Window *gWindow) : gWindow(gWindow) {} DarkRP2D(SDL_Window *gWindow) : gWindow(gWindow) {}
~DarkRP2D(); ~DarkRP2D();
void create(); void create();
@@ -29,7 +28,6 @@ class DarkRP2D {
void update(unsigned int delta); void update(unsigned int delta);
void render(unsigned int delta); void render(unsigned int delta);
void resize(int width, int height); void resize(int width, int height);
}; };
#endif #endif

View File

@@ -4,7 +4,6 @@
#include <SDL_image.h> #include <SDL_image.h>
#include <GL/glew.h> #include <GL/glew.h>
#include "Desktoplauncher.h" #include "Desktoplauncher.h"
#include "DarkRP2D.h" #include "DarkRP2D.h"
#include "Utility.h" #include "Utility.h"
@@ -18,9 +17,11 @@ static bool gameQuit = false;
void dispose(); void dispose();
bool initSDL(); bool initSDL();
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
//Init SDL_Window, SDL_Surface, SDL_Renderer //Init SDL_Window, SDL_Surface, SDL_Renderer
if(!initSDL()) { if (!initSDL())
{
dispose(); dispose();
return 1; return 1;
} }
@@ -29,7 +30,8 @@ int main(int argc, char *argv[]) {
DarkRP2D resA(gWindow); DarkRP2D resA(gWindow);
resA.create(); resA.create();
unsigned int lastTime = SDL_GetTicks(), currentTime, elapsedTime; unsigned int lastTime = SDL_GetTicks(), currentTime, elapsedTime;
while( !gameQuit ) { while (!gameQuit)
{
currentTime = SDL_GetTicks(); currentTime = SDL_GetTicks();
elapsedTime = currentTime - lastTime; elapsedTime = currentTime - lastTime;
lastTime = currentTime; lastTime = currentTime;
@@ -40,32 +42,38 @@ int main(int argc, char *argv[]) {
return 0; return 0;
} }
void Game::exit() { void Game::exit()
{
gameQuit = true; gameQuit = true;
} }
namespace Game { namespace Game
{
float DDPI, HDPI, VDPI; float DDPI, HDPI, VDPI;
} }
bool initSDL() { bool initSDL()
{
int result = SetProcessDPIAware(); // NEEDED or SDL_GetDisplayDPI returns wrong numbers int result = SetProcessDPIAware(); // NEEDED or SDL_GetDisplayDPI returns wrong numbers
cout << "Result: " << result << endl; cout << "Result: " << result << endl;
//Init SDL //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; cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
return false; return false;
} }
//Init Window //Init Window
uint32_t WindowFlags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL; 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); gWindow = SDL_CreateWindow("SDL TEST", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Game::SCREEN_WIDTH, Game::SCREEN_HEIGHT, WindowFlags);
if( gWindow == NULL) { if (gWindow == NULL)
{
cout << "Window could not be created! SDL_Error: " << SDL_GetError() << endl; cout << "Window could not be created! SDL_Error: " << SDL_GetError() << endl;
return false; return false;
} }
//Read in Display DPI //Read in Display DPI
int returnDisplayDPI = SDL_GetDisplayDPI(SDL_GetWindowDisplayIndex(gWindow), &Game::DDPI, &Game::HDPI, &Game::VDPI); int returnDisplayDPI = SDL_GetDisplayDPI(SDL_GetWindowDisplayIndex(gWindow), &Game::DDPI, &Game::HDPI, &Game::VDPI);
if(returnDisplayDPI != 0) { if (returnDisplayDPI != 0)
{
cout << "DisplayDPI cannot be loaded! SDL_ERROR: " << SDL_GetError() << endl; cout << "DisplayDPI cannot be loaded! SDL_ERROR: " << SDL_GetError() << endl;
} }
cout << "DPI: DDPI: " << Game::DDPI << " HDPI: " << Game::HDPI << " VPDI: " << Game::VDPI << endl; cout << "DPI: DDPI: " << Game::DDPI << " HDPI: " << Game::HDPI << " VPDI: " << Game::VDPI << endl;
@@ -77,30 +85,36 @@ bool initSDL() {
//Init OpenGL Renderer //Init OpenGL Renderer
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glcontext = SDL_GL_CreateContext(gWindow); glcontext = SDL_GL_CreateContext(gWindow);
if(glcontext == NULL) { if (glcontext == NULL)
{
cout << "Could not create OpenGL Context: " << SDL_GetError() << endl; cout << "Could not create OpenGL Context: " << SDL_GetError() << endl;
return false; return false;
} }
//Init GLEW //Init GLEW
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
GLenum err = glewInit(); GLenum err = glewInit();
if(GLEW_OK != err) { if (GLEW_OK != err)
{
cout << "Init of Glew failed: " << glewGetErrorString(err) << endl; cout << "Init of Glew failed: " << glewGetErrorString(err) << endl;
return false; return false;
} }
//Init debug callback function //Init debug callback function
#ifndef NDEBUG #ifndef NDEBUG
if (GLEW_ARB_debug_output) { if (GLEW_ARB_debug_output)
{
Utility::printInfo(Utility::GLEW, "Supporting 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!"); Utility::printInfo(Utility::GLEW, "Supporting AMD debug output!");
} }
if (GLEW_KHR_debug) { if (GLEW_KHR_debug)
{
Utility::printInfo(Utility::GLEW, "Supporting KHR debug output!"); Utility::printInfo(Utility::GLEW, "Supporting KHR debug output!");
} }
if(glDebugMessageCallback) { if (glDebugMessageCallback)
{
glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT);
Utility::printInfo(Utility::OpenGL, "Register OpenGL debug callback"); Utility::printInfo(Utility::OpenGL, "Register OpenGL debug callback");
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
@@ -112,14 +126,17 @@ bool initSDL() {
0, 0,
&unusedIds, &unusedIds,
true); true);
} else { }
else
{
Utility::printWarning(Utility::OpenGL, "glDebugMessageCallback not available"); Utility::printWarning(Utility::OpenGL, "glDebugMessageCallback not available");
} }
#endif #endif
return true; return true;
} }
void dispose() { void dispose()
{
SDL_GL_DeleteContext(glcontext); SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);
SDL_Quit(); SDL_Quit();

View File

@@ -3,7 +3,8 @@
#include <string> #include <string>
namespace Game { namespace Game
{
const int SCREEN_WIDTH = 800; const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600; const int SCREEN_HEIGHT = 600;
@@ -15,8 +16,7 @@ extern float HDPI;
//vertical DPI of the display used while starting //vertical DPI of the display used while starting
extern float VDPI; extern float VDPI;
void exit(); void exit();
} } // namespace Game
#endif #endif

View File

@@ -3,8 +3,8 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <iostream> #include <iostream>
Mesh::Mesh(std::vector<Vertex2D> vertices, std::vector<unsigned int> indices, Texture2D texture)
Mesh::Mesh(std::vector<Vertex2D> vertices, std::vector<unsigned int> indices, Texture2D texture) { {
this->texture = texture; this->texture = texture;
this->vertices = vertices; this->vertices = vertices;
this->indices = indices; this->indices = indices;
@@ -12,7 +12,8 @@ Mesh::Mesh(std::vector<Vertex2D> vertices, std::vector<unsigned int> indices, Te
setupMesh(); setupMesh();
} }
void Mesh::setupMesh() { void Mesh::setupMesh()
{
//Create VAO & Bind //Create VAO & Bind
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO); glBindVertexArray(VAO);
@@ -36,21 +37,25 @@ void Mesh::setupMesh() {
glBindVertexArray(0); glBindVertexArray(0);
} }
Mesh::~Mesh() { Mesh::~Mesh()
{
std::cout << "Deleting Mesh" << std::endl; std::cout << "Deleting Mesh" << std::endl;
glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO); glDeleteBuffers(1, &EBO);
glDeleteBuffers(1, &VAO); glDeleteBuffers(1, &VAO);
} }
unsigned int Mesh::getIndicesCount() const { unsigned int Mesh::getIndicesCount() const
{
return indices.size(); return indices.size();
} }
void Mesh::bindVAO() const { void Mesh::bindVAO() const
{
glBindVertexArray(this->VAO); glBindVertexArray(this->VAO);
} }
void Mesh::bindTexture() const { void Mesh::bindTexture() const
{
texture.Bind(); texture.Bind();
} }

View File

@@ -7,12 +7,14 @@
//Aggregate class -> so following construction is available //Aggregate class -> so following construction is available
//Vertex2D{glm::vec2(-0.5f,-0.5f), glm::vec2(0.0f,1.0f)} //Vertex2D{glm::vec2(-0.5f,-0.5f), glm::vec2(0.0f,1.0f)}
struct Vertex2D { struct Vertex2D
{
glm::vec2 position; glm::vec2 position;
glm::vec2 texCoord; glm::vec2 texCoord;
}; };
class Mesh { class Mesh
{
private: private:
unsigned int VBO, VAO, EBO; unsigned int VBO, VAO, EBO;

View File

@@ -1,11 +1,13 @@
#include "Renderer.h" #include "Renderer.h"
#include <GL/glew.h> #include <GL/glew.h>
Renderer::Renderer() { Renderer::Renderer()
{
glClearColor(1.f, 1.f, 1.f, 1.f); 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 //Bind texture of mesh
mesh.bindTexture(); mesh.bindTexture();
mesh.bindVAO(); mesh.bindVAO();
@@ -13,6 +15,7 @@ void Renderer::draw(const Mesh& mesh, Shader& shader) {
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); glClear(GL_COLOR_BUFFER_BIT);
} }

View File

@@ -4,7 +4,8 @@
#include "Mesh.h" #include "Mesh.h"
#include "Shader.h" #include "Shader.h"
class Renderer { class Renderer
{
public: public:
Renderer(); Renderer();
void draw(const Mesh &mesh, Shader &shader); void draw(const Mesh &mesh, Shader &shader);

View File

@@ -2,12 +2,14 @@
#include <iostream> #include <iostream>
Shader &Shader::Use() { Shader &Shader::Use()
{
glUseProgram(this->ID); glUseProgram(this->ID);
return *this; 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; GLuint sVertex, sFragment, gShader;
// Vertex Shader // Vertex Shader
sVertex = glCreateShader(GL_VERTEX_SHADER); sVertex = glCreateShader(GL_VERTEX_SHADER);
@@ -42,54 +44,63 @@ void Shader::Compile(const GLchar* vertexSource, const GLchar* fragmentSource, c
glDeleteShader(gShader); glDeleteShader(gShader);
} }
void Shader::SetFloat(const GLchar *name, GLfloat value, GLboolean useShader) { void Shader::SetFloat(const GLchar *name, GLfloat value, GLboolean useShader)
{
if (useShader) if (useShader)
this->Use(); this->Use();
glUniform1f(glGetUniformLocation(this->ID, name), value); 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) if (useShader)
this->Use(); this->Use();
glUniform1i(glGetUniformLocation(this->ID, name), value); 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) if (useShader)
this->Use(); this->Use();
glUniform2f(glGetUniformLocation(this->ID, name), x, y); 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) if (useShader)
this->Use(); this->Use();
glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y); 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) if (useShader)
this->Use(); this->Use();
glUniform3f(glGetUniformLocation(this->ID, name), x, y, z); 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) if (useShader)
this->Use(); this->Use();
glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z); 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) if (useShader)
this->Use(); this->Use();
glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w); 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) if (useShader)
this->Use(); this->Use();
glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z, value.w); 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) if (useShader)
this->Use(); this->Use();
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, GL_FALSE, glm::value_ptr(matrix)); 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; GLint success;
GLchar infoLog[1024]; GLchar infoLog[1024];
if (type != "PROGRAM") if (type != "PROGRAM")

View File

@@ -7,7 +7,6 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
// General purpsoe shader object. Compiles from file, generates // 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. // functions for easy management.
@@ -32,6 +31,7 @@ public:
void SetVector4f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w, 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 SetVector4f(const GLchar *name, const glm::vec4 &value, GLboolean useShader = false);
void SetMatrix4(const GLchar *name, const glm::mat4 &matrix, GLboolean useShader = false); void SetMatrix4(const GLchar *name, const glm::mat4 &matrix, GLboolean useShader = false);
private: private:
// Checks if compilation or linking failed and if so, print the error logs // 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);

View File

@@ -2,14 +2,14 @@
#include "Texture.h" #include "Texture.h"
Texture2D::Texture2D() 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); 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->Width = width;
this->Height = height; this->Height = height;
// Create Texture // Create Texture
@@ -31,6 +31,7 @@ void Texture2D::Generate(GLuint width, GLuint height, unsigned char* data) {
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
void Texture2D::Bind() const { void Texture2D::Bind() const
{
glBindTexture(GL_TEXTURE_2D, this->ID); glBindTexture(GL_TEXTURE_2D, this->ID);
} }

View File

@@ -2,18 +2,22 @@
#ifndef NDEBUG #ifndef NDEBUG
namespace Utility{ namespace Utility
namespace Debug { {
namespace Debug
{
ImGuiLogger openGLLogger; ImGuiLogger openGLLogger;
} }
} } // namespace Utility
void Utility::Debug::ImGuiLogger::Clear() { void Utility::Debug::ImGuiLogger::Clear()
{
Buf.clear(); Buf.clear();
LineOffsets.clear(); LineOffsets.clear();
} }
void Utility::Debug::ImGuiLogger::AddLog(const char* fmt, ...) IM_FMTARGS(2) { void Utility::Debug::ImGuiLogger::AddLog(const char *fmt, ...) IM_FMTARGS(2)
{
int old_size = Buf.size(); int old_size = Buf.size();
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
@@ -25,27 +29,37 @@
ScrollToBottom = true; ScrollToBottom = true;
} }
void Utility::Debug::ImGuiLogger::Draw(const char* title, bool* p_open) { void Utility::Debug::ImGuiLogger::Draw(const char *title, bool *p_open)
{
ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
ImGui::Begin(title, p_open); ImGui::Begin(title, p_open);
if (ImGui::Button("Clear")) Clear(); if (ImGui::Button("Clear"))
Clear();
ImGui::SameLine(); ImGui::SameLine();
bool copy = ImGui::Button("Copy"); bool copy = ImGui::Button("Copy");
ImGui::SameLine(); ImGui::SameLine();
ImGui::Checkbox("Errors", &ShowError); ImGui::SameLine(); ImGui::Checkbox("Errors", &ShowError);
ImGui::Checkbox("Deprecated Behavior", &ShowDeprecatedBehavior); ImGui::SameLine(); ImGui::SameLine();
ImGui::Checkbox("Undefined Behavior", &ShowUndefinedBehavior); ImGui::SameLine(); ImGui::Checkbox("Deprecated Behavior", &ShowDeprecatedBehavior);
ImGui::SameLine();
ImGui::Checkbox("Undefined Behavior", &ShowUndefinedBehavior);
ImGui::SameLine();
ImGui::Checkbox("Portability", &ShowPortability); ImGui::Checkbox("Portability", &ShowPortability);
ImGui::Checkbox("Performance", &ShowPerformance); ImGui::SameLine(); ImGui::Checkbox("Performance", &ShowPerformance);
ImGui::Checkbox("Marker", &ShowMarker); ImGui::SameLine(); ImGui::SameLine();
ImGui::Checkbox("Push Group", &ShowPushGroup); ImGui::SameLine(); ImGui::Checkbox("Marker", &ShowMarker);
ImGui::Checkbox("Pop Group", &ShowPopGroup); ImGui::SameLine(); ImGui::SameLine();
ImGui::Checkbox("Push Group", &ShowPushGroup);
ImGui::SameLine();
ImGui::Checkbox("Pop Group", &ShowPopGroup);
ImGui::SameLine();
ImGui::Checkbox("Other", &ShowOther); ImGui::Checkbox("Other", &ShowOther);
Filter.Draw("Filter", -100.0f); Filter.Draw("Filter", -100.0f);
ImGui::Separator(); ImGui::Separator();
ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
if (copy) ImGui::LogToClipboard(); if (copy)
ImGui::LogToClipboard();
if (Filter.IsActive()) if (Filter.IsActive())
{ {
@@ -79,10 +93,12 @@ void APIENTRY Utility::openglCallbackFunction(GLenum source,
GLenum severity, GLenum severity,
GLsizei length, GLsizei length,
const GLchar *message, const GLchar *message,
const void* userParam){ const void *userParam)
{
std::string _severity; std::string _severity;
switch (severity){ switch (severity)
{
case GL_DEBUG_SEVERITY_LOW: case GL_DEBUG_SEVERITY_LOW:
_severity = "LOW"; _severity = "LOW";
break; break;
@@ -97,34 +113,41 @@ void APIENTRY Utility::openglCallbackFunction(GLenum source,
break; break;
} }
switch (type) { switch (type)
{
case GL_DEBUG_TYPE_ERROR: 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()); Debug::openGLLogger.AddLog("ERROR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str());
} }
break; break;
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: 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()); Debug::openGLLogger.AddLog("DEPRECATED_BEHAVIOR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str());
} }
break; break;
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: 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()); Debug::openGLLogger.AddLog("UNDEFINED_BEHAVIOR: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str());
} }
break; break;
case GL_DEBUG_TYPE_PORTABILITY: 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()); Debug::openGLLogger.AddLog("PORTATBILITY: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str());
} }
break; break;
case GL_DEBUG_TYPE_PERFORMANCE: 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()); Debug::openGLLogger.AddLog("PERFORMANCE: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str());
} }
break; break;
case GL_DEBUG_TYPE_OTHER: 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()); Debug::openGLLogger.AddLog("OTHER: %s ID: %d SEVERITY: %s\n", message, id, _severity.c_str());
} }
break; break;
@@ -132,13 +155,16 @@ void APIENTRY Utility::openglCallbackFunction(GLenum source,
} }
static const char *Errornames[] = {"OpenGL", "SDL", "GLEW", "Other"}; static const char *Errornames[] = {"OpenGL", "SDL", "GLEW", "Other"};
void Utility::printWarning(ERROR_TYPE type, std::string msg) { void Utility::printWarning(ERROR_TYPE type, std::string msg)
{
std::cout << "Warning[" << Errornames[type] << "]: " << msg << std::endl; 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; 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; std::cout << "Info[" << Errornames[type] << "]: " << msg << std::endl;
} }

View File

@@ -10,10 +10,12 @@
#include "imgui.h" #include "imgui.h"
#endif #endif
namespace Utility{ namespace Utility
{
#ifndef NDEBUG #ifndef NDEBUG
namespace Debug { namespace Debug
{
// Usage: // Usage:
// static ExampleAppLog my_log; // static ExampleAppLog my_log;
@@ -44,10 +46,16 @@ struct ImGuiLogger
extern ImGuiLogger openGLLogger; extern ImGuiLogger openGLLogger;
} } // namespace Debug
#endif //NDEBUG #endif //NDEBUG
enum ERROR_TYPE { OpenGL = 0, SDL, GLEW, Other}; enum ERROR_TYPE
{
OpenGL = 0,
SDL,
GLEW,
Other
};
void APIENTRY openglCallbackFunction(GLenum source, void APIENTRY openglCallbackFunction(GLenum source,
GLenum type, GLenum type,
@@ -57,13 +65,11 @@ void APIENTRY openglCallbackFunction(GLenum source,
const GLchar *message, const GLchar *message,
const void *userParam); const void *userParam);
void printWarning(ERROR_TYPE type, std::string msg); void printWarning(ERROR_TYPE type, std::string msg);
void printError(ERROR_TYPE type, std::string msg); void printError(ERROR_TYPE type, std::string msg);
void printInfo(ERROR_TYPE type, std::string msg); void printInfo(ERROR_TYPE type, std::string msg);
} } // namespace Utility
#endif // UTILITY_H #endif // UTILITY_H

View File

@@ -1,7 +1,8 @@
#ifndef POSITION_H #ifndef POSITION_H
#define POSITION_H #define POSITION_H
struct Position { struct Position
{
Position(float x = 0.0f, float y = 0.0f) : x(x), y(y) {} Position(float x = 0.0f, float y = 0.0f) : x(x), y(y) {}
float x, y; float x, y;

View File

@@ -2,22 +2,27 @@
#include "../DesktopLauncher.h" #include "../DesktopLauncher.h"
void QuitCommand::execute(OrtographicCamera& camera) { void QuitCommand::execute(OrtographicCamera &camera)
{
Game::exit(); Game::exit();
} }
void WCommand::execute(OrtographicCamera& camera) { void WCommand::execute(OrtographicCamera &camera)
{
camera.zoomIn(0.01f); camera.zoomIn(0.01f);
} }
void ACommand::execute(OrtographicCamera& camera) { void ACommand::execute(OrtographicCamera &camera)
{
camera.translate(glm::vec2(-0.1f, 0.0f)); camera.translate(glm::vec2(-0.1f, 0.0f));
} }
void SCommand::execute(OrtographicCamera& camera) { void SCommand::execute(OrtographicCamera &camera)
{
camera.zoomIn(-0.01f); camera.zoomIn(-0.01f);
} }
void DCommand::execute(OrtographicCamera& camera) { void DCommand::execute(OrtographicCamera &camera)
{
camera.translate(glm::vec2(0.1f, 0.0f)); camera.translate(glm::vec2(0.1f, 0.0f));
} }

View File

@@ -3,33 +3,38 @@
#include "../Camera.h" #include "../Camera.h"
class Command { class Command
{
public: public:
virtual ~Command() {} virtual ~Command() {}
virtual void execute(OrtographicCamera &camera) = 0; virtual void execute(OrtographicCamera &camera) = 0;
}; };
class QuitCommand : public Command { class QuitCommand : public Command
{
public: public:
virtual void execute(OrtographicCamera &camera) override; virtual void execute(OrtographicCamera &camera) override;
}; };
class WCommand : public Command { class WCommand : public Command
{
public: public:
virtual void execute(OrtographicCamera &camera) override; virtual void execute(OrtographicCamera &camera) override;
}; };
class ACommand : public Command { class ACommand : public Command
{
public: public:
virtual void execute(OrtographicCamera &camera) override; virtual void execute(OrtographicCamera &camera) override;
}; };
class SCommand : public Command { class SCommand : public Command
{
public: public:
virtual void execute(OrtographicCamera &camera) override; virtual void execute(OrtographicCamera &camera) override;
}; };
class DCommand : public Command { class DCommand : public Command
{
public: public:
virtual void execute(OrtographicCamera &camera) override; virtual void execute(OrtographicCamera &camera) override;
}; };
#endif #endif

View File

@@ -2,7 +2,8 @@
#include <iostream> #include <iostream>
InputHandler::InputHandler() { InputHandler::InputHandler()
{
W = new WCommand(); W = new WCommand();
A = new ACommand(); A = new ACommand();
S = new SCommand(); S = new SCommand();
@@ -10,7 +11,8 @@ InputHandler::InputHandler() {
quit = new QuitCommand(); quit = new QuitCommand();
} }
InputHandler::~InputHandler() { InputHandler::~InputHandler()
{
//Free all Commands //Free all Commands
delete W; delete W;
delete A; delete A;
@@ -19,11 +21,14 @@ InputHandler::~InputHandler() {
delete quit; delete quit;
} }
Command* InputHandler::handleInput(SDL_Event *event) { Command *InputHandler::handleInput(SDL_Event *event)
if(event == NULL) { {
if (event == NULL)
{
return NULL; return NULL;
} }
switch (event->type) { switch (event->type)
{
case SDL_QUIT: case SDL_QUIT:
return quit; return quit;
break; break;
@@ -33,7 +38,8 @@ Command* InputHandler::handleInput(SDL_Event *event) {
break; break;
case SDL_KEYUP: case SDL_KEYUP:
std::cout << "The Key " << SDL_GetKeyName(event->key.keysym.sym) << " has been released!" << std::endl; std::cout << "The Key " << SDL_GetKeyName(event->key.keysym.sym) << " has been released!" << std::endl;
switch(event->key.keysym.sym) { switch (event->key.keysym.sym)
{
case SDLK_w: case SDLK_w:
return W; return W;
break; break;
@@ -46,7 +52,8 @@ Command* InputHandler::handleInput(SDL_Event *event) {
case SDLK_d: case SDLK_d:
return D; return D;
break; break;
default: break; default:
break;
} }
return NULL; return NULL;
break; break;

View File

@@ -4,7 +4,8 @@
#include "Command.h" #include "Command.h"
class InputHandler { class InputHandler
{
public: public:
~InputHandler(); ~InputHandler();
InputHandler(); InputHandler();

View File

@@ -4,24 +4,21 @@
#include <iostream> #include <iostream>
GameScreen::GameScreen(Renderer* renderer, Assets* assets) { GameScreen::GameScreen(Renderer *renderer, Assets *assets)
{
std::cout << "GameScreen got created!" << std::endl; std::cout << "GameScreen got created!" << std::endl;
this->renderer = renderer; this->renderer = renderer;
this->assets = assets; this->assets = assets;
//Vertex2D can be defined like this, because it an aggregate class. //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 // see https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special
std::vector<Vertex2D> vertices({ std::vector<Vertex2D> vertices({Vertex2D{glm::vec2(-0.5f, -0.5f), glm::vec2(0.0f, 1.0f)},
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, 1.0f)},
Vertex2D{glm::vec2(0.5f, 0.5f), glm::vec2(1.0f, 0.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)} Vertex2D{glm::vec2(-0.5f, 0.5f), glm::vec2(0.0f, 0.0f)}});
});
std::vector<unsigned int> indices({ std::vector<unsigned int> indices({0, 1, 3,
0,1,3, 1, 2, 3});
1,2,3
});
obj = new Mesh(vertices, indices, assets->getTexture("police_officer")); obj = new Mesh(vertices, indices, assets->getTexture("police_officer"));
@@ -34,21 +31,23 @@ GameScreen::GameScreen(Renderer* renderer, Assets* assets) {
//charakter.assign<Position>(); //charakter.assign<Position>();
} }
GameScreen::~GameScreen() { GameScreen::~GameScreen()
{
delete camera; delete camera;
delete obj; delete obj;
delete inputHandler; delete inputHandler;
} }
void GameScreen::hide() { void GameScreen::hide()
{
} }
void GameScreen::pause() { void GameScreen::pause()
{
} }
void GameScreen::render() { void GameScreen::render()
{
//Drawing //Drawing
Shader defaultShader = assets->getShader("defaultShader"); Shader defaultShader = assets->getShader("defaultShader");
defaultShader.SetMatrix4("MVP", camera->getCombinedMatrix(), true); defaultShader.SetMatrix4("MVP", camera->getCombinedMatrix(), true);
@@ -56,24 +55,27 @@ void GameScreen::render() {
renderer->draw(*obj, defaultShader); renderer->draw(*obj, defaultShader);
} }
void GameScreen::update() { void GameScreen::update()
{
SDL_Event event; SDL_Event event;
while(SDL_PollEvent(&event) != 0) { while (SDL_PollEvent(&event) != 0)
{
Command *command = inputHandler->handleInput(&event); Command *command = inputHandler->handleInput(&event);
if (command) { if (command)
{
command->execute(*camera); command->execute(*camera);
} }
} }
} }
void GameScreen::resize() { void GameScreen::resize()
{
} }
void GameScreen::resume() { void GameScreen::resume()
{
} }
void GameScreen::show() { void GameScreen::show()
{
} }

View File

@@ -8,7 +8,8 @@
#include "../Camera.h" #include "../Camera.h"
#include "../input/InputHandler.h" #include "../input/InputHandler.h"
class GameScreen : public Screen { class GameScreen : public Screen
{
public: public:
GameScreen(Renderer *renderer, Assets *assets); GameScreen(Renderer *renderer, Assets *assets);
~GameScreen(); ~GameScreen();

View File

@@ -1,7 +1,8 @@
#ifndef SCREEN_H #ifndef SCREEN_H
#define SCREEN_H #define SCREEN_H
class Screen { class Screen
{
protected: protected:
Screen() {} Screen() {}
@@ -16,7 +17,6 @@ class Screen {
virtual void resize() = 0; virtual void resize() = 0;
virtual void resume() = 0; virtual void resume() = 0;
virtual void show() = 0; virtual void show() = 0;
}; };
#endif #endif