Compare commits

...

48 Commits

Author SHA1 Message Date
veclavtalica
75890b1a71 /apps/tools/twndel: thingies 2025-03-15 07:59:55 +03:00
veclavtalica
73db3e57dc various small tweaks 2025-03-13 03:08:35 +03:00
veclavtalica
2975aa2dfb comparing with whole number floats is okay. 2025-03-13 02:54:40 +03:00
veclavtalica
6726faf719 twn_textures: lock cache when adding requests 2025-03-13 01:18:07 +03:00
veclavtalica
183dfa6be5 twn_lines: combine differently colored lines into same batch 2025-03-12 12:00:53 +03:00
veclavtalica
e974194af0 /docs/wiki: G1. Trigonometry starter 2025-03-12 01:36:42 +03:00
veclavtalica
8607aa48ec /apps/tools/twndel: face hovering 2025-03-11 07:40:18 +03:00
veclavtalica
f6600dfbda norm is needed after all. 2025-03-11 07:09:55 +03:00
veclavtalica
bdabd04388 /apps/tools/twndel: reversable triangulation, fix to point finding with triangles 2025-03-11 07:02:52 +03:00
veclavtalica
0e075ec334 /apps/tools/twndel: texture painting, face selection 2025-03-11 04:58:44 +03:00
veclavtalica
b256fc903a /apps/tools/twndel: triangluation, triangle render 2025-03-11 02:41:03 +03:00
veclavtalica
b52ecaeaa0 remove logging 2025-03-11 01:54:36 +03:00
veclavtalica
37e46e9a7e /apps/tools/twndel: initial texture projection work 2025-03-11 01:53:45 +03:00
veclavtalica
a472e6af52 /apps/demos/scenery: simpler normal_at(), clean one bit 2025-03-11 01:28:08 +03:00
veclavtalica
66b2f04d9d fix missing texture sizing, fix bordered repeated texture upload, make quads default to repeating for now 2025-03-10 22:10:53 +03:00
veclavtalica
90f4097070 /apps/templates/zig: depend on /src/*.zig sources for rebuilding, add .gitignore 2025-03-10 10:11:10 +03:00
veclavtalica
829ff4780c zig template 2025-03-10 09:58:28 +03:00
veclavtalica
8e15c9ec3c twn_util: dont add empty list 2025-03-10 07:05:45 +03:00
veclavtalica
474ea84a77 /apps/tools/twndel: some data 2025-03-10 06:54:23 +03:00
veclavtalica
7b8b9416ba twn_util: file_read() :images 2025-03-10 06:54:10 +03:00
veclavtalica
8ed8158ae6 twn_util: file_read() with :exists 2025-03-10 05:34:29 +03:00
veclavtalica
48e3a4c233 /share/twn_api.json: add new logging funcs 2025-03-10 05:29:56 +03:00
veclavtalica
56530f9864 twn_util: final cleaning up, introducton of powerful file_read() 2025-03-10 05:19:58 +03:00
veclavtalica
f86f3dd41a /apps/tools/twndel: use roundf for snapping 2025-03-09 08:12:29 +03:00
veclavtalica
adae6be7e5 /apps/tools/twndel: camera translation 2025-03-09 07:38:32 +03:00
veclavtalica
cd3033f9c4 /apps/tools/twndel: add reference lines 2025-03-09 06:53:54 +03:00
veclavtalica
e11e63f273 /apps/tools/twndel: more plane awareness 2025-03-09 06:24:05 +03:00
veclavtalica
75737b738f /apps/tools/twndel: plane intersection based point move 2025-03-09 00:30:20 +03:00
veclavtalica
ce2c2513aa /apps/tool/twndel: grab.png 2025-03-08 21:57:53 +03:00
veclavtalica
36c0af9953 hopefully more portable way of packaging binary embeds 2025-03-08 18:11:39 +03:00
veclavtalica
826622cd58 twn_draw: proper ortho unproject 2025-03-08 04:38:56 +03:00
veclavtalica
78b6a26de9 twndel: fix y axis point move 2025-03-08 04:10:14 +03:00
veclavtalica
5f7b8bac6d undo, axis editing 2025-03-08 02:20:31 +03:00
veclavtalica
6d6230c6a1 twn_audio: fix ogg vorbis sample reuse 2025-03-08 01:38:38 +03:00
veclavtalica
c07e16490e model tool progress: initial selection 2025-03-08 00:50:47 +03:00
veclavtalica
f5e55bb997 /apps/tools/twndel: point selection, sounds and other assets 2025-03-07 20:56:19 +03:00
veclavtalica
1e6e323fe1 twn_draw: remove logging 2025-03-07 20:21:36 +03:00
veclavtalica
dbf9599fe5 twn_api.json: add draw_camera_unproject() 2025-03-07 19:40:56 +03:00
veclavtalica
923cd81571 twn_draw: draw_camera_unproject() 2025-03-07 19:31:46 +03:00
veclavtalica
733a1786ab twn_draw: multiply zoom by 0.1 to match projections 2025-03-07 11:16:31 +03:00
veclavtalica
a03e1d885d twn_camera: fix aspect of ortho projection 2025-03-07 11:07:05 +03:00
veclavtalica
67feb5974a /apps/tools/twndel: start the thing 2025-03-07 07:10:44 +03:00
veclavtalica
5be4ed4645 twn_lines: bail on empty batch 2025-03-07 07:05:20 +03:00
veclavtalica
4a41f47a58 twn_textures: fix freeing of missed texture 2025-03-07 06:19:44 +03:00
veclavtalica
35bb26705a twn_text: use full ascii range 2025-03-07 05:16:46 +03:00
veclavtalica
13bc71a28d twn_text: embed default font 2025-03-07 03:35:35 +03:00
veclavtalica
b97a155de4 /bin: make utilities quit on error 2025-03-07 02:56:12 +03:00
veclavtalica
5df80addeb /apps/demos/scenery: no drifting :( 2025-03-07 00:51:07 +03:00
66 changed files with 2397 additions and 181 deletions

View File

@ -287,6 +287,24 @@ function(link_deps target)
endfunction()
function(put_townengine output_directory)
cmake_path(GET TWN_OUT_DIR STEM LAST_ONLY target)
add_executable(${target} ${TWN_ROOT_DIR}/src/twn_main.c)
target_link_options(${target} PRIVATE $<$<BOOL:${LINUX}>:-Wl,-rpath,$ORIGIN/>)
set_target_properties(${TWN_TARGET} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
set_target_properties(${target} PROPERTIES
OUTPUT_NAME ${target}
LIBRARY_OUTPUT_DIRECTORY ${output_directory}
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
give_options(${target})
include_deps(${target})
link_deps(${target})
target_link_libraries(${target} PUBLIC ${TWN_TARGET})
endfunction()
function(use_townengine sources output_directory)
cmake_path(GET TWN_OUT_DIR STEM LAST_ONLY target)
@ -354,5 +372,18 @@ link_deps(twn_third_parties)
give_options(${TWN_TARGET})
include_deps(${TWN_TARGET})
link_deps(${TWN_TARGET})
target_link_libraries(${TWN_TARGET} PUBLIC twn_third_parties)
target_include_directories(${TWN_TARGET} PRIVATE ${TWN_ROOT_DIR}/src)
target_link_libraries(${TWN_TARGET} PUBLIC
twn_third_parties
${CMAKE_CURRENT_BINARY_DIR}/font.o)
# embed resources
# TODO: think of a portable way to compress/decompress them
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/font.o
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -E env CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR} sh bin/prep-embed.sh
DEPENDS share/assets/Dernyns256.ttf)
add_custom_target(asset-compilation ALL DEPENDS
${CMAKE_CURRENT_BINARY_DIR}/font.o)

View File

@ -29,10 +29,6 @@ void game_tick(void) {
ctx.debug = !ctx.debug;
}
if (input_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 */

View File

