feat(player) Implement key event handling.
Adds keyboard input handling to control the "player".
This commit is contained in:
parent
dc4dd650a4
commit
c7130ac3dd
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user