townengine/apps/testgame/scenes/title.c

73 lines
1.8 KiB
C

#include "title.h"
#include "ingame.h"
#include "../world.h"
#include "../player.h"
#include "twn_game_api.h"
#include <stdio.h>
static void title_tick(struct state *state) {
struct scene_title *scn = (struct scene_title *)state->scene;
(void)scn;
if (input_is_action_just_pressed(&state->ctx->input, "ui_accept")) {
switch_to(state, ingame_scene);
}
m_sprite("/assets/title.png", ((t_frect) {
((float)RENDER_BASE_WIDTH / 2) - ((float)320 / 2), 64, 320, 128 }));
/* draw the tick count as an example of dynamic text */
size_t text_str_len = snprintf(NULL, 0, "%lu", state->ctx->tick_count) + 1;
char *text_str = cmalloc(text_str_len);
snprintf(text_str, text_str_len, "%lu", state->ctx->tick_count);
const char *font = "fonts/kenney-pixel.ttf";
int text_h = 32;
int text_w = get_text_width(text_str, text_h, font);
push_rectangle(
(t_frect) {
.x = 0,
.y = 0,
.w = (float)text_w,
.h = (float)text_h,
},
(t_color) { 0, 0, 0, 255 }
);
push_text(
text_str,
(t_fvec2){ 0, 0 },
text_h,
(t_color) { 255, 255, 255, 255 },
font
);
free(text_str);
}
static void title_end(struct state *state) {
struct scene_title *scn = (struct scene_title *)state->scene;
player_destroy(scn->player);
world_destroy(scn->world);
free(state->scene);
}
struct scene *title_scene(struct state *state) {
(void)state;
struct scene_title *new_scene = ccalloc(1, sizeof *new_scene);
new_scene->base.tick = title_tick;
new_scene->base.end = title_end;
new_scene->world = world_create();
new_scene->player = player_create(new_scene->world);
return (struct scene *)new_scene;
}