move platformes and scenery to /apps/demos/
This commit is contained in:
20
apps/demos/scenery/CMakeLists.txt
Normal file
20
apps/demos/scenery/CMakeLists.txt
Normal file
@ -0,0 +1,20 @@
|
||||
cmake_minimum_required(VERSION 3.21)
|
||||
|
||||
project(scenery LANGUAGES C)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
endif()
|
||||
|
||||
add_subdirectory($ENV{TWNROOT} $ENV{TWNBUILDDIR})
|
||||
|
||||
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})
|
3
apps/demos/scenery/data/packs/data.toml
Normal file
3
apps/demos/scenery/data/packs/data.toml
Normal file
@ -0,0 +1,3 @@
|
||||
[[deps]]
|
||||
source = "../../common-data" # where does it come from, might be an url
|
||||
name = "common-data" # should be globally unique
|
11
apps/demos/scenery/data/twn.toml
Normal file
11
apps/demos/scenery/data/twn.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[about]
|
||||
title = "Serene Scenery"
|
||||
developer = "Townengine Team"
|
||||
app_id = "platformer-demo"
|
||||
dev_id = "townengine-team"
|
||||
|
||||
[game]
|
||||
base_render_width = 640
|
||||
base_render_height = 360
|
||||
|
||||
[engine]
|
80
apps/demos/scenery/game.c
Normal file
80
apps/demos/scenery/game.c
Normal file
@ -0,0 +1,80 @@
|
||||
#include "state.h"
|
||||
#include "scenes/scene.h"
|
||||
#include "scenes/title.h"
|
||||
|
||||
#include "twn_game_api.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.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("debug_toggle");
|
||||
input_bind_action_control("debug_toggle", CONTROL_BACKSPACE);
|
||||
|
||||
input_add_action("debug_dump_atlases");
|
||||
input_bind_action_control("debug_dump_atlases", CONTROL_HOME);
|
||||
|
||||
input_add_action("player_left");
|
||||
input_bind_action_control("player_left", CONTROL_A);
|
||||
|
||||
input_add_action("player_right");
|
||||
input_bind_action_control("player_right", CONTROL_D);
|
||||
|
||||
input_add_action("player_forward");
|
||||
input_bind_action_control("player_forward", CONTROL_W);
|
||||
|
||||
input_add_action("player_backward");
|
||||
input_bind_action_control("player_backward", CONTROL_S);
|
||||
|
||||
input_add_action("player_jump");
|
||||
input_bind_action_control("player_jump", CONTROL_SPACE);
|
||||
|
||||
input_add_action("player_run");
|
||||
input_bind_action_control("player_run", CONTROL_LSHIFT);
|
||||
|
||||
input_add_action("ui_accept");
|
||||
input_bind_action_control("ui_accept", CONTROL_RETURN);
|
||||
|
||||
input_add_action("mouse_capture_toggle");
|
||||
input_bind_action_control("mouse_capture_toggle", CONTROL_ESCAPE);
|
||||
}
|
||||
|
||||
State *state = ctx.udata;
|
||||
|
||||
if (input_is_action_just_pressed("debug_toggle")) {
|
||||
ctx.debug = !ctx.debug;
|
||||
}
|
||||
|
||||
if (input_is_action_just_pressed("debug_dump_atlases")) {
|
||||
textures_dump_atlases();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
117
apps/demos/scenery/scenes/ingame.c
Normal file
117
apps/demos/scenery/scenes/ingame.c
Normal file
@ -0,0 +1,117 @@
|
||||
#include "ingame.h"
|
||||
#include "title.h"
|
||||
#include "scene.h"
|
||||
|
||||
#include "twn_game_api.h"
|
||||
#include "twn_vec.h"
|
||||
|
||||
#define STB_PERLIN_IMPLEMENTATION
|
||||
#include <stb_perlin.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
static void ingame_tick(State *state) {
|
||||
SceneIngame *scn = (SceneIngame *)state->scene;
|
||||
|
||||
if (input_is_mouse_captured()) {
|
||||
const float sensitivity = 0.6f; /* TODO: put this in a better place */
|
||||
scn->yaw += (float)ctx.mouse_relative_position.x * sensitivity;
|
||||
scn->pitch -= (float)ctx.mouse_relative_position.y * sensitivity;
|
||||
scn->pitch = clampf(scn->pitch, -89.0f, 89.0f);
|
||||
|
||||
const float yaw_rad = scn->yaw * (float)DEG2RAD;
|
||||
const float pitch_rad = scn->pitch * (float)DEG2RAD;
|
||||
|
||||
scn->cam.target = m_vec_norm(((Vec3){
|
||||
cosf(yaw_rad) * cosf(pitch_rad),
|
||||
sinf(pitch_rad),
|
||||
sinf(yaw_rad) * cosf(pitch_rad)
|
||||
}));
|
||||
}
|
||||
|
||||
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("player_left"))
|
||||
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(right, speed));
|
||||
|
||||
if (input_is_action_pressed("player_right"))
|
||||
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(right, speed));
|
||||
|
||||
if (input_is_action_pressed("player_forward"))
|
||||
scn->cam.pos = vec3_add(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
|
||||
if (input_is_action_pressed("player_backward"))
|
||||
scn->cam.pos = vec3_sub(scn->cam.pos, m_vec_scale(scn->cam.target, speed));
|
||||
|
||||
if (input_is_action_pressed("player_jump"))
|
||||
scn->cam.pos.y += speed;
|
||||
|
||||
if (input_is_action_pressed("player_run"))
|
||||
scn->cam.pos.y -= speed;
|
||||
|
||||
/* toggle mouse capture with end key */
|
||||
if (input_is_action_just_pressed("mouse_capture_toggle")) {
|
||||
input_set_mouse_captured(!input_is_mouse_captured());
|
||||
}
|
||||
|
||||
draw_camera(&scn->cam);
|
||||
|
||||
#define TERRAIN_FREQUENCY 0.1f
|
||||
|
||||
for (int ly = 64; ly--;) {
|
||||
for (int lx = 64; lx--;) {
|
||||
float x = SDL_truncf(scn->cam.pos.x + 32 - lx);
|
||||
float y = SDL_truncf(scn->cam.pos.z + 32 - ly);
|
||||
|
||||
float d0 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
|
||||
float d1 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)y * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
|
||||
float d2 = stb_perlin_noise3((float)(x + 1) * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
|
||||
float d3 = stb_perlin_noise3((float)x * TERRAIN_FREQUENCY, (float)(y - 1) * TERRAIN_FREQUENCY, 0, 0, 0, 0) * 3 - 6;
|
||||
|
||||
draw_triangle("/assets/grass.png",
|
||||
(Vec3){ (float)x, d0, (float)y },
|
||||
(Vec3){ (float)x + 1, d1, (float)y },
|
||||
(Vec3){ (float)x, d3, (float)y - 1 },
|
||||
(Vec2){ 128, 128 },
|
||||
(Vec2){ 128, 0 },
|
||||
(Vec2){ 0, 128 });
|
||||
|
||||
draw_triangle("/assets/grass.png",
|
||||
(Vec3){ (float)x + 1, d1, (float)y },
|
||||
(Vec3){ (float)x + 1, d2, (float)y - 1 },
|
||||
(Vec3){ (float)x, d3, (float)y - 1 },
|
||||
(Vec2){ 128, 0 },
|
||||
(Vec2){ 0, 0 },
|
||||
(Vec2){ 0, 128 });
|
||||
}
|
||||
}
|
||||
|
||||
draw_skybox("/assets/miramar/miramar_*.tga");
|
||||
draw_fog(0.9, 1.0, 0.05, (Color){ 140, 147, 160, 255 });
|
||||
}
|
||||
|
||||
|
||||
static void ingame_end(State *state) {
|
||||
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->cam = (Camera){ .pos = { 32, 0, 1 }, .up = { 0, 1, 0 }, .fov = (float)M_PI_2 };
|
||||
|
||||
m_audio(m_set(path, "music/mod65.xm"),
|
||||
m_opt(channel, "soundtrack"),
|
||||
m_opt(repeat, true));
|
||||
|
||||
input_set_mouse_captured(true);
|
||||
|
||||
return (Scene *)new_scene;
|
||||
}
|
25
apps/demos/scenery/scenes/ingame.h
Normal file
25
apps/demos/scenery/scenes/ingame.h
Normal 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
apps/demos/scenery/scenes/scene.c
Normal file
8
apps/demos/scenery/scenes/scene.c
Normal 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
apps/demos/scenery/scenes/scene.h
Normal file
16
apps/demos/scenery/scenes/scene.h
Normal 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
|
61
apps/demos/scenery/scenes/title.c
Normal file
61
apps/demos/scenery/scenes/title.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include "title.h"
|
||||
#include "ingame.h"
|
||||
|
||||
#include "twn_game_api.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
static void title_tick(State *state) {
|
||||
SceneTitle *scn = (SceneTitle *)state->scene;
|
||||
(void)scn;
|
||||
|
||||
if (input_is_action_just_pressed("ui_accept")) {
|
||||
switch_to(state, ingame_scene);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
m_sprite("/assets/title.png", ((Rect) {
|
||||
((float)ctx.base_draw_w / 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 = draw_text_width(text_str, text_h, font);
|
||||
|
||||
draw_rectangle(
|
||||
(Rect) {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.w = (float)text_w,
|
||||
.h = (float)text_h,
|
||||
},
|
||||
(Color) { 0, 0, 0, 255 }
|
||||
);
|
||||
draw_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
apps/demos/scenery/scenes/title.h
Normal file
16
apps/demos/scenery/scenes/title.h
Normal 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
apps/demos/scenery/state.h
Normal file
20
apps/demos/scenery/state.h
Normal 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
|
Reference in New Issue
Block a user