Compare commits

...

2 Commits

Author SHA1 Message Date
8b2dcb496e work on world storage 2025-10-11 00:39:26 +03:00
e58a4fd20c fix textures being drawn upside down on macos 2025-10-10 23:03:07 +03:00
6 changed files with 68 additions and 37 deletions

View File

@ -10,16 +10,8 @@ cam : Camera = .{
position = .{3.0, 5.0, 3.0} position = .{3.0, 5.0, 3.0}
}; };
world : World;
#scope_export #scope_export
game_init :: () { game_init :: () {
world.ground[500][500] = .GRASS;
world.ground[500][501] = .GRASS;
world.ground[500][499] = .GRASS;
world.ground[499][500] = .GRASS;
world.ground[500][500] = .GRASS;
world.ground[501][500] = .GRASS;
} }
game_ui :: () { game_ui :: () {
@ -32,5 +24,6 @@ game_tick :: () {
game_draw :: () { game_draw :: () {
create_set_cam_rendering_task(cam); create_set_cam_rendering_task(cam);
create_world_rendering_tasks(*world); curworld := get_current_world();
if curworld.valid then create_world_rendering_tasks(curworld.world);
} }

View File

@ -46,8 +46,6 @@ pointerHit : bool;
pointerX : s32; pointerX : s32;
pointerY : s32; pointerY : s32;
world : World;
show_trile_preview : bool = true; show_trile_preview : bool = true;
trile_preview_x : int = 0; trile_preview_x : int = 0;
trile_preview_y : int = 0; trile_preview_y : int = 0;
@ -86,8 +84,6 @@ get_level_editor_camera :: () -> Camera {
tick_level_editor_camera :: () { tick_level_editor_camera :: () {
if console_open_ignore_input then return; if console_open_ignore_input then return;
world.ground[500][500] = .GRASS;
if get_time() - lastInputTime > CAMERA_INACTIVE_TIME_TO_ORBIT { // idle rotating camera if get_time() - lastInputTime > CAMERA_INACTIVE_TIME_TO_ORBIT { // idle rotating camera
cameraRotation += cast(float) delta_time; cameraRotation += cast(float) delta_time;
} }
@ -176,12 +172,13 @@ tick_level_editor_camera :: () {
} }
draw_tacoma_tab :: (theme: *GR.Overall_Theme, total_r: GR.Rect) { draw_tacoma_tab :: (theme: *GR.Overall_Theme, total_r: GR.Rect) {
curworld := get_current_world();
r := total_r; r := total_r;
r.h = ui_h(3,0); r.h = ui_h(3,0);
#if HAS_TACOMA { #if HAS_TACOMA {
if GR.button(r, "Render with Tacoma", *theme.button_theme) { if GR.button(r, "Render with Tacoma", *theme.button_theme) {
cam := get_level_editor_camera(); cam := get_level_editor_camera();
gen_reference(tacomaResolution, tacomaResolution, .{tacomaExposure, tacomaContrast, tacomaSaturation}, .{cam.target, cam.position, tacomaSamples, true}, world); gen_reference(tacomaResolution, tacomaResolution, .{tacomaExposure, tacomaContrast, tacomaSaturation}, .{cam.target, cam.position, tacomaSamples, true}, curworld.world.*);
} }
r.y += r.h; r.y += r.h;
if current_screenshot.valid { if current_screenshot.valid {
@ -266,6 +263,7 @@ draw_tools_tab :: (theme: *GR.Overall_Theme, total_r: GR.Rect) {
} }
handle_tool_click :: (x: int, y: int, z: int) { handle_tool_click :: (x: int, y: int, z: int) {
curworld := get_current_world();
if editMode == { if editMode == {
case .TRILES; case .TRILES;
if editor_current_trile != null then add_trile(editor_current_trile.name, cast(float)x, cast(float)y, cast(float)z); if editor_current_trile != null then add_trile(editor_current_trile.name, cast(float)x, cast(float)y, cast(float)z);
@ -273,13 +271,14 @@ handle_tool_click :: (x: int, y: int, z: int) {
ray := get_mouse_ray(*get_level_editor_camera()); ray := get_mouse_ray(*get_level_editor_camera());
hit, point := ray_plane_collision_point(ray, 0, 100); hit, point := ray_plane_collision_point(ray, 0, 100);
if hit { if hit {
world.ground[floor(point.y).(int) + 500][floor(point.x).(int) + 500] = groundType; curworld.world.ground[floor(point.y).(int) + 500][floor(point.x).(int) + 500] = groundType;
} }
} }
} }
add_trile :: (name: string, x: float, y: float, z: float) { add_trile :: (name: string, x: float, y: float, z: float) {
curworld := get_current_world();
loose_float_comp :: (a: float, b: float) -> bool { loose_float_comp :: (a: float, b: float) -> bool {
return abs(a-b) < 0.001; return abs(a-b) < 0.001;
@ -288,7 +287,7 @@ add_trile :: (name: string, x: float, y: float, z: float) {
// Check if the position is already occupied. @ToDo: we would probably like to // Check if the position is already occupied. @ToDo: we would probably like to
// have some acceleration structure like a hashmap to speed up this check when // have some acceleration structure like a hashmap to speed up this check when
// we are checking for collisions. // we are checking for collisions.
for world.positions { for curworld.world.positions {
for v, idx: it.positions { for v, idx: it.positions {
if loose_float_comp(v.x, x) if loose_float_comp(v.x, x)
&& loose_float_comp(v.y, y) && loose_float_comp(v.y, y)
@ -299,7 +298,7 @@ add_trile :: (name: string, x: float, y: float, z: float) {
} }
} }
for world.positions { for curworld.world.positions {
if it.trileName == name { if it.trileName == name {
array_add(*it.positions, Vector4.{x,y,z,1}); array_add(*it.positions, Vector4.{x,y,z,1});
return; return;
@ -310,7 +309,7 @@ add_trile :: (name: string, x: float, y: float, z: float) {
}; };
array_add(*poses.positions, Vector4.{x,y,z,1}); array_add(*poses.positions, Vector4.{x,y,z,1});
array_add(*world.positions, poses); array_add(*curworld.world.positions, poses);
} @Command } @Command
@ -334,8 +333,10 @@ tick_level_editor :: () {
} }
draw_level_editor :: () { draw_level_editor :: () {
curworld := get_current_world();
if !curworld.valid then return;
create_set_cam_rendering_task(get_level_editor_camera()); create_set_cam_rendering_task(get_level_editor_camera());
create_world_rendering_tasks(*world); create_world_rendering_tasks(curworld.world);
} }
draw_level_editor_ui :: (theme: *GR.Overall_Theme) { draw_level_editor_ui :: (theme: *GR.Overall_Theme) {
@ -359,13 +360,14 @@ draw_level_editor_ui :: (theme: *GR.Overall_Theme) {
} }
r.y += tab_r.h; r.y += tab_r.h;
curworld := get_current_world();
if current_tab == { if current_tab == {
case .TOOLS; case .TOOLS;
draw_tools_tab(theme, r); draw_tools_tab(theme, r);
case .TACOMA; case .TACOMA;
draw_tacoma_tab(theme, r); draw_tacoma_tab(theme, r);
case .INFO; case .INFO;
autoedit(r, *world.conf, theme); if curworld.valid then autoedit(r, *curworld.world.conf, theme);
} }
draw_trile_picker(theme); draw_trile_picker(theme);
} }

View File

@ -54,3 +54,18 @@ get_render_size :: () -> (s32, s32) {
// wl, hl := get_low_res(w,h, 360); // wl, hl := get_low_res(w,h, 360);
return w, h; return w, h;
} }
flip_y_if_plat :: inline (v: Vector2) -> Vector2 {
return .{v.x, flip_if_plat(v.y)};
}
// Some platforms draw UVs in weird reverse way
// so this function here does the flip on those platforms
// so we don't need to do the platform check in many places.
flip_if_plat :: inline (n: float) -> float {
#if OS == .MACOS {
return 1 - n;
} else {
return n;
}
}

View File

@ -544,10 +544,10 @@ create_postprocess_pipeline :: () {
gPipelines.postprocess.pipeline = sg_make_pipeline(*pipeline); gPipelines.postprocess.pipeline = sg_make_pipeline(*pipeline);
quad_vertices : [16]float = .[ quad_vertices : [16]float = .[
-1.0, 1.0, 0.0, 1.0, // top-let -1.0, 1.0, 0.0, flip_if_plat(1.0), // top-let
-1.0, -1.0, 0.0, 0.0, // bottom-let -1.0, -1.0, 0.0, flip_if_plat(0.0), // bottom-let
1.0, -1.0, 1.0, 0.0, // bottom-right 1.0, -1.0, 1.0, flip_if_plat(0.0), // bottom-right
1.0, 1.0, 1.0, 1.0, // top-right 1.0, 1.0, 1.0, flip_if_plat(1.0), // top-right
]; ];
quad_indices : [6]u16 = .[ quad_indices : [6]u16 = .[
0, 1, 2, 0, 2, 3 0, 1, 2, 0, 2, 3

View File

@ -253,9 +253,9 @@ immediate_triangle :: (p0: Vector3, p1: Vector3, p2: Vector3, c0 := Vector4.{1,1
tri.uv[1] = nullUV; tri.uv[1] = nullUV;
tri.uv[2] = nullUV; tri.uv[2] = nullUV;
} else { } else {
tri.uv[0] = uv0; tri.uv[0] = flip_y_if_plat(uv0);
tri.uv[1] = uv1; tri.uv[1] = flip_y_if_plat(uv1);
tri.uv[2] = uv2; tri.uv[2] = flip_y_if_plat(uv2);
} }
arb_tri_add(tri); arb_tri_add(tri);

View File

@ -2,18 +2,38 @@
Pool :: #import "Pool"; Pool :: #import "Pool";
current_world : struct { Current_World :: struct {
world : World; world : *World;
pool : Pool.Pool; // A memory pool to allocate stuff for the lifetime of this level being active. pool : Pool.Pool; // A memory pool to allocate stuff for the lifetime of this level being active.
valid : bool = false; valid : bool = false;
}; };
world_table :: Table(string, World); current_world : Current_World;
world_table : Table(string, World);
#scope_export #scope_export
nworld :: (name: string) {
w : World;
table_set(*world_table, name, w);
} @Command;
lworld :: (name: string) {
if current_world.valid {
Pool.reset(*current_world.pool);
} else {
Pool.set_allocators(*current_world.pool);
}
worldptr := table_find_pointer(*world_table, name);
current_world.world = worldptr;
current_world.valid = true;
} @Command;
get_current_world :: () -> *Current_World {
return *current_world;
}
sworlds :: () { sworlds :: () {
Jaison :: #import "Jaison"; Jaison :: #import "Jaison";
worlds : [..]World; worlds : [..]World;
@ -33,8 +53,8 @@ lworlds :: () {
s := load_string_from_pack("./game/resources/worlds.json"); s := load_string_from_pack("./game/resources/worlds.json");
success, worlds := Jaison.json_parse_string(s, [..]World,, temp); success, worlds := Jaison.json_parse_string(s, [..]World,, temp);
for worlds { for worlds {
set_trile(sprint("%",it.name), trile_from_serialize_form(it)); // set_trile(sprint("%",it.name), trile_from_serialize_form(it));
print("Loaded %\n", it.name); // print("Loaded %\n", it.name);
} }
} @Command } @Command
@ -81,12 +101,13 @@ TrilePositions :: struct {
} }
Ground_Tile :: enum { Ground_Tile :: enum {
WATER = 0; WATER;
GRASS = 1; GRASS;
SAND = 2; SAND;
} }
World :: struct { World :: struct {
name : string;
conf : World_Config; conf : World_Config;
positions : [..]TrilePositions; positions : [..]TrilePositions;
ground : [1000][1000]Ground_Tile; ground : [1000][1000]Ground_Tile;