diff --git a/src/DarkRP2D.cpp b/src/DarkRP2D.cpp index eee6d14..2a6bfe0 100644 --- a/src/DarkRP2D.cpp +++ b/src/DarkRP2D.cpp @@ -2,17 +2,14 @@ #include #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; } diff --git a/src/DarkRP2D.h b/src/DarkRP2D.h index 875c685..aed1ce0 100644 --- a/src/DarkRP2D.h +++ b/src/DarkRP2D.h @@ -2,6 +2,7 @@ #define DARKRP2D_H #include +#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 diff --git a/src/DesktopLauncher.cpp b/src/DesktopLauncher.cpp index 80f7710..100f9a1 100644 --- a/src/DesktopLauncher.cpp +++ b/src/DesktopLauncher.cpp @@ -2,6 +2,7 @@ #include #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; } diff --git a/src/input/InputHandler.cpp b/src/input/InputHandler.cpp new file mode 100644 index 0000000..175da30 --- /dev/null +++ b/src/input/InputHandler.cpp @@ -0,0 +1,48 @@ +#include + +#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;*/ +} diff --git a/src/input/InputHandler.h b/src/input/InputHandler.h new file mode 100644 index 0000000..91245bb --- /dev/null +++ b/src/input/InputHandler.h @@ -0,0 +1,26 @@ +#ifndef INPUTHANDLER_H +#define INPUTHANDLER_H + +#include +#include +#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 commandStream; + +public: + InputHandler(); + ~InputHandler(); + void inputEvent(SDL_Event *e); + Command* handleInput(); +}; + +#endif // INPUTHANDLER_H diff --git a/src/input/commands/Command.h b/src/input/commands/Command.h new file mode 100644 index 0000000..c32385a --- /dev/null +++ b/src/input/commands/Command.h @@ -0,0 +1,11 @@ +#ifndef COMMAND_H +#define COMMAND_H + +class Command { +public: + virtual ~Command() {} + virtual void execute() = 0; + +}; + +#endif // COMMAND_H diff --git a/src/input/commands/TestCommand.cpp b/src/input/commands/TestCommand.cpp new file mode 100644 index 0000000..ff5ec2c --- /dev/null +++ b/src/input/commands/TestCommand.cpp @@ -0,0 +1,6 @@ +#include +#include "TestCommand.h" + +void TestCommand::execute() { + std::cout << "Test Command launched!" << std::endl; +} diff --git a/src/input/commands/TestCommand.h b/src/input/commands/TestCommand.h new file mode 100644 index 0000000..ba39959 --- /dev/null +++ b/src/input/commands/TestCommand.h @@ -0,0 +1,11 @@ +#ifndef TESTCOMMAND_H +#define TESTCOMMAND_H + +#include "Command.h" + +class TestCommand : public Command { +public: + virtual void execute(); +}; + +#endif //TESTCOMMAND_H diff --git a/src/utility/Utility.h b/src/utility/Utility.h new file mode 100644 index 0000000..fcf7a96 --- /dev/null +++ b/src/utility/Utility.h @@ -0,0 +1,12 @@ +#ifndef UTILITY_H +#define UTILITY_H + +#include +#include +#include + +namespace utility { + SDL_Texture* loadTexture( std::string path, SDL_Renderer* gRenderer ); +} + +#endif // UTILITY_H diff --git a/src/utility/Utitlity.cpp b/src/utility/Utitlity.cpp new file mode 100644 index 0000000..d8adb5b --- /dev/null +++ b/src/utility/Utitlity.cpp @@ -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; + } + +}