Some test inputs and commentarys

This commit is contained in:
Julian Nießner
2018-05-25 12:13:06 +02:00
parent 3169790299
commit ae5fa3f2aa
14 changed files with 108 additions and 33 deletions

View File

@@ -2,10 +2,6 @@
#include <glm/gtc/matrix_transform.hpp>
OrtographicCamera::~OrtographicCamera() {
}
void OrtographicCamera::setPosition(glm::vec2 position) {
this->cameraPos = position;
}
@@ -19,14 +15,14 @@ void OrtographicCamera::setZoomLevel(float newZoom) {
}
void OrtographicCamera::zoomIn(float targetZoom) {
zoom -= targetZoom;
}
void OrtographicCamera::translate(glm::vec2) {
void OrtographicCamera::translate(glm::vec2 target) {
cameraPos = cameraPos - target;
}
void OrtographicCamera::smoothTranslate(glm::vec2) {
void OrtographicCamera::smoothTranslate(glm::vec2 target) {
}
@@ -40,8 +36,9 @@ 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*this->zoom, 0.0f, height*this->zoom);
projection = glm::ortho(0.0f, (width/DPI)*this->zoom, 0.0f, (height/DPI)*this->zoom);
return projection;
}

View File

@@ -6,12 +6,10 @@
class OrtographicCamera {
private:
glm::vec2 cameraPos;
float zoom = 0.05f;
float zoom;
public:
OrtographicCamera() : cameraPos(glm::vec2(-5.0f,-5.0f)) {}
~OrtographicCamera();
OrtographicCamera() : cameraPos(glm::vec2(-5.0f,-5.0f)), zoom(0.05f) {}
void setPosition(glm::vec2);
glm::vec2 getPosition();
@@ -19,8 +17,8 @@ class OrtographicCamera {
void setZoomLevel(float newZoom);
void zoomIn(float targetZoom);
void translate(glm::vec2);
void smoothTranslate(glm::vec2);
void translate(glm::vec2 target);
void smoothTranslate(glm::vec2 target);
glm::mat4 getViewMatrix();
glm::mat4 getProjectionMatrix();

View File

@@ -18,7 +18,6 @@ DarkRP2D::~DarkRP2D() {
#endif
delete gameScreen;
delete assets;
delete inputHandler;
delete renderer;
}
@@ -31,7 +30,6 @@ void DarkRP2D::create () {
renderer = new Renderer();
inputHandler = new InputHandler();
font = new BitmapFont("arial.ttf");
gameScreen = new GameScreen(renderer,assets);
@@ -67,13 +65,6 @@ void DarkRP2D::loop(unsigned int delta) {
}
void DarkRP2D::update(unsigned int delta) {
SDL_Event event;
while(SDL_PollEvent(&event) != 0) {
Command* command = inputHandler->handleInput(&event);
if (command) {
command->execute();
}
}
gameScreen->update();
}

View File

@@ -17,7 +17,6 @@ class DarkRP2D {
SDL_Window* gWindow;
Assets *assets;
Renderer* renderer;
InputHandler *inputHandler;
BitmapFont *font;
Screen *gameScreen;

View File

@@ -49,6 +49,8 @@ namespace Game {
}
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 ) {
cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
@@ -62,7 +64,10 @@ bool initSDL() {
return false;
}
//Read in Display DPI
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) {
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);

View File

@@ -5,6 +5,8 @@
#include <glm/glm.hpp>
#include <vector>
//Aggregate class -> so following construction is available
//Vertex2D{glm::vec2(-0.5f,-0.5f), glm::vec2(0.0f,1.0f)}
struct Vertex2D {
glm::vec2 position;
glm::vec2 texCoord;

View File

@@ -4,7 +4,7 @@
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_NEAREST_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_NEAREST)
{
glGenTextures(1, &this->ID);
}

23
src/input/Command.cpp Normal file
View File

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

View File

@@ -1,10 +1,34 @@
#ifndef COMMAND_H
#define COMMAND_H
#include "../Camera.h"
class Command {
public:
virtual ~Command() {}
virtual void execute() = 0;
virtual void execute(OrtographicCamera& camera) = 0;
};
class QuitCommand : 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 SCommand : public Command {
public:
virtual void execute(OrtographicCamera& camera) override;
};
class DCommand : public Command {
public:
virtual void execute(OrtographicCamera& camera) override;
};

View File

@@ -1,14 +1,22 @@
#include "InputHandler.h"
#include "../DesktopLauncher.h"
#include <iostream>
InputHandler::InputHandler() {
W = new WCommand();
A = new ACommand();
S = new SCommand();
D = new DCommand();
quit = new QuitCommand();
}
InputHandler::~InputHandler() {
//Free all Commands
delete W;
delete A;
delete S;
delete D;
delete quit;
}
Command* InputHandler::handleInput(SDL_Event *event) {
@@ -17,8 +25,7 @@ Command* InputHandler::handleInput(SDL_Event *event) {
}
switch (event->type) {
case SDL_QUIT:
Game::exit();
return NULL;
return quit;
break;
case SDL_KEYDOWN:
std::cout << "The Key " << SDL_GetKeyName(event->key.keysym.sym) << " has been pressed!" << std::endl;
@@ -26,6 +33,21 @@ Command* InputHandler::handleInput(SDL_Event *event) {
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;
break;
default:

View File

@@ -15,5 +15,6 @@ class InputHandler {
Command* A;
Command* S;
Command* D;
Command* quit;
};
#endif

View File

@@ -1,5 +1,6 @@
#include "GameScreen.h"
#include <entityx/entityx.h>
#include <SDL.h>
#include <iostream>
@@ -8,6 +9,8 @@ GameScreen::GameScreen(Renderer* renderer, Assets* assets) {
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)},
@@ -22,6 +25,7 @@ GameScreen::GameScreen(Renderer* renderer, Assets* assets) {
obj = new Mesh(vertices,indices,assets->getTexture("police_officer"));
inputHandler = new InputHandler();
camera = new OrtographicCamera();
//Create Entities
@@ -33,6 +37,7 @@ GameScreen::GameScreen(Renderer* renderer, Assets* assets) {
GameScreen::~GameScreen() {
delete camera;
delete obj;
delete inputHandler;
}
void GameScreen::hide() {
@@ -52,7 +57,13 @@ void GameScreen::render() {
}
void GameScreen::update() {
SDL_Event event;
while(SDL_PollEvent(&event) != 0) {
Command* command = inputHandler->handleInput(&event);
if (command) {
command->execute(*camera);
}
}
}
void GameScreen::resize() {

View File

@@ -6,6 +6,7 @@
#include "../Assets.h"
#include "../Mesh.h"
#include "../Camera.h"
#include "../input/InputHandler.h"
class GameScreen : public Screen {
public:
@@ -23,6 +24,7 @@ class GameScreen : public Screen {
private:
Renderer* renderer;
Assets* assets;
InputHandler* inputHandler;
OrtographicCamera *camera;
Mesh *obj;