From 77e1080c2486503dd91fa0218fc675cf97886e01 Mon Sep 17 00:00:00 2001 From: Radonchnk Date: Sun, 20 Apr 2025 00:12:04 +0100 Subject: [PATCH] refactor, splitting code into files --- src/main.zig | 78 +++++++++++++++++----------------------- src/world/generation.zig | 26 ++++++++++++++ src/world/render.zig | 32 +++++++++++++++++ test.zig | 6 ++++ 4 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 src/world/generation.zig create mode 100644 src/world/render.zig create mode 100644 test.zig diff --git a/src/main.zig b/src/main.zig index cf6ae68..bf01614 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3,9 +3,10 @@ const std = @import("std"); const raylib_helper = @import("lib_helpers/raylib_helper.zig"); const raylib = raylib_helper.raylib; const v3 = raylib_helper.v3; -const znoise = @import("znoise"); const chunks = @import("world/chunk.zig"); +const generation = @import("world/generation.zig"); +const render = @import("world/render.zig"); const TILE_TEXTURE_RESOLUTION = 16; @@ -46,7 +47,7 @@ pub fn main() !void { defer raylib.CloseWindow(); raylib.DisableCursor(); - raylib.SetWindowState(raylib.FLAG_VSYNC_HINT); + //raylib.SetWindowState(raylib.FLAG_VSYNC_HINT); raylib.SetWindowState(raylib.FLAG_FULLSCREEN_MODE); var camera = raylib.Camera3D{ @@ -59,8 +60,8 @@ pub fn main() !void { const tiles = raylib.LoadImage("resources/images/tiles.png"); const texture = raylib.LoadTextureFromImage(tiles); - const tile_rows = @divExact(@as(u32, @intCast(tiles.height)), TILE_TEXTURE_RESOLUTION); - const tile_columns = @divExact(@as(u32, @intCast(tiles.width)), TILE_TEXTURE_RESOLUTION); + const tile_rows = @divExact(@as(u32, @intCast(tiles.height)), TILE_TEXTURE_RESOLUTION); + const tile_columns = @divExact(@as(u32, @intCast(tiles.width)), TILE_TEXTURE_RESOLUTION); defer raylib.UnloadTexture(texture); raylib.UnloadImage(tiles); @@ -77,43 +78,31 @@ pub fn main() !void { //} raylib.SetShaderValue(shader, raylib.GetShaderLocation(shader, "textureTiling"), &.{ @as(f32, @floatFromInt(tile_columns)), @as(f32, @floatFromInt(tile_rows)) }, raylib.SHADER_UNIFORM_VEC2); - var chunk = try chunks.Chunk.init(a7r); + // genegate chunk + var chunk = try generation.generate_world(a7r, 413445, 3445, 0.1); defer chunk.deinit(); - const height_generator = znoise.FnlGenerator{ .seed = 413445 }; - const tile_type_generator = znoise.FnlGenerator{ .seed = 4435, .frequency = 0.1 }; - for (0..32) |raw_x| for (0..32) |raw_y| for (0..32) |raw_z| { - const x: u5 = @intCast(raw_x); - const y: u5 = @intCast(raw_y); - const z: u5 = @intCast(raw_z); - const xf: f32 = @floatFromInt(raw_x); - const yf: f32 = @floatFromInt(raw_y); - const zf: f32 = @floatFromInt(raw_z); - const height: f32 = (height_generator.noise2(xf, zf) + 1) * 16 + @as(f32, if(x > 24) 4.0 else 0.0) + @as(f32, if(z < 8) 4.0 else 0.0); - const tile_type: u32 = if (tile_type_generator.noise3(xf, yf, zf) > 0) 1 else 2; - if (height >= yf) chunk.setTile(x, y, z, tile_type); - }; - - if (benchmark_chunk_meshing) { - var tmp: u64 = 0; - for (0..500) |_| { - const start = try std.time.Instant.now(); - const model = raylib.LoadChunkModelFromMesh(try chunk.createMesh(tile_rows, tile_columns)); - defer raylib.UnloadChunkModel(model); - const end = try std.time.Instant.now(); - tmp += end.since(start); - } - std.debug.print("\nchunk meshing time: {d:.3}ms\n\n", .{ - @as(f64, @floatFromInt(tmp)) / std.time.ns_per_ms / 500, - }); - } - - const model = raylib.LoadChunkModelFromMesh(try chunk.createMesh(tile_rows, tile_columns)); + // generate chunk model for just created chunk + const model = try render.chunk_to_model(chunk, texture, shader, tile_rows, tile_columns); defer raylib.UnloadChunkModel(model); - model.materials[0].maps[raylib.MATERIAL_MAP_DIFFUSE].texture = texture; - model.materials[0].shader = shader; + + + // if (benchmark_chunk_meshing) { + // var tmp: u64 = 0; + // for (0..500) |_| { + // const start = try std.time.Instant.now(); + // const model = raylib.LoadChunkModelFromMesh(try chunk.createMesh(tile_rows, tile_columns)); + // defer raylib.UnloadChunkModel(model); + // const end = try std.time.Instant.now(); + // tmp += end.since(start); + // } + // std.debug.print("\nchunk meshing time: {d:.3}ms\n\n", .{ + // @as(f64, @floatFromInt(tmp)) / std.time.ns_per_ms / 500, + // }); + // } while (!raylib.WindowShouldClose()) { + raylib.ClearBackground(raylib.BLACK); const right = v3.neg(v3.nor(v3.cross(camera.up, v3.sub(camera.target, camera.position)))); @@ -142,16 +131,13 @@ pub fn main() !void { raylib.BeginDrawing(); defer raylib.EndDrawing(); - const model_position = v3.new(0, 0, 0); - - { - raylib.BeginMode3D(camera); - defer raylib.EndMode3D(); - raylib.BeginShaderMode(shader); - defer raylib.EndShaderMode(); - - raylib.DrawChunkModel(model, model_position, 0.5, raylib.WHITE); - } + // draw models of chunks + inline for (0..10) |x| for (0..10) |y| for (0..10) |z| { + const chunk_pos_x: u64 = @intCast(x); + const chunk_pos_y: u64 = @intCast(y); + const chunk_pos_z: u64 = @intCast(z); + try render.draw_model(model, camera, shader, chunk_pos_x, chunk_pos_y, chunk_pos_z); + }; raylib.DrawFPS(10, 10); try drawCameraPosition(camera, 10, 30); diff --git a/src/world/generation.zig b/src/world/generation.zig new file mode 100644 index 0000000..c896ebd --- /dev/null +++ b/src/world/generation.zig @@ -0,0 +1,26 @@ +const std = @import("std"); +const chunks = @import("../world/chunk.zig"); +const znoise = @import("znoise"); +const A7r = std.mem.Allocator; +//const Chunk = chunks.Chunk; + + +pub fn generate_world(a7r: A7r, seed: i32, tile_seed: i32, frequency: f32) !chunks.Chunk { + var chunk = try chunks.Chunk.init(a7r); + + const height_generator = znoise.FnlGenerator{ .seed = seed }; + const tile_type_generator = znoise.FnlGenerator{ .seed = tile_seed, .frequency = frequency }; + for (0..32) |raw_x| for (0..32) |raw_y| for (0..32) |raw_z| { + const x: u5 = @intCast(raw_x); + const y: u5 = @intCast(raw_y); + const z: u5 = @intCast(raw_z); + const xf: f32 = @floatFromInt(raw_x); + const yf: f32 = @floatFromInt(raw_y); + const zf: f32 = @floatFromInt(raw_z); + const height: f32 = height_generator.noise2(xf, zf) * 80; + const tile_type: u32 = if (tile_type_generator.noise3(xf, yf, zf) > 0) 1 else 2; + if (height >= yf) chunk.setTile(x, y, z, tile_type); + }; + + return chunk; +} \ No newline at end of file diff --git a/src/world/render.zig b/src/world/render.zig new file mode 100644 index 0000000..fec627d --- /dev/null +++ b/src/world/render.zig @@ -0,0 +1,32 @@ +const chunks = @import("../world/chunk.zig"); +const Chunk = chunks.Chunk; + +const raylib_helper = @import("../lib_helpers/raylib_helper.zig"); +const raylib = raylib_helper.raylib; +const ChunkModel = raylib.ChunkModel; +const Texture2D = raylib.Texture2D; +const Shader = raylib.Shader; +const Camera3D = raylib.Camera3D; +const v3 = raylib_helper.v3; + + +pub fn chunk_to_model(chunk: Chunk, texture: Texture2D, shader: Shader, tile_rows: u32, tile_columns: u32) !ChunkModel { + const model = raylib.LoadChunkModelFromMesh(try chunk.createMesh(tile_rows, tile_columns)); + model.materials[0].maps[raylib.MATERIAL_MAP_DIFFUSE].texture = texture; + model.materials[0].shader = shader; + + return model; +} + +pub fn draw_model(model: ChunkModel, camera: Camera3D, shader: Shader, chunk_pos_x: u64, chunk_pos_y: u64, chunk_pos_z: u64) !void { + + const model_position = v3.new(@floatFromInt(chunk_pos_x*16), @floatFromInt(chunk_pos_y*16), @floatFromInt(chunk_pos_z*16)); + + { + raylib.BeginMode3D(camera); + defer raylib.EndMode3D(); + raylib.BeginShaderMode(shader); + defer raylib.EndShaderMode(); + raylib.DrawChunkModel(model, model_position, 0.5, raylib.WHITE); + } +} \ No newline at end of file diff --git a/test.zig b/test.zig new file mode 100644 index 0000000..1b9c8c6 --- /dev/null +++ b/test.zig @@ -0,0 +1,6 @@ +const std = @import("std"); + +pub fn main() !void { + const stdout = std.io.getStdOut().writer(); + try stdout.print("hello world!\n", .{}); +} \ No newline at end of file