application separation
This commit is contained in:
99
apps/testgame/scenes/ingame.c
Normal file
99
apps/testgame/scenes/ingame.c
Normal file
@ -0,0 +1,99 @@
|
||||
#include "ingame.h"
|
||||
#include "title.h"
|
||||
#include "scene.h"
|
||||
|
||||
#include "townengine/game_api.h"
|
||||
|
||||
|
||||
static void ingame_tick(struct state *state) {
|
||||
struct scene_ingame *scn = (struct scene_ingame *)state->scene;
|
||||
|
||||
world_drawdef(scn->world);
|
||||
player_calc(scn->player);
|
||||
|
||||
static t_camera cam = { .pos = { 0 }, .target = { 0, 0, -1 }, .up = { 0, -1, 0 }, .fov = (float)M_PI_2 };
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_left"))
|
||||
cam.pos.x -= 0.01f;
|
||||
if (input_is_action_pressed(&ctx.input, "player_right"))
|
||||
cam.pos.x += 0.01f;
|
||||
if (input_is_action_pressed(&ctx.input, "player_jump"))
|
||||
cam.pos.z -= 0.01f;
|
||||
|
||||
push_camera(&cam);
|
||||
|
||||
push_sprite_ex((t_frect){ .x = 32, .y = 64, .w = 64, .h = 64 }, (t_push_sprite_args){
|
||||
.path = "/assets/light.png",
|
||||
.color = (t_color){255, 0, 0, 255}, });
|
||||
|
||||
push_sprite_ex((t_frect){ .x = 48, .y = 64, .w = 64, .h = 64 }, (t_push_sprite_args){
|
||||
.path = "/assets/light.png",
|
||||
.color = (t_color){0, 255, 0, 255}, });
|
||||
|
||||
push_sprite_ex((t_frect){ .x = 64, .y = 64, .w = 64, .h = 64 }, (t_push_sprite_args){
|
||||
.path = "/assets/light.png",
|
||||
.color = (t_color){0, 0, 255, 255}, });
|
||||
|
||||
push_sprite_ex((t_frect){ .x = 32, .y = 32, .w = 64, .h = 64 }, (t_push_sprite_args){
|
||||
.path = "/assets/player/baron-walk.png",
|
||||
.color = (t_color){255, 255, 255, 255},
|
||||
.rotation = (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64, });
|
||||
|
||||
push_sprite_ex((t_frect){ .x = 64, .y = 32, .w = 64, .h = 64 }, (t_push_sprite_args){
|
||||
.path = "/assets/player/baron-walk.png",
|
||||
.color = (t_color){255, 255, 255, 255},
|
||||
.rotation = (float)M_PI / 64, });
|
||||
|
||||
push_sprite_ex((t_frect){ .x = 96, .y = 32, .w = 64, .h = 64 }, (t_push_sprite_args){
|
||||
.path = "/assets/player/baron-walk.png",
|
||||
.color = (t_color){255, 255, 255, 255}, });
|
||||
|
||||
push_sprite_ex((t_frect){ .x = 128, .y = 32, .w = 128, .h = 64 }, (t_push_sprite_args){
|
||||
.path = "/assets/player/baron-walk.png",
|
||||
.color = (t_color){255, 255, 255, 255},
|
||||
.rotation = (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64, });
|
||||
|
||||
unfurl_triangle("/assets/big-violet.png",
|
||||
(t_fvec3){ -1, -1, 0 },
|
||||
(t_fvec3){ 1, -1, 0 },
|
||||
(t_fvec3){ 1, 1, 0 },
|
||||
(t_shvec2){ 0, 2048 },
|
||||
(t_shvec2){ 2048, 2048 },
|
||||
(t_shvec2){ 2048, 0 });
|
||||
|
||||
unfurl_triangle("/assets/big-violet.png",
|
||||
(t_fvec3){ 1, 1, 0 },
|
||||
(t_fvec3){ -1, 1, 0 },
|
||||
(t_fvec3){ -1, -1, 0 },
|
||||
(t_shvec2){ 2048, 0 },
|
||||
(t_shvec2){ 0, 0 },
|
||||
(t_shvec2){ 0, 2048 });
|
||||
}
|
||||
|
||||
|
||||
static void ingame_end(struct state *state) {
|
||||
struct scene_ingame *scn = (struct scene_ingame *)state->scene;
|
||||
player_destroy(scn->player);
|
||||
world_destroy(scn->world);
|
||||
free(state->scene);
|
||||
}
|
||||
|
||||
|
||||
struct scene *ingame_scene(struct state *state) {
|
||||
(void)state;
|
||||
|
||||
struct scene_ingame *new_scene = ccalloc(1, sizeof *new_scene);
|
||||
new_scene->base.tick = ingame_tick;
|
||||
new_scene->base.end = ingame_end;
|
||||
|
||||
new_scene->world = world_create();
|
||||
new_scene->player = player_create(new_scene->world);
|
||||
|
||||
play_audio_ex("music/repeat-test.xm", "soundtrack", (t_play_audio_args){
|
||||
.volume = 0.8f,
|
||||
.panning = -0.5f,
|
||||
.repeat = true,
|
||||
});
|
||||
|
||||
return (struct scene *)new_scene;
|
||||
}
|
23
apps/testgame/scenes/ingame.h
Normal file
23
apps/testgame/scenes/ingame.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef INGAME_H
|
||||
#define INGAME_H
|
||||
|
||||
|
||||
#include "townengine/game_api.h"
|
||||
#include "../state.h"
|
||||
#include "scene.h"
|
||||
#include "../player.h"
|
||||
#include "../world.h"
|
||||
|
||||
|
||||
struct scene_ingame {
|
||||
struct scene base;
|
||||
|
||||
struct world *world;
|
||||
struct player *player;
|
||||
};
|
||||
|
||||
|
||||
struct scene *ingame_scene(struct state *state);
|
||||
|
||||
|
||||
#endif
|
8
apps/testgame/scenes/scene.c
Normal file
8
apps/testgame/scenes/scene.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include "scene.h"
|
||||
#include "../state.h"
|
||||
|
||||
|
||||
void switch_to(struct state *state, struct scene *(*scene_func)(struct state *)) {
|
||||
state->next_scene = scene_func(state);
|
||||
state->is_scene_switching = true;
|
||||
}
|
16
apps/testgame/scenes/scene.h
Normal file
16
apps/testgame/scenes/scene.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef SCENE_H
|
||||
#define SCENE_H
|
||||
|
||||
|
||||
struct state;
|
||||
struct scene {
|
||||
char *id;
|
||||
void (*tick)(struct state *);
|
||||
void (*end)(struct state *);
|
||||
};
|
||||
|
||||
|
||||
void switch_to(struct state *state, struct scene *(*scene_func)(struct state *));
|
||||
|
||||
|
||||
#endif
|
42
apps/testgame/scenes/title.c
Normal file
42
apps/testgame/scenes/title.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include "title.h"
|
||||
#include "ingame.h"
|
||||
#include "../world.h"
|
||||
#include "../player.h"
|
||||
#include "townengine/game_api.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);
|
||||
}
|
||||
|
||||
|
||||
push_sprite("/assets/title.png", (t_frect) {
|
||||
(RENDER_BASE_WIDTH / 2) - (320 / 2), 64, 320, 128
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
23
apps/testgame/scenes/title.h
Normal file
23
apps/testgame/scenes/title.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef TITLE_H
|
||||
#define TITLE_H
|
||||
|
||||
|
||||
#include "townengine/game_api.h"
|
||||
#include "../state.h"
|
||||
#include "scene.h"
|
||||
#include "../player.h"
|
||||
#include "../world.h"
|
||||
|
||||
|
||||
struct scene_title {
|
||||
struct scene base;
|
||||
|
||||
struct world *world;
|
||||
struct player *player;
|
||||
};
|
||||
|
||||
|
||||
struct scene *title_scene(struct state *state);
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user