Added inputSystem

This commit is contained in:
Gulum
2017-10-24 15:58:03 +02:00
parent eb5c1e4ec3
commit 70857cb258
10 changed files with 171 additions and 19 deletions

View File

@@ -2,17 +2,14 @@
#include <SDL2/SDL_image.h>
#include "DarkRP2D.h"
#include "utility/utility.h"
#include "utility/Utility.h"
void DarkRP2D::create(SDL_Renderer *gRenderer) {
void DarkRP2D::create(SDL_Renderer *gRenderer, InputHandler *inputHandler) {
this->gRenderer = gRenderer;
this->inputHandler = inputHandler;
//Load PNG texture
gTexture = utility::loadTexture( "assets/police_officer.png", gRenderer);
}
}
unsigned int ups = 0;
unsigned int acc = 0;
@@ -20,7 +17,10 @@
accumulator += deltaTime;
acc += deltaTime;
//Process Input
Command *command = inputHandler->handleInput();
if (command) {
command->execute();
}
skippedFrames = 0;
while (accumulator >= MS_PER_UPDATE && skippedFrames <= MAX_FRAMESKIP) {
@@ -46,8 +46,14 @@
//Clear screen
SDL_RenderClear( gRenderer );
SDL_Rect textureRect; //create a rect
textureRect.x = 0; //controls the rect's x coordinate
textureRect.y = 0; // controls the rect's y coordinte
textureRect.w = 64; // controls the width of the rect
textureRect.h = 64; // controls the height of the rect
//Render texture to screen
SDL_RenderCopy( gRenderer, gTexture, NULL, NULL );
SDL_RenderCopy( gRenderer, gTexture, NULL, &textureRect );
//Update screen
SDL_RenderPresent( gRenderer );
@@ -57,8 +63,8 @@
}
void DarkRP2D::dispose() {
//Free loaded image
SDL_DestroyTexture( gTexture );
gTexture = NULL;
DarkRP2D::~DarkRP2D() {
//Free loaded image
SDL_DestroyTexture( gTexture );
gTexture = NULL;
}

View File

@@ -2,6 +2,7 @@
#define DARKRP2D_H
#include <SDL2/SDL.h>
#include "input/InputHandler.h"
class DarkRP2D {
private:
@@ -9,6 +10,7 @@ private:
int skippedFrames = 0;
//test texture
SDL_Texture* gTexture = NULL;
InputHandler *inputHandler;
public:
static const int UPDATES_PER_SECOND = 60;
@@ -18,12 +20,12 @@ public:
SDL_Renderer* gRenderer = NULL;
DarkRP2D() {}
void create(SDL_Renderer *gRenderer);
~DarkRP2D();
void create(SDL_Renderer *gRenderer, InputHandler *inputHandler);
void loop(unsigned int deltaTime);
void update(float deltaInSec);
void render(float deltaInSec);
void resize(int width, int height);
void dispose();
};
#endif // DARKRP2D_H

View File

@@ -2,6 +2,7 @@
#include <SDL2/SDL.h>
#include "DarkRP2D.h"
#include "input/InputHandler.h"
#define GAME_NAME "DarkRP2D"
@@ -11,6 +12,7 @@ SDL_Window* gWindow = NULL; //The window we'll be rendering to
SDL_Surface* gScreenSurface = NULL; //The surface contained by the window
SDL_Renderer* gRenderer = NULL;
DarkRP2D* darkRP2D = NULL;
InputHandler* inputHandler = NULL;
void PrintEvent(const SDL_Event *event) {
if (event->type == SDL_WINDOWEVENT) {
@@ -111,16 +113,16 @@ bool init() {
}
}
}
inputHandler = new InputHandler();
darkRP2D = new DarkRP2D();
darkRP2D->create(gRenderer);
darkRP2D->create(gRenderer, inputHandler);
return success;
}
void close() {
darkRP2D->dispose();
delete darkRP2D;
delete inputHandler;
SDL_DestroyRenderer( gRenderer );
SDL_FreeSurface( gScreenSurface );
SDL_DestroyWindow( gWindow );
@@ -151,6 +153,8 @@ int main(int argc, char *argv[]) {
//User requests quit
if( e.type == SDL_QUIT ) {
running = false;
} else if ((e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) && e.key.repeat == 0) {
inputHandler->inputEvent(&e);
}
PrintEvent(&e);
}
@@ -160,5 +164,5 @@ int main(int argc, char *argv[]) {
close();
return success;
return !success;
}

View File

@@ -0,0 +1,48 @@
#include <cstddef>
#include "InputHandler.h"
InputHandler::InputHandler() {
Command* com = new TestCommand();
buttonW = com;
buttonA = com;
buttonS = com;
buttonD = com;
}
Command* InputHandler::handleInput() {
if(cur == commandStream.size()) {
return NULL;
}
return commandStream[cur++];
}
void InputHandler::inputEvent(SDL_Event *e) {
switch(e->key.keysym.sym) {
case SDLK_w:
commandStream.push_back(buttonW);
break;
case SDLK_a:
commandStream.push_back(buttonA);
break;
case SDLK_s:
commandStream.push_back(buttonS);
break;
case SDLK_d:
commandStream.push_back(buttonD);
break;
default:
break;
}
}
InputHandler::~InputHandler() {
delete buttonW;
/*delete buttonA;
delete buttonS;
delete buttonD;*/
}

26
src/input/InputHandler.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef INPUTHANDLER_H
#define INPUTHANDLER_H
#include <vector>
#include <SDL2/SDL.h>
#include "commands/TestCommand.h"
class InputHandler {
private:
Command* buttonW = NULL;
Command* buttonA = NULL;
Command* buttonS = NULL;
Command* buttonD = NULL;
unsigned int cur = 0;
std::vector<Command*> commandStream;
public:
InputHandler();
~InputHandler();
void inputEvent(SDL_Event *e);
Command* handleInput();
};
#endif // INPUTHANDLER_H

View File

@@ -0,0 +1,11 @@
#ifndef COMMAND_H
#define COMMAND_H
class Command {
public:
virtual ~Command() {}
virtual void execute() = 0;
};
#endif // COMMAND_H

View File

@@ -0,0 +1,6 @@
#include <iostream>
#include "TestCommand.h"
void TestCommand::execute() {
std::cout << "Test Command launched!" << std::endl;
}

View File

@@ -0,0 +1,11 @@
#ifndef TESTCOMMAND_H
#define TESTCOMMAND_H
#include "Command.h"
class TestCommand : public Command {
public:
virtual void execute();
};
#endif //TESTCOMMAND_H

12
src/utility/Utility.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef UTILITY_H
#define UTILITY_H
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
namespace utility {
SDL_Texture* loadTexture( std::string path, SDL_Renderer* gRenderer );
}
#endif // UTILITY_H

26
src/utility/Utitlity.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "utility.h"
namespace utility {
SDL_Texture* loadTexture( std::string path, SDL_Renderer* gRenderer ) {
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL ) {
printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
}
else {
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
if( newTexture == NULL ) {
printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
return newTexture;
}
}