refactor(player): Implement state-based input.

This commit is contained in:
Ritchie Cunningham 2025-09-12 23:54:38 +01:00
parent c7130ac3dd
commit 22d60c7192
4 changed files with 50 additions and 25 deletions

View File

@ -86,27 +86,31 @@ void Bettola::process_events(void) {
_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;
case SDLK_W: _input.up = true; break;
case SDLK_S: _input.down = true; break;
case SDLK_A: _input.left = true; break;
case SDLK_D: _input.right = true; 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;
case SDLK_W: _input.up = false; break;
case SDLK_S: _input.down = false; break;
case SDLK_A: _input.left = false; break;
case SDLK_D: _input.right = false; break;
}
}
}
}
void Bettola::update(double dt) {
float dir_x = 0.0f;
float dir_y = 0.0f;
if(_input.up) dir_y -= 1.0f;
if(_input.down) dir_y += 1.0f;
if(_input.left) dir_x -= 1.0f;
if(_input.right) dir_x += 1.0f;
_player.set_velocity_direction(dir_x, dir_y);
_player.update(dt);
static char window_title[256];
@ -127,8 +131,18 @@ void Bettola::render(void) {
_shader.use();
glm::mat4 model = glm::mat4(1.0f);
#if 0
model = glm::translate(model, glm::vec3(_player.get_x(), _player.get_y(), 0.0f));
model = glm::scale(model, glm::vec3(_player.get_width(), _player.get_height(), 1.0f));
#endif
/* to achieve Scale -> Rotate -> Translate,
* we need to apply operations in reverse!
*/
glm::mat4 trans = glm::translate(glm::mat4(1.0f), glm::vec3(_player.get_x(),
_player.get_y(),0.0f));
glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(_player.get_width(),
_player.get_height(), 1.0f));
model = trans * scale;
unsigned int model_loc = glGetUniformLocation(_shader.get_id(), "model");
glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(model));

View File

@ -24,6 +24,13 @@ private:
bool create_gl_context(void);
void setup_quad(void);
struct InputState {
bool up = false;
bool down = false;
bool left = false;
bool right = false;
};
bool _is_running;
SDL_Window* _window;
@ -35,4 +42,5 @@ private:
glm::mat4 _projection;
Player _player;
InputState _input;
};

View File

@ -1,4 +1,7 @@
#include "player.h"
#include <cstdio>
#include <glm/glm.hpp>
#include <glm/vec2.hpp>
Player::Player(void) :
_x(400.0f),
@ -14,15 +17,13 @@ void Player::update(double 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::set_velocity_direction(float dir_x, float dir_y) {
glm::vec2 direction = { dir_x, dir_y };
void Player::stop_vertical(void) {
_vy = 0.0f;
}
if(glm::length(direction) > 0.0f) {
direction = glm::normalize(direction);
}
void Player::stop_horizontal(void) {
_vx = 0.0f;
_vx = direction.x * _speed;
_vy = direction.y * _speed;
}

View File

@ -13,10 +13,12 @@ public:
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; }
float get_height() const { return _height; }
void set_velocity_direction(float dir_x, float dir_y);
float get_x() const { return _x; }
float get_y() const { return _y; }
float get_width() const { return _width; }
float get_height() const { return _height; }
private:
float _x;