[Add] Added mutators for the x/y coordinates of a sprite.

[Add] Implemented Input methods and made a moveable entity.
This commit is contained in:
Rtch90 2012-04-10 23:29:19 +01:00
parent e894dddcb7
commit 6241b32288
6 changed files with 52 additions and 19 deletions

View File

@ -1,7 +1,7 @@
#include "Player.h" #include "Player.h"
Player::Player(void) { Player::Player(void) {
PLAYER_SPEED = 15;
} }
Player::~Player(void) { Player::~Player(void) {
@ -24,5 +24,22 @@ void Player::Render(void) {
} }
void Player::ProcessEvents(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);
}
} }

View File

@ -17,6 +17,7 @@ public:
void ProcessEvents(void); void ProcessEvents(void);
private: private:
float PLAYER_SPEED;
Sprite* _player; Sprite* _player;
Texture* _playerTexture; Texture* _playerTexture;
float _rotationAngle; float _rotationAngle;

View File

@ -52,6 +52,10 @@ void Game::Shutdown(void) {
delete _player; delete _player;
} }
void Game::ProcessEvents(void) {
_player->ProcessEvents();
}
void Game::OnResize(int width, int height) { void Game::OnResize(int width, int height) {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);

View File

@ -14,6 +14,8 @@ public:
void Render(void); void Render(void);
void Shutdown(void); void Shutdown(void);
void ProcessEvents(void);
void OnResize(int width, int height); void OnResize(int width, int height);
private: private:

View File

@ -13,11 +13,13 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <time.h> #include <time.h>
#include "Game.h" #include "Game.h"
#include "../IO/Input.h"
#include "../Global/Globals.h" #include "../Global/Globals.h"
#include "../Global/Constants.h" #include "../Global/Constants.h"
#include "../System/Debug.h" #include "../System/Debug.h"
void Destroy(void) { void Destroy(void) {
DestroyInput();
SDL_FreeSurface(screen); SDL_FreeSurface(screen);
SDL_Quit(); SDL_Quit();
} }
@ -77,12 +79,13 @@ int main(int argc, char** argv) {
Debug::logger->message("\n ----- Logic -----"); Debug::logger->message("\n ----- Logic -----");
game.Init(); game.Init();
CreateInput();
bool isRunning = true; bool isRunning = true;
while(isRunning) { while(isRunning) {
while(SDL_PollEvent(&event)){ while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) { if((event.type == SDL_QUIT) || KeyStillDown(SDLK_ESCAPE)) {
isRunning = false; isRunning = false;
break; break;
} }
@ -93,6 +96,8 @@ int main(int argc, char** argv) {
} }
} }
UpdateInput();
game.ProcessEvents();
game.Render(); game.Render();
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
} }

View File

@ -13,6 +13,8 @@ public:
virtual void Draw() const; virtual void Draw() const;
const Vec2& GetPosition() const { return position; } 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& GetSize() const { return size; }
const Vec2& GetScale() const { return scale; } const Vec2& GetScale() const { return scale; }
float GetRotation() const { return rotation; } float GetRotation() const { return rotation; }
@ -20,6 +22,8 @@ public:
const Texture* GetTexture() const { 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 SetScale(const Vec2& scale) { this->scale = scale; }
void SetRotation(float rotation) { this->rotation = rotation; } void SetRotation(float rotation) { this->rotation = rotation; }
void SetTexture(Texture* texture); void SetTexture(Texture* texture);