mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
add input timeout and input keypresses
This commit is contained in:
@@ -2,10 +2,11 @@
|
||||
|
||||
#include <SDL_events.h>
|
||||
#include <SDL_scancode.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../game/time.h"
|
||||
#include "window.h"
|
||||
|
||||
// TODO: this'll likely be insufficient, due to no code delays being applied, or it registers *just* the keydown event
|
||||
__attribute__((const)) static int procscancode(SDL_Scancode code) {
|
||||
switch (code) {
|
||||
case SDL_SCANCODE_Q:
|
||||
@@ -37,15 +38,17 @@ __attribute__((const)) static int procscancode(SDL_Scancode code) {
|
||||
}
|
||||
}
|
||||
|
||||
int input_getdat(void) {
|
||||
int movdat = 0;
|
||||
int input_getdat(time_t time) {
|
||||
static int movdat = 0;
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_QUIT: window_close(); break;
|
||||
case SDL_KEYDOWN: movdat |= procscancode(e.key.keysym.scancode); break;
|
||||
case SDL_KEYUP: movdat &= ~procscancode(e.key.keysym.scancode); break;
|
||||
}
|
||||
}
|
||||
|
||||
return movdat;
|
||||
static time_t timeout = 0;
|
||||
return movdat & (-!!time_poll(time, 64, &timeout));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <time.h>
|
||||
|
||||
/* 8 bit enumeration storing the movement data */
|
||||
enum movdat {
|
||||
MOVL = 1, // move left
|
||||
@@ -12,4 +14,4 @@ enum movdat {
|
||||
|
||||
/* returns an OR'd string from `enum movdat`, containing the movement data.
|
||||
* assumes that SDL has been initialized and a window has successfully been created. */
|
||||
__attribute__((__pure__)) int input_getdat(void);
|
||||
__attribute__((__pure__)) int input_getdat(time_t time);
|
||||
|
||||
@@ -46,7 +46,7 @@ void window_free(void) {
|
||||
void window_open(void) {
|
||||
while (!close) {
|
||||
size_t time = time_pull();
|
||||
game_update(input_getdat(), time);
|
||||
game_update(input_getdat(time), time);
|
||||
render_update();
|
||||
|
||||
static time_t timeout = 0;
|
||||
|
||||
Reference in New Issue
Block a user