149 lines
4.0 KiB
Zig
149 lines
4.0 KiB
Zig
const std = @import("std");
|
|
const gl = @cImport(@cInclude("GL/gl.h"));
|
|
const ow = @cImport(@cInclude("openwindow.h"));
|
|
|
|
const Renderer = @import("renderer.zig");
|
|
const Game = @import("game.zig");
|
|
const Grid = @import("grid.zig");
|
|
const Draw = @import("draw.zig");
|
|
|
|
const Transform = @import("transform.zig");
|
|
|
|
const math = @import("math.zig");
|
|
const common = @import("common.zig");
|
|
|
|
pub fn main() !void {
|
|
const window: *ow.Window = ow.openWindow("game", 1920, 1080);
|
|
defer ow.closeWindow(window);
|
|
|
|
var aa = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
defer aa.deinit();
|
|
const allocator = aa.allocator();
|
|
|
|
var renderer = try Renderer.init(allocator, @ptrCast(&window.width), @ptrCast(&window.height));
|
|
|
|
var g = try Game.init(&renderer, 4, 4);
|
|
|
|
var d = try Draw.init(&renderer);
|
|
try addCommonAttribs(&d);
|
|
|
|
try d.drawLineInstanced(
|
|
.{ .{ 0, 1004 }, .{ 0, -4 }},
|
|
&.{
|
|
.{ 0, 0 },
|
|
.{ 100, 0 },
|
|
.{ 200, 0 },
|
|
.{ 300, 0 },
|
|
.{ 400, 0 },
|
|
.{ 500, 0 },
|
|
.{ 600, 0 },
|
|
.{ 700, 0 },
|
|
.{ 800, 0 },
|
|
.{ 900, 0 },
|
|
.{ 1000, 0 },
|
|
},
|
|
4,
|
|
0,
|
|
common.Color.fromHex(0x000000FF)
|
|
);
|
|
|
|
try d.drawLineInstanced(
|
|
.{ .{ 1004, 0 }, .{ -4, 0 }},
|
|
&.{
|
|
.{ 0, 0 },
|
|
.{ 0, 100 },
|
|
.{ 0, 200 },
|
|
.{ 0, 300 },
|
|
.{ 0, 400 },
|
|
.{ 0, 500 },
|
|
.{ 0, 600 },
|
|
.{ 0, 700 },
|
|
.{ 0, 800 },
|
|
.{ 0, 900 },
|
|
.{ 0, 1000 },
|
|
},
|
|
4,
|
|
0,
|
|
common.Color.fromHex(0x000000FF)
|
|
);
|
|
|
|
try d.drawRectangle(.{
|
|
.{ 0, 0 },
|
|
.{ 100, 0 },
|
|
.{ 100, 100 },
|
|
.{ 0, 100 },
|
|
}, 0, common.Color.fromHex(0xFF0000FF),
|
|
);
|
|
|
|
try d.drawCircle(.{ 50, 50 },
|
|
0.01,
|
|
50,
|
|
1.0,
|
|
.clockwise,
|
|
common.Color.fromHex(0x00FF00FF)
|
|
);
|
|
|
|
var globalTransform = Transform.init();
|
|
|
|
while (!ow.windowKeyPressed(window, @ptrCast(@constCast(ow.WINDOW_KEY_ESC)))) {
|
|
ow.windowHandleEvents(window);
|
|
defer ow.windowDraw(window);
|
|
|
|
handleInput(window, &g);
|
|
|
|
const ortho = math.Mat4x4.orthographic(0.0, @floatFromInt(window.width), 0.0, @floatFromInt(window.height), -1.0, 1.0);
|
|
globalTransform.set(.orthographic, ortho);
|
|
globalTransform.set(.translation, math.Mat4x4.translation(g.camera.position));
|
|
globalTransform.set(.scale, math.Mat4x4.scale(g.camera.scale));
|
|
gl.glUniformMatrix4fv(@intCast(d.getUniform("uTransform").loc), 1, gl.GL_FALSE, &globalTransform.get().data[0][0]);
|
|
try renderer.renderDraw(&d);
|
|
}
|
|
}
|
|
|
|
fn addCommonAttribs(d: *Draw) !void {
|
|
|
|
// General
|
|
|
|
try d.addAttrib("aPos", .{
|
|
.size = 3,
|
|
.type = gl.GL_FLOAT,
|
|
.normalized = gl.GL_FALSE,
|
|
}, false);
|
|
try d.addAttrib("aColor", .{
|
|
.size = 4,
|
|
.type = gl.GL_FLOAT,
|
|
.normalized = gl.GL_FALSE,
|
|
}, false);
|
|
|
|
// Instanced
|
|
|
|
try d.addAttrib("aInstanceOffset", .{
|
|
.size = 3,
|
|
.type = gl.GL_FLOAT,
|
|
.normalized = gl.GL_FALSE,
|
|
}, true);
|
|
}
|
|
|
|
fn handleInput(window: *ow.Window, game: *Game) void {
|
|
const dt: f32 = @floatCast(ow.windowGetDeltaTime(window));
|
|
const speed = 300;
|
|
|
|
if (ow.windowKeyHeld(window, @ptrCast(@constCast(ow.WINDOW_KEY_W))))
|
|
game.camera.position[1] -= speed*dt;
|
|
|
|
if (ow.windowKeyHeld(window, @ptrCast(@constCast(ow.WINDOW_KEY_S))))
|
|
game.camera.position[1] += speed*dt;
|
|
|
|
if (ow.windowKeyHeld(window, @ptrCast(@constCast(ow.WINDOW_KEY_A))))
|
|
game.camera.position[0] += speed*dt;
|
|
|
|
if (ow.windowKeyHeld(window, @ptrCast(@constCast(ow.WINDOW_KEY_D))))
|
|
game.camera.position[0] -= speed*dt;
|
|
|
|
if (ow.windowKeyHeld(window, @ptrCast(@constCast(ow.WINDOW_KEY_Q))))
|
|
game.camera.scale += 2*dt;
|
|
|
|
if (ow.windowKeyHeld(window, @ptrCast(@constCast(ow.WINDOW_KEY_E))))
|
|
game.camera.scale -= 2*dt;
|
|
}
|