Formatted classes
This commit is contained in:
11
src/Assets.h
11
src/Assets.h
@@ -9,15 +9,16 @@
|
||||
#include "Texture.h"
|
||||
#include "Shader.h"
|
||||
|
||||
class Assets {
|
||||
private:
|
||||
class Assets
|
||||
{
|
||||
private:
|
||||
std::map<std::string, Texture2D> textures;
|
||||
std::map<std::string, Shader> 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
|
||||
|
||||
@@ -6,20 +6,25 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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<GLchar, Character>(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
|
||||
|
||||
@@ -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<GLchar, Character> characters;
|
||||
GLuint VAO, VBO;
|
||||
glm::mat4 projection;
|
||||
|
||||
@@ -2,52 +2,60 @@
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
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()
|
||||
{
|
||||
}
|
||||
@@ -3,13 +3,14 @@
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
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();
|
||||
|
||||
108
src/DarkRP2D.cpp
108
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 <GL/glew.h>
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
@@ -5,31 +5,29 @@ class DarkRP2D;
|
||||
#include <SDL.h>
|
||||
#include <vector>
|
||||
|
||||
|
||||
#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
|
||||
|
||||
@@ -4,23 +4,24 @@
|
||||
#include <SDL_image.h>
|
||||
#include <GL/glew.h>
|
||||
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
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
|
||||
31
src/Mesh.cpp
31
src/Mesh.cpp
@@ -3,8 +3,8 @@
|
||||
#include <GL/glew.h>
|
||||
#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->vertices = vertices;
|
||||
this->indices = indices;
|
||||
@@ -12,22 +12,23 @@ Mesh::Mesh(std::vector<Vertex2D> vertices, std::vector<unsigned int> 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();
|
||||
}
|
||||
|
||||
18
src/Mesh.h
18
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<Vertex2D> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
Texture2D texture;
|
||||
std::vector<Vertex2D> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
Texture2D texture;
|
||||
|
||||
Mesh(std::vector<Vertex2D> vertices, std::vector<unsigned int> indices,Texture2D textures);
|
||||
Mesh(std::vector<Vertex2D> vertices, std::vector<unsigned int> indices, Texture2D textures);
|
||||
~Mesh();
|
||||
|
||||
unsigned int getIndicesCount() const;
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
#include "Renderer.h"
|
||||
#include <GL/glew.h>
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -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
|
||||
@@ -2,12 +2,14 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
36
src/Shader.h
36
src/Shader.h
@@ -7,34 +7,34 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
|
||||
// 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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
184
src/Utility.cpp
184
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;
|
||||
}
|
||||
@@ -7,13 +7,15 @@
|
||||
#include <string>
|
||||
|
||||
#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<int> 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<int> 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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,76 +4,78 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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<Vertex2D> 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<unsigned int> indices({
|
||||
0,1,3,
|
||||
1,2,3
|
||||
});
|
||||
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(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<unsigned int> 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<Position>();
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user