Some test inputs and commentarys
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 13 KiB |
@@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
OrtographicCamera::~OrtographicCamera() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void OrtographicCamera::setPosition(glm::vec2 position) {
|
void OrtographicCamera::setPosition(glm::vec2 position) {
|
||||||
this->cameraPos = position;
|
this->cameraPos = position;
|
||||||
}
|
}
|
||||||
@@ -19,14 +15,14 @@ void OrtographicCamera::setZoomLevel(float newZoom) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void OrtographicCamera::zoomIn(float targetZoom) {
|
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)
|
//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;
|
||||||
|
int DPI = 2; // 200% windows settings
|
||||||
glm::mat4 projection;
|
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;
|
return projection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
src/Camera.h
10
src/Camera.h
@@ -6,12 +6,10 @@
|
|||||||
class OrtographicCamera {
|
class OrtographicCamera {
|
||||||
private:
|
private:
|
||||||
glm::vec2 cameraPos;
|
glm::vec2 cameraPos;
|
||||||
|
float zoom;
|
||||||
float zoom = 0.05f;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
OrtographicCamera() : cameraPos(glm::vec2(-5.0f,-5.0f)) {}
|
OrtographicCamera() : cameraPos(glm::vec2(-5.0f,-5.0f)), zoom(0.05f) {}
|
||||||
~OrtographicCamera();
|
|
||||||
|
|
||||||
void setPosition(glm::vec2);
|
void setPosition(glm::vec2);
|
||||||
glm::vec2 getPosition();
|
glm::vec2 getPosition();
|
||||||
@@ -19,8 +17,8 @@ class OrtographicCamera {
|
|||||||
void setZoomLevel(float newZoom);
|
void setZoomLevel(float newZoom);
|
||||||
void zoomIn(float targetZoom);
|
void zoomIn(float targetZoom);
|
||||||
|
|
||||||
void translate(glm::vec2);
|
void translate(glm::vec2 target);
|
||||||
void smoothTranslate(glm::vec2);
|
void smoothTranslate(glm::vec2 target);
|
||||||
|
|
||||||
glm::mat4 getViewMatrix();
|
glm::mat4 getViewMatrix();
|
||||||
glm::mat4 getProjectionMatrix();
|
glm::mat4 getProjectionMatrix();
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ DarkRP2D::~DarkRP2D() {
|
|||||||
#endif
|
#endif
|
||||||
delete gameScreen;
|
delete gameScreen;
|
||||||
delete assets;
|
delete assets;
|
||||||
delete inputHandler;
|
|
||||||
delete renderer;
|
delete renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +30,6 @@ void DarkRP2D::create () {
|
|||||||
|
|
||||||
renderer = new Renderer();
|
renderer = new Renderer();
|
||||||
|
|
||||||
inputHandler = new InputHandler();
|
|
||||||
font = new BitmapFont("arial.ttf");
|
font = new BitmapFont("arial.ttf");
|
||||||
|
|
||||||
gameScreen = new GameScreen(renderer,assets);
|
gameScreen = new GameScreen(renderer,assets);
|
||||||
@@ -67,13 +65,6 @@ void DarkRP2D::loop(unsigned int delta) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DarkRP2D::update(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();
|
gameScreen->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ class DarkRP2D {
|
|||||||
SDL_Window* gWindow;
|
SDL_Window* gWindow;
|
||||||
Assets *assets;
|
Assets *assets;
|
||||||
Renderer* renderer;
|
Renderer* renderer;
|
||||||
InputHandler *inputHandler;
|
|
||||||
BitmapFont *font;
|
BitmapFont *font;
|
||||||
Screen *gameScreen;
|
Screen *gameScreen;
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,8 @@ namespace Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool initSDL() {
|
bool initSDL() {
|
||||||
|
int result = SetProcessDPIAware(); // NEEDED or SDL_GetDisplayDPI returns wrong numbers
|
||||||
|
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;
|
||||||
@@ -62,7 +64,10 @@ bool initSDL() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//Read in Display DPI
|
//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;
|
cout << "DPI: DDPI: " << Game::DDPI << " HDPI: " << Game::HDPI << " VPDI: " << Game::VDPI << endl;
|
||||||
//Mask deprectated functions
|
//Mask deprectated functions
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
//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 position;
|
||||||
glm::vec2 texCoord;
|
glm::vec2 texCoord;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
|
|
||||||
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_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);
|
glGenTextures(1, &this->ID);
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/input/Command.cpp
Normal file
23
src/input/Command.cpp
Normal 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));
|
||||||
|
}
|
||||||
@@ -1,10 +1,34 @@
|
|||||||
#ifndef COMMAND_H
|
#ifndef COMMAND_H
|
||||||
#define COMMAND_H
|
#define COMMAND_H
|
||||||
|
|
||||||
|
#include "../Camera.h"
|
||||||
|
|
||||||
class Command {
|
class Command {
|
||||||
public:
|
public:
|
||||||
virtual ~Command() {}
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,22 @@
|
|||||||
#include "InputHandler.h"
|
#include "InputHandler.h"
|
||||||
#include "../DesktopLauncher.h"
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
InputHandler::InputHandler() {
|
InputHandler::InputHandler() {
|
||||||
|
W = new WCommand();
|
||||||
|
A = new ACommand();
|
||||||
|
S = new SCommand();
|
||||||
|
D = new DCommand();
|
||||||
|
quit = new QuitCommand();
|
||||||
}
|
}
|
||||||
|
|
||||||
InputHandler::~InputHandler() {
|
InputHandler::~InputHandler() {
|
||||||
//Free all Commands
|
//Free all Commands
|
||||||
|
delete W;
|
||||||
|
delete A;
|
||||||
|
delete S;
|
||||||
|
delete D;
|
||||||
|
delete quit;
|
||||||
}
|
}
|
||||||
|
|
||||||
Command* InputHandler::handleInput(SDL_Event *event) {
|
Command* InputHandler::handleInput(SDL_Event *event) {
|
||||||
@@ -17,8 +25,7 @@ Command* InputHandler::handleInput(SDL_Event *event) {
|
|||||||
}
|
}
|
||||||
switch (event->type) {
|
switch (event->type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
Game::exit();
|
return quit;
|
||||||
return NULL;
|
|
||||||
break;
|
break;
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
std::cout << "The Key " << SDL_GetKeyName(event->key.keysym.sym) << " has been pressed!" << std::endl;
|
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;
|
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) {
|
||||||
|
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;
|
return NULL;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -15,5 +15,6 @@ class InputHandler {
|
|||||||
Command* A;
|
Command* A;
|
||||||
Command* S;
|
Command* S;
|
||||||
Command* D;
|
Command* D;
|
||||||
|
Command* quit;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "GameScreen.h"
|
#include "GameScreen.h"
|
||||||
#include <entityx/entityx.h>
|
#include <entityx/entityx.h>
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -8,6 +9,8 @@ GameScreen::GameScreen(Renderer* renderer, Assets* assets) {
|
|||||||
this->renderer = renderer;
|
this->renderer = renderer;
|
||||||
this->assets = assets;
|
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({
|
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)},
|
||||||
@@ -22,6 +25,7 @@ GameScreen::GameScreen(Renderer* renderer, Assets* assets) {
|
|||||||
|
|
||||||
obj = new Mesh(vertices,indices,assets->getTexture("police_officer"));
|
obj = new Mesh(vertices,indices,assets->getTexture("police_officer"));
|
||||||
|
|
||||||
|
inputHandler = new InputHandler();
|
||||||
camera = new OrtographicCamera();
|
camera = new OrtographicCamera();
|
||||||
|
|
||||||
//Create Entities
|
//Create Entities
|
||||||
@@ -33,6 +37,7 @@ GameScreen::GameScreen(Renderer* renderer, Assets* assets) {
|
|||||||
GameScreen::~GameScreen() {
|
GameScreen::~GameScreen() {
|
||||||
delete camera;
|
delete camera;
|
||||||
delete obj;
|
delete obj;
|
||||||
|
delete inputHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameScreen::hide() {
|
void GameScreen::hide() {
|
||||||
@@ -52,7 +57,13 @@ void GameScreen::render() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GameScreen::update() {
|
void GameScreen::update() {
|
||||||
|
SDL_Event event;
|
||||||
|
while(SDL_PollEvent(&event) != 0) {
|
||||||
|
Command* command = inputHandler->handleInput(&event);
|
||||||
|
if (command) {
|
||||||
|
command->execute(*camera);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameScreen::resize() {
|
void GameScreen::resize() {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "../Assets.h"
|
#include "../Assets.h"
|
||||||
#include "../Mesh.h"
|
#include "../Mesh.h"
|
||||||
#include "../Camera.h"
|
#include "../Camera.h"
|
||||||
|
#include "../input/InputHandler.h"
|
||||||
|
|
||||||
class GameScreen : public Screen {
|
class GameScreen : public Screen {
|
||||||
public:
|
public:
|
||||||
@@ -23,6 +24,7 @@ class GameScreen : public Screen {
|
|||||||
private:
|
private:
|
||||||
Renderer* renderer;
|
Renderer* renderer;
|
||||||
Assets* assets;
|
Assets* assets;
|
||||||
|
InputHandler* inputHandler;
|
||||||
|
|
||||||
OrtographicCamera *camera;
|
OrtographicCamera *camera;
|
||||||
Mesh *obj;
|
Mesh *obj;
|
||||||
|
|||||||
Reference in New Issue
Block a user