-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add root module, threaded buffer to file storage
- Loading branch information
1 parent
a4d42df
commit dbbe873
Showing
16 changed files
with
233 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const std = @import("std"); | ||
|
||
pub fn build(b: *std.Build) void { | ||
const target = b.standardTargetOptions(.{}); | ||
const optimize = b.standardOptimizeOption(.{}); | ||
|
||
const exe = b.addExecutable(.{ | ||
.name = "zipper.opengl", | ||
.root_source_file = b.path("src/main.zig"), | ||
.target = target, | ||
.optimize = optimize, | ||
}); | ||
|
||
b.installArtifact(exe); | ||
|
||
// zglfw: zig-gamedev | ||
const zglfw = b.dependency("zglfw", .{}); | ||
exe.root_module.addImport("zglfw", zglfw.module("root")); | ||
exe.linkLibrary(zglfw.artifact("glfw")); | ||
|
||
// OpenGL bindings: zigglgen | ||
const gl_bindings = @import("zigglgen").generateBindingsModule(b, .{ | ||
.api = .gl, | ||
.version = .@"4.0", | ||
.profile = .core, | ||
.extensions = &.{} | ||
}); | ||
exe.root_module.addImport("gl", gl_bindings); | ||
|
||
// Zipper | ||
const zipper = b.dependency("zipper", .{}); | ||
exe.root_module.addImport("zipper", zipper.module("root")); | ||
|
||
const run_cmd = b.addRunArtifact(exe); | ||
run_cmd.step.dependOn(b.getInstallStep()); | ||
const run_step = b.step("run", "Run the app"); | ||
run_step.dependOn(&run_cmd.step); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.{ | ||
.name = "zig.opengl", | ||
.version = "0.0.0", | ||
.dependencies = .{ | ||
.zipper = .{ | ||
.path = "../../", | ||
}, | ||
.zigglgen = .{ | ||
.path = "./libs/zigglgen", | ||
}, | ||
.zglfw = .{ | ||
.path = "./libs/zig-gamedev/libs/zglfw", | ||
}, | ||
}, | ||
.paths = .{ | ||
"build.zig", | ||
"build.zig.zon", | ||
"src", | ||
}, | ||
} |
Submodule zig-gamedev
added at
d96ecc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const std = @import("std"); | ||
const zglfw = @import("zglfw"); | ||
const gl = @import("gl"); | ||
const zipper = @import("zipper"); | ||
|
||
fn getProcAddress(prefixed_name: [*:0]const u8) ?gl.PROC { | ||
return @alignCast(zglfw.getProcAddress(std.mem.span(prefixed_name))); | ||
} | ||
|
||
pub fn main() !void { | ||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
defer arena.deinit(); | ||
const allocator = arena.allocator(); | ||
|
||
var procs: gl.ProcTable = undefined; | ||
|
||
try zglfw.init(); | ||
const window = try zglfw.Window.create(300, 300, "zipper-opengl", null); | ||
defer window.destroy(); | ||
zglfw.makeContextCurrent(window); | ||
|
||
if (!procs.init(getProcAddress)) return error.InitFailed; | ||
gl.makeProcTableCurrent(&procs); | ||
defer gl.makeProcTableCurrent(null); | ||
|
||
const size = window.getFramebufferSize(); | ||
const width = size[0]; | ||
const height = size[1]; | ||
|
||
var r: f32 = 0.0; | ||
var g: f32 = 0.0; | ||
var b: f32 = 0.0; | ||
|
||
var is_recording = false; | ||
var frame: usize = 0; | ||
var pixels: []u8 = try allocator.alloc(u8, @as(usize, @intCast(width)) * @as(usize, @intCast(height)) * 4); | ||
try zipper.init(allocator, "zig-opengl", "zipper-examples"); | ||
defer zipper.deinit(); | ||
|
||
while (!window.shouldClose() and window.getKey(.escape) != .press) { | ||
gl.ClearColor(r, g, b, 1.0); | ||
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); | ||
|
||
if (window.getKey(.r) == .press) { | ||
is_recording = true; | ||
} | ||
if (window.getKey(.s) == .press) { | ||
is_recording = false; | ||
} | ||
|
||
if (is_recording) { | ||
std.debug.print("\nSaving frame {d}...", .{ frame }); | ||
gl.ReadPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, @ptrCast(pixels[0..])); | ||
try zipper.putPixels(pixels[0..], width, height, frame); | ||
frame += 1; | ||
} | ||
|
||
zglfw.pollEvents(); | ||
window.swapBuffers(); | ||
|
||
r += 0.005; | ||
g += 0.007; | ||
b += 0.009; | ||
r = @mod(r, 1.0); | ||
g = @mod(g, 1.0); | ||
b = @mod(b, 1.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const std = @import("std"); | ||
const ImageData = @import("main.zig").ImageData; | ||
|
||
var allocator: std.mem.Allocator = undefined; | ||
var file_prefix: []const u8 = undefined; | ||
var foldername: []const u8 = undefined; | ||
|
||
const max_threads = 8; | ||
var buffers: [max_threads]std.ArrayList(ImageData) = undefined; | ||
var background_threads: std.ArrayList(std.Thread) = undefined; | ||
|
||
pub fn init(alloc: std.mem.Allocator, filename: []const u8, folder: []const u8) !void { | ||
allocator = alloc; | ||
file_prefix = filename; | ||
foldername = folder; | ||
background_threads = std.ArrayList(std.Thread).init(allocator); | ||
for(0..max_threads) |i| { | ||
buffers[i] = std.ArrayList(ImageData).init(allocator); | ||
const t = try std.Thread.spawn(.{}, sendPayloads, .{ i }); | ||
try background_threads.append(t); | ||
} | ||
} | ||
|
||
pub fn deinit() void { | ||
for (buffers) |b| b.deinit(); | ||
for(background_threads.items) |t| t.detach(); | ||
} | ||
|
||
pub fn putPixels(pixels: []u8, width: i32, height: i32, frame: usize) !void { | ||
const payload = ImageData { | ||
.frame_num = frame, | ||
.ext = "png", | ||
.filename = file_prefix, | ||
.foldername = foldername, | ||
.imageData = pixels, | ||
.width = width, | ||
.height = height, | ||
}; | ||
try buffers[@mod(frame, max_threads)].append(payload); | ||
} | ||
|
||
fn sendPayloads(buffer_idx: usize) !void { | ||
while(true) { | ||
if (buffers[buffer_idx].items.len == 0) continue; | ||
const payload = buffers[buffer_idx].pop(); | ||
std.debug.print("\n>>> Sending {s} #{d}", .{ payload.filename, payload.frame_num }); | ||
|
||
var string_stream = std.ArrayList(u8).init(allocator); | ||
defer string_stream.deinit(); | ||
try std.json.stringify(payload, .{}, string_stream.writer()); | ||
|
||
var body = std.ArrayList(u8).init(allocator); | ||
defer body.deinit(); | ||
|
||
var http_client = std.http.Client { .allocator = allocator }; | ||
defer http_client.deinit(); | ||
const request = try http_client.fetch(.{ | ||
.method = .PUT, | ||
.location = .{ .url = "http://127.0.0.1:8000" }, | ||
.response_storage = .{ | ||
.dynamic = &body, | ||
}, | ||
.payload = string_stream.items, | ||
}); | ||
if (request.status != .ok) { | ||
std.debug.print("\nError on sending request: {any}", .{ request.status }); | ||
} | ||
} | ||
} |