Add restart and handle quitting with callback
This commit is contained in:
6
game.c
6
game.c
@@ -153,6 +153,11 @@ static unsigned ship_entity_id;
|
|||||||
static unsigned ship_shape_id;
|
static unsigned ship_shape_id;
|
||||||
static unsigned fire_shape_id;
|
static unsigned fire_shape_id;
|
||||||
|
|
||||||
|
static void restart()
|
||||||
|
{
|
||||||
|
game_init(aspect);
|
||||||
|
}
|
||||||
|
|
||||||
static entity_t *add_entity(unsigned *id_out)
|
static entity_t *add_entity(unsigned *id_out)
|
||||||
{
|
{
|
||||||
const unsigned id = entity_count++;
|
const unsigned id = entity_count++;
|
||||||
@@ -451,6 +456,7 @@ static void handle_collisions(const collision_t *collisions, unsigned count)
|
|||||||
void game_init(float _aspect)
|
void game_init(float _aspect)
|
||||||
{
|
{
|
||||||
input_on_shoot(shoot);
|
input_on_shoot(shoot);
|
||||||
|
input_on_restart(restart);
|
||||||
|
|
||||||
aspect = _aspect;
|
aspect = _aspect;
|
||||||
dead = false;
|
dead = false;
|
||||||
|
|||||||
25
input.c
25
input.c
@@ -15,15 +15,14 @@ enum {
|
|||||||
input_state_t input;
|
input_state_t input;
|
||||||
|
|
||||||
static int input_fd;
|
static int input_fd;
|
||||||
|
|
||||||
|
static input_callback_t restart;
|
||||||
|
static input_callback_t quit;
|
||||||
static input_callback_t shoot;
|
static input_callback_t shoot;
|
||||||
|
|
||||||
static void key_press(int key)
|
static void key_press(int key)
|
||||||
{
|
{
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case KEY_Q:
|
|
||||||
input.quit = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
++input.thrust;
|
++input.thrust;
|
||||||
break;
|
break;
|
||||||
@@ -35,6 +34,14 @@ static void key_press(int key)
|
|||||||
--input.spin;
|
--input.spin;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case KEY_Q:
|
||||||
|
if (quit != nullptr)
|
||||||
|
quit();
|
||||||
|
break;
|
||||||
|
case KEY_R:
|
||||||
|
if (restart != nullptr)
|
||||||
|
restart();
|
||||||
|
break;
|
||||||
case KEY_SPACE:
|
case KEY_SPACE:
|
||||||
if (shoot != nullptr)
|
if (shoot != nullptr)
|
||||||
shoot();
|
shoot();
|
||||||
@@ -88,6 +95,16 @@ void input_on_shoot(input_callback_t cb)
|
|||||||
shoot = cb;
|
shoot = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void input_on_restart(input_callback_t cb)
|
||||||
|
{
|
||||||
|
restart = cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
void input_on_quit(input_callback_t cb)
|
||||||
|
{
|
||||||
|
quit = cb;
|
||||||
|
}
|
||||||
|
|
||||||
void input_handle()
|
void input_handle()
|
||||||
{
|
{
|
||||||
struct input_event ev;
|
struct input_event ev;
|
||||||
|
|||||||
4
input.h
4
input.h
@@ -6,7 +6,6 @@ typedef void (*input_callback_t)();
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
int spin;
|
int spin;
|
||||||
int thrust;
|
int thrust;
|
||||||
bool quit;
|
|
||||||
} input_state_t;
|
} input_state_t;
|
||||||
|
|
||||||
extern input_state_t input;
|
extern input_state_t input;
|
||||||
@@ -15,6 +14,9 @@ int input_init();
|
|||||||
void input_cleanup();
|
void input_cleanup();
|
||||||
|
|
||||||
void input_on_shoot(input_callback_t cb);
|
void input_on_shoot(input_callback_t cb);
|
||||||
|
void input_on_restart(input_callback_t cb);
|
||||||
|
void input_on_quit(input_callback_t cb);
|
||||||
|
|
||||||
void input_handle();
|
void input_handle();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
12
main.c
12
main.c
@@ -8,6 +8,13 @@
|
|||||||
|
|
||||||
#define MAX(a, b) ((a) < (b) ? (b) : (a))
|
#define MAX(a, b) ((a) < (b) ? (b) : (a))
|
||||||
|
|
||||||
|
static bool run;
|
||||||
|
|
||||||
|
static void quit()
|
||||||
|
{
|
||||||
|
run = false;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
const int input_fd = input_init();
|
const int input_fd = input_init();
|
||||||
@@ -15,6 +22,8 @@ int main()
|
|||||||
rng_init();
|
rng_init();
|
||||||
game_init(renderer_params.aspect);
|
game_init(renderer_params.aspect);
|
||||||
|
|
||||||
|
input_on_quit(quit);
|
||||||
|
|
||||||
renderer_clear();
|
renderer_clear();
|
||||||
renderer_swap();
|
renderer_swap();
|
||||||
|
|
||||||
@@ -22,7 +31,8 @@ int main()
|
|||||||
const int max_fd = MAX(input_fd, drm_fd);
|
const int max_fd = MAX(input_fd, drm_fd);
|
||||||
fd_set set;
|
fd_set set;
|
||||||
|
|
||||||
while (!input.quit) {
|
run = true;
|
||||||
|
while (run) {
|
||||||
FD_ZERO(&set);
|
FD_ZERO(&set);
|
||||||
FD_SET(input_fd, &set);
|
FD_SET(input_fd, &set);
|
||||||
FD_SET(drm_fd, &set);
|
FD_SET(drm_fd, &set);
|
||||||
|
|||||||
Reference in New Issue
Block a user