townengine/apps/demos/platformer/game.c

80 lines
2.1 KiB
C
Raw Normal View History

2024-07-08 00:44:20 +00:00
#include "state.h"
#include "scenes/scene.h"
#include "scenes/title.h"
#include "twn_game_api.h"
2024-08-21 13:55:34 +00:00
2024-07-08 00:44:20 +00:00
#include <stdio.h>
#include <malloc.h>
#include <stdint.h>
void game_tick(void) {
2024-08-21 13:55:34 +00:00
if (ctx.initialization_needed) {
if (!ctx.udata) {
ctx.udata = ccalloc(1, sizeof (State));
2024-08-21 13:55:34 +00:00
State *state = ctx.udata;
2024-08-21 13:55:34 +00:00
state->ctx = &ctx;
state->scene = title_scene(state);
}
input_add_action("debug_toggle");
input_bind_action_control("debug_toggle", CONTROL_BACKSPACE);
2024-07-08 00:44:20 +00:00
input_add_action("debug_dump_atlases");
input_bind_action_control("debug_dump_atlases", CONTROL_HOME);
input_add_action("player_left");
input_bind_action_control("player_left", CONTROL_A);
2024-07-08 00:44:20 +00:00
input_add_action("player_right");
input_bind_action_control("player_right", CONTROL_D);
2024-07-30 21:05:28 +00:00
input_add_action("player_forward");
input_bind_action_control("player_forward", CONTROL_W);
2024-07-30 21:05:28 +00:00
input_add_action("player_backward");
input_bind_action_control("player_backward", CONTROL_S);
2024-07-08 00:44:20 +00:00
input_add_action("player_jump");
input_bind_action_control("player_jump", CONTROL_SPACE);
2024-07-30 21:05:28 +00:00
input_add_action("player_run");
input_bind_action_control("player_run", CONTROL_LSHIFT);
2024-07-08 00:44:20 +00:00
input_add_action("ui_accept");
input_bind_action_control("ui_accept", CONTROL_RETURN);
2024-07-30 21:05:28 +00:00
input_add_action("mouse_capture_toggle");
input_bind_action_control("mouse_capture_toggle", CONTROL_ESCAPE);
2024-07-08 00:44:20 +00:00
}
State *state = ctx.udata;
2024-07-08 00:44:20 +00:00
if (input_is_action_just_pressed("debug_toggle")) {
2024-07-08 00:44:20 +00:00
ctx.debug = !ctx.debug;
}
if (input_is_action_just_pressed("debug_dump_atlases")) {
textures_dump_atlases();
}
2024-07-08 00:44:20 +00:00
state->scene->tick(state);
/* there's a scene switch pending, we can do it now that the tick is done */
if (state->next_scene != NULL) {
state->scene->end(state);
state->scene = state->next_scene;
state->is_scene_switching = false;
state->next_scene = NULL;
}
}
void game_end(void) {
State *state = ctx.udata;
2024-07-08 00:44:20 +00:00
state->scene->end(state);
free(state);
}