/apps: separate /apps/testgame into /apps/platformer and /apps/scenery, update /apps/template

This commit is contained in:
veclavtalica committed 2024-09-25 18:29:58 +03:00
1 parent 65425f5b2e
commit a7d97a1070
26 files changed
+312 -36

No files matched your search

@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.21)
project(testgame LANGUAGES C)
project(platfromer LANGUAGES C)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+57
View File
@@ -0,0 +1,57 @@
#include "ingame.h"
#include "title.h"
#include "scene.h"
#include "twn_game_api.h"
static void ingame_tick(State *state) {
SceneIngame *scn = (SceneIngame *)state->scene;
world_drawdef(scn->world);
player_calc(scn->player);
const Vec3 right = m_vec_norm(m_vec_cross(scn->cam.target, scn->cam.up));
const float speed = 0.04f; /* TODO: put this in a better place */
if (input_is_action_pressed(&ctx.input, "player_left"))
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed));
if (input_is_action_pressed(&ctx.input, "player_right"))
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(right, speed));
if (input_is_action_pressed(&ctx.input, "player_forward"))
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
if (input_is_action_pressed(&ctx.input, "player_backward"))
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
if (input_is_action_pressed(&ctx.input, "player_jump"))
scn->cam.pos.y += speed;
if (input_is_action_pressed(&ctx.input, "player_run"))
scn->cam.pos.y -= speed;
}
static void ingame_end(State *state) {
SceneIngame *scn = (SceneIngame *)state->scene;
player_destroy(scn->player);
world_destroy(scn->world);
free(state->scene);
}
Scene *ingame_scene(State *state) {
(void)state;
SceneIngame *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);
new_scene->cam = (Camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
return (Scene *)new_scene;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+23
View File
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.21)
project(scenery LANGUAGES C)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
# add root townengine cmake file
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(../../ ../../../.build)
endif()
set(SOURCE_FILES
game.c
state.h
scenes/scene.c scenes/scene.h
scenes/title.c scenes/title.h
scenes/ingame.c scenes/ingame.h
)
use_townengine(${PROJECT_NAME} "${SOURCE_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ../../data)
+7
View File
@@ -0,0 +1,7 @@
#!/bin/env sh
if [ $1 = "web" ]; then
emcmake cmake -B .build-web "${@:2}" && cmake --build .build-web
else
cmake -B .build "$@" && cmake --build .build
fi
+73
View File
@@ -0,0 +1,73 @@
#include "state.h"
#include "scenes/scene.h"
#include "scenes/title.h"
#include "twn_game_api.h"
#include <SDL_scancode.h>
#include <stdio.h>
#include <malloc.h>
#include <stdint.h>
void game_tick(void) {
if (ctx.initialization_needed) {
if (!ctx.udata) {
ctx.udata = ccalloc(1, sizeof (State));
State *state = ctx.udata;
state->ctx = &ctx;
state->scene = title_scene(state);
}
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");
input_bind_action_scancode(&ctx.input, "player_left", SDL_SCANCODE_A);
input_add_action(&ctx.input, "player_right");
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);
input_add_action(&ctx.input, "player_jump");
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);
input_add_action(&ctx.input, "ui_accept");
input_bind_action_scancode(&ctx.input, "ui_accept", SDL_SCANCODE_RETURN);
input_add_action(&ctx.input, "mouse_capture_toggle");
input_bind_action_scancode(&ctx.input, "mouse_capture_toggle", SDL_SCANCODE_ESCAPE);
}
State *state = ctx.udata;
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) {
State *state = ctx.udata;
state->scene->end(state);
free(state);
}
@@ -11,9 +11,6 @@
static void ingame_tick(State *state) {
SceneIngame *scn = (SceneIngame *)state->scene;
world_drawdef(scn->world);
player_calc(scn->player);
if (input_is_mouse_captured(&ctx.input)) {
const float sensitivity = 0.6f; /* TODO: put this in a better place */
scn->yaw += (float)ctx.input.mouse_relative_position.x * sensitivity;
@@ -55,30 +52,6 @@ static void ingame_tick(State *state) {
input_set_mouse_captured(&ctx.input, !input_is_mouse_captured(&ctx.input));
}
m_sprite(m_set(path, "/assets/9slice.png"),
m_set(rect, ((Rect){ 16, 16, 128, 128 })),
m_opt(texture_region, ((Rect){ 0, 0, (float)(ctx.tick_count % 48), (float)(ctx.tick_count % 48) })));
m_sprite(m_set(path, "/assets/light.png"),
m_set(rect, ((Rect){ 48, 64, 64, 64 })),
m_opt(color, ((Color){ 255, 0, 0, 255 })));
m_sprite(m_set(path, "/assets/light.png"),
m_set(rect, ((Rect){ 64, 64, 64, 64 })),
m_opt(color, ((Color){ 0, 255, 0, 255 })));
m_sprite(m_set(path, "/assets/light.png"),
m_set(rect, ((Rect){ 80, 64, 64, 64 })),
m_opt(color, ((Color){ 0, 0, 255, 255 })));
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
m_set(rect, ((Rect){ 32, 32, 64, 64 })),
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
m_sprite(m_set(path, "/assets/player/baron-walk.png"),
m_set(rect, ((Rect){ 128, 32, 128, 64 })),
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
set_camera(&scn->cam);
#define TERRAIN_FREQUENCY 0.1f
@@ -111,9 +84,6 @@ static void ingame_tick(State *state) {
static void ingame_end(State *state) {
SceneIngame *scn = (SceneIngame *)state->scene;
player_destroy(scn->player);
world_destroy(scn->world);
free(state->scene);
}
@@ -125,9 +95,6 @@ Scene *ingame_scene(State *state) {
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);
new_scene->cam = (Camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
audio_play_ex("music/mod65.xm", "soundtrack", (PlayAudioArgs){
+25
View File
@@ -0,0 +1,25 @@
#ifndef INGAME_H
#define INGAME_H
#include "twn_game_api.h"
#include "../state.h"
#include "scene.h"
typedef struct SceneIngame {
Scene base;
Camera cam;
/* TODO: put this in a better place */
float yaw;
float pitch;
float roll;
} SceneIngame;
Scene *ingame_scene(State *state);
#endif
+8
View File
@@ -0,0 +1,8 @@
#include "scene.h"
#include "../state.h"
void switch_to(State *state, Scene *(*scene_func)(State *)) {
state->next_scene = scene_func(state);
state->is_scene_switching = true;
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef SCENE_H
#define SCENE_H
typedef struct State State;
typedef struct Scene {
char *id;
void (*tick)(State *);
void (*end)(State *);
} Scene;
void switch_to(State *state, Scene *(*scene_func)(State *));
#endif
+64
View File
@@ -0,0 +1,64 @@
#include "title.h"
#include "ingame.h"
#include "twn_game_api.h"
#include <stdio.h>
static void title_tick(State *state) {
SceneTitle *scn = (SceneTitle *)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", ((Rect) {
((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 = text_get_width(text_str, text_h, font);
push_rectangle(
(Rect) {
.x = 0,
.y = 0,
.w = (float)text_w,
.h = (float)text_h,
},
(Color) { 0, 0, 0, 255 }
);
push_text(
text_str,
(Vec2){ 0, 0 },
text_h,
(Color) { 255, 255, 255, 255 },
font
);
free(text_str);
}
static void title_end(State *state) {
free(state->scene);
}
Scene *title_scene(State *state) {
(void)state;
SceneTitle *new_scene = ccalloc(1, sizeof *new_scene);
new_scene->base.tick = title_tick;
new_scene->base.end = title_end;
return (Scene *)new_scene;
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef TITLE_H
#define TITLE_H
#include "../state.h"
#include "scene.h"
typedef struct SceneTitle {
Scene base;
} SceneTitle;
Scene *title_scene(State *state);
#endif
+20
View File
@@ -0,0 +1,20 @@
#ifndef STATE_H
#define STATE_H
#include "twn_game_api.h"
#include <stdbool.h>
typedef struct Scene Scene;
typedef struct State {
Context *ctx;
Scene *scene;
Scene *next_scene;
bool is_scene_switching;
} State;
#endif
+1 -1
View File
@@ -1,4 +1,4 @@
#include "townengine/game_api.h"
#include "twn_game_api.h"
#include "state.h"
#include <malloc.h>
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef STATE_H
#define STATE_H
#include "townengine/game_api.h"
#include "twn_game_api.h"
/* populate it with state information */
struct state {