@ -30,10 +30,6 @@ void game_tick(void) {
ctx.debug = !ctx.debug;
}
if (input_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 */

View File

@ -58,13 +58,15 @@ static float heightmap[TERRAIN_DISTANCE][TERRAIN_DISTANCE];
#define VEHICLE_WIDTH 1.7f
#define VEHICLE_HEIGHT 1.3f
/* spring constant */
#define VEHICLE_SPRING_K 20000.0f
#define VEHICLE_SPRING_GK 80000.0f
#define VEHICLE_SPRING_K 22000.0f
#define VEHICLE_SPRING_K_SHOCK 18000.0f
#define VEHICLE_SPRING_GK 70000.0f
/* damping constant */
#define VEHICLE_SPRING_C 800.0f
#define VEHICLE_SPRING_C_SHOCK 500.0f
#define VEHICLE_SPRING_GC 100.0f
#define VEHICLE_FRICTION_S 1000.0f
#define VEHICLE_FRICTION_K 100.0f
#define VEHICLE_FRICTION_K 110.0f
#define VEHICLE_FRICTION_V 4000.0f
/* TODO: shock springs, that are more loose, which are used to simulate the wheels */
@ -107,6 +109,12 @@ static float height_at(SceneIngame *scn, Vec2 position);
static Vec3 normal_at(SceneIngame *scn, Vec2 position);
static inline float clampf(float f, float min, float max) {
const float t = f < min ? min : f;
return t > max ? max : t;
}
static void draw_vehicle(SceneIngame *scn) {
for (size_t i = 0; i < 12; ++i)
draw_line_3d(vbp[vbs[i][0]], vbp[vbs[i][1]], 1, (Color){255, 255, 255, 255});
@ -122,13 +130,23 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 Facc[8] = {0};
/* steering */
if (input_action_pressed("player_left"))
bool steered = false;
if (input_action_pressed("player_left")) {
vehicle_turning_extend -= vehicle_turning_speed;
steered = true;
}
if (input_action_pressed("player_right"))
if (input_action_pressed("player_right")) {
vehicle_turning_extend += vehicle_turning_speed;
steered = true;
}
if (!steered)
vehicle_turning_extend -= copysignf(vehicle_turning_speed * 0.9f, vehicle_turning_extend);
vehicle_turning_extend = clampf(vehicle_turning_extend, -vehicle_turning_extend_limit, vehicle_turning_extend_limit);
if (fabsf(vehicle_turning_extend) <= 0.11f)
vehicle_turning_extend = 0;
Vec3 const Fg = { .y = -VEHICLE_MASS * G_CONST };
for (size_t i = 0; i < 8; ++i)
@ -146,7 +164,9 @@ static void process_vehicle(SceneIngame *scn) {
float const lar = vec3_length(vec3_sub(vbpi[vbs[i][1]], vbpi[vbs[i][0]]));
float const x = vec3_length(pd) - lar;
float const v = vec3_dot(vec3_sub(v1, v0), pn);
Vec3 const Fs = vec3_scale(pn, -VEHICLE_SPRING_K * x - VEHICLE_SPRING_C * v);
float const spring_k = i == 2 | i == 5 || i == 8 || i == 11 ? VEHICLE_SPRING_K_SHOCK : VEHICLE_SPRING_K;
float const spring_c = i == 2 | i == 5 || i == 8 || i == 11 ? VEHICLE_SPRING_C_SHOCK : VEHICLE_SPRING_C;
Vec3 const Fs = vec3_scale(pn, -spring_k * x - spring_c * v);
Facc[vbs[i][0]] = vec3_sub(Facc[vbs[i][0]], Fs);
Facc[vbs[i][1]] = vec3_add(Facc[vbs[i][1]], Fs);
}
@ -156,16 +176,16 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 const p = vbp[i];
Vec3 const v = vbv[i];
float const h = height_at(scn, (Vec2){ p.x, p.z });
Vec3 const fwd = vec3_norm(vec3_sub(vbp[1], vbp[0]));
if (h >= p.y) {
/* back wheel processing: acceleration */
if (i == 0 || i == 3 || i == 4 || i == 7) {
if (i == 0 || i == 3) {
float scale = 0;
if (scn->camera_mode == 2 && input_action_pressed("player_forward"))
scale += 1;
if (scn->camera_mode == 2 && input_action_pressed("player_backward"))
scale -= 1;
Vec3 const dir = vec3_sub(vbp[1], vbp[0]);
Facc[i] = vec3_add(Facc[i], vec3_scale(vec3_norm(dir), 6500 * scale));
Facc[i] = vec3_add(Facc[i], vec3_scale(fwd, 6500 * scale));
}
/* normal force, for displacement */
@ -194,21 +214,28 @@ static void process_vehicle(SceneIngame *scn) {
/* not at rest, stop accelerating along the surface */
} else if (vol + (Fol / VEHICLE_MASS) * ctx.frame_duration <= fkxn * ctx.frame_duration * 2) {
/* ugh ... */
vbv[i] = vec3_add(vbv[i], vo);
vbv[i] = vec3_add(v, vo);
/* just apply friction */
} else {
Fo = vec3_scale(von, -fkxn * 400);
}
Facc[i] = vec3_add(Facc[i], Fo);
/* rear wheel friction */
if (i == 0 || i == 3) {
Vec3 const pn = vec3_cross(fwd, n);
Vec3 const Fp = vec3_scale(pn, vec3_dot(v, pn) * -VEHICLE_FRICTION_V);
Facc[i] = vec3_add(Facc[i], Fp);
}
/* front wheel processing */
if (i == 1 || i == 5 || i == 2 || i == 6) {
if (i == 1 || i == 2) {
/* steering influences "center of turning", which is a point */
/* laying on line defined by rear axle */
/* front arms are rotated to be perpendicular to center of turning, */
/* which then are used to dissipate forces, thus providing control */
Vec3 const rear_bar = vec3_sub(vbp[0], vbp[3]);
Vec3 const rear_center = vec3_add(vbp[3], vec3_scale(rear_bar, 0.5));
Vec3 const rear_center = vec3_scale(vec3_add(vbp[0], vbp[3]), 0.5);
Vec3 a, b, r;
if (i == 1) {
a = vec3_sub(vbp[3], vbp[2]);
@ -226,9 +253,9 @@ static void process_vehicle(SceneIngame *scn) {
Vec3 const n = vec3_norm(vec3_cross(a, b));
Vec3 const wheel = vec3_norm(vec3_rotate(arm, -arm_angle, n));
Vec3 const p = vec3_norm(vec3_cross(wheel, n));
draw_line_3d(r, vec3_add(r, wheel), 1, (Color){0,255,255,255});
draw_line_3d(r, vec3_add(r, p), 1, (Color){0,255,255,255});
Vec3 const Fp = vec3_scale(p, vec3_dot(vbv[i], p) * -VEHICLE_FRICTION_V);
Vec3 const Fp = vec3_scale(p, vec3_dot(v, p) * -VEHICLE_FRICTION_V);
Facc[i] = vec3_add(Facc[i], Fp);
}
}
@ -305,10 +332,10 @@ static Vec3 normal_at(SceneIngame *scn, Vec2 position) {
float const height1 = heightmap[x + 1][y];
float const height2 = heightmap[x][y + 1];
Vec3 const a = { .x = -1, .y = height0 - height1, .z = 0 };
Vec3 const a = { .x = 1, .y = height0 - height1, .z = 0 };
Vec3 const b = { .x = 0, .y = height0 - height2, .z = -1 };
return vec3_scale(vec3_norm(vec3_cross(a, b)), -1);
return vec3_norm(vec3_cross(a, b));
}
/* TODO: don't operate on triangles, instead interpolate on quads */
@ -504,12 +531,8 @@ static void ingame_tick(State *state) {
input_action("mouse_capture_toggle", "ESCAPE");
input_action("toggle_camera_mode", "C");
// draw_model("models/test.obj", (Vec3){0}, (Vec3){0,0,1}, (Vec3){1.f / 64,1.f / 64,1.f / 64});
// draw_model("models/test2.obj", (Vec3){0}, (Vec3){0,0,1}, (Vec3){1.f / 64,1.f / 64,1.f / 64});
// draw_model("models/bunny.obj", (Vec3){0}, (Vec3){0,0,1}, (Vec3){4.,4.,4.});
if (scn->mouse_captured) {
const float sensitivity = 0.4f * (float)DEG2RAD; /* TODO: put this in a better place */
const float sensitivity = 0.4f * (float)(M_PI / 180); /* TODO: put this in a better place */
scn->yaw += (float)ctx.mouse_movement.x * sensitivity;
scn->pitch -= (float)ctx.mouse_movement.y * sensitivity;
scn->pitch = clampf(scn->pitch, (float)-M_PI * 0.49f, (float)M_PI * 0.49f);

19
apps/templates/zig/.gitignore vendored Normal file
View File

@ -0,0 +1,19 @@
# ignore executables
*
!*.*
!*/
**/*.so
**/*.dll
**/*.exe
**/*.trace
**/*.js
**/*.wasm
**/*.wasm.map
**/*.data
**/*.html
data/scripts/twnapi.lua
build/
.zig-cache/
zig-out/

View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.21)
project(twngame LANGUAGES C)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
add_subdirectory($ENV{TWNROOT} ${CMAKE_BINARY_DIR}/twn)
cmake_path(GET CMAKE_SOURCE_DIR STEM LAST_ONLY GAME_PROJECT_NAME)
put_townengine(${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB_RECURSE zig-sources ${CMAKE_CURRENT_SOURCE_DIR}/src/*.zig)
# TODO: support static build
# TODO: propagate release switches
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/libgame.so
COMMAND env zig build
DEPENDS ${TWN_TARGET} ${zig-sources}
)
add_custom_target(
zig-step ALL
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/libgame.so
)

View File

@ -0,0 +1,58 @@
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
// This creates a "module", which represents a collection of source files alongside
// some compilation options, such as optimization mode and linked system libraries.
// Every executable or library we compile will be based on one or more modules.
const lib_mod = b.createModule(.{
// `root_source_file` is the Zig "entry point" of the module. If a module
// only contains e.g. external object files, you can make this `null`.
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
lib_mod.addIncludePath(b.path("../../../"));
lib_mod.addIncludePath(b.path("../../../include/"));
lib_mod.addLibraryPath(b.path("./"));
// Now, we will create a static library based on the module we created above.
// This creates a `std.Build.Step.Compile`, which is the build step responsible
// for actually invoking the compiler.
const lib = b.addLibrary(.{
.linkage = .dynamic,
.name = "game",
.root_module = lib_mod,
});
lib.linkSystemLibrary("townengine");
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
const install_artifact = b.addInstallArtifact(lib, .{
.dest_dir = .{
.override = .{
.custom = "../",
},
},
});
b.getInstallStep().dependOn(&install_artifact.step);
}

View File

@ -0,0 +1,27 @@
# This file contains everything about the engine and your game that can be
# configured before it runs.
#
# Optional settings are commented out, with their default values shown.
# Invalid values in these settings will be ignored.
# Data about your game as an application
[about]
title = "Zig Awesomeness"
developer = "notwanp"
app_id = "yourzigthing"
dev_id = "definatelynotwanp"
# Game runtime details
[game]
resolution = [ 640, 480 ]
background_color = [ 255, 125, 0, 255 ]
#debug = true
# Engine tweaks. You probably don't need to change these
[engine]
#ticks_per_second = 60 # minimum of 8
#keybind_slots = 3 # minimum of 1
#texture_atlas_size = 2048 # minimum of 32
#font_texture_size = 2048 # minimum of 1024
#font_oversampling = 4 # minimum of 0
#font_filtering = "linear" # possible values: "nearest", "linear"

View File

@ -0,0 +1,24 @@
const std = @import("std");
const c = @cImport({
@cInclude("twn_game_api.h");
});
export fn game_tick() void {
tick() catch |err| {
std.log.err(@errorName(err));
};
}
export fn game_end() void {
end() catch |err| {
std.log.err(@errorName(err));
};
}
fn tick() !void {
if (c.ctx.initialization_needed) {
std.debug.print("lmao\n", .{});
}
}
fn end() !void {}

View File

@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.21)
project(twndel LANGUAGES C)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
add_subdirectory($ENV{TWNROOT} ${CMAKE_BINARY_DIR}/twn)
set(SOURCE_FILES
tool.c
state.h
)
cmake_path(GET CMAKE_SOURCE_DIR STEM LAST_ONLY GAME_PROJECT_NAME)
use_townengine("${SOURCE_FILES}" ${CMAKE_CURRENT_SOURCE_DIR})

BIN
apps/tools/twndel/data/bong.ogg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
apps/tools/twndel/data/camera.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
apps/tools/twndel/data/center.png (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

BIN
apps/tools/twndel/data/drop.ogg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
apps/tools/twndel/data/grab.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
apps/tools/twndel/data/point.png (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
apps/tools/twndel/data/selectin.ogg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
apps/tools/twndel/data/selectout.ogg (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
apps/tools/twndel/data/switch.ogg (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,12 @@
[about]
title = "Townengine Modeling Tool"
developer = "twnteam"
app_id = "twndel"
dev_id = "twnteam"
# Game runtime details
[game]
resolution = [ 640, 480 ]
background_color = [ 255, 125, 0, 255 ]
[engine]

BIN
apps/tools/twndel/data/x.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
apps/tools/twndel/data/y.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
apps/tools/twndel/data/z.png (Stored with Git LFS) Normal file

Binary file not shown.

118
apps/tools/twndel/state.h Normal file
View File

@ -0,0 +1,118 @@
#ifndef STATE_H
#define STATE_H
#include "twn_game_api.h"
#define POINTS_PER_METER 128
#define UNDO_STACK_SIZE 32
#define CAMERA_FOV ((float)M_PI_2 * 0.75f)
#define POINT_LIMIT 65534
#define FACE_LIMIT 2048
#define OBJECT_LIMIT 16
#define TEXTURE_LIMIT 32
#define INVALID_POINT (POINT_LIMIT+1)
#define INVALID_FACE (FACE_LIMIT+1)
#define INVALID_OBJECT (OBJECT_LIMIT+1)
#define INVALID_TEXTURE (TEXTURE_LIMIT+1)
#define CAMERA_ROTATION_SPEED 0.04f
#define CAMERA_TRANSLATION_SPEED 0.04f
#define SELECTION_SPHERE_RADIUS 32
/* should be an odd number */
#define SNAP_LINES_SHOW 7
#define SNAP_LINES_WIDTH 1.0f
#define SNAP_LINES_COLOR ((Color){200,200,200,150})
typedef struct Operation {
enum {
OPERATION_MOVE_POINT,
OPERATION_SET_TEXTURE,
OPERATION_TRIANGULATE,
} kind;
union {
struct {
uint16_t point;
int16_t delta_x;
int16_t delta_y;
int16_t delta_z;
uint8_t object;
} move_point;
struct {
uint16_t face;
int16_t delta_texture;
uint8_t object;
} set_texture;
struct {
uint16_t old_face;
uint16_t new_face;
uint8_t object;
} triangulate;
} data;
bool chained;
} Operation;
typedef struct Point {
int16_t x, y, z;
} Point;
/* TODO: store topology in terms on edge connections? might be bad, as it's stateful */
/* triangles have p3 = INVALID_POINT */
/* lines have p2, p3 = INVALID_POINT */
/* billboards have p1, p2, p3 = INVALID_POINT */
typedef struct Face {
uint16_t p[4];
/* texture origin, as point on face plane, in absolute coordinates */
int16_t tex_x, tex_y;
uint8_t texture;
uint8_t tex_scale;
} Face;
typedef struct Object {
char *name;
bool is_invisible;
Point position;
char *textures[TEXTURE_LIMIT + 1];
uint8_t textures_sz;
Point rotation;
Face faces[FACE_LIMIT];
uint16_t faces_sz;
} Object;
typedef struct State {
Operation op_stack[UNDO_STACK_SIZE];
uint32_t op_stack_ptr;
bool op_active;
Vec3 active_center;
Vec3 camera_position;
Vec3 camera_direction;
float camera_zoom;
bool camera_is_orthographic;
/* defaults to wireframe */
bool solid_display_mode;
/* positions skipped */
uint8_t grid_snap_granularity;
Point points[POINT_LIMIT];
uint16_t points_sz;
Object objects[OBJECT_LIMIT];
uint8_t objects_sz;
/* which axes are blocked and which are not */
/* order: x, y, z */
bool axis_mask[3];
char *current_texture;
uint8_t current_hovered_obj;
uint16_t current_hovered_face;
} State;
#endif

972
apps/tools/twndel/tool.c Normal file
View File

@ -0,0 +1,972 @@
#include "twn_game_api.h"
#include "state.h"
#include "twn_vec.h"
#include <SDL.h>
/* planned features: */
/* grid-based, bounded space (65536 / POINTS_PER_METER meters), which allows for efficient storage */
/* 65534 point limit, 16 object limit, 2048 faces per object, 32 textures per object */
/* triangles and quads only */
/* support for billboards and flat two sided quads */
/* texture painting */
/* bones with mesh animations, snapping to grid, with no weights */
/* billboard render to specified angles */
/* 1 point light primitive lighting */
/* live edited textures are capped at 128x128 */
/* assumptions: */
/* up is always (0,1,0) */
/* preallocations everywhere */
static State state;
static bool init;
static uint8_t new_object(const char *name) {
if (state.objects_sz >= OBJECT_LIMIT)
return INVALID_OBJECT;
state.objects_sz++;
state.objects[state.objects_sz-1].name = SDL_strdup(name);
return state.objects_sz-1;
}
static uint16_t new_point(int16_t x, int16_t y, int16_t z) {
if (state.points_sz >= POINT_LIMIT)
return INVALID_POINT;
state.points_sz++;
state.points[state.points_sz-1] = (Point){x, y, z};
return state.points_sz-1;
}
static uint16_t push_face(uint8_t object,
uint16_t p0, uint16_t p1, uint16_t p2, uint16_t p3,
uint8_t texture, uint8_t tex_scale, int16_t tex_x, int16_t tex_y)
{
Object *o = &state.objects[object];
o->faces_sz++;
o->faces[o->faces_sz-1] = (Face) {
.p = {p0, p1, p2, p3},
.texture = texture,
.tex_scale = tex_scale,
.tex_x = tex_x,
.tex_y = tex_y,
};
return o->faces_sz-1;
}
static uint8_t push_texture(uint8_t object, char *texture) {
Object *o = &state.objects[object];
/* check whether it's already here */
for (uint8_t i = 0; i < o->textures_sz; ++i)
if (SDL_strcmp(o->textures[i], texture) == 0)
return i;
o->textures_sz++;
o->textures[o->textures_sz-1] = SDL_strdup(texture);
return o->textures_sz-1;
}
/* TODO: use tombstones instead? it would be easier to maintain, by a lot */
/* note: make sure nothing depends on none */
static void pop_face(uint8_t object, uint16_t face) {
Object *o = &state.objects[object];
if (face != o->faces_sz-1)
o->faces[face] = o->faces[o->faces_sz-1];
o->faces_sz--;
}
static void push_operation(Operation operation, bool active) {
state.op_stack_ptr++;
uint8_t op = state.op_stack_ptr % UNDO_STACK_SIZE;
state.op_stack[op] = operation;
state.op_active = active;
}
static void extend_operation(Operation operation) {
uint8_t op = state.op_stack_ptr % UNDO_STACK_SIZE;
Operation ext = state.op_stack[op];
ext.chained = true;
state.op_stack[op] = operation;
push_operation(ext, state.op_active);
}
static inline Vec3 point_to_vec3(uint8_t object, uint16_t point) {
Object *o = &state.objects[object];
return (Vec3){ (o->position.x + state.points[point].x) / (float)POINTS_PER_METER,
(o->position.y + state.points[point].y) / (float)POINTS_PER_METER,
(o->position.z + state.points[point].z) / (float)POINTS_PER_METER };
}
/* TODO: something is wrong, figure out how to introduce rotation to this. */
static inline Vec2 project_texture_coordinate(Vec2 origin, Vec3 plane, Vec3 point, float scale) {
Vec3 right = vec3_norm(vec3_cross(plane, fabsf(0.0f - plane.y) < 1e-9f ? (Vec3){0,1,0} : (Vec3){1,0,0}));
Vec3 up = vec3_norm(vec3_cross(right, plane));
Vec2 pp = { vec3_dot(point, right), vec3_dot(point, up) };
return vec2_scale(vec2_sub(origin, pp), scale);
}
static void render_object(uint8_t object) {
Object *o = &state.objects[object];
if (o->is_invisible)
return;
for (uint16_t fi = 0; fi < o->faces_sz; ++fi) {
Face *f = &o->faces[fi];
if (f->p[2] != INVALID_POINT) {
Vec3 p0 = point_to_vec3(object, f->p[0]);
Vec3 p1 = point_to_vec3(object, f->p[1]);
Vec3 p2 = point_to_vec3(object, f->p[2]);
if (state.solid_display_mode) {
Vec3 n = vec3_norm(vec3_cross(vec3_sub(p1, p0), vec3_sub(p2, p0)));
Vec2 to = { f->tex_x / (float)POINTS_PER_METER, f->tex_y / (float)POINTS_PER_METER, };
Vec2 tul = project_texture_coordinate(to, n, p0, (float)f->tex_scale);
Vec2 tdl = project_texture_coordinate(to, n, p1, (float)f->tex_scale);
Vec2 tdr = project_texture_coordinate(to, n, p2, (float)f->tex_scale);
draw_triangle(o->textures[f->texture],
p0, p1, p2,
tul, tdl, tdr,
(Color){255,255,255,255},
(Color){255,255,255,255},
(Color){255,255,255,255});
if (f->p[3] != INVALID_POINT) {
Vec3 p3 = point_to_vec3(object, f->p[3]);
Vec2 tur = project_texture_coordinate(to, n, p3, (float)f->tex_scale);
draw_triangle(o->textures[f->texture],
p2, p3, p0,
tdr, tur, tul,
(Color){255,255,255,255},
(Color){255,255,255,255},
(Color){255,255,255,255});
}
} else {
draw_line_3d(p0, p1, 1, (Color){255,255,255,255});
draw_line_3d(p1, p2, 1, (Color){255,255,255,255});
if (f->p[3] == INVALID_POINT)
draw_line_3d(p2, p0, 1, (Color){255,255,255,255});
else {
Vec3 p3 = point_to_vec3(object, f->p[3]);
draw_line_3d(p2, p3, 1, (Color){255,255,255,255});
draw_line_3d(p3, p0, 1, (Color){255,255,255,255});
}
}
} else
SDL_assert_always(false);
}
}
static uint8_t new_cube(Point pos, Point size) {
uint8_t object = new_object("cube");
uint16_t p0 = new_point(pos.x - size.x / 2, pos.y - size.y / 2, pos.z - size.z / 2);
uint16_t p1 = new_point(pos.x - size.x / 2, pos.y + size.y / 2, pos.z - size.z / 2);
uint16_t p2 = new_point(pos.x + size.x / 2, pos.y + size.y / 2, pos.z - size.z / 2);
uint16_t p3 = new_point(pos.x + size.x / 2, pos.y - size.y / 2, pos.z - size.z / 2);
uint16_t p4 = new_point(pos.x - size.x / 2, pos.y - size.y / 2, pos.z + size.z / 2);
uint16_t p5 = new_point(pos.x - size.x / 2, pos.y + size.y / 2, pos.z + size.z / 2);
uint16_t p6 = new_point(pos.x + size.x / 2, pos.y + size.y / 2, pos.z + size.z / 2);
uint16_t p7 = new_point(pos.x + size.x / 2, pos.y - size.y / 2, pos.z + size.z / 2);
uint8_t tex = push_texture(object, "/data/placeholder.png");
push_face(object, p2, p3, p0, p1, tex, 128, pos.x - size.x / 2, pos.y - size.y / 2);
push_face(object, p5, p4, p7, p6, tex, 128, pos.x - size.x / 2, pos.y - size.y / 2);
push_face(object, p1, p0, p4, p5, tex, 128, pos.x - size.x / 2, pos.y - size.y / 2);
push_face(object, p6, p7, p3, p2, tex, 128, pos.x - size.x / 2, pos.y - size.y / 2);
push_face(object, p2, p1, p5, p6, tex, 128, pos.x - size.x / 2, pos.y - size.y / 2);
push_face(object, p0, p3, p7, p4, tex, 128, pos.x - size.x / 2, pos.y - size.y / 2);
return object;
}
static void process_camera_rotation(void) {
float horizontal_rotation = 0;
float vertical_rotation = 0;
if (input_action_pressed("camera_rotate_left"))
horizontal_rotation -= CAMERA_ROTATION_SPEED;
if (input_action_pressed("camera_rotate_right"))
horizontal_rotation += CAMERA_ROTATION_SPEED;
if (input_action_pressed("camera_rotate_up"))
vertical_rotation -= CAMERA_ROTATION_SPEED;
if (input_action_pressed("camera_rotate_down"))
vertical_rotation += CAMERA_ROTATION_SPEED;
Vec3 front = vec3_cross(state.camera_direction, (Vec3){0,1,0});
Vec3 local_position = vec3_sub(state.active_center, state.camera_position);
Vec3 new_local_position = vec3_rotate(local_position, horizontal_rotation, (Vec3){0,1,0});
state.camera_direction = vec3_rotate(state.camera_direction, horizontal_rotation, (Vec3){0,1,0});
Vec3 new_rot = vec3_rotate(state.camera_direction, vertical_rotation, front);
/* only apply if it's in limits */
float d = vec3_dot(new_rot, (Vec3){0,-1,0});
if (fabsf(d) <= 0.999f) {
new_local_position = vec3_rotate(new_local_position, vertical_rotation, front);
state.camera_direction = new_rot;
}
state.camera_position = vec3_sub(state.active_center, new_local_position);
}
static void process_camera_translation(void) {
Vec3 right = vec3_norm(vec3_cross(state.camera_direction, (Vec3){0,1,0}));
Vec3 up = vec3_norm(vec3_cross(state.camera_direction, right));
Vec3 was = state.camera_position;
if (input_action_pressed("camera_rotate_left"))
state.camera_position = vec3_sub(state.camera_position, vec3_scale(right, CAMERA_TRANSLATION_SPEED));
if (input_action_pressed("camera_rotate_right"))
state.camera_position = vec3_add(state.camera_position, vec3_scale(right, CAMERA_TRANSLATION_SPEED));
if (input_action_pressed("camera_rotate_up"))
state.camera_position = vec3_sub(state.camera_position, vec3_scale(up, CAMERA_TRANSLATION_SPEED));
if (input_action_pressed("camera_rotate_down"))
state.camera_position = vec3_add(state.camera_position, vec3_scale(up, CAMERA_TRANSLATION_SPEED));
state.active_center = vec3_add(state.active_center, vec3_sub(state.camera_position, was));
draw_billboard("/data/camera.png",
vec3_add(state.camera_position, vec3_scale(state.camera_direction, vec3_length(state.camera_position))),
(Vec2){0.2f,0.2f},
(Rect){0},
(Color){255,255,255,255},
false);
/* show relation to origin */
draw_billboard("/data/center.png",
(Vec3){0},
(Vec2){0.1f,0.1f},
(Rect){0},
(Color){255,255,255,255},
false);
draw_line_3d((Vec3){0}, state.active_center, 1, (Color){255,255,255,255});
}
static void process_camera_movement(void) {
input_action("camera_rotate_left", "A");
input_action("camera_rotate_right", "D");
input_action("camera_rotate_up", "W");
input_action("camera_rotate_down", "S");
input_action("camera_lock_rotation", "SPACE");
if (input_action_pressed("camera_lock_rotation")) {
process_camera_translation();
} else {
process_camera_rotation();
}
}
static inline DrawCameraUnprojectResult unproject_point(Vec2 point) {
return draw_camera_unproject(
point,
state.camera_position,
state.camera_direction,
(Vec3){0, 1, 0},
state.camera_is_orthographic ? 0 : CAMERA_FOV,
state.camera_zoom,
100 );
}
static bool find_closest_point(uint8_t* object_result, uint16_t *point_result) {
DrawCameraUnprojectResult pos_and_ray = unproject_point(ctx.mouse_position);
/* step over every selectable object and find points closest to the view ray */
/* by constructing triangles and finding their height, from perpendicular */
uint16_t closest_point = INVALID_POINT;
uint8_t closest_obj = INVALID_OBJECT;
float closest_distance = INFINITY;
for (uint8_t obj = 0; obj < state.objects_sz; ++obj) {
Object *o = &state.objects[obj];
if (o->is_invisible)
continue;
/* TODO: is it possible to skip repeated points? does it matter? */
/* as we limit the point could we could actually have bool array preallocated for this */
for (uint16_t fi = 0; fi < o->faces_sz; ++fi) {
Face *f = &o->faces[fi];
for (uint16_t pi = 0; pi < 4; ++pi) {
if (f->p[pi] == INVALID_POINT) break;
Vec3 p = point_to_vec3(obj, f->p[pi]);
Vec3 d = vec3_sub(pos_and_ray.position, p);
Vec3 b = vec3_cross(d, pos_and_ray.direction);
float ray_dist = vec3_length(b);
if (ray_dist > ((float)SELECTION_SPHERE_RADIUS / POINTS_PER_METER))
continue;
float dist = vec3_length(vec3_sub(pos_and_ray.position, vec3_add(p, b)));
if (dist < closest_distance) {
closest_distance = dist;
closest_obj = obj;
closest_point = f->p[pi];
}
}
}
}
if (closest_point == INVALID_POINT)
return false;
if (object_result)
*object_result = closest_obj;
if (point_result)
*point_result = closest_point;
return true;
}
/* o = vector origin */
/* v = vector direction, normalized */
/* p = any point on plane */
/* n = normal of a plane */
static bool vector_plane_intersection(Vec3 o, Vec3 v, Vec3 p, Vec3 n, Vec3 *out) {
float dot = vec3_dot(n, v);
if (fabsf(dot) > FLT_EPSILON) {
Vec3 w = vec3_sub(o, p);
float fac = -vec3_dot(n, w) / dot;
*out = vec3_add(o, vec3_scale(v, fac));
return true;
}
/* vector and plane are perpendicular, assume that it lies exactly on it */
return false;
}
static bool find_closest_face(uint8_t* object_result, uint16_t *face_result) {
DrawCameraUnprojectResult cam = unproject_point(ctx.mouse_position);
uint8_t closest_obj = INVALID_OBJECT;
uint16_t closest_face = INVALID_FACE;
float closest_distance = INFINITY;
for (uint8_t oi = 0; oi < state.objects_sz; ++oi) {
Object *o = &state.objects[oi];
for (uint16_t fi = 0; fi < o->faces_sz; ++fi) {
Face *f = &o->faces[fi];
if (f->p[1] == INVALID_POINT) continue;
Vec3 p0 = point_to_vec3(oi, f->p[0]);
Vec3 p1 = point_to_vec3(oi, f->p[1]);
Vec3 p2 = point_to_vec3(oi, f->p[2]);
Vec3 n = vec3_norm(vec3_cross(vec3_sub(p1, p0), vec3_sub(p2, p0)));
/* culling */
if (vec3_dot(state.camera_direction, n) >= 0) continue;
Vec3 i;
if (!vector_plane_intersection(cam.position, cam.direction, p0, n, &i))
continue;
float dist = vec3_length(vec3_sub(p0, i));
if (dist >= closest_distance) continue;
/* left normals are used to determine whether point lies to the left for all forming lines */
Vec3 ln0 = vec3_norm(vec3_cross(n, vec3_sub(p1, p0)));
if (vec3_dot(ln0, vec3_sub(p0, i)) >= 0) continue;
Vec3 ln1 = vec3_norm(vec3_cross(n, vec3_sub(p2, p1)));
if (vec3_dot(ln1, vec3_sub(p1, i)) >= 0) continue;
if (f->p[3] == INVALID_POINT) {
/* triangle */
Vec3 ln2 = vec3_norm(vec3_cross(n, vec3_sub(p0, p2)));
if (vec3_dot(ln2, vec3_sub(p2, i)) >= 0) continue;
} else {
/* quad */
Vec3 p3 = point_to_vec3(oi, f->p[3]);
Vec3 ln2 = vec3_norm(vec3_cross(n, vec3_sub(p3, p2)));
if (vec3_dot(ln2, vec3_sub(p2, i)) >= 0) continue;
Vec3 ln3 = vec3_norm(vec3_cross(n, vec3_sub(p0, p3)));
if (vec3_dot(ln3, vec3_sub(p3, i)) >= 0) continue;
}
closest_distance = dist;
closest_face = fi;
closest_obj = oi;
}
}
if (closest_face == INVALID_FACE)
return false;
if (object_result)
*object_result = closest_obj;
if (face_result)
*face_result = closest_face;
return true;
}
static void show_snap_lines(Vec3 p) {
float step = 1.0f / ((float)POINTS_PER_METER / state.grid_snap_granularity);
int const lines_per_side = (SNAP_LINES_SHOW - 1) / 2;
for (int l = -lines_per_side; l <= lines_per_side; ++l) {
if (!state.axis_mask[0]) {
Vec3 c = vec3_add(p, vec3_scale((Vec3){1,0,0}, step * (float)l));
draw_line_3d(
vec3_add(c, vec3_scale((Vec3){0,0,1}, SNAP_LINES_WIDTH)),
vec3_add(c, vec3_scale((Vec3){0,0,1}, -SNAP_LINES_WIDTH)),
1,
SNAP_LINES_COLOR);
}
if (!state.axis_mask[1]) {
Vec3 axis = fabsf(vec3_dot(state.camera_direction, (Vec3){0,0,1})) >= 0.5f ? (Vec3){1,0,0} : (Vec3){0,0,1};
Vec3 c = vec3_add(p, vec3_scale((Vec3){0,1,0}, step * (float)l));
draw_line_3d(
vec3_add(c, vec3_scale(axis, SNAP_LINES_WIDTH)),
vec3_add(c, vec3_scale(axis, -SNAP_LINES_WIDTH)),
1,
SNAP_LINES_COLOR);
}
if (!state.axis_mask[2]) {
Vec3 c = vec3_add(p, vec3_scale((Vec3){0,0,1}, step * (float)l));
draw_line_3d(
vec3_add(c, vec3_scale((Vec3){1,0,0}, SNAP_LINES_WIDTH)),
vec3_add(c, vec3_scale((Vec3){1,0,0}, -SNAP_LINES_WIDTH)),
1,
SNAP_LINES_COLOR);
}
}
}
static bool process_operation_move_point(Operation *op) {
/* finish dragging around */
/* TODO: dont keep empty ops on stack? */
if (input_action_just_released("select")) {
state.op_active = false;
return false;
}
float size = sinf(ctx.frame_number / 10) * 0.05f + 0.05f;
draw_billboard("/data/grab.png",
point_to_vec3(op->data.move_point.object, op->data.move_point.point),
(Vec2){size, size},
(Rect){0},
(Color){255,255,255,255},
false);
DrawCameraUnprojectResult cam = unproject_point(ctx.mouse_position);
Vec3 p = point_to_vec3(op->data.move_point.object, op->data.move_point.point);
bool point_moved = false;
/* figure out which planes are angled acutely against the viewing direction */
bool y_closer_than_horizon = fabsf(vec3_dot(state.camera_direction, (Vec3){0,1,0})) >= 0.5f;
Vec3 plane_normal_x = y_closer_than_horizon ? (Vec3){0,1,0} : (Vec3){0,0,1};
Vec3 plane_normal_z = y_closer_than_horizon ? (Vec3){0,1,0} : (Vec3){1,0,0};
/* show snapping in lines */
show_snap_lines(p);
Vec3 s;
if (!state.axis_mask[0]) {
if (vector_plane_intersection(cam.position, cam.direction, p, plane_normal_x, &s)) {
int16_t xch = (int16_t)(roundf(vec3_dot(vec3_sub(s, p), (Vec3){1,0,0}) * (float)POINTS_PER_METER));
xch -= xch % state.grid_snap_granularity;
state.points[op->data.move_point.point].x += xch;
op->data.move_point.delta_x += xch;
if (xch != 0) point_moved = true;
}
}
if (!state.axis_mask[1]) {
if (vector_plane_intersection(cam.position, cam.direction, p, (Vec3){0,0,1}, &s)) {
int16_t ych = (int16_t)(roundf(vec3_dot(vec3_sub(s, p), (Vec3){0,1,0}) * (float)POINTS_PER_METER));
ych -= ych % state.grid_snap_granularity;
state.points[op->data.move_point.point].y += ych;
op->data.move_point.delta_y += ych;
if (ych != 0) point_moved = true;
}
}
if (!state.axis_mask[2]) {
if (vector_plane_intersection(cam.position, cam.direction, p, plane_normal_z, &s)) {
int16_t zch = (int16_t)(roundf(vec3_dot(vec3_sub(s, p), (Vec3){0,0,1}) * (float)POINTS_PER_METER));
zch -= zch % state.grid_snap_granularity;
state.points[op->data.move_point.point].z += zch;
op->data.move_point.delta_z += zch;
if (zch != 0) point_moved = true;
}
}
if (point_moved) {
audio_play("/data/bong.ogg", NULL, false, 0.12f, 0.0f);
return true;
}
return false;
}
static void reverse_operation_move_point(Operation *op) {
SDL_assert(op->kind == OPERATION_MOVE_POINT);
state.points[op->data.move_point.point].x -= op->data.move_point.delta_x;
state.points[op->data.move_point.point].y -= op->data.move_point.delta_y;
state.points[op->data.move_point.point].z -= op->data.move_point.delta_z;
audio_play("/data/drop.ogg", NULL, false, 0.4f, 0.0f);
}
static void reverse_operation_set_texture(Operation *op) {
SDL_assert(op->kind == OPERATION_SET_TEXTURE);
Object *o = &state.objects[op->data.set_texture.object];
o->faces[op->data.set_texture.face].texture += op->data.set_texture.delta_texture;
audio_play("/data/drop.ogg", NULL, false, 0.4f, 0.0f);
}
static void reverse_triangulation(Operation *op) {
SDL_assert(op->kind == OPERATION_TRIANGULATE);
Object *o = &state.objects[op->data.set_texture.object];
Face *fn = &o->faces[op->data.triangulate.new_face];
Face *fo = &o->faces[op->data.triangulate.old_face];
fo->p[3] = fo->p[2];
fo->p[2] = fn->p[1];
pop_face(op->data.set_texture.object, op->data.triangulate.new_face);
}
/* TODO: reverse of this */
static void try_subdividing_from_moving(uint8_t object, uint16_t point) {
Object *o = &state.objects[object];
bool not_first = false;
for (uint16_t fi = 0; fi < o->faces_sz; ++fi) {
Face *f = &o->faces[fi];
if (f->p[3] == INVALID_POINT) continue;
for (uint16_t pi = 0; pi < 4; ++pi) {
if (f->p[pi] == point) {
Face new0 = *f;
new0.p[0] = f->p[pi];
new0.p[1] = f->p[(pi + 1) % 4];
new0.p[2] = f->p[(pi + 3) % 4];
new0.p[3] = INVALID_POINT;
uint16_t newf = push_face(
object,
f->p[(pi + 1) % 4],
f->p[(pi + 2) % 4],
f->p[(pi + 3) % 4],
INVALID_POINT,
f->texture,
f->tex_scale,
f->tex_x,
f->tex_y);
*f = new0;
extend_operation((Operation){
.kind = OPERATION_TRIANGULATE,
.data = { .triangulate = { .new_face = newf, .old_face = fi, .object = object} },
.chained = not_first,
});
not_first = true;
}
}
}
}
static void process_undo(void) {
if (state.op_active)
state.op_active = false;
/* TODO: checks and defined limit */
Operation *op = &state.op_stack[state.op_stack_ptr % UNDO_STACK_SIZE];
state.op_stack_ptr--;
switch (op->kind) {
case OPERATION_MOVE_POINT: {
reverse_operation_move_point(op);
break;
}
case OPERATION_SET_TEXTURE: {
reverse_operation_set_texture(op);
break;
}
case OPERATION_TRIANGULATE: {
reverse_triangulation(op);
break;
}
default:
(void)0;
}
/* pop another if they're chained together */
if (op->chained) process_undo();
}
static void process_operations(void) {
if (input_action_just_pressed("undo"))
process_undo();
if (!state.op_active) {
/* point dragging */
if (!state.current_texture) {
uint16_t point_select; uint8_t obj_select;
if (find_closest_point(&obj_select, &point_select)) {
draw_billboard("/data/point.png",
point_to_vec3(obj_select, point_select),
(Vec2){0.05f, 0.05f},
(Rect){0},
(Color){255,255,255,255},
false);
if (input_action_just_pressed("select"))
push_operation((Operation){
.kind = OPERATION_MOVE_POINT,
.data = { .move_point = { .point = point_select, .object = obj_select } },
}, true );
}
/* texture setting */
} else {
uint8_t obj_select; uint16_t face_select;
if (find_closest_face(&obj_select, &face_select)) {
state.current_hovered_face = face_select;
state.current_hovered_obj = obj_select;
if (input_action_just_pressed("rotate")) {
Face *f = &state.objects[obj_select].faces[face_select];
int16_t tex_x = f->tex_x;
int16_t tex_y = f->tex_y;
f->tex_x = -tex_y;
f->tex_y = tex_x;
}
if (input_action_pressed("select") && state.current_texture) {
uint8_t new_tex = push_texture(obj_select, state.current_texture);
uint8_t cur_tex = state.objects[obj_select].faces[face_select].texture;
if (new_tex != cur_tex) {
state.objects[obj_select].faces[face_select].texture = new_tex;
audio_play("/data/bong.ogg", NULL, false, 0.5f, 0.0f);
push_operation((Operation){
.kind = OPERATION_SET_TEXTURE,
.data = { .set_texture = {
.face = face_select,
.object = obj_select,
.delta_texture = cur_tex - new_tex,
}},
}, false );
}
}
}
}
}
if (state.op_active) {
Operation *op = &state.op_stack[state.op_stack_ptr % UNDO_STACK_SIZE];
switch (op->kind) {
case OPERATION_MOVE_POINT: {
bool update = process_operation_move_point(op);
if (update)
try_subdividing_from_moving(op->data.move_point.object, op->data.move_point.point);
break;
}
case OPERATION_SET_TEXTURE:
case OPERATION_TRIANGULATE:
default:
(void)0;
}
}
}
static void draw_axes(void) {
/* axis helpers */
/* idea: double selection of axes for diagonal edits */
draw_line_3d(state.active_center, (Vec3){256,0,0}, 1, state.axis_mask[0] ? (Color){0,0,0,75} : (Color){255,0,0,125});
draw_billboard("/data/x.png",
vec3_add(state.active_center, (Vec3){2,0,0}),
(Vec2){0.1f, 0.1f},
(Rect){0},
state.axis_mask[0] ? (Color){0,0,0,255} : (Color){255,0,0,255},
false);
draw_line_3d(state.active_center, (Vec3){0,256,0}, 1, state.axis_mask[1] ? (Color){0,0,0,75} : (Color){75,125,25,125});
draw_billboard("/data/y.png",
vec3_add(state.active_center, (Vec3){0,1.5f,0}),
(Vec2){0.1f, 0.1f},
(Rect){0},
state.axis_mask[1] ? (Color){0,0,0,255} : (Color){75,125,25,255},
false);
draw_line_3d(state.active_center, (Vec3){0,0,256}, 1, state.axis_mask[2] ? (Color){0,0,0,75} : (Color){0,0,255,125});
draw_billboard("/data/z.png",
vec3_add(state.active_center, (Vec3){0,0,2}),
(Vec2){0.1f, 0.1f},
(Rect){0},
state.axis_mask[2] ? (Color){0,0,0,255} : (Color){0,0,255,255},
false);
}
static void draw_hovered_face_border(void) {
if (state.current_hovered_obj == INVALID_OBJECT || state.current_hovered_face == INVALID_FACE)
return;
Object *o = &state.objects[state.current_hovered_obj];
Face *f = &o->faces[state.current_hovered_face];
if (f->p[3] != INVALID_POINT) {
Vec3 p0 = point_to_vec3(state.current_hovered_obj, f->p[0]);
Vec3 p1 = point_to_vec3(state.current_hovered_obj, f->p[1]);
Vec3 p2 = point_to_vec3(state.current_hovered_obj, f->p[2]);
Vec3 p3 = point_to_vec3(state.current_hovered_obj, f->p[3]);
Vec3 center = vec3_scale(vec3_add(p2, p0), 0.5);
float size = sinf(ctx.frame_number / 10) * 0.05f + 0.1f;
Vec3 c0 = vec3_add(p0, vec3_scale(vec3_sub(p0, center), size));
Vec3 c1 = vec3_add(p1, vec3_scale(vec3_sub(p1, center), size));
Vec3 c2 = vec3_add(p2, vec3_scale(vec3_sub(p2, center), size));
Vec3 c3 = vec3_add(p3, vec3_scale(vec3_sub(p3, center), size));
draw_line_3d(c0, c1, 1, (Color){255,255,255,255});
draw_line_3d(c1, c2, 1, (Color){255,255,255,255});
draw_line_3d(c2, c3, 1, (Color){255,255,255,255});
draw_line_3d(c3, c0, 1, (Color){255,255,255,255});
} else {
Vec3 p0 = point_to_vec3(state.current_hovered_obj, f->p[0]);
Vec3 p1 = point_to_vec3(state.current_hovered_obj, f->p[1]);
Vec3 p2 = point_to_vec3(state.current_hovered_obj, f->p[2]);
Vec3 center = vec3_scale(vec3_add(p0, vec3_add(p1, p2)), 0.33f);
float size = sinf(ctx.frame_number / 10) * 0.05f + 0.1f;
Vec3 c0 = vec3_add(p0, vec3_scale(vec3_sub(p0, center), size));
Vec3 c1 = vec3_add(p1, vec3_scale(vec3_sub(p1, center), size));
Vec3 c2 = vec3_add(p2, vec3_scale(vec3_sub(p2, center), size));
draw_line_3d(c0, c1, 1, (Color){255,255,255,255});
draw_line_3d(c1, c2, 1, (Color){255,255,255,255});
draw_line_3d(c2, c0, 1, (Color){255,255,255,255});
}
}
static void display_texture_selection(void) {
String list = file_read("/data/assets/", ":images");
if (!list.data)
return;
draw_rectangle((Rect){0, 480 - 50, 640, 480}, (Color){0,0,0,126});
char *selected = NULL;
char *saveptr = NULL;
int count = 0;
char const *part = SDL_strtokr(list.data, "\n", &saveptr);
do {
Rect box = (Rect){(float)count * 50, 480 - 48, 48, 48};
draw_sprite(part,
box,
(Rect){0,0,64,64},
(Color){255,255,255,255},
0, false, false, true);
count++;
if (state.current_texture && SDL_strcmp(part, state.current_texture) == 0)
draw_box(box, 1, (Color){255,255,255,255});
if (rect_intersects(box, (Rect){ctx.mouse_position.x, ctx.mouse_position.y, 1, 1}))
selected = SDL_strdup(part);
} while ((part = SDL_strtokr(NULL, "\n", &saveptr)));
if (selected) {
draw_text(selected, (Vec2){0, 480 - 48 - 24}, 24, (Color){255,255,255,255}, NULL);
if (input_action_just_pressed("select")) {
if (state.current_texture) SDL_free(state.current_texture);
state.current_texture = selected;
} else
SDL_free(selected);
}
}
static void process_camera_inputs(void) {
if (input_action_just_pressed("toggle_display_mode")) {
audio_play("/data/click.wav", NULL, false, 0.7f, 0.0f);
state.solid_display_mode = !state.solid_display_mode;
if (state.current_texture) {
SDL_free(state.current_texture);
state.current_texture = NULL;
}
}
if (input_action_just_pressed("toggle_projection")) {
audio_play("/data/pop.wav", NULL, false, 0.8f, 0.0f);
state.camera_is_orthographic = !state.camera_is_orthographic;
}
if (input_action_just_pressed("toggle_x_axis")) {
state.axis_mask[0] = 0; state.axis_mask[1] = 1; state.axis_mask[2] = 1;
}
if (input_action_just_pressed("toggle_y_axis")) {
state.axis_mask[0] = 1; state.axis_mask[1] = 0; state.axis_mask[2] = 1;
}
if (input_action_just_pressed("toggle_z_axis")) {
state.axis_mask[0] = 1; state.axis_mask[1] = 1; state.axis_mask[2] = 0;
}
/* TODO: determine bounding box for this? */
/* TODO: no idea whether it's all correct in terms of directions. */
if (input_action_just_pressed("camera_front")) {
audio_play("/data/pop.wav", NULL, false, 0.8f, 0.0f);
state.camera_is_orthographic = true;
state.camera_direction = (Vec3){0,0,-1};
state.camera_position = vec3_add(state.active_center, (Vec3){0,0,2});
}
if (input_action_just_pressed("camera_back")) {
audio_play("/data/pop.wav", NULL, false, 0.8f, 0.0f);
state.camera_is_orthographic = true;
state.camera_direction = (Vec3){0,0,1};
state.camera_position = vec3_add(state.active_center, (Vec3){0,0,-2});
}
if (input_action_just_pressed("camera_left")) {
audio_play("/data/pop.wav", NULL, false, 0.8f, 0.0f);
state.camera_is_orthographic = true;
state.camera_direction = (Vec3){1,0,0};
state.camera_position = vec3_add(state.active_center, (Vec3){-2,0,0});
}
if (input_action_just_pressed("camera_right")) {
audio_play("/data/pop.wav", NULL, false, 0.8f, 0.0f);
state.camera_is_orthographic = true;
state.camera_direction = (Vec3){-1,0,0};
state.camera_position = vec3_add(state.active_center, (Vec3){2,0,0});
}
/* TODO: broken */
if (input_action_just_pressed("camera_above")) {
audio_play("/data/pop.wav", NULL, false, 0.8f, 0.0f);
state.camera_is_orthographic = true;
state.camera_direction = (Vec3){0,-1,0};
state.camera_position = vec3_add(state.active_center, (Vec3){0,2,0});
}
/* TODO: broken */
if (input_action_just_pressed("camera_below")) {
audio_play("/data/pop.wav", NULL, false, 0.8f, 0.0f);
state.camera_is_orthographic = true;
state.camera_direction = (Vec3){0,1,0};
state.camera_position = vec3_add(state.active_center, (Vec3){0,-2,0});
}
}
void game_tick(void) {
if (!init) {
/* default state */
new_cube((Point){0,0,0}, (Point){POINTS_PER_METER,POINTS_PER_METER,POINTS_PER_METER});
state.camera_position = (Vec3){2,1,2};
state.camera_direction = vec3_norm(((Vec3){-2,-1,-2}));
state.camera_zoom = 0.5f;
state.grid_snap_granularity = 16;
state.axis_mask[1] = 1; state.axis_mask[2] = 1;
init = true;
}
state.current_hovered_face = INVALID_FACE;
state.current_hovered_obj = INVALID_OBJECT;
input_action("toggle_display_mode", "Q");
input_action("toggle_projection", "TAB");
input_action("toggle_x_axis", "Z");
input_action("toggle_y_axis", "X");
input_action("toggle_z_axis", "C");
input_action("select", "LCLICK");
input_action("rotate", "R");
input_action("undo", "F");
input_action("camera_front", "KP0");
input_action("camera_back", "KP1");
input_action("camera_left", "KP2");
input_action("camera_right", "KP3");
input_action("camera_above", "KP4");
input_action("camera_below", "KP5");
process_camera_inputs();
process_camera_movement();
process_operations();
/* helpres */
draw_axes();
draw_hovered_face_border();
for (uint8_t obj = 0; obj < state.objects_sz; ++obj)
render_object(obj);
if (state.solid_display_mode)
display_texture_selection();
draw_text("twndel\x03", (Vec2){0, 2}, 32, (Color){255,255,255,200}, NULL);
draw_camera(
state.camera_position,
state.camera_direction,
(Vec3){0, 1, 0},
state.camera_is_orthographic ? 0 : CAMERA_FOV,
state.camera_zoom,
100
);
}
void game_end(void) {
for (uint8_t obj = 0; obj < state.objects_sz; ++obj) {
Object *o = &state.objects[obj];
for (uint8_t t = 0; t < o->textures_sz; ++t)
SDL_free(o->textures[t]);
SDL_free(o->name);
}
if (state.current_texture) {
SDL_free(state.current_texture);
state.current_texture = NULL;
}
}

View File

@ -9,10 +9,6 @@ endif()
add_subdirectory($ENV{TWNROOT} ${CMAKE_BINARY_DIR}/twn)
set(FLAGS
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:--no-dynlib-game>
)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/game.c
COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/bindgen.py $ENV{TWNROOT}/share/twn_api.json ${FLAGS} > ${CMAKE_CURRENT_SOURCE_DIR}/luabind.c

View File

@ -112,6 +112,9 @@ for procedure, procedure_desc in api["procedures"].items():
binding += " lua_pushnumber(L, (float)(%s(%s)));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
elif procedure_desc["return"] == "char *":
binding += " lua_pushstring(L, %s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
elif procedure_desc["return"] == "String":
binding += " String result = %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
binding += " lua_pushlstring(L, result.data, (int)result.length);\n"
elif type(procedure_desc["return"]) is dict or procedure_desc["return"] in api["types"]:
type_desc = procedure_desc["return"] if type(procedure_desc["return"]) is dict else api["types"][procedure_desc["return"]]
binding += " %s result = %s(%s);\n" % (type_desc["c_type"], procedure, ", ".join(param["name"] for param in procedure_desc["params"]))

View File

@ -26,7 +26,7 @@ static int physfs_loader(lua_State *L) {
static const char *name_breaker = NULL;
if (name_breaker && SDL_strcmp(name, name_breaker) == 0) {
log_critical("Recursive load on itself from lua module (%s)", name_breaker);
log_string(name_breaker, "Recursive load on itself from lua module");
return 0;
} name_breaker = name;
@ -40,26 +40,26 @@ static int physfs_loader(lua_State *L) {
SDL_asprintf(&final_path, "/scripts/%s.lua", path_copy);
SDL_free(path_copy);
if (!file_exists(final_path)) {
if (SDL_strcmp(file_read(final_path, ":exists").data, "yes") == 0) {
char *error_message = NULL;
SDL_asprintf(&error_message, "could not find module %s in filesystem", name);
lua_pushstring(L, error_message);
free(error_message);
SDL_free(error_message);
free(final_path);
SDL_free(final_path);
return 1;
}
unsigned char *buf = NULL;
int64_t buf_size = file_to_bytes(final_path, &buf);
free(final_path);
char *final_path_binary = NULL;
SDL_asprintf(&final_path_binary, "%s%s", final_path, ":binary");
String file = file_read(final_path, ":binary");
SDL_free(final_path);
/* TODO: use reader interface for streaming instead */
int const result = luaL_loadbuffer(L, (char *)buf, buf_size, name);
free(buf);
int const result = luaL_loadbuffer(L, file.data, (size_t)file.length, name);
if (result != LUA_OK)
log_critical("%s", lua_tostring(L, -1));
log_string(lua_tostring(L, -1), NULL);
return result == LUA_OK;
}
@ -110,7 +110,7 @@ static void *custom_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
static void exchange_lua_states(lua_State *from, lua_State *to, int level, int index) {
if (level >= UDATA_NESTING_LIMIT) {
log_critical("ctx.udata nesting limit is reached (%u)", UDATA_NESTING_LIMIT);
log_string("ctx.udata nesting limit is reached", NULL);
return;
}
@ -143,7 +143,7 @@ static void exchange_lua_states(lua_State *from, lua_State *to, int level, int i
break;
default:
/* TODO: provide a path and type of it for better diagnostic */
log_warn("Unserializable udata found and is ignored");
log_string("Unserializable udata found and is ignored", NULL);
break;
}
}
@ -167,7 +167,6 @@ void game_tick(void) {
SDL_assert(!lua_isnoneornil(state->L, -1));
SDL_assert(!lua_isnoneornil(state->L, -2));
if (!lua_isnoneornil(state->L, -1)) {
log_info("Exchanging lua states...");
lua_newtable(new_state);
exchange_lua_states(state->L, new_state, 0, -1);
lua_setfield(new_state, -2, "udata");
@ -216,24 +215,21 @@ void game_tick(void) {
bindgen_load_twn(state->L);
/* now finally get to running the code */
unsigned char *game_buf = NULL;
size_t game_buf_size = file_to_bytes("/scripts/game.lua", &game_buf);
String file = file_read("/scripts/game.lua", ":binary");
/* TODO: use reader interface for streaming instead */
if (luaL_loadbuffer(state->L, (char *)game_buf, game_buf_size, "game.lua") == LUA_OK) {
if (luaL_loadbuffer(state->L, file.data, (size_t)file.length, "game.lua") == LUA_OK) {
if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) {
log_critical("%s", lua_tostring(state->L, -1));
log_string(luaL_tolstring(state->L, -1, NULL), "Error executing /scripts/game.lua entry");
lua_pop(state->L, 1);
} else
state->loaded_successfully = true;
} else {
/* got some sort of error, it should be pushed on top of the stack */
SDL_assert(lua_isstring(state->L, -1));
log_critical("Error loading /scripts/game.lua entry: %s", luaL_tolstring(state->L, -1, NULL));
log_string(luaL_tolstring(state->L, -1, NULL), "Error loading /scripts/game.lua entry");
lua_pop(state->L, 1);
}
SDL_free(game_buf);
/* from this point we have access to everything defined in lua */
}
@ -251,7 +247,7 @@ void game_tick(void) {
lua_getglobal(state->L, "game_tick");
if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) {
log_critical("%s", lua_tostring(state->L, -1));
log_string(luaL_tolstring(state->L, -1, NULL), "Error executing game_tick()");
lua_pop(state->L, 1);
}

View File

@ -1,6 +1,6 @@
#!/bin/env sh
# single header api generator with clang
set +e
set -e
clang -I./ -P -E -nostdinc -nobuiltininc -DTWN_NOT_C $TWNROOT/include/twn_game_api.h 2> /dev/null | clang-format

8
bin/prep-embed.sh Normal file
View File

@ -0,0 +1,8 @@
#!/bin/env sh
set -e
# packages embedded resources with objcopy, so that it is more portable
# ld.lld on windows doesn't recognize --format binary, sadly
objdump=$(objdump -i)
bdfname=$(echo "$objdump" | sed -n 2p)
objcopy -I binary -O "$bdfname" share/assets/Dernyns256.ttf "$CMAKE_CURRENT_BINARY_DIR/font.o"

View File

@ -1,7 +1,7 @@
#!/bin/env sh
# townengine tooling interface
set +e
set -e
exe="$(basename $PWD)"
toolpath="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"

View File

@ -9,7 +9,7 @@
<a>Awesomeness</a>
<table style="padding-top:1em">
<tr><td>T1.</strong> <a href="#about-townengine">About Townengnine</a></td>
<td>G1.</strong> <a href="#making-2dot5d-shooters">Making 2.5D Shooters</a></td>
<td>G1.</strong> <a href="#trigonometry">Trigonometry</a></td>
</tr>
<tr><td>T2.</strong> <a href="#input-system">Input System</a></td>
</tr>
@ -32,7 +32,7 @@
<blockquote style="margin-top:0">
<p style="margin:0">T3.1 <a href="packaging.html#overview">Overview</a></p>
</blockquote>
<p style="margin-bottom:0"><a name="making-2dot5d-shooters"></a>G1. </strong><a href="making-2dot5d-shooters.html">Making 2.5D Shooters</strong></a></p>
<p style="margin-bottom:0"><a name="trigonometry"></a>G1. </strong><a href="trigonometry.html">Trigonometry</strong></a></p>
<blockquote style="margin-top:0">
</blockquote>
</body>

View File

@ -6,7 +6,7 @@
</head>
<body>
<h1 style="margin-bottom:0in">X1. {About}<span style="float:right">{twn}</span></h1>
<a href="index.html">Go back
<a href="index.html">Go back</a>
<p><a name="{Section}"></a><strong>X1.1 </strong><strong>{Section}</strong>
<blockquote>
<p>Text

View File

@ -0,0 +1,46 @@
<!doctype html>
<html>
<head>
<title>Townengine Wiki : Trigonometry</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 style="margin-bottom:0in">G1. Trigonometry<span style="float:right">{twn}</span></h1>
<a href="index.html">Go back</a>
<blockquote>
<p>Truth is, most games are played in continuous space, where geometry matters.
Math classes turn out to be not so useless after all.
But even if you don't know your numbers well, you can compose functions by their meaning, and not number logic.
This page will try to help you with that, providing examples.
</blockquote>
<p><strong>the Hell are Vectors</strong>
<blockquote>
<p>There are distinct meanings they posses: <i>position in space</i>, <i>travel between points</i> and <i>direction</i>.
Often you need to juggle those representation around.
<p>For example, player and enemy positions by themselves don't tell you anything about their spatial relation.
For that you need to compute the <i>difference between them</i>, which is plain per component subtraction.
Resulted direction of travel depends on order of inputs, - it will point to position on the right side, the one you subtract with, but not from.
<p>It's important to understand that after subtraction the travel vector doesn't have relation with original positions,
it is effectively in a <i>subspace</i>, where (0,0,0) is at position you subtracted with to get it.
Operation is reversible by means of addition, resulting in position in space once again.
<p>I often forget what the direction is pointed at but here's a quick way to remind yourself: take two numbers, 10 and 0, and try subtracting them in different order.
Sign of result will tell you which part is pointed at, +10 -> direction is to the 10, -10 -> direction is to the 0. (Draw it in your head)
<p>Sometimes direction is more important than the travel, for which you need to drop the length of it.
This is called <i>normalization</i>, where result is called a <i>normal</i> (You probably heard those terms).
This is achieved by dividing every component by the length (magnitude) of vector itself.
Length is computed as <i>sqrt(x*x, y*y, z*z)</i>. This effectively creates a vector of length 1.
As every direction sized the same, when graphed they trace a circle.
</blockquote>
<p><strong>Center between two points</strong>
<blockquote>
<p><b>Method one. </b>Add two positions and divide them by two: (a + b) / 2. This is cheap, but harder to remember.
<p><b>Method two. </b>Find the travel between points, divide it by two and then translate the origin: (a + (a - b) / 2)
</blockquote>
</body>
</html>

View File

@ -8,6 +8,7 @@
/* plays audio file at specified channel or at scratch channel if NULL is passed, without ability to refer to it later */
/* path path must contain valid file extension to infer which file format it is */
/* supported formats: .ogg, .xm */
/* mono or stereo only */
TWN_API void audio_play(const char *audio,
const char *channel, /* optional */
bool repeat, /* default: false */

View File

@ -100,6 +100,20 @@ TWN_API void draw_camera(Vec3 position, /* optional, default: (0, 0, 0)
float zoom, /* optional, default: 1 */
float draw_distance); /* optional, default: 100 */
/* find position and looking direction of a 2d point into 3d space */
/* make sure it's called with matching parameters to last draw_camera(), if you need them to match */
typedef struct DrawCameraUnprojectResult {
Vec3 position;
Vec3 direction;
} DrawCameraUnprojectResult;
TWN_API DrawCameraUnprojectResult draw_camera_unproject(Vec2 point,
Vec3 position,
Vec3 direction,
Vec3 up,
float fov,
float zoom,
float draw_distance);
/* same as draw_camera(), but with first person controller in mind */
/* direction and up vectors are inferred from roll, pitch and yaw parameters (in radians) */
/* return value is direction and up vectors, so that you can use them in logic (such as controllers) */

View File

@ -40,4 +40,12 @@ typedef struct Rect {
} Rect;
/* data packet exchanged between parties, assumed immutable */
/* length is whole number */
typedef struct String {
char *data;
float length;
} String;
#endif

View File

@ -8,48 +8,37 @@
#include <stddef.h>
#include <stdbool.h>
/* here as it's not part of standard C */
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288 /**< pi */
#endif
#ifndef TWN_NOT_C
#include <math.h>
/* read data from virtual filesystem, with assumption that you know what that underlying data is */
/* this routine supports commands, which define operations performed on filepaths */
/* empty result is returned when something goes wrong or file doesn't exist, with an error logged */
/* defined commands: */
/* <path:string> -- reads utf8 encoded file to null terminated string */
/* <path:binary> -- reads arbitrary binary file, resulted encoding depends on environment (could be base64, for example) */
/* <path:exists> -- returns "yes" or "no" ascii string */
/* <path:images> -- returns newline separated utf8 list of image files in given path */
TWN_API String file_read(char const *file, const char *operation);
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288 /**< pi */
#endif
/* multiply by these to convert degrees <---> radians */
#define DEG2RAD (M_PI / 180)
#define RAD2DEG (180 / M_PI)
TWN_API void log_info(const char *restrict format, ...);
TWN_API void log_critical(const char *restrict format, ...);
TWN_API void log_warn(const char *restrict format, ...);
/* saves all texture atlases as BMP files in the write directory */
TWN_API void textures_dump_atlases(void);
/* TODO: this is why generics were invented. sorry, i'm tired today */
TWN_API float clampf(float f, float min, float max);
/* sets buf_out to a pointer to a byte buffer which must be freed. */
/* returns the size of this buffer. */
TWN_API int64_t file_to_bytes(const char *path, unsigned char **buf_out);
/* returns a pointer to a string which must be freed */
TWN_API char *file_to_str(const char *path);
/* returns true if the file exists in the filesystem */
TWN_API bool file_exists(const char *path);
#endif /* TWN_NOT_C */
/* read file to null terminated string, it is freed when the frame ends */
TWN_API char const *file_read(char const *file);
/* commit write to a file, which meaning depends on interpretation */
/* file_read should not be affected for current frame by this, operation is pending */
/* defined commands: */
/* <path:string> -- save utf8 file */
/* <path:binary> -- save binary file */
/* <path:image> -- write image data to a PNG file, potentially updating running textures used */
/* -- format is: [width:2b][height:2][alpha:1][data:(3+alpha)*width*height] */
TWN_API void file_write(char const *file, const char *operation, String string);
/* TODO: move to external templated lib */
/* calculates the overlap of two rectangles */
TWN_API Rect rect_overlap(Rect a, Rect b);
/* returns true if two rectangles are intersecting */
TWN_API bool rect_intersects(Rect a, Rect b);
/* TODO: move to external templated lib */
/* decrements a floating point second-based timer, stopping at 0.0f */
/* meant for poll based real time logic in game logic */
/* note that it should be decremented only on the next tick after its creation */
@ -60,10 +49,13 @@ typedef struct TimerElapseSecondsResult {
} TimerElapseSecondsResult;
TWN_API TimerElapseSecondsResult timer_elapse_seconds(float seconds_left, float interval);
TWN_API void log_string(char const *value, char const *identity);
TWN_API void log_float(float value, char const *identity);
TWN_API void log_vec2(Vec2 value, char const *identity);
TWN_API void log_vec3(Vec3 value, char const *identity);
TWN_API void log_rect(Rect value, char const *identity);
/* IDEA: NES debugger style frame slices that show for how long and when certain profile range takes place */
TWN_API void profile_start(char const *profile);
TWN_API void profile_end(char const *profile);

BIN
share/assets/Dernyns256.ttf Normal file

Binary file not shown.

442
share/assets/license.txt Normal file
View File

@ -0,0 +1,442 @@
256 Font (c) by Dernyn, Gagugaflicks, Joel Di.
dernyn - at- stonedcoder.org
I take full credit for the 128+ newly added glyphs to the classic CP437 standard.
first time ever done since TTF!
256 font by Dernyn is licensed under a
Creative Commons Attribution-ShareAlike 4.0 International License.
You should have received a copy of the license along with this
work. If not, see <http://creativecommons.org/licenses/by-sa/4.0/>.
or read below.
Attribution-ShareAlike 4.0 International
=======================================================================
Creative Commons Corporation ("Creative Commons") is not a law firm and
does not provide legal services or legal advice. Distribution of
Creative Commons public licenses does not create a lawyer-client or
other relationship. Creative Commons makes its licenses and related
information available on an "as-is" basis. Creative Commons gives no
warranties regarding its licenses, any material licensed under their
terms and conditions, or any related information. Creative Commons
disclaims all liability for damages resulting from their use to the
fullest extent possible.
Using Creative Commons Public Licenses
Creative Commons public licenses provide a standard set of terms and
conditions that creators and other rights holders may use to share
original works of authorship and other material subject to copyright
and certain other rights specified in the public license below. The
following considerations are for informational purposes only, are not
exhaustive, and do not form part of our licenses.
Considerations for licensors: Our public licenses are
intended for use by those authorized to give the public
permission to use material in ways otherwise restricted by
copyright and certain other rights. Our licenses are
irrevocable. Licensors should read and understand the terms
and conditions of the license they choose before applying it.
Licensors should also secure all rights necessary before
applying our licenses so that the public can reuse the
material as expected. Licensors should clearly mark any
material not subject to the license. This includes other CC-
licensed material, or material used under an exception or
limitation to copyright. More considerations for licensors:
wiki.creativecommons.org/Considerations_for_licensors
Considerations for the public: By using one of our public
licenses, a licensor grants the public permission to use the
licensed material under specified terms and conditions. If
the licensor's permission is not necessary for any reason--for
example, because of any applicable exception or limitation to
copyright--then that use is not regulated by the license. Our
licenses grant only permissions under copyright and certain
other rights that a licensor has authority to grant. Use of
the licensed material may still be restricted for other
reasons, including because others have copyright or other
rights in the material. A licensor may make special requests,
such as asking that all changes be marked or described.
Although not required by our licenses, you are encouraged to
respect those requests where reasonable. More considerations
for the public:
wiki.creativecommons.org/Considerations_for_licensees
=======================================================================
Creative Commons Attribution-ShareAlike 4.0 International Public
License
By exercising the Licensed Rights (defined below), You accept and agree
to be bound by the terms and conditions of this Creative Commons
Attribution-ShareAlike 4.0 International Public License ("Public
License"). To the extent this Public License may be interpreted as a
contract, You are granted the Licensed Rights in consideration of Your
acceptance of these terms and conditions, and the Licensor grants You
such rights in consideration of benefits the Licensor receives from
making the Licensed Material available under these terms and
conditions.
Section 1 -- Definitions.
a. Adapted Material means material subject to Copyright and Similar
Rights that is derived from or based upon the Licensed Material
and in which the Licensed Material is translated, altered,
arranged, transformed, or otherwise modified in a manner requiring
permission under the Copyright and Similar Rights held by the
Licensor. For purposes of this Public License, where the Licensed
Material is a musical work, performance, or sound recording,
Adapted Material is always produced where the Licensed Material is
synched in timed relation with a moving image.
b. Adapter's License means the license You apply to Your Copyright
and Similar Rights in Your contributions to Adapted Material in
accordance with the terms and conditions of this Public License.
c. BY-SA Compatible License means a license listed at
creativecommons.org/compatiblelicenses, approved by Creative
Commons as essentially the equivalent of this Public License.
d. Copyright and Similar Rights means copyright and/or similar rights
closely related to copyright including, without limitation,
performance, broadcast, sound recording, and Sui Generis Database
Rights, without regard to how the rights are labeled or
categorized. For purposes of this Public License, the rights
specified in Section 2(b)(1)-(2) are not Copyright and Similar
Rights.
e. Effective Technological Measures means those measures that, in the
absence of proper authority, may not be circumvented under laws
fulfilling obligations under Article 11 of the WIPO Copyright
Treaty adopted on December 20, 1996, and/or similar international
agreements.
f. Exceptions and Limitations means fair use, fair dealing, and/or
any other exception or limitation to Copyright and Similar Rights
that applies to Your use of the Licensed Material.
g. License Elements means the license attributes listed in the name
of a Creative Commons Public License. The License Elements of this
Public License are Attribution and ShareAlike.
h. Licensed Material means the artistic or literary work, database,
or other material to which the Licensor applied this Public
License.
i. Licensed Rights means the rights granted to You subject to the
terms and conditions of this Public License, which are limited to
all Copyright and Similar Rights that apply to Your use of the
Licensed Material and that the Licensor has authority to license.
j. Licensor means the individual(s) or entity(ies) granting rights
under this Public License.
k. Share means to provide material to the public by any means or
process that requires permission under the Licensed Rights, such
as reproduction, public display, public performance, distribution,
dissemination, communication, or importation, and to make material
available to the public including in ways that members of the
public may access the material from a place and at a time
individually chosen by them.
l. Sui Generis Database Rights means rights other than copyright
resulting from Directive 96/9/EC of the European Parliament and of
the Council of 11 March 1996 on the legal protection of databases,
as amended and/or succeeded, as well as other essentially
equivalent rights anywhere in the world.
m. You means the individual or entity exercising the Licensed Rights
under this Public License. Your has a corresponding meaning.
Section 2 -- Scope.
a. License grant.
1. Subject to the terms and conditions of this Public License,
the Licensor hereby grants You a worldwide, royalty-free,
non-sublicensable, non-exclusive, irrevocable license to
exercise the Licensed Rights in the Licensed Material to:
a. reproduce and Share the Licensed Material, in whole or
in part; and
b. produce, reproduce, and Share Adapted Material.
2. Exceptions and Limitations. For the avoidance of doubt, where
Exceptions and Limitations apply to Your use, this Public
License does not apply, and You do not need to comply with
its terms and conditions.
3. Term. The term of this Public License is specified in Section
6(a).
4. Media and formats; technical modifications allowed. The
Licensor authorizes You to exercise the Licensed Rights in
all media and formats whether now known or hereafter created,
and to make technical modifications necessary to do so. The
Licensor waives and/or agrees not to assert any right or
authority to forbid You from making technical modifications
necessary to exercise the Licensed Rights, including
technical modifications necessary to circumvent Effective
Technological Measures. For purposes of this Public License,
simply making modifications authorized by this Section 2(a)
(4) never produces Adapted Material.
5. Downstream recipients.
a. Offer from the Licensor -- Licensed Material. Every
recipient of the Licensed Material automatically
receives an offer from the Licensor to exercise the
Licensed Rights under the terms and conditions of this
Public License.
b. Additional offer from the Licensor -- Adapted Material.
Every recipient of Adapted Material from You
automatically receives an offer from the Licensor to
exercise the Licensed Rights in the Adapted Material
under the conditions of the Adapter's License You apply.
c. No downstream restrictions. You may not offer or impose
any additional or different terms or conditions on, or
apply any Effective Technological Measures to, the
Licensed Material if doing so restricts exercise of the
Licensed Rights by any recipient of the Licensed
Material.
6. No endorsement. Nothing in this Public License constitutes or
may be construed as permission to assert or imply that You
are, or that Your use of the Licensed Material is, connected
with, or sponsored, endorsed, or granted official status by,
the Licensor or others designated to receive attribution as
provided in Section 3(a)(1)(A)(i).
b. Other rights.
1. Moral rights, such as the right of integrity, are not
licensed under this Public License, nor are publicity,
privacy, and/or other similar personality rights; however, to
the extent possible, the Licensor waives and/or agrees not to
assert any such rights held by the Licensor to the limited
extent necessary to allow You to exercise the Licensed
Rights, but not otherwise.
2. Patent and trademark rights are not licensed under this
Public License.
3. To the extent possible, the Licensor waives any right to
collect royalties from You for the exercise of the Licensed
Rights, whether directly or through a collecting society
under any voluntary or waivable statutory or compulsory
licensing scheme. In all other cases the Licensor expressly
reserves any right to collect such royalties.
Section 3 -- License Conditions.
Your exercise of the Licensed Rights is expressly made subject to the
following conditions.
a. Attribution.
1. If You Share the Licensed Material (including in modified
form), You must:
a. retain the following if it is supplied by the Licensor
with the Licensed Material:
i. identification of the creator(s) of the Licensed
Material and any others designated to receive
attribution, in any reasonable manner requested by
the Licensor (including by pseudonym if
designated);
ii. a copyright notice;
iii. a notice that refers to this Public License;
iv. a notice that refers to the disclaimer of
warranties;
v. a URI or hyperlink to the Licensed Material to the
extent reasonably practicable;
b. indicate if You modified the Licensed Material and
retain an indication of any previous modifications; and
c. indicate the Licensed Material is licensed under this
Public License, and include the text of, or the URI or
hyperlink to, this Public License.
2. You may satisfy the conditions in Section 3(a)(1) in any
reasonable manner based on the medium, means, and context in
which You Share the Licensed Material. For example, it may be
reasonable to satisfy the conditions by providing a URI or
hyperlink to a resource that includes the required
information.
3. If requested by the Licensor, You must remove any of the
information required by Section 3(a)(1)(A) to the extent
reasonably practicable.
b. ShareAlike.
In addition to the conditions in Section 3(a), if You Share
Adapted Material You produce, the following conditions also apply.
1. The Adapter's License You apply must be a Creative Commons
license with the same License Elements, this version or
later, or a BY-SA Compatible License.
2. You must include the text of, or the URI or hyperlink to, the
Adapter's License You apply. You may satisfy this condition
in any reasonable manner based on the medium, means, and
context in which You Share Adapted Material.
3. You may not offer or impose any additional or different terms
or conditions on, or apply any Effective Technological
Measures to, Adapted Material that restrict exercise of the
rights granted under the Adapter's License You apply.
Section 4 -- Sui Generis Database Rights.
Where the Licensed Rights include Sui Generis Database Rights that
apply to Your use of the Licensed Material:
a. for the avoidance of doubt, Section 2(a)(1) grants You the right
to extract, reuse, reproduce, and Share all or a substantial
portion of the contents of the database;
b. if You include all or a substantial portion of the database
contents in a database in which You have Sui Generis Database
Rights, then the database in which You have Sui Generis Database
Rights (but not its individual contents) is Adapted Material,
including for purposes of Section 3(b); and
c. You must comply with the conditions in Section 3(a) if You Share
all or a substantial portion of the contents of the database.
For the avoidance of doubt, this Section 4 supplements and does not
replace Your obligations under this Public License where the Licensed
Rights include other Copyright and Similar Rights.
Section 5 -- Disclaimer of Warranties and Limitation of Liability.
a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
c. The disclaimer of warranties and limitation of liability provided
above shall be interpreted in a manner that, to the extent
possible, most closely approximates an absolute disclaimer and
waiver of all liability.
Section 6 -- Term and Termination.
a. This Public License applies for the term of the Copyright and
Similar Rights licensed here. However, if You fail to comply with
this Public License, then Your rights under this Public License
terminate automatically.
b. Where Your right to use the Licensed Material has terminated under
Section 6(a), it reinstates:
1. automatically as of the date the violation is cured, provided
it is cured within 30 days of Your discovery of the
violation; or
2. upon express reinstatement by the Licensor.
For the avoidance of doubt, this Section 6(b) does not affect any
right the Licensor may have to seek remedies for Your violations
of this Public License.
c. For the avoidance of doubt, the Licensor may also offer the
Licensed Material under separate terms or conditions or stop
distributing the Licensed Material at any time; however, doing so
will not terminate this Public License.
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
License.
Section 7 -- Other Terms and Conditions.
a. The Licensor shall not be bound by any additional or different
terms or conditions communicated by You unless expressly agreed.
b. Any arrangements, understandings, or agreements regarding the
Licensed Material not stated herein are separate from and
independent of the terms and conditions of this Public License.
Section 8 -- Interpretation.
a. For the avoidance of doubt, this Public License does not, and
shall not be interpreted to, reduce, limit, restrict, or impose
conditions on any use of the Licensed Material that could lawfully
be made without permission under this Public License.
b. To the extent possible, if any provision of this Public License is
deemed unenforceable, it shall be automatically reformed to the
minimum extent necessary to make it enforceable. If the provision
cannot be reformed, it shall be severed from this Public License
without affecting the enforceability of the remaining terms and
conditions.
c. No term or condition of this Public License will be waived and no
failure to comply consented to unless expressly agreed to by the
Licensor.
d. Nothing in this Public License constitutes or may be interpreted
as a limitation upon, or waiver of, any privileges and immunities
that apply to the Licensor or You, including from the legal
processes of any jurisdiction or authority.
=======================================================================
Creative Commons is not a party to its public
licenses. Notwithstanding, Creative Commons may elect to apply one of
its public licenses to material it publishes and in those instances
will be considered the “Licensor.” The text of the Creative Commons
public licenses is dedicated to the public domain under the CC0 Public
Domain Dedication. Except for the limited purpose of indicating that
material is shared under a Creative Commons public license or as
otherwise permitted by the Creative Commons policies published at
creativecommons.org/policies, Creative Commons does not authorize the
use of the trademark "Creative Commons" or any other trademark or logo
of Creative Commons without its prior written consent including,
without limitation, in connection with any unauthorized modifications
to any of its public licenses or any other arrangements,
understandings, or agreements concerning use of licensed material. For
the avoidance of doubt, this paragraph does not form part of the
public licenses.
Creative Commons may be contacted at creativecommons.org.

View File

@ -251,6 +251,29 @@
"restriction": "parameters"
},
"draw_camera_unproject": {
"module": "draw",
"symbol": "camera_unproject",
"header": "twn_draw.h",
"params": [
{ "name": "point", "type": "Vec2", "default": { "x": 0, "y": 0 } },
{ "name": "position", "type": "Vec3", "default": { "x": 0, "y": 0, "z": 0 } },
{ "name": "direction", "type": "Vec3", "default": { "x": 0, "y": 0, "z": -1 } },
{ "name": "up", "type": "Vec3", "default": { "x": 0, "y": 1, "z": 0 } },
{ "name": "fov", "type": "float", "default": 1.57079632679 },
{ "name": "zoom", "type": "float", "default": 1 },
{ "name": "draw_distance", "type": "float", "default": 100 }
],
"return": {
"fields": [
{ "name": "position", "type": "Vec3" },
{ "name": "direction", "type": "Vec3" }
],
"c_type": "DrawCameraUnprojectResult"
},
"restriction": "parameters"
},
"draw_skybox": {
"module": "draw",
"symbol": "skybox",
@ -289,9 +312,10 @@
"symbol": "read",
"header": "twn_util.h",
"params": [
{ "name": "file", "type": "char *" }
{ "name": "file", "type": "char *" },
{ "name": "operation", "type": "char *" }
],
"return": "char *",
"return": "String",
"restriction": "asset"
},
@ -325,6 +349,26 @@
"restriction": "parameters"
},
"log_string": {
"module": "util",
"symbol": "log_string",
"header": "twn_util.h",
"params": [
{ "name": "value", "type": "char *" },
{ "name": "identity", "type": "char *", "default": {} }
]
},
"log_float": {
"module": "util",
"symbol": "log_float",
"header": "twn_util.h",
"params": [
{ "name": "value", "type": "float" },
{ "name": "identity", "type": "char *", "default": {} }
]
},
"log_vec2": {
"module": "util",
"symbol": "log_vec2",

View File

@ -55,7 +55,7 @@ static void game_object_file_action(char const *path, enum FilewatchAction actio
#pragma GCC diagnostic pop
if (fabsf(0.0f - ctx.game.frame_number) > 0.00001f) {
if (ctx.game.frame_number != 0.0f) {
log_info("Game object was reloaded\n");
reset_state();
}

View File

@ -26,8 +26,8 @@ void draw_billboard(char const *texture,
batch_p = &ctx.billboard_batches[hmlenu(ctx.billboard_batches) - 1]; /* TODO: can last index be used? */
}
bool const texture_region_valid = fabsf(texture_region.w - texture_region.h) > 0.00001f
&& fabsf(0.0f - texture_region.w) > 0.00001f;
bool const texture_region_valid = texture_region.h != 0.0f
&& texture_region.w != 0.0f;
struct SpaceBillboard billboard = {
.color = color,

View File

@ -5,6 +5,7 @@
#include "twn_types.h"
#include "twn_util.h"
#include "twn_vec.h"
#include "twn_vec_c.h"
#include "twn_deferred_commands.h"
#include <SDL2/SDL.h>
@ -432,30 +433,27 @@ TWN_API void draw_camera_2d(Vec2 position,
/* TODO: check for NaNs and alike */
void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom, float draw_distance) {
bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
bool const orthographic = fov == 0.0f;
if (!orthographic && fov >= (float)(M_PI))
log_warn("Invalid fov given (%f)", (double)fov);
float const aspect = (float)ctx.base_render_width / (float)ctx.base_render_height;
Camera const camera = {
.fov = fov,
.pos = position,
.target = vec3_norm(direction),
.up = up,
.viewbox = {
(Vec2){ 1/-zoom, 1/zoom },
(Vec2){ 1/zoom, 1/-zoom }
-aspect/zoom, aspect/zoom,
1/zoom, -1/zoom
},
.far_z = draw_distance
};
if (!orthographic)
camera_projection_matrix = camera_perspective(&camera);
else
camera_projection_matrix = camera_orthographic(&camera);
camera_far_z = draw_distance;
camera_projection_matrix = orthographic ? camera_orthographic(&camera) : camera_perspective(&camera);
camera_look_at_matrix = camera_look_at(&camera);
camera_far_z = draw_distance;
}
@ -469,7 +467,7 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
float zoom,
float draw_distance)
{
bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
bool const orthographic = fov == 0.0f;
if (!orthographic && fov >= (float)(M_PI))
log_warn("Invalid fov given (%f)", (double)fov);
@ -497,6 +495,69 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
}
DrawCameraUnprojectResult draw_camera_unproject(Vec2 point,
Vec3 position,
Vec3 direction,
Vec3 up,
float fov,
float zoom,
float draw_distance)
{
bool const orthographic = fov == 0.0f;
if (!orthographic && fov >= (float)(M_PI))
log_warn("Invalid fov given (%f)", (double)fov);
float const aspect = (float)ctx.base_render_width / (float)ctx.base_render_height;
Camera const camera = {
.fov = fov,
.pos = position,
.target = vec3_norm(direction),
.up = up,
.viewbox = {
-aspect/zoom, aspect/zoom,
1/zoom, -1/zoom
},
.far_z = draw_distance
};
point.y = (float)ctx.base_render_height - point.y;
Vec4 v;
v.x = 2.0f * point.x / (float)ctx.base_render_width - 1.0f;
v.y = 2.0f * point.y / (float)ctx.base_render_height - 1.0f;
v.z = 2.0f * 1.0f - 1.0f;
v.w = 1.0f;
/* simpler case, just shoot a point from viewbox face along the supplied direction */
if (orthographic) {
Vec3 right = vec3_norm(vec3_cross(direction, up));
Vec3 aup = vec3_norm(vec3_cross(direction, right));
return (DrawCameraUnprojectResult){
.direction = direction,
.position = vec3_add(vec3_add(position, vec3_scale(right, v.x * aspect/zoom)), vec3_scale(aup, -v.y * aspect/zoom)),
};
}
/* TODO: sanity check for point being in proper box? it would produce bogus results otherwise */
Matrix4 const projection_matrix = orthographic ? camera_orthographic(&camera) : camera_perspective(&camera);
Matrix4 const look_at_matrix = camera_look_at(&camera);
/* TODO: cache matrices for repeated camera inputs, those are expensive */
Matrix4 const inverse_view_proj = matrix_inverse(matrix_multiply(projection_matrix, look_at_matrix));
v = matrix_vector_multiply(inverse_view_proj, v);
/* TODO: is it even ever not equal to 1 in our case ? */
v = vec4_scale(v, 1.0f / v.w);
return (DrawCameraUnprojectResult){
.position = (Vec3){ v.x, v.y, v.z },
.direction = vec3_norm(vec3_sub(position, (Vec3){ v.x, v.y, v.z })),
};
}
void set_depth_range(double low, double high) {
depth_range_low = low;
depth_range_high = high;

View File

@ -78,7 +78,7 @@ void start_render_frame(void) {
void end_render_frame(void) {
if (!ctx.render_double_buffered || (fabsf(1.0f - ctx.game.frame_number) < 0.00001f)) {
if (!ctx.render_double_buffered || ctx.game.frame_number == 0.0f) {
issue_deferred_draw_commands();
SDL_GL_SwapWindow(ctx.window);
arrsetlen(deferred_commands, 0);
@ -561,7 +561,7 @@ void finally_draw_command(DeferredCommandDraw command) {
glDepthRange(command.depth_range_low, command.depth_range_high);
/* TODO: cache it for constant parameters, which is a common case */
if (command.pipeline == PIPELINE_SPACE && fabsf(0.0f - ctx.game_copy.fog_density) >= 0.00001f) {
if (command.pipeline == PIPELINE_SPACE && ctx.game_copy.fog_density != 0.0f) {
glEnable(GL_FOG);
/* clamp to valid range */

View File

@ -208,7 +208,7 @@ void delete_gpu_texture(GPUTexture texture) {
}
void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height) {
void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height, int xoffset, int yoffset) {
glBindTexture(GL_TEXTURE_2D, texture);
int format;
@ -223,6 +223,12 @@ void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int widt
return;
}
if (xoffset != 0 || yoffset != 0)
glPixelStorei(GL_UNPACK_ROW_LENGTH, width + xoffset * 2);
void *start = xoffset != 0 || yoffset != 0
? &(((uint8_t *)pixels)[xoffset * channels + yoffset * (width + xoffset * 2) * channels]) : pixels;
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
@ -231,7 +237,10 @@ void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int widt
height,
format,
GL_UNSIGNED_BYTE,
pixels);
start);
if (xoffset != 0 || yoffset != 0)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}

View File

@ -16,7 +16,7 @@ GPUTexture create_gpu_texture(TextureFilter filter, bool generate_mipmaps, int c
void delete_gpu_texture(GPUTexture texture);
void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height);
void upload_gpu_texture(GPUTexture texture, void *pixels, int channels, int width, int height, int xoffset, int yoffset);
void bind_gpu_texture(GPUTexture texture);

View File

@ -68,7 +68,7 @@ void draw_line_3d(Vec3 start,
struct LineVertex const v1 = { .position = finish, .color = color };
/* 3d case, unordered depth based draw */
struct LineBatchItemKey const key = { .color = color, .thickness = (uint8_t)(floorf(thickness)) };
struct LineBatchItemKey const key = { .thickness = (uint8_t)(floorf(thickness)) };
struct LineBatchItem *batch_p = hmgetp_null(ctx.line_batches, key);
if (!batch_p) {
hmput(ctx.line_batches, key, ((struct LinePrimitive){.thickness = thickness, .color = color}));
@ -83,6 +83,9 @@ void draw_line_3d(Vec3 start,
void render_lines(LinePrimitive const *line, bool is_3d) {
DeferredCommandDraw command = {0};
if (arrlenu(line->vertices) == 0)
return;
VertexBuffer buffer = get_scratch_vertex_array();
specify_vertex_buffer(buffer, line->vertices, arrlenu(line->vertices) * sizeof (*line->vertices));

View File

@ -239,10 +239,15 @@ void finally_draw_space_quads_batch(const MeshBatch *batch,
const Rect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key);
const Rect dims = textures_get_dims(&ctx.texture_cache, texture_key);
const float wr = srcrect.w / dims.w;
const float hr = srcrect.h / dims.h;
const float xr = srcrect.x / dims.w;
const float yr = srcrect.y / dims.h;
// const float wr = srcrect.w / dims.w;
// const float hr = srcrect.h / dims.h;
// const float xr = srcrect.x / dims.w;
// const float yr = srcrect.y / dims.h;
const float wr = 1;
const float hr = 1;
const float xr = 1;
const float yr = 1;
/* update pixel-based uvs to correspond with texture atlases */
for (size_t i = 0; i < primitives_len; ++i) {
@ -293,6 +298,7 @@ void finally_draw_space_quads_batch(const MeshBatch *batch,
};
command.texture_key = texture_key;
command.texture_repeat = true;
command.textured = true;
command.element_buffer = get_quad_element_buffer();

View File

@ -28,8 +28,8 @@ void draw_sprite(char const *path,
{
/* if .w and .h are zeroed then assume whole region */
/* TODO: don't check here, just move to redner code ? */
bool const texture_region_valid = fabsf(texture_region.w - texture_region.h) > 0.00001f
&& fabsf(0.0f - texture_region.w) > 0.00001f;
bool const texture_region_valid = texture_region.h != 0.0f
&& texture_region.w != 0.0f;
SpritePrimitive sprite = {
.rect = rect,

View File

@ -7,10 +7,6 @@
#include <stb_truetype.h>
#include <stb_ds.h>
#define ASCII_START 32
#define ASCII_END 128
#define NUM_DISPLAY_ASCII ((ASCII_END - ASCII_START) + 1)
/* should be large enough that there's virtually never more than one block */
#define STRING_ARENA_BLOCK_SIZE 512000
@ -43,6 +39,14 @@ typedef struct FontFileCacheItem {
static FontFileCacheItem *font_file_cache_hash;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreserved-identifier"
extern uint8_t _binary_share_assets_Dernyns256_ttf_start[];
extern uint8_t _binary_share_assets_Dernyns256_ttf_end[];
#pragma GCC diagnostic pop
static void string_arena_init(StringArena *arena) {
arena->head = cmalloc(sizeof *arena->head);
@ -121,7 +125,13 @@ static FontData *text_load_font_data(const char *path, int height_px) {
buf = font_file_ptr->value.buffer;
buf_len = font_file_ptr->value.len;
} else {
buf_len = file_to_bytes(path, &buf);
/* TODO: use and reuse this on fonts that are not found as well */
if (SDL_strncmp(path, "!", 1) == 0) {
buf_len = _binary_share_assets_Dernyns256_ttf_end - _binary_share_assets_Dernyns256_ttf_start;
buf = _binary_share_assets_Dernyns256_ttf_start;
} else {
buf_len = file_to_bytes(path, &buf);
}
if (buf_len == -1) {
/* TODO: have a fallback default font */
log_warn("Font %s not found", path);
@ -164,7 +174,8 @@ static FontData *text_load_font_data(const char *path, int height_px) {
bitmap,
1,
(int)ctx.font_texture_size,
(int)ctx.font_texture_size
(int)ctx.font_texture_size,
0, 0
);
SDL_free(bitmap);
@ -284,7 +295,8 @@ void text_cache_deinit(TextCache *cache) {
}
for (size_t i = 0; i < shlenu(font_file_cache_hash); ++i) {
SDL_free(font_file_cache_hash[i].value.buffer);
if (font_file_cache_hash[i].value.buffer != _binary_share_assets_Dernyns256_ttf_start)
SDL_free(font_file_cache_hash[i].value.buffer);
}
shfree(font_file_cache_hash);
@ -300,11 +312,7 @@ void text_cache_reset_arena(TextCache *cache) {
void draw_text(const char *string, Vec2 position, float height, Color color, const char *font) {
if (!font) {
log_warn("Default font isn't yet implemented");
return;
}
if (!font) font = "!";
if (!ensure_font_cache(font, (int)height))
return;
@ -334,6 +342,7 @@ void draw_text(const char *string, Vec2 position, float height, Color color, con
float draw_text_width(const char *string, float height, const char *font) {
if (!font) font = "!";
if (!ensure_font_cache(font, (int)height))
return 0;

View File

@ -8,7 +8,7 @@
#include <stb_truetype.h>
#define ASCII_START 32
#define ASCII_START 1
#define ASCII_END 128
#define NUM_DISPLAY_ASCII ((ASCII_END - ASCII_START) + 1)

View File

@ -9,6 +9,9 @@
#include <physfs.h>
#include <physfsrwops.h>
#define STB_VORBIS_MAX_CHANNELS 2
#define STB_VORBIS_NO_STDIO
#define STB_VORBIS_NO_INTEGER_CONVERSION
#define STB_VORBIS_NO_PUSHDATA_API
#define STB_VORBIS_HEADER_ONLY
#include <stb_vorbis.c>
@ -69,6 +72,7 @@ static AudioFileType infer_audio_file_type(const char *path) {
size_t const path_len = SDL_strlen(path);
for (int i = 0; i < AUDIO_FILE_TYPE_COUNT; ++i) {
if (path_len <= audio_exts_len[i]) continue;
if (SDL_strncmp(&path[path_len - audio_exts_len[i]], audio_exts[i], audio_exts_len[i]) == 0)
return (AudioFileType)i;
}
@ -196,7 +200,7 @@ static union AudioContext init_audio_context(const char *path, AudioFileType typ
static void free_audio_channel(AudioChannel channel) {
switch (channel.file_type) {
case AUDIO_FILE_TYPE_OGG: {
SDL_free(channel.context.vorbis.data);
stb_vorbis_close(channel.context.vorbis.handle);
break;
}
case AUDIO_FILE_TYPE_WAV: {

View File

@ -59,15 +59,15 @@ Matrix4 camera_orthographic(const Camera *const camera) {
Matrix4 result = {0};
const float rl = 1.0f / (camera->viewbox[0].y - camera->viewbox[0].x);
const float tb = 1.0f / (camera->viewbox[1].x - camera->viewbox[1].y);
const float rl = 1.0f / (camera->viewbox.y - camera->viewbox.x);
const float tb = 1.0f / (camera->viewbox.z - camera->viewbox.w);
const float fn = -1.0f / (camera->far_z - -camera->far_z);
result.row[0].x = 2.0f * rl;
result.row[1].y = 2.0f * tb;
result.row[2].z = 2.0f * fn;
result.row[3].x = -(camera->viewbox[0].y + camera->viewbox[0].x) * rl;
result.row[3].y = -(camera->viewbox[1].x + camera->viewbox[1].y) * tb;
result.row[3].x = -(camera->viewbox.y + camera->viewbox.x) * rl;
result.row[3].y = -(camera->viewbox.z + camera->viewbox.w) * tb;
result.row[3].z = (camera->far_z + -camera->far_z) * fn;
result.row[3].w = 1.0f;

View File

@ -12,7 +12,7 @@ typedef struct Camera {
Vec3 pos; /* eye position */
Vec3 target; /* normalized target vector */
Vec3 up; /* normalized up vector */
Vec2 viewbox[2]; /* othrographic aabb, ((left, right), (top, bottom)) */
Vec4 viewbox; /* othrographic aabb, ((left, right), (top, bottom)) */
float fov; /* field of view, in radians */
float far_z;
} Camera;

View File

@ -52,7 +52,6 @@ typedef struct EngineContext {
MeshBatchItem *quad_batches;
struct LineBatchItem {
struct LineBatchItemKey {
Color color;
uint8_t thickness;
} key;
struct LinePrimitive value;

View File

@ -265,7 +265,7 @@ static void resolve_pack_dependencies(const char *pack_name) {
/* no package manifest provided, abort */
goto OK_NO_MANIFEST;
char *manifest_file = file_to_str(path);
char *manifest_file = file_to_str(path, NULL);
if (!manifest_file) {
CRY_PHYSFS("Pack manifest file loading failed");
goto ERR_PACK_MANIFEST_FILE_LOADING;
@ -375,7 +375,7 @@ static bool initialize(void) {
/* load the config file into an opaque table */
{
char *config_file = file_to_str("/twn.toml");
char *config_file = file_to_str("/twn.toml", NULL);
if (config_file == NULL) {
CRY("Configuration file loading failed", "Cannot access /twn.toml");
goto fail;

View File

@ -73,19 +73,20 @@ static SDL_Surface *gen_missing_texture_surface(void) {
SDL_LockMutex(textures_load_mutex);
if (!missing_texture_surface) {
uint8_t *data = SDL_malloc(64 * 64 * 3);
for (int y = 0; y < 64; ++y) {
for (int x = 0; x < 64; ++x) {
int const dim = 64 + TEXTURE_BORDER_REPEAT_SIZE * 2;
uint8_t *data = SDL_malloc(dim * dim * 3);
for (int y = 0; y < dim; ++y) {
for (int x = 0; x < dim; ++x) {
/* diagonal stripes, chosen so that asked pixel uvs are corresponding to final output */
data[(y * 64 + x) * 3 + 0] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
data[(y * 64 + x) * 3 + 1] = (x / 2 + y / 2) % 2 == 0 ? 0 : 0;
data[(y * 64 + x) * 3 + 2] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
data[(y * dim + x) * 3 + 0] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
data[(y * dim + x) * 3 + 1] = (x / 2 + y / 2) % 2 == 0 ? 0 : 0;
data[(y * dim + x) * 3 + 2] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0;
}
}
missing_texture_surface = SDL_CreateRGBSurfaceFrom(data, 64, 64,
missing_texture_surface = SDL_CreateRGBSurfaceFrom(data, dim, dim,
3 * 8,
64 * 3,
dim * 3,
rmask, gmask, bmask, 0);
}
@ -249,7 +250,7 @@ static void add_new_atlas(TextureCache *cache) {
static void upload_texture_from_surface(GPUTexture texture, SDL_Surface *surface) {
SDL_LockSurface(surface);
upload_gpu_texture(texture, surface->pixels, surface->format->BytesPerPixel, surface->w, surface->h);
upload_gpu_texture(texture, surface->pixels, surface->format->BytesPerPixel, surface->w, surface->h, 0, 0);
SDL_UnlockSurface(surface);
}
@ -299,10 +300,8 @@ static stbrp_rect *create_rects_from_cache(TextureCache *cache) {
continue;
/* only put it once */
if (!missing_texture_used && cache->hash[i].value.data == missing_texture_surface) {
if (!missing_texture_used && cache->hash[i].value.data == missing_texture_surface)
missing_texture_used = true;
continue;
}
const SDL_Surface *surface_data = cache->hash[i].value.data;
stbrp_rect new_rect = {
@ -412,12 +411,14 @@ void textures_cache_deinit(TextureCache *cache) {
/* free cache hashes */
for (size_t i = 0; i < shlenu(cache->hash); ++i) {
/* TODO: better to have field that stores the source of memory directly, ugh */
if (cache->hash[i].value.srcrect.w < 2048 && cache->hash[i].value.srcrect.h < 2048)
(void)0; /* do nothing, memory owned by surface */
else if (missing_texture_surface == NULL || cache->hash[i].value.data->pixels != missing_texture_surface->pixels)
stbi_image_free(cache->hash[i].value.data->pixels);
else
if (missing_texture_surface != NULL && cache->hash[i].value.data->pixels == missing_texture_surface->pixels) {
missing_texture_surface = NULL;
SDL_free(cache->hash[i].value.data->pixels);
}
else if (cache->hash[i].value.srcrect.w < 2048 && cache->hash[i].value.srcrect.h < 2048)
(void)0; /* do nothing, memory owned by surface */
else
stbi_image_free(cache->hash[i].value.data->pixels);
SDL_FreeSurface(cache->hash[i].value.data);
}
shfree(cache->hash);
@ -508,13 +509,13 @@ static TextureKey textures_load(TextureCache *cache, const char *path) {
/* place a dummy for future lookups to know it will be loaded */
/* as well as a place for worker to fill in */
SDL_LockMutex(textures_load_mutex);
shput(cache->hash, path, (Texture){0});
/* append a new request, use stable indices */
struct TextureLoadRequest const request = {
.index = shlenu(cache->hash) - 1,
};
SDL_LockMutex(textures_load_mutex);
arrpush(texture_load_queue, request);
SDL_UnlockMutex(textures_load_mutex);
@ -707,8 +708,8 @@ void textures_bind_repeating(const TextureCache *cache, TextureKey key) {
const GPUTexture repeating_texture = create_gpu_texture(TEXTURE_FILTER_NEAREAST,
false,
texture.data->format->BytesPerPixel,
texture.data->w,
texture.data->h);
texture.data->w - TEXTURE_BORDER_REPEAT_SIZE * 2,
texture.data->h - TEXTURE_BORDER_REPEAT_SIZE * 2);
SDL_LockSurface(texture.data);
@ -716,8 +717,10 @@ void textures_bind_repeating(const TextureCache *cache, TextureKey key) {
upload_gpu_texture(repeating_texture,
texture.data->pixels,
texture.data->format->BytesPerPixel,
texture.data->w,
texture.data->h);
texture.data->w - TEXTURE_BORDER_REPEAT_SIZE * 2,
texture.data->h - TEXTURE_BORDER_REPEAT_SIZE * 2,
TEXTURE_BORDER_REPEAT_SIZE,
TEXTURE_BORDER_REPEAT_SIZE);
SDL_UnlockSurface(texture.data);

View File

@ -109,12 +109,6 @@ void *ccalloc(size_t num, size_t size) {
}
float clampf(float f, float min, float max) {
const float t = f < min ? min : f;
return t > max ? max : t;
}
int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
@ -133,7 +127,7 @@ int64_t file_to_bytes(const char *path, unsigned char **buf_out) {
}
char *file_to_str(const char *path) {
char *file_to_str(const char *path, size_t *out_len) {
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
if (handle == NULL) {
@ -151,23 +145,114 @@ char *file_to_str(const char *path) {
str_out[len] = '\0';
if (out_len)
*out_len = len;
return str_out;
}
static char **read_files;
char const *file_read(char const *file) {
char *s = file_to_str(file);
struct ImageCollectionData {
char *list;
size_t length;
};
static PHYSFS_EnumerateCallbackResult image_collection_callback(void *data,
const char *origdir,
const char *fname)
{
struct ImageCollectionData *result = data;
(void)origdir;
if (s) arrpush(read_files, s);
/* TODO: settle down on supported images */
size_t file_len = SDL_strlen(fname);
size_t dir_len = SDL_strlen(origdir);
return s;
if (CHECK_ENDING(fname, file_len, ".png")) {
result->list = SDL_realloc(result->list, result->length + dir_len + file_len + 1);
if (result->length != 0)
result->list[result->length-1] = '\n';
SDL_strlcpy(&result->list[result->length], origdir, dir_len);
result->length += dir_len;
result->list[result->length-1] = '/';
SDL_strlcpy(&result->list[result->length], fname, file_len + 1);
result->length += file_len + 1;
result->list[result->length-1] = '\0';
}
return PHYSFS_ENUM_OK;
}
/* TODO: caching on file and operation inputs */
static String *read_files;
String file_read(char const *file, char const *operation) {
if (!file) {
log_warn("No file specified");
return (String){NULL, 0};
}
if (!operation) {
log_warn("No operation specified");
return (String){NULL, 0};
}
if (SDL_strncmp(operation, ":string", sizeof (":string") - 1) == 0) {
size_t length;
char *data = file_to_str(file, &length);
if (!data)
return (String){NULL, 0};
String s = {data, (float)length};
arrpush(read_files, s);
return s;
} else if (SDL_strncmp(operation, ":binary", sizeof (":binary") - 1) == 0) {
uint8_t *data;
int64_t length = file_to_bytes(file, &data);
if (length == -1)
return (String){NULL, 0};
String s = {(char *)data, (float)length};
arrpush(read_files, s);
return s;
} else if (SDL_strncmp(operation, ":exists", sizeof (":exists") - 1) == 0) {
bool exists = file_exists(file);
return exists ? (String){"yes", 3} : (String){"no", 2};
} else if (SDL_strncmp(operation, ":images", sizeof (":images") - 1) == 0) {
struct ImageCollectionData list = {0};
if (PHYSFS_enumerate(file, image_collection_callback, &list) == 0) {
CRY_PHYSFS("Error enumerating images");
if (list.list) SDL_free(list.list);
return (String){NULL, 0};
}
if (list.list == NULL)
return (String){NULL, 0};
String s = {(char *)list.list, (float)list.length - 1};
arrpush(read_files, s);
return s;
} else
log_warn("No valid operation specified by %s", operation);
return (String){NULL, 0};
}
void file_read_garbage_collect(void) {
for (size_t i = 0; i < arrlenu(read_files); ++i)
SDL_free(read_files[i]);
for (size_t i = 0; i < arrlenu(read_files); ++i) {
SDL_assert_always(read_files[i].data && read_files[i].length >= 0);
SDL_free(read_files[i].data);
}
arrfree(read_files);
read_files = NULL;
}
@ -342,6 +427,18 @@ void profile_list_stats(void) {
}
}
void log_string(char const *value, char const *message) {
if (!value) return;
if (!message)
log_info("%s", value);
else
log_info("%s: %s", message, value);
}
void log_float(float value, char const *message) {
if (!message) message = "float";
log_info("%s = %f", message, (double)value);
}
void log_vec2(Vec2 vector, char const *message) {
if (!message) message = "Vec2";

View File

@ -21,6 +21,12 @@ void cry_impl(const char *file, const int line, const char *title, const char *t
#define CRY_PHYSFS(title) \
cry_impl(__FILE__, __LINE__, title, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()))
#define CHECK_ENDING(p_s, p_ss, p_e) ((p_ss) >= sizeof p_e && SDL_strncmp(&((p_s)[(p_ss) - (sizeof p_e - 1)]), p_e, sizeof p_e - 1) == 0)
void log_info(const char *restrict format, ...);
void log_critical(const char *restrict format, ...);
void log_warn(const char *restrict format, ...);
/* for when there's absolutely no way to continue */
_Noreturn void die_abruptly(void);
@ -31,6 +37,23 @@ void profile_list_stats(void);
void file_read_garbage_collect(void);
/* sets buf_out to a pointer to a byte buffer which must be freed. */
/* returns the size of this buffer. */
int64_t file_to_bytes(const char *path, unsigned char **buf_out);
/* returns a pointer to a string which must be freed */
char *file_to_str(const char *path, size_t *out_len);
/* saves all texture atlases as BMP files in the write directory */
void textures_dump_atlases(void);
bool file_exists(const char *path);
static inline float clampf(float f, float min, float max) {
const float t = f < min ? min : f;
return t > max ? max : t;
}
/* http://www.azillionmonkeys.com/qed/sqroot.html */
static inline float fast_sqrt(float x)
{

98
src/twn_vec_c.h Normal file
View File

@ -0,0 +1,98 @@
#ifndef TWN_VEC_C_H
#define TWN_VEC_C_H
#include "twn_types_c.h"
#include <stdint.h>
static inline Vec4 vec4_scale(Vec4 a, float s) {
return (Vec4) { a.x * s, a.y * s, a.z * s, a.w * s };
}
static inline Matrix4 matrix_multiply(Matrix4 a, Matrix4 b) {
Matrix4 dest;
float const
a00 = a.row[0].x, a01 = a.row[0].y, a02 = a.row[0].z, a03 = a.row[0].w,
a10 = a.row[1].x, a11 = a.row[1].y, a12 = a.row[1].z, a13 = a.row[1].w,
a20 = a.row[2].x, a21 = a.row[2].y, a22 = a.row[2].z, a23 = a.row[2].w,
a30 = a.row[3].x, a31 = a.row[3].y, a32 = a.row[3].z, a33 = a.row[3].w,
b00 = b.row[0].x, b01 = b.row[0].y, b02 = b.row[0].z, b03 = b.row[0].w,
b10 = b.row[1].x, b11 = b.row[1].y, b12 = b.row[1].z, b13 = b.row[1].w,
b20 = b.row[2].x, b21 = b.row[2].y, b22 = b.row[2].z, b23 = b.row[2].w,
b30 = b.row[3].x, b31 = b.row[3].y, b32 = b.row[3].z, b33 = b.row[3].w;
dest.row[0].x = a00 * b00 + a10 * b01 + a20 * b02 + a30 * b03;
dest.row[0].y = a01 * b00 + a11 * b01 + a21 * b02 + a31 * b03;
dest.row[0].z = a02 * b00 + a12 * b01 + a22 * b02 + a32 * b03;
dest.row[0].w = a03 * b00 + a13 * b01 + a23 * b02 + a33 * b03;
dest.row[1].x = a00 * b10 + a10 * b11 + a20 * b12 + a30 * b13;
dest.row[1].y = a01 * b10 + a11 * b11 + a21 * b12 + a31 * b13;
dest.row[1].z = a02 * b10 + a12 * b11 + a22 * b12 + a32 * b13;
dest.row[1].w = a03 * b10 + a13 * b11 + a23 * b12 + a33 * b13;
dest.row[2].x = a00 * b20 + a10 * b21 + a20 * b22 + a30 * b23;
dest.row[2].y = a01 * b20 + a11 * b21 + a21 * b22 + a31 * b23;
dest.row[2].z = a02 * b20 + a12 * b21 + a22 * b22 + a32 * b23;
dest.row[2].w = a03 * b20 + a13 * b21 + a23 * b22 + a33 * b23;
dest.row[3].x = a00 * b30 + a10 * b31 + a20 * b32 + a30 * b33;
dest.row[3].y = a01 * b30 + a11 * b31 + a21 * b32 + a31 * b33;
dest.row[3].z = a02 * b30 + a12 * b31 + a22 * b32 + a32 * b33;
dest.row[3].w = a03 * b30 + a13 * b31 + a23 * b32 + a33 * b33;
return dest;
}
static inline Matrix4 matrix_inverse(Matrix4 mat) {
Matrix4 dest;
float const
a = mat.row[0].x, b = mat.row[0].y, c = mat.row[0].z, d = mat.row[0].w,
e = mat.row[1].x, f = mat.row[1].y, g = mat.row[1].z, h = mat.row[1].w,
i = mat.row[2].x, j = mat.row[2].y, k = mat.row[2].z, l = mat.row[2].w,
m = mat.row[3].x, n = mat.row[3].y, o = mat.row[3].z, p = mat.row[3].w,
c1 = k * p - l * o, c2 = c * h - d * g, c3 = i * p - l * m,
c4 = a * h - d * e, c5 = j * p - l * n, c6 = b * h - d * f,
c7 = i * n - j * m, c8 = a * f - b * e, c9 = j * o - k * n,
c10 = b * g - c * f, c11 = i * o - k * m, c12 = a * g - c * e,
idt = 1.0f/(c8*c1+c4*c9+c10*c3+c2*c7-c12*c5-c6*c11), ndt = -idt;
dest.row[0].x = (f * c1 - g * c5 + h * c9) * idt;
dest.row[0].y = (b * c1 - c * c5 + d * c9) * ndt;
dest.row[0].z = (n * c2 - o * c6 + p * c10) * idt;
dest.row[0].w = (j * c2 - k * c6 + l * c10) * ndt;
dest.row[1].x = (e * c1 - g * c3 + h * c11) * ndt;
dest.row[1].y = (a * c1 - c * c3 + d * c11) * idt;
dest.row[1].z = (m * c2 - o * c4 + p * c12) * ndt;
dest.row[1].w = (i * c2 - k * c4 + l * c12) * idt;
dest.row[2].x = (e * c5 - f * c3 + h * c7) * idt;
dest.row[2].y = (a * c5 - b * c3 + d * c7) * ndt;
dest.row[2].z = (m * c6 - n * c4 + p * c8) * idt;
dest.row[2].w = (i * c6 - j * c4 + l * c8) * ndt;
dest.row[3].x = (e * c9 - f * c11 + g * c7) * ndt;
dest.row[3].y = (a * c9 - b * c11 + c * c7) * idt;
dest.row[3].z = (m * c10 - n * c12 + o * c8) * ndt;
dest.row[3].w = (i * c10 - j * c12 + k * c8) * idt;
return dest;
}
static inline Vec4 matrix_vector_multiply(Matrix4 m, Vec4 v) {
Vec4 res;
res.x = m.row[0].x * v.x + m.row[1].x * v.y + m.row[2].x * v.z + m.row[3].x * v.w;
res.y = m.row[0].y * v.x + m.row[1].y * v.y + m.row[2].y * v.z + m.row[3].y * v.w;
res.z = m.row[0].z * v.x + m.row[1].z * v.y + m.row[2].z * v.z + m.row[3].z * v.w;
res.w = m.row[0].w * v.x + m.row[1].w * v.y + m.row[2].w * v.z + m.row[3].w * v.w;
return res;
}
#endif