refactor, splitting code into files
This commit is contained in:
parent
6bf75f87ab
commit
77e1080c24
4 changed files with 96 additions and 46 deletions
72
src/main.zig
72
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{
|
||||
|
|
@ -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));
|
||||
// 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);
|
||||
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));
|
||||
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);
|
||||
|
|
|
|||
26
src/world/generation.zig
Normal file
26
src/world/generation.zig
Normal file
|
|
@ -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;
|
||||
}
|
||||
32
src/world/render.zig
Normal file
32
src/world/render.zig
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
6
test.zig
Normal file
6
test.zig
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn main() !void {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("hello world!\n", .{});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue