Skip to content

Commit 99bf4de

Browse files
committed
console: Add keypress command
1 parent 259455d commit 99bf4de

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/console/console.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ void console_tick(game_state *gs) {
359359
con->y_pos = 0;
360360
}
361361
}
362+
console_release_keypress();
362363
}
363364

364365
void console_add_cmd(const char *name, command_func func, const char *doc) {
@@ -372,6 +373,20 @@ void console_remove_cmd(const char *name) {
372373
hashmap_del_str(&con->cmds, name);
373374
}
374375

376+
void console_release_keypress(void) {
377+
if(con->keypress_time > 0) {
378+
con->keypress_time--;
379+
return;
380+
}
381+
if(con->keypress_scancode != SDL_SCANCODE_UNKNOWN) {
382+
Uint8 *state = (Uint8 *)SDL_GetKeyboardState(NULL);
383+
state[con->keypress_scancode] = 0;
384+
385+
con->keypress_time = 0;
386+
con->keypress_scancode = SDL_SCANCODE_UNKNOWN;
387+
}
388+
}
389+
375390
bool console_window_is_open(void) {
376391
return con->is_open;
377392
}

src/console/console.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ void console_tick(game_state *gs);
1616
void console_add_cmd(const char *name, command_func func, const char *doc);
1717
void console_remove_cmd(const char *name);
1818

19+
// Potentially release a key that has been pressed by the keypress command.
20+
void console_release_keypress(void);
21+
1922
void console_output_add(const char *text);
2023
void console_output_addline(const char *text);
2124

src/console/console_cmd.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ int console_cmd_god(game_state *gs, int argc, char **argv) {
273273
return 0;
274274
}
275275

276+
int console_keypress(game_state *gs, int argc, char **argv) {
277+
SDL_Event sdlevent;
278+
memset(&sdlevent, 0, sizeof(sdlevent));
279+
sdlevent.type = SDL_KEYDOWN;
280+
sdlevent.key.keysym.sym = SDLK_DOWN;
281+
sdlevent.key.keysym.scancode = SDL_SCANCODE_DOWN;
282+
SDL_PushEvent(&sdlevent);
283+
284+
Uint8 *state = (Uint8 *)SDL_GetKeyboardState(NULL);
285+
state[SDL_SCANCODE_DOWN] = 1;
286+
287+
con->keypress_time = 20;
288+
con->keypress_scancode = SDL_SCANCODE_DOWN;
289+
290+
return 0;
291+
}
292+
276293
int console_kreissack(game_state *gs, int argc, char **argv) {
277294
game_player *p1 = game_state_get_player(gs, 0);
278295
p1->sp_wins = (2046 ^ (2 << p1->pilot->pilot_id));
@@ -472,6 +489,7 @@ void console_init_cmd(void) {
472489
console_add_cmd("stun", &console_cmd_stun, "Stun the other player");
473490
console_add_cmd("rein", &console_cmd_rein, "R-E-I-N!");
474491
console_add_cmd("god", &console_cmd_god, "Enable god mode");
492+
console_add_cmd("keypress", &console_keypress, "Push a keypress into the input queue");
475493
console_add_cmd("kreissack", &console_kreissack, "Fight Kreissack");
476494
console_add_cmd("ez-destruct", &console_cmd_ez_destruct, "Punch = destruction, kick = scrap");
477495
console_add_cmd("warp", &console_toggle_warp, "Toggle warp speed");

src/console/console_type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ typedef struct console {
2424
int y_pos;
2525
hashmap cmds; // string -> command
2626
text *text;
27+
int keypress_time;
28+
int keypress_scancode;
2729
} console;
2830

2931
typedef struct command {

0 commit comments

Comments
 (0)