From 6241b3228840fece52c07204b58054b803494af0 Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Tue, 10 Apr 2012 23:29:19 +0100 Subject: [PATCH] [Add] Added mutators for the x/y coordinates of a sprite. [Add] Implemented Input methods and made a moveable entity. --- src/Actor/Player.cpp | 21 +++++++++++++++++++-- src/Actor/Player.h | 1 + src/Main/Game.cpp | 4 ++++ src/Main/Game.h | 18 ++++++++++-------- src/Main/main.cpp | 9 +++++++-- src/Sprite/Sprite.h | 18 +++++++++++------- 6 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/Actor/Player.cpp b/src/Actor/Player.cpp index c37f7ac..14c6f5c 100644 --- a/src/Actor/Player.cpp +++ b/src/Actor/Player.cpp @@ -1,7 +1,7 @@ #include "Player.h" Player::Player(void) { - + PLAYER_SPEED = 15; } Player::~Player(void) { @@ -24,5 +24,22 @@ void Player::Render(void) { } void Player::ProcessEvents(void) { - + float x = _player->GetX(); + float y = _player->GetY(); + if(KeyStillDown(SDLK_w)) { + y -= PLAYER_SPEED; + _player->SetY(y); + } + if(KeyStillDown(SDLK_s)) { + y += PLAYER_SPEED; + _player->SetY(y); + } + if(KeyStillDown(SDLK_a)) { + x -= PLAYER_SPEED; + _player->SetX(x); + } + if(KeyStillDown(SDLK_d)) { + x += PLAYER_SPEED; + _player->SetX(x); + } } diff --git a/src/Actor/Player.h b/src/Actor/Player.h index a3670b9..382f0aa 100644 --- a/src/Actor/Player.h +++ b/src/Actor/Player.h @@ -17,6 +17,7 @@ public: void ProcessEvents(void); private: + float PLAYER_SPEED; Sprite* _player; Texture* _playerTexture; float _rotationAngle; diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp index 1ac0336..826e3c2 100644 --- a/src/Main/Game.cpp +++ b/src/Main/Game.cpp @@ -52,6 +52,10 @@ void Game::Shutdown(void) { delete _player; } +void Game::ProcessEvents(void) { + _player->ProcessEvents(); +} + void Game::OnResize(int width, int height) { glViewport(0, 0, width, height); diff --git a/src/Main/Game.h b/src/Main/Game.h index 7a4c09e..87486dd 100644 --- a/src/Main/Game.h +++ b/src/Main/Game.h @@ -6,16 +6,18 @@ class Sprite; class Game { public: - Game(void); - ~Game(void); + Game(void); + ~Game(void); - bool Init(void); - void Prepare(float dt); - void Render(void); - void Shutdown(void); + bool Init(void); + void Prepare(float dt); + void Render(void); + void Shutdown(void); - void OnResize(int width, int height); + void ProcessEvents(void); + + void OnResize(int width, int height); private: - Player* _player; + Player* _player; }; diff --git a/src/Main/main.cpp b/src/Main/main.cpp index c26262b..2dc6337 100644 --- a/src/Main/main.cpp +++ b/src/Main/main.cpp @@ -13,11 +13,13 @@ #include #include #include "Game.h" +#include "../IO/Input.h" #include "../Global/Globals.h" #include "../Global/Constants.h" #include "../System/Debug.h" void Destroy(void) { + DestroyInput(); SDL_FreeSurface(screen); SDL_Quit(); } @@ -77,12 +79,13 @@ int main(int argc, char** argv) { Debug::logger->message("\n ----- Logic -----"); game.Init(); + CreateInput(); bool isRunning = true; while(isRunning) { - while(SDL_PollEvent(&event)){ - if(event.type == SDL_QUIT) { + while(SDL_PollEvent(&event)) { + if((event.type == SDL_QUIT) || KeyStillDown(SDLK_ESCAPE)) { isRunning = false; break; } @@ -93,6 +96,8 @@ int main(int argc, char** argv) { } } + UpdateInput(); + game.ProcessEvents(); game.Render(); SDL_GL_SwapBuffers(); } diff --git a/src/Sprite/Sprite.h b/src/Sprite/Sprite.h index 52e8d2c..6e231a6 100644 --- a/src/Sprite/Sprite.h +++ b/src/Sprite/Sprite.h @@ -12,14 +12,18 @@ public: virtual void Update(float dt); virtual void Draw() const; - const Vec2& GetPosition() const { return position; } - const Vec2& GetSize() const { return size; } - const Vec2& GetScale() const { return scale; } - float GetRotation() const { return rotation; } - Texture* GetTexture() { return texture; } - const Texture* GetTexture() const { return texture; } + const Vec2& GetPosition() const { return position; } + float GetX(void) { return position.x; } + float GetY(void) { return position.y; } + const Vec2& GetSize() const { return size; } + const Vec2& GetScale() const { return scale; } + float GetRotation() const { return rotation; } + Texture* GetTexture() { return texture; } + const Texture* GetTexture() const { return texture; } - void SetPosition(const Vec2& position) { this->position = position; } + void SetPosition(const Vec2& position) { this->position = position; } + void SetX(float xArg) { this->position.x = xArg; } + void SetY(float yArg) { this->position.y = yArg; } void SetScale(const Vec2& scale) { this->scale = scale; } void SetRotation(float rotation) { this->rotation = rotation; } void SetTexture(Texture* texture);