2024-07-29 20:40:15 +00:00
|
|
|
#include "townengine/game_api.h"
|
2024-07-08 00:44:20 +00:00
|
|
|
#include "state.h"
|
|
|
|
#include "scenes/scene.h"
|
|
|
|
#include "scenes/title.h"
|
|
|
|
|
2024-07-30 21:05:28 +00:00
|
|
|
#include <SDL_scancode.h>
|
2024-07-08 00:44:20 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
|
|
void game_tick(void) {
|
|
|
|
if (ctx.tick_count == 0) {
|
|
|
|
ctx.udata = ccalloc(1, sizeof (struct state));
|
|
|
|
struct state *state = ctx.udata;
|
|
|
|
state->ctx = &ctx;
|
|
|
|
state->scene = title_scene(state);
|
|
|
|
|
|
|
|
input_add_action(&ctx.input, "debug_dump_atlases");
|
|
|
|
input_bind_action_scancode(&ctx.input, "debug_dump_atlases", SDL_SCANCODE_HOME);
|
|
|
|
|
|
|
|
input_add_action(&ctx.input, "debug_toggle");
|
|
|
|
input_bind_action_scancode(&ctx.input, "debug_toggle", SDL_SCANCODE_BACKSPACE);
|
|
|
|
|
|
|
|
input_add_action(&ctx.input, "player_left");
|
2024-07-30 21:05:28 +00:00
|
|
|
input_bind_action_scancode(&ctx.input, "player_left", SDL_SCANCODE_A);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
input_add_action(&ctx.input, "player_right");
|
2024-07-30 21:05:28 +00:00
|
|
|
input_bind_action_scancode(&ctx.input, "player_right", SDL_SCANCODE_D);
|
|
|
|
|
|
|
|
input_add_action(&ctx.input, "player_forward");
|
|
|
|
input_bind_action_scancode(&ctx.input, "player_forward", SDL_SCANCODE_W);
|
|
|
|
|
|
|
|
input_add_action(&ctx.input, "player_backward");
|
|
|
|
input_bind_action_scancode(&ctx.input, "player_backward", SDL_SCANCODE_S);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
input_add_action(&ctx.input, "player_jump");
|
2024-07-30 21:05:28 +00:00
|
|
|
input_bind_action_scancode(&ctx.input, "player_jump", SDL_SCANCODE_SPACE);
|
|
|
|
|
|
|
|
input_add_action(&ctx.input, "player_run");
|
|
|
|
input_bind_action_scancode(&ctx.input, "player_run", SDL_SCANCODE_LSHIFT);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
|
|
|
input_add_action(&ctx.input, "ui_accept");
|
|
|
|
input_bind_action_scancode(&ctx.input, "ui_accept", SDL_SCANCODE_RETURN);
|
2024-07-30 21:05:28 +00:00
|
|
|
|
|
|
|
input_add_action(&ctx.input, "mouse_capture_toggle");
|
|
|
|
input_bind_action_scancode(&ctx.input, "mouse_capture_toggle", SDL_SCANCODE_END);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct state *state = ctx.udata;
|
|
|
|
|
|
|
|
if (input_is_action_just_pressed(&ctx.input, "debug_dump_atlases")) {
|
|
|
|
textures_dump_atlases(&ctx.texture_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_is_action_just_pressed(&ctx.input, "debug_toggle")) {
|
|
|
|
ctx.debug = !ctx.debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
struct state *state = ctx.udata;
|
|
|
|
state->scene->end(state);
|
|
|
|
free(state);
|
|
|
|
}
|