diff options
| author | Kyren223 <Kyren223@proton.me> | 2025-07-25 19:13:00 +0300 |
|---|---|---|
| committer | Kyren223 <Kyren223@proton.me> | 2025-07-25 19:13:00 +0300 |
| commit | 1388408a88a1f313e9f71b0b1d023390d73d3b7f (patch) | |
| tree | d09da300df887cad3bffbdc0a3693b2fa5b38746 | |
| parent | ddf14daac53f3a926bc5910e75f5bdfdf435c781 (diff) | |
Basic grid stuff
| -rw-r--r-- | build.zig | 6 | ||||
| -rw-r--r-- | src/main.zig | 156 |
2 files changed, 149 insertions, 13 deletions
@@ -1,4 +1,5 @@ const std = @import("std"); +const rlz = @import("raylib_zig"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); @@ -20,9 +21,12 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, + .platform = rlz.PlatformBackend.glfw, + .linux_display_backend = rlz.LinuxDisplayBackend.X11, + // See https://github.com/Not-Nik/raylib-zig/issues/219 // We can either have users download both, or maybe try disabling lld? - .shared = true, + // .shared = true, }); const raylib = raylib_dep.module("raylib"); // main raylib module diff --git a/src/main.zig b/src/main.zig index 5cfbe53..105b673 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,38 +1,170 @@ const std = @import("std"); const rl = @import("raylib"); +const assert = std.debug.assert; -pub fn main() anyerror!void { - // var frameArena = std.heap.ArenaAllocator.init(std.heap.page_allocator); - // const allocator = frameArena.allocator(); - // defer frameArena.deinit(); +const cell_size = 16; +const world_width = 16 * cell_size; +const world_height = 9 * cell_size; + +var screen_width: i32 = 1280; +var screen_height: i32 = 720; +pub fn main() anyerror!void { // Initialization //-------------------------------------------------------------------------------------- - const screenWidth = 800; - const screenHeight = 450; - rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window"); + rl.initWindow(screen_width, screen_height, "Hackathon"); defer rl.closeWindow(); // Close window and OpenGL context + // rl.setWindowState(.{ .window_resizable = true }); rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second rl.setExitKey(.caps_lock); //-------------------------------------------------------------------------------------- + var game = Game.init(); + // Main game loop while (!rl.windowShouldClose()) { // Detect window close button or ESC key // Update //---------------------------------------------------------------------------------- - // TODO: Update your variables here + game.process(); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- + game.render(); + //---------------------------------------------------------------------------------- + } +} + +const Cell = struct { + color: rl.Color = rl.Color.red, +}; + +const Wave = struct { + arena: std.heap.ArenaAllocator, + map: [world_height][world_width]Cell = [_][world_width]Cell{[_]Cell{Cell{}} ** world_width} ** world_height, +}; + +const Game = struct { + camera: rl.Camera2D, + frame_arena: std.heap.ArenaAllocator, + wave: Wave, + + fn init() Game { + return .{ + .camera = .{ + .target = .{ .x = 128, .y = 128 }, + .offset = .{ + .x = @as(f32, @floatFromInt(screen_width)) / 2, + .y = @as(f32, @floatFromInt(screen_height)) / 2, + }, + .rotation = 0, + .zoom = @as(f32, @floatFromInt(screen_height)) / world_height, + }, + .frame_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator), + .wave = .{ + .arena = std.heap.ArenaAllocator.init(std.heap.page_allocator), + }, + }; + } + + fn frameStart(self: *Game) void { + const reset_successful = self.frame_arena.reset(.retain_capacity); + assert(reset_successful); + } + + fn process(self: *Game) void { + std.debug.print("Delta time: {d}\n", .{rl.getFrameTime()}); + + self.updateCamera(); + } + + fn render(self: *Game) void { + const a = self.frame_arena.allocator(); + const map = self.wave.map; + const camera = self.camera; + rl.beginDrawing(); defer rl.endDrawing(); - rl.clearBackground(.white); + rl.clearBackground(rl.Color.sky_blue); - rl.drawText("Congrats! You created your first window!", 190, 200, 20, .light_gray); - //---------------------------------------------------------------------------------- + { + rl.beginMode2D(camera); + defer rl.endMode2D(); + + for (0..map.len) |y| { + for (0..map[0].len) |x| { + const cell = map[y][x]; + const isEven = (y + x) % 2 == 0; + if (isEven) { + rl.drawRectangle( + @intCast(x * cell_size), + @intCast(y * cell_size), + cell_size, + cell_size, + rl.Color.green, + ); + } else { + rl.drawRectangle( + @intCast(x * cell_size), + @intCast(y * cell_size), + cell_size, + cell_size, + cell.color, + ); + } + } + } + } + + rl.drawRectangle( + 10, + 10, + @divTrunc(screen_width, 5), + @divTrunc(screen_height, 5), + rl.fade(rl.Color.white, 0.6), + ); + rl.drawRectangleLines( + 10, + 10, + @divTrunc(screen_width, 5), + @divTrunc(screen_height, 5), + rl.Color.black, + ); + + const font_size = @divTrunc(screen_width, 80); + + const debug_info = std.fmt.allocPrintZ( + a, + \\FPS: {} + \\Screen: {}x{} + \\World: {}x{} ({}) + , + .{ rl.getFPS(), screen_width, screen_height, world_width, world_height, cell_size }, + ) catch return; + rl.drawText(debug_info, 20, 20, font_size, .black); } -} + + fn updateCamera(self: *Game) void { + screen_width = rl.getScreenWidth(); + screen_height = rl.getScreenHeight(); + + self.camera.offset = .{ + .x = @as(f32, @floatFromInt(screen_width)) / 2, + .y = @as(f32, @floatFromInt(screen_height)) / 2, + }; + self.camera.target = .{ + .x = world_width / 2, + .y = world_height / 2, + }; + + // Take the average between the ratios + // This avoids "cheating" by changing the ratio to an extreme value + // in order to see more terrain in a certain axis + const width_ratio = @as(f32, @floatFromInt(screen_width)) / world_width; + const height_ratio = @as(f32, @floatFromInt(screen_height)) / world_height; + self.camera.zoom = (width_ratio + height_ratio) / 2; + } +}; |
