areas implementation
This commit is contained in:
		
							
								
								
									
										38
									
								
								src/areas.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/areas.zig
									
									
									
									
									
										Normal file
									
								
							@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
@@ -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;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										22
									
								
								src/main.zig
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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 } };
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								src/structures.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								src/structures.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
const AreaLocation = enum {
 | 
			
		||||
    TopLeft,
 | 
			
		||||
    TopRight,
 | 
			
		||||
    BottomLeft,
 | 
			
		||||
    BottomRihgt,
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user