summaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 105b673950f858c3d4654e98801a066b86ac5669 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const std = @import("std");
const rl = @import("raylib");
const assert = std.debug.assert;

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
    //--------------------------------------------------------------------------------------

    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
        //----------------------------------------------------------------------------------
        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(rl.Color.sky_blue);

        {
            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;
    }
};