diff --git a/src/bettola.cpp b/src/bettola.cpp index 536a520..d231e30 100644 --- a/src/bettola.cpp +++ b/src/bettola.cpp @@ -41,10 +41,10 @@ Bettola::~Bettola(void) { } int Bettola::run(void) { - if(!init_sdl()) return -1; - if(!create_window()) return -1; - if(!create_gl_context()) return -1; - if(!init_glew()) return -1; + if(!init_sdl()) return -1; + if(!create_window()) return -1; + if(!create_gl_context()) return -1; + if(!init_glew()) return -1; if(!_shader.load_from_files("assets/shaders/simple.vert", "assets/shaders/simple.frag")) { @@ -84,11 +84,31 @@ void Bettola::process_events(void) { while(SDL_PollEvent(&event)) { if(event.type == SDL_EVENT_QUIT) { _is_running = false; + } else if(event.type == SDL_EVENT_KEY_DOWN) { + switch(event.key.key) { + case SDLK_W: _player.move_up(); break; + case SDLK_S: _player.move_down(); break; + case SDLK_A: _player.move_left(); break; + case SDLK_D: _player.move_right(); break; + } + } else if(event.type == SDL_EVENT_KEY_UP) { + switch(event.key.key) { + case SDLK_W: + case SDLK_S: + _player.stop_vertical(); + break; + case SDLK_A: + case SDLK_D: + _player.stop_horizontal(); + break; + } } } } void Bettola::update(double dt) { + _player.update(dt); + static char window_title[256]; static double time_since_title_update = 0.0; diff --git a/src/game/player.cpp b/src/game/player.cpp index 64e164c..bc2ea12 100644 --- a/src/game/player.cpp +++ b/src/game/player.cpp @@ -4,8 +4,25 @@ Player::Player(void) : _x(400.0f), _y(300.0f), _width(50.0f), - _height(50.0f) {} + _height(50.0f), + _vx(0.0f), + _vy(0.0f), + _speed(200.0f) {} void Player::update(double dt) { - /* Do some shit? */ + _x += _vx * dt; + _y += _vy * dt; +} + +void Player::move_up(void) { _vy = -_speed; } +void Player::move_down(void) { _vy = _speed; } +void Player::move_left(void) { _vx = -_speed; } +void Player::move_right(void) { _vx = _speed; } + +void Player::stop_vertical(void) { + _vy = 0.0f; +} + +void Player::stop_horizontal(void) { + _vx = 0.0f; } diff --git a/src/game/player.h b/src/game/player.h index 3cb4122..0597e1d 100644 --- a/src/game/player.h +++ b/src/game/player.h @@ -6,6 +6,13 @@ public: void update(double dt); + void move_up(void); + void move_down(void); + void move_left(void); + void move_right(void); + void stop_vertical(void); + void stop_horizontal(void); + float get_x() const { return _x; } float get_y() const { return _y; } float get_width() const { return _width; } @@ -16,4 +23,8 @@ private: float _y; float _width; float _height; + + float _vx; + float _vy; + float _speed; };