this repo has no description
at main 3.1 kB view raw
1const std = @import("std"); 2const lib = @import("zig_civ"); 3const rl = @import("raylib"); 4 5const screen_offset_x: f32 = 400.0; // center of 800px screen 6const screen_offset_y: f32 = 225.0; // center of 450px screen 7 8fn draw_hex(hex: lib.hex.HexCell) !void { 9 var corners: [6]lib.hex.Point = undefined; 10 for (0..6) |i| { 11 const corner = lib.hex.calculate_hex_corners(hex.center, hex.outer_radius, i); 12 corners[i] = corner; 13 } 14 15 for (0..6) |i| { 16 const start = corners[i]; 17 const end = corners[@mod(i + 1, 6)]; 18 19 const start_x: i32 = @intFromFloat(@round(start.x + screen_offset_x)); 20 const start_y: i32 = @intFromFloat(@round(start.y + screen_offset_y)); 21 const end_x: i32 = @intFromFloat(@round(end.x + screen_offset_x)); 22 const end_y: i32 = @intFromFloat(@round(end.y + screen_offset_y)); 23 24 rl.drawLine(start_x, start_y, end_x, end_y, .black); 25 } 26 27 const center_x: i32 = @intFromFloat(@round(hex.center.x + screen_offset_x - 10)); 28 const center_y: i32 = @intFromFloat(@round(hex.center.y + screen_offset_y - 6)); 29 30 var buf: [64]u8 = undefined; 31 const text = try std.fmt.bufPrintZ(&buf, "{d} {d}", .{ hex.cubic.q, hex.cubic.r }); 32 rl.drawText(text, center_x, center_y, 12, .black); 33} 34 35pub fn main() !void { 36 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 37 defer _ = gpa.deinit(); 38 const allocator = gpa.allocator(); 39 40 // Axial template: 7x7 grid, center at [3][3], indexed [r+3][q+3] 41 // Hexagon shape for radius 3 42 //const template = [7][7]u1{ 43 //// q: -3 -2 -1 0 1 2 3 44 //.{ 0, 0, 0, 1, 1, 1, 1 }, // r = -3 45 //.{ 0, 0, 1, 1, 1, 1, 1 }, // r = -2 46 //.{ 0, 1, 1, 1, 1, 1, 1 }, // r = -1 47 //.{ 1, 1, 1, 1, 1, 1, 1 }, // r = 0 48 //.{ 1, 1, 1, 1, 1, 1, 0 }, // r = 1 49 //.{ 1, 1, 1, 1, 1, 0, 0 }, // r = 2 50 //.{ 1, 1, 1, 1, 0, 0, 0 }, // r = 3 51 //}; 52 53 //const template_rombus = [7][7]u1{ 54 //.{ 1, 1, 1, 1, 1, 1, 1 }, 55 //.{ 1, 1, 1, 1, 1, 1, 1 }, 56 //.{ 1, 1, 1, 1, 1, 1, 1 }, 57 //.{ 1, 1, 1, 1, 1, 1, 1 }, 58 //.{ 1, 1, 1, 1, 1, 1, 1 }, 59 //.{ 1, 1, 1, 1, 1, 1, 1 }, 60 //.{ 1, 1, 1, 1, 1, 1, 1 }, 61 //}; 62 63 const template_left_triangle = [7][7]u1{ 64 .{ 1, 1, 1, 1, 1, 1, 1 }, 65 .{ 1, 1, 1, 1, 1, 1, 0 }, 66 .{ 1, 1, 1, 1, 1, 0, 0 }, 67 .{ 1, 1, 1, 1, 0, 0, 0 }, 68 .{ 1, 1, 1, 0, 0, 0, 0 }, 69 .{ 1, 1, 0, 0, 0, 0, 0 }, 70 .{ 1, 0, 0, 0, 0, 0, 0 }, 71 }; 72 73 const template = template_left_triangle; 74 75 var grid = try lib.world.generateFromTemplate(3, &template, 40.0, allocator); 76 defer grid.deinit(allocator); 77 78 // Initialization 79 const screenWidth = 800; 80 const screenHeight = 450; 81 const fps = 60; 82 83 rl.initWindow(screenWidth, screenHeight, "raylib.hex-zig [core] example - basic window"); 84 defer rl.closeWindow(); 85 86 rl.setTargetFPS(fps); 87 88 // Main game loop 89 while (!rl.windowShouldClose()) { 90 rl.beginDrawing(); 91 defer rl.endDrawing(); 92 93 rl.clearBackground(.white); 94 95 for (grid.items) |cell| { 96 try draw_hex(cell); 97 } 98 } 99}