generate + render cheese using linked lists as base
This commit is contained in:
parent
f0430ea354
commit
331aa6b52d
2 changed files with 38 additions and 24 deletions
56
src/main.zig
56
src/main.zig
|
|
@ -5,6 +5,7 @@ const raylib = raylib_helper.raylib;
|
|||
const v3 = raylib_helper.v3;
|
||||
|
||||
const chunks = @import("world/chunk.zig");
|
||||
const Chunk = chunks.Chunk;
|
||||
const generation = @import("world/generation.zig");
|
||||
const render = @import("world/render.zig");
|
||||
const ModelData = render.ModelData;
|
||||
|
|
@ -81,33 +82,46 @@ pub fn main() !void {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
// generate array list of chunk pointers
|
||||
var pointer_list_models = try std.ArrayList(?*ModelData).initCapacity(a7r, 1000);
|
||||
// all of the generated chunks
|
||||
var chunk_list = std.DoublyLinkedList(?*Chunk){};
|
||||
defer {
|
||||
// Free each model stored
|
||||
for (pointer_list_models.items) |maybe_ptr| {
|
||||
if (maybe_ptr) |ptr| {
|
||||
a7r.destroy(ptr);
|
||||
}
|
||||
// cleanup
|
||||
while (chunk_list.popFirst()) |node| {
|
||||
a7r.destroy(node);
|
||||
}
|
||||
|
||||
pointer_list_models.deinit();
|
||||
}
|
||||
|
||||
|
||||
// generate array list of rendered chunk pointers
|
||||
var render_queue = std.DoublyLinkedList(?*ModelData){};
|
||||
defer {
|
||||
// cleanup
|
||||
while (chunk_list.popFirst()) |node| {
|
||||
a7r.destroy(node);
|
||||
}
|
||||
}
|
||||
|
||||
const seed = 564553;
|
||||
inline for (0..10) |x| for (0..3) |y| for (0..10) |z| {
|
||||
inline for (0..25) |x| for (0..16) |y| for (0..25) |z| {
|
||||
const position = v3.new(@floatFromInt(x), @floatFromInt(y), @floatFromInt(z));
|
||||
|
||||
// generate chunk
|
||||
const chunk = try generation.generate_chunk(a7r, position, seed, @divTrunc(seed, 2), 0.1);
|
||||
defer chunk.deinit();
|
||||
const chunk_ptr = try a7r.create(Chunk);
|
||||
chunk_ptr.* = chunk;
|
||||
|
||||
const chunk_node = try a7r.create(std.DoublyLinkedList(?*Chunk).Node);
|
||||
chunk_node.* = .{ .data = chunk_ptr };
|
||||
chunk_list.append(chunk_node);
|
||||
|
||||
// generate model out of chunk
|
||||
const model = try render.chunk_to_model(chunk, texture, shader, tile_rows, tile_columns);
|
||||
|
||||
// Allocate memory for the model on the heap
|
||||
const model_ptr = try a7r.create(ModelData);
|
||||
model_ptr.* = model;
|
||||
|
||||
try pointer_list_models.append(model_ptr);
|
||||
|
||||
const model_node = try a7r.create(std.DoublyLinkedList(?*ModelData).Node);
|
||||
model_node.* = .{ .data = model_ptr };
|
||||
render_queue.append(model_node);
|
||||
};
|
||||
|
||||
// if (benchmark_chunk_meshing) {
|
||||
|
|
@ -155,10 +169,10 @@ pub fn main() !void {
|
|||
defer raylib.EndDrawing();
|
||||
|
||||
// draw models of chunks
|
||||
for (pointer_list_models.items) |model_data_pointer| {
|
||||
if (model_data_pointer != null) {
|
||||
try render.draw_model(model_data_pointer.?.*, camera, shader);
|
||||
}
|
||||
var it = render_queue.first;
|
||||
while (it) |node| {
|
||||
try render.draw_model(node.data.?.*, camera, shader);
|
||||
it = node.next;
|
||||
}
|
||||
|
||||
raylib.DrawFPS(10, 10);
|
||||
|
|
|
|||
|
|
@ -21,10 +21,10 @@ pub fn generate_chunk(a7r: A7r, position: Vector3, seed: i32, tile_seed: i32, fr
|
|||
const xf: f32 = position.x * 32 + @as(f32, @floatFromInt(raw_x));
|
||||
const yf: f32 = position.y * 32 + @as(f32, @floatFromInt(raw_y));
|
||||
const zf: f32 = position.z * 32 + @as(f32, @floatFromInt(raw_z));
|
||||
const height: f32 = height_generator.noise2(xf, zf) * 50 + 50;
|
||||
//std.debug.print("x: {} y: {} z: {}", .{position.x, position.y, position.z});
|
||||
// const height: f32 = height_generator.noises(xf, zf) * 50 + 50;
|
||||
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 (height >= yf) chunk.setTile(x, y, z, tile_type);
|
||||
if (height_generator.noise3(xf, yf, zf) > 0) chunk.setTile(x, y, z, tile_type);
|
||||
};
|
||||
|
||||
return chunk;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue