From a9a37b5ca68e1123baae9064987dca8fbd50d610 Mon Sep 17 00:00:00 2001 From: Marto Date: Sun, 20 Jul 2025 13:53:41 +0200 Subject: [PATCH] areas implementation --- src/areas.zig | 38 ++++++++++++++++++++++++++++++++++++++ src/globals.zig | 16 ++++++++++++---- src/main.zig | 22 ++++++++++++++++++---- src/spawn-area.zig | 12 +++--------- src/structures.zig | 6 ++++++ 5 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 src/areas.zig create mode 100644 src/structures.zig diff --git a/src/areas.zig b/src/areas.zig new file mode 100644 index 0000000..7f0c625 --- /dev/null +++ b/src/areas.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const rl = @import("raylib"); +const spawn_area = @import("spawn-area.zig"); +const structures = @import("structures.zig"); + +const Areas = struct { + areas: []spawn_area.SpawnArea, + allocator: std.mem.Allocator, + + pub fn init(allocator: std.mem.Allocator, size: usize) !Areas { + const values = std.enums.values(structures.AreaLocation); + if (size > values.len) size = values.len; + + var used_areas = std.ArrayList(structures.AreaLocation).init(allocator); + defer used_areas.deinit(); + + // here we generate size random AreaLocation enums that aren't in used_areas + // the random generator will only generate AreaLocation enums that are not in + // + // todo implement function which will subtract values from used_areas (after RANDOM) + // based on this we will fill data with 0..size data and return the final value + + const data = try allocator.alloc(structures.AreaLocation, size); + // todo data must be set before returned + return Areas{ + .areas = data, + }; + } + + pub fn deinit(self: *Areas) void { + self.allocator.free(self.areas); + } + + pub fn draw(areas: *Areas) void { + // todo implement draw + _ = areas; + } +}; diff --git a/src/globals.zig b/src/globals.zig index b08f9d3..10da674 100644 --- a/src/globals.zig +++ b/src/globals.zig @@ -1,13 +1,21 @@ const rl = @import("raylib"); -pub var screen_width = 1920; -pub var screen_height = 1080; +var screen_width: i32 = 1920; +var screen_height: i32 = 1080; -fn setWindowSize(new_width: usize, new_height: usize) void { +pub fn getScreenWidth() i32 { + return screen_width; +} + +pub fn getScreenHeihgt() i32 { + return screen_height; +} + +pub fn setWindowSize(new_width: i32, new_height: i32) void { screen_width = new_width; screen_height = new_height; } -fn checkWindowSizeChanged(new_width: usize, new_height: usize) bool { +pub fn checkWindowSizeChanged(new_width: i32, new_height: i32) bool { return screen_width != new_width or screen_height != new_height; } diff --git a/src/main.zig b/src/main.zig index 84ff9d0..509f549 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,26 +1,40 @@ const std = @import("std"); const rl = @import("raylib"); +const globals = @import("globals.zig"); +const areas = @import("areas.zig"); pub fn main() !void { - const size = .{ 1920, 1080 }; + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); rl.setConfigFlags(.{ .msaa_4x_hint = true, .window_resizable = true, - .window_maximized = true, }); - rl.initWindow(size[0], size[1], "Promet SIM"); + rl.initWindow(globals.getScreenWidth, globals.getScreenHeihgt, "Promet SIM"); defer rl.closeWindow(); rl.maximizeWindow(); - rl.setTargetFPS(60); + var spAreas = try areas.Areas.init(allocator, 3); + defer spAreas.deinit(); + while (!rl.windowShouldClose()) { rl.beginDrawing(); defer rl.endDrawing(); + const new_width = rl.getScreenWidth(); + const new_height = rl.getScreenHeight(); + + if (globals.checkWindowSizeChanged(new_width, new_height)) { + globals.setWindowSize(new_width, new_height); + } + rl.clearBackground(.light_gray); + + // draw areas } } diff --git a/src/spawn-area.zig b/src/spawn-area.zig index b868aca..4064a94 100644 --- a/src/spawn-area.zig +++ b/src/spawn-area.zig @@ -1,18 +1,12 @@ const rl = @import("raylib"); +const structures = @import("structures.zig"); -const AreaLocation = enum { - TopLeft, - TopRight, - BottomLeft, - BottomRihgt, -}; - -const SpawnArea = struct { +pub const SpawnArea = struct { location: rl.Vector2, // cars size: .{ i32, i32 }, - pub fn init(loc: AreaLocation) SpawnArea { + pub fn init(loc: structures.AreaLocation) SpawnArea { return SpawnArea{ .location = loc, .size = .{ undefined, undefined } }; } }; diff --git a/src/structures.zig b/src/structures.zig new file mode 100644 index 0000000..d6fcee1 --- /dev/null +++ b/src/structures.zig @@ -0,0 +1,6 @@ +const AreaLocation = enum { + TopLeft, + TopRight, + BottomLeft, + BottomRihgt, +};