hot reloading and friends
This commit is contained in:
@ -4,11 +4,13 @@
|
||||
#include <malloc.h>
|
||||
|
||||
|
||||
void game_tick(void) {
|
||||
/* do your initialization on first tick */
|
||||
if (ctx.tick_count == 0) {
|
||||
void game_tick(t_ctx *ctx) {
|
||||
/* do state initialization when engine asks for it */
|
||||
/* it could happen multiple times per application run, as game code is reloadable */
|
||||
if (ctx.initialization_needed) {
|
||||
/* application data could be stored in ctx.udata and retrieved anywhere */
|
||||
ctx.udata = ccalloc(1, sizeof (struct state));
|
||||
if (!ctx.udata)
|
||||
ctx.udata = ccalloc(1, sizeof (struct state));
|
||||
}
|
||||
|
||||
/* a lot of data is accessible from `ctx`, look into `townengine/context.h` for more */
|
||||
|
@ -23,8 +23,4 @@ set(SOURCE_FILES
|
||||
scenes/ingame.c scenes/ingame.h
|
||||
)
|
||||
|
||||
use_townengine(${PROJECT_NAME} "${SOURCE_FILES}")
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
use_townengine(${PROJECT_NAME} "${SOURCE_FILES}" ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
@ -1,8 +1,9 @@
|
||||
#include "townengine/game_api.h"
|
||||
#include "state.h"
|
||||
#include "scenes/scene.h"
|
||||
#include "scenes/title.h"
|
||||
|
||||
#include "townengine/game_api.h"
|
||||
|
||||
#include <SDL_scancode.h>
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
@ -10,11 +11,16 @@
|
||||
|
||||
|
||||
void game_tick(void) {
|
||||
if (ctx.tick_count == 0) {
|
||||
ctx.udata = ccalloc(1, sizeof (struct state));
|
||||
if (ctx.initialization_needed) {
|
||||
if (!ctx.udata) {
|
||||
ctx.udata = ccalloc(1, sizeof (struct state));
|
||||
|
||||
struct state *state = ctx.udata;
|
||||
state->ctx = &ctx;
|
||||
state->scene = title_scene(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);
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include "scene.h"
|
||||
|
||||
#include "townengine/game_api.h"
|
||||
#include "townengine/tabela.h"
|
||||
|
||||
#define STB_PERLIN_IMPLEMENTATION
|
||||
#include <stb_perlin.h>
|
||||
@ -15,8 +14,6 @@ static void ingame_tick(struct state *state) {
|
||||
world_drawdef(scn->world);
|
||||
player_calc(scn->player);
|
||||
|
||||
static t_camera cam = { .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
|
||||
|
||||
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;
|
||||
@ -26,32 +23,32 @@ static void ingame_tick(struct state *state) {
|
||||
const float yaw_rad = scn->yaw * (float)DEG2RAD;
|
||||
const float pitch_rad = scn->pitch * (float)DEG2RAD;
|
||||
|
||||
cam.target = m_vec_norm(((t_fvec3){
|
||||
scn->cam.target = m_vec_norm(((t_fvec3){
|
||||
cosf(yaw_rad) * cosf(pitch_rad),
|
||||
sinf(pitch_rad),
|
||||
sinf(yaw_rad) * cosf(pitch_rad)
|
||||
}));
|
||||
}
|
||||
|
||||
const t_fvec3 right = m_vec_norm(m_vec_cross(cam.target, cam.up));
|
||||
const t_fvec3 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"))
|
||||
cam.pos = fvec3_sub(cam.pos, m_vec_scale(right, speed));
|
||||
scn->cam.pos = fvec3_sub(scn->cam.pos, m_vec_scale(right, speed));
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_right"))
|
||||
cam.pos = fvec3_add(cam.pos, m_vec_scale(right, speed));
|
||||
scn->cam.pos = fvec3_add(scn->cam.pos, m_vec_scale(right, speed));
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_forward"))
|
||||
cam.pos = fvec3_add(cam.pos, m_vec_scale(cam.target, speed));
|
||||
scn->cam.pos = fvec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_backward"))
|
||||
cam.pos = fvec3_sub(cam.pos, m_vec_scale(cam.target, speed));
|
||||
scn->cam.pos = fvec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_jump"))
|
||||
cam.pos.y += speed;
|
||||
scn->cam.pos.y += speed;
|
||||
|
||||
if (input_is_action_pressed(&ctx.input, "player_run"))
|
||||
cam.pos.y -= speed;
|
||||
scn->cam.pos.y -= speed;
|
||||
|
||||
/* toggle mouse capture with end key */
|
||||
if (input_is_action_just_pressed(&ctx.input, "mouse_capture_toggle")) {
|
||||
@ -84,16 +81,16 @@ static void ingame_tick(struct state *state) {
|
||||
m_set(rect, ((t_frect){ 128, 32, 128, 64 })),
|
||||
m_opt(rotation, (float)M_PI * 2 * (float)(ctx.tick_count % 64) / 64 ));
|
||||
|
||||
set_camera(&cam);
|
||||
set_camera(&scn->cam);
|
||||
|
||||
#define TERRAIN_FREQUENCY 0.1f
|
||||
|
||||
for (int y = 64; y--;) {
|
||||
for (int x = 64; x--;) {
|
||||
float d0 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 10 - 6;
|
||||
float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 10 - 6;
|
||||
float d2 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 10 - 6;
|
||||
float d3 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 10 - 6;
|
||||
float d0 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
||||
float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
||||
float d2 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
||||
float d3 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 20 - 6;
|
||||
|
||||
unfurl_triangle("/assets/grass.gif",
|
||||
(t_fvec3){ (float)x, d0, (float)y },
|
||||
@ -133,6 +130,8 @@ struct scene *ingame_scene(struct state *state) {
|
||||
new_scene->world = world_create();
|
||||
new_scene->player = player_create(new_scene->world);
|
||||
|
||||
new_scene->cam = (t_camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
|
||||
|
||||
play_audio_ex("music/mod65.xm", "soundtrack", (t_play_audio_args){
|
||||
.repeat = true,
|
||||
.volume = 1.0f
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
|
||||
#include "townengine/game_api.h"
|
||||
|
||||
#include "../state.h"
|
||||
#include "scene.h"
|
||||
#include "../player.h"
|
||||
@ -15,6 +16,8 @@ struct scene_ingame {
|
||||
struct world *world;
|
||||
struct player *player;
|
||||
|
||||
t_camera cam;
|
||||
|
||||
/* TODO: put this in a better place */
|
||||
float yaw;
|
||||
float pitch;
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "ingame.h"
|
||||
#include "../world.h"
|
||||
#include "../player.h"
|
||||
|
||||
#include "townengine/game_api.h"
|
||||
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "world.h"
|
||||
#include "townengine/game_api.h"
|
||||
|
||||
#include "world.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
Reference in New Issue
Block a user