Added camera operations and better input handling
This commit is contained in:
@@ -3,9 +3,12 @@
|
|||||||
layout (location = 0) in vec2 aPos;
|
layout (location = 0) in vec2 aPos;
|
||||||
layout (location = 1) in vec2 texCoord;
|
layout (location = 1) in vec2 texCoord;
|
||||||
|
|
||||||
|
uniform mat4 projection;
|
||||||
|
uniform mat4 view;
|
||||||
|
|
||||||
out vec2 TexCoord;
|
out vec2 TexCoord;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = vec4(aPos.x,aPos.y,0.,1.0);
|
gl_Position = projection * view * vec4(aPos.xy,0.0,1.0);
|
||||||
TexCoord = texCoord;
|
TexCoord = texCoord;
|
||||||
}
|
}
|
||||||
|
|||||||
49
src/Camera.cpp
Normal file
49
src/Camera.cpp
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include "Camera.h"
|
||||||
|
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
|
OrtographicCamera::~OrtographicCamera() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void OrtographicCamera::setPosition(glm::vec2 position) {
|
||||||
|
this->cameraPos = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec2 OrtographicCamera::getPosition() {
|
||||||
|
return this->cameraPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OrtographicCamera::setZoomLevel(float newZoom) {
|
||||||
|
this->zoom = newZoom;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OrtographicCamera::zoomIn(float targetZoom) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void OrtographicCamera::translate(glm::vec2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void OrtographicCamera::smoothTranslate(glm::vec2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::mat4 OrtographicCamera::getViewMatrix() {
|
||||||
|
glm::mat4 view(1);
|
||||||
|
view = glm::translate(view, glm::vec3(-cameraPos,0.f));
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::mat4 OrtographicCamera::getProjectionMatrix() {
|
||||||
|
int width = 800;
|
||||||
|
int height = 600;
|
||||||
|
glm::mat4 projection;
|
||||||
|
projection = glm::ortho(0.0f, width*this->zoom, 0.0f, height*this->zoom);
|
||||||
|
return projection;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OrtographicCamera::update() {
|
||||||
|
|
||||||
|
}
|
||||||
31
src/Camera.h
Normal file
31
src/Camera.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef CAMERA_H
|
||||||
|
#define CAMERA_H
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
class OrtographicCamera {
|
||||||
|
private:
|
||||||
|
glm::vec2 cameraPos;
|
||||||
|
|
||||||
|
float zoom = 0.01f;
|
||||||
|
|
||||||
|
public:
|
||||||
|
OrtographicCamera() : cameraPos(glm::vec2(-5.0f,-5.0f)) {}
|
||||||
|
~OrtographicCamera();
|
||||||
|
|
||||||
|
void setPosition(glm::vec2);
|
||||||
|
glm::vec2 getPosition();
|
||||||
|
|
||||||
|
void setZoomLevel(float newZoom);
|
||||||
|
void zoomIn(float targetZoom);
|
||||||
|
|
||||||
|
void translate(glm::vec2);
|
||||||
|
void smoothTranslate(glm::vec2);
|
||||||
|
|
||||||
|
glm::mat4 getViewMatrix();
|
||||||
|
glm::mat4 getProjectionMatrix();
|
||||||
|
|
||||||
|
void update();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -8,6 +8,7 @@ DarkRP2D::~DarkRP2D() {
|
|||||||
delete assets;
|
delete assets;
|
||||||
delete obj;
|
delete obj;
|
||||||
delete inputHandler;
|
delete inputHandler;
|
||||||
|
delete camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DarkRP2D::create () {
|
void DarkRP2D::create () {
|
||||||
@@ -32,17 +33,19 @@ void DarkRP2D::create () {
|
|||||||
inputHandler = new InputHandler();
|
inputHandler = new InputHandler();
|
||||||
font = new BitmapFont("arial.ttf");
|
font = new BitmapFont("arial.ttf");
|
||||||
|
|
||||||
|
camera = new OrtographicCamera();
|
||||||
|
|
||||||
glClearColor(1.f,1.f,1.f,1.f);
|
glClearColor(1.f,1.f,1.f,1.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DarkRP2D::loop(SDL_Event *event, 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(event, delta);
|
update(delta);
|
||||||
ups++;
|
ups++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,8 +59,16 @@ void DarkRP2D::loop(SDL_Event *event, unsigned int delta) {
|
|||||||
fps++;
|
fps++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DarkRP2D::update(SDL_Event *event, unsigned int delta) {
|
void DarkRP2D::update(unsigned int delta) {
|
||||||
inputHandler->handleInput(event);
|
SDL_Event event;
|
||||||
|
while(SDL_PollEvent(&event) != 0) {
|
||||||
|
Command* command = inputHandler->handleInput(&event);
|
||||||
|
if (command) {
|
||||||
|
command->execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//camera update
|
||||||
}
|
}
|
||||||
|
|
||||||
void DarkRP2D::render(unsigned int delta) {
|
void DarkRP2D::render(unsigned int delta) {
|
||||||
@@ -65,8 +76,9 @@ void DarkRP2D::render(unsigned int delta) {
|
|||||||
|
|
||||||
//Drawing
|
//Drawing
|
||||||
Shader defaultShader = assets->getShader("defaultShader");
|
Shader defaultShader = assets->getShader("defaultShader");
|
||||||
defaultShader.Use();
|
defaultShader.SetMatrix4("view",camera->getViewMatrix(),true);
|
||||||
//Draw Triangle
|
defaultShader.SetMatrix4("projection",camera->getProjectionMatrix(),true);
|
||||||
|
//Draw Mesh
|
||||||
obj->draw();
|
obj->draw();
|
||||||
|
|
||||||
//texttest
|
//texttest
|
||||||
|
|||||||
@@ -4,10 +4,12 @@
|
|||||||
class DarkRP2D;
|
class DarkRP2D;
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Assets.h"
|
#include "Assets.h"
|
||||||
#include "Mesh.h"
|
#include "Mesh.h"
|
||||||
#include "InputHandler.h"
|
#include "InputHandler.h"
|
||||||
#include "BitmapFont.h"
|
#include "BitmapFont.h"
|
||||||
|
#include "Camera.h"
|
||||||
|
|
||||||
class DarkRP2D {
|
class DarkRP2D {
|
||||||
private:
|
private:
|
||||||
@@ -16,14 +18,15 @@ class DarkRP2D {
|
|||||||
Mesh *obj;
|
Mesh *obj;
|
||||||
InputHandler *inputHandler;
|
InputHandler *inputHandler;
|
||||||
BitmapFont *font;
|
BitmapFont *font;
|
||||||
|
OrtographicCamera *camera;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
DarkRP2D(SDL_Window *gWindow) : gWindow(gWindow) {}
|
DarkRP2D(SDL_Window *gWindow) : gWindow(gWindow) {}
|
||||||
~DarkRP2D();
|
~DarkRP2D();
|
||||||
void create ();
|
void create ();
|
||||||
void loop(SDL_Event *event, unsigned int delta);
|
void loop(unsigned int delta);
|
||||||
void update(SDL_Event *event, 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);
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
//While application is running
|
//While application is running
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
SDL_Event e;
|
|
||||||
DarkRP2D resA(gWindow);
|
DarkRP2D resA(gWindow);
|
||||||
resA.create();
|
resA.create();
|
||||||
unsigned int lastTime = SDL_GetTicks(), currentTime, elapsedTime;
|
unsigned int lastTime = SDL_GetTicks(), currentTime, elapsedTime;
|
||||||
@@ -33,16 +32,7 @@ int main(int argc, char *argv[]) {
|
|||||||
currentTime = SDL_GetTicks();
|
currentTime = SDL_GetTicks();
|
||||||
elapsedTime = currentTime - lastTime;
|
elapsedTime = currentTime - lastTime;
|
||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
if(SDL_PollEvent(&e) != 0) {
|
resA.loop(elapsedTime);
|
||||||
if(e.type == SDL_QUIT) {
|
|
||||||
quit = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
resA.loop(&e,elapsedTime);
|
|
||||||
} else {
|
|
||||||
resA.loop(NULL,elapsedTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose();
|
dispose();
|
||||||
|
|||||||
@@ -10,18 +10,21 @@ InputHandler::~InputHandler() {
|
|||||||
//Free all Commands
|
//Free all Commands
|
||||||
}
|
}
|
||||||
|
|
||||||
void InputHandler::handleInput(SDL_Event *event) {
|
Command* InputHandler::handleInput(SDL_Event *event) {
|
||||||
if(event == NULL) {
|
if(event == NULL) {
|
||||||
return;
|
return NULL;
|
||||||
}
|
}
|
||||||
switch (event->type) {
|
switch (event->type) {
|
||||||
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;
|
||||||
|
return NULL;
|
||||||
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;
|
||||||
|
return NULL;
|
||||||
break;
|
break;
|
||||||
default: break;
|
default:
|
||||||
|
return NULL;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
|
|
||||||
class InputHandler {
|
class InputHandler {
|
||||||
public:
|
public:
|
||||||
void handleInput(SDL_Event *event);
|
|
||||||
~InputHandler();
|
~InputHandler();
|
||||||
InputHandler();
|
InputHandler();
|
||||||
|
Command* handleInput(SDL_Event *event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Command* W;
|
Command* W;
|
||||||
|
|||||||
@@ -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), 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), Filter_Max(GL_LINEAR)
|
||||||
{
|
{
|
||||||
glGenTextures(1, &this->ID);
|
glGenTextures(1, &this->ID);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user