fix input system

This commit is contained in:
Quinn
2024-08-14 15:36:50 +02:00
parent 4077379b47
commit 95eb808337
4 changed files with 6 additions and 15 deletions

View File

@@ -2,7 +2,6 @@
I wanted a project to learn C and the SDL framework in, and settled upon this game as I thought it was a simple and approachable project for me to execute. I wanted a project to learn C and the SDL framework in, and settled upon this game as I thought it was a simple and approachable project for me to execute.
### TODO: ### TODO:
- make a better input system (one that doesn't crash the game)
- make beeping sound effects - make beeping sound effects
- make the game freeze when you lost instead of closing - make the game freeze when you lost instead of closing
- fix constants in level colision detection - fix constants in level colision detection

View File

@@ -48,12 +48,12 @@ void level_init(Level* level) {
} }
//updates the level //updates the level
void level_update(Level* level, bool* keys) { void level_update(Level* level, const Uint8* keys) {
Bouncer* bouncer = &level->bouncer; Bouncer* bouncer = &level->bouncer;
Ball* ball = &level->ball; Ball* ball = &level->ball;
// if move bouncer LEFT // if move bouncer LEFT
if (keys[SDLK_a]) { if (keys[SDL_SCANCODE_A] || keys[SDL_SCANCODE_LEFT]) {
ball->moving = true; ball->moving = true;
if (bouncer->pos.x < 0 == false) { if (bouncer->pos.x < 0 == false) {
@@ -62,7 +62,7 @@ void level_update(Level* level, bool* keys) {
} }
// if move bouncer RIGHT // if move bouncer RIGHT
if (keys[SDLK_d]) { if (keys[SDL_SCANCODE_D] || keys[SDL_SCANCODE_RIGHT]) {
ball->moving = true; ball->moving = true;
if ((bouncer->pos.x + bouncer->width) > SCREEN_WIDTH == false) { if ((bouncer->pos.x + bouncer->width) > SCREEN_WIDTH == false) {

View File

@@ -32,4 +32,4 @@ typedef struct {
void level_init(Level* level); void level_init(Level* level);
void level_update(Level* level, bool* keys); void level_update(Level* level, const Uint8* keys);

View File

@@ -17,7 +17,7 @@ static int init(SDL_Window** window, SDL_Renderer** renderer, Level* level) {
} }
// called on each game update // called on each game update
static bool update(Level* level, bool* keys, RenderData* render_data) { static bool update(Level* level, const Uint8* keys, RenderData* render_data) {
const clock_t clock_start = clock(); const clock_t clock_start = clock();
//update the event keys //update the event keys
@@ -28,13 +28,6 @@ static bool update(Level* level, bool* keys, RenderData* render_data) {
case SDL_QUIT: case SDL_QUIT:
return false; return false;
break; break;
case SDL_KEYDOWN:
keys[e.key.keysym.sym] = true;
break;
case SDL_KEYUP:
keys[e.key.keysym.sym] = false;
break;
} }
} }
} }
@@ -71,7 +64,6 @@ int main(void) {
Level level = { 0 }; //stores the game's state Level level = { 0 }; //stores the game's state
SDL_Window* window = NULL; //the window that is given to the OS SDL_Window* window = NULL; //the window that is given to the OS
SDL_Renderer* renderer = NULL; //the renderer used to draw to the window SDL_Renderer* renderer = NULL; //the renderer used to draw to the window
bool keys[322] = { 0 }; //stores the key states TODO: find a better method than to use 322 bytes
// initialize // initialize
{ {
@@ -85,7 +77,7 @@ int main(void) {
// game-loop // game-loop
RenderData render_data = { window, renderer, &level }; //contains the data which is used to render the game RenderData render_data = { window, renderer, &level }; //contains the data which is used to render the game
while (update(&level, keys, &render_data) && (level.stop == false)); while (update(&level, SDL_GetKeyboardState(NULL), &render_data) && (level.stop == false));
// frees media and shuts down SDL // frees media and shuts down SDL
close(window, renderer); close(window, renderer);