-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdebugdraw.zig
More file actions
94 lines (74 loc) · 2.98 KB
/
debugdraw.zig
File metadata and controls
94 lines (74 loc) · 2.98 KB
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
const std = @import("std");
const delve = @import("delve");
const app = delve.app;
const debug = delve.debug;
const graphics = delve.platform.graphics;
const colors = delve.colors;
const images = delve.images;
const input = delve.platform.input;
const math = delve.math;
const modules = delve.modules;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub const test_asset = @embedFile("static/test_transparent.gif");
var time: f32 = 0.0;
var texture: graphics.Texture = undefined;
var test_image: images.Image = undefined;
// This example shows the simple debug drawing functions.
// These functions are slow, but a quick way to get stuff on screen!
pub fn main() !void {
// Pick the allocator to use depending on platform
const builtin = @import("builtin");
if (builtin.os.tag == .wasi or builtin.os.tag == .emscripten) {
// Web builds hack: use the C allocator to avoid OOM errors
// See https://github.com/ziglang/zig/issues/19072
try delve.init(std.heap.c_allocator);
} else {
// Using the default allocator will let us detect memory leaks
try delve.init(delve.mem.createDefaultAllocator());
}
try registerModule();
try app.start(app.AppConfig{ .title = "Delve Framework - Debug Draw Example" });
}
pub fn registerModule() !void {
const debugDrawExample = modules.Module{
.name = "debug_draw_example",
.init_fn = on_init,
.tick_fn = on_tick,
.draw_fn = on_draw,
.cleanup_fn = on_cleanup,
};
try modules.registerModule(debugDrawExample);
}
fn on_init() !void {
debug.log("Debug draw example module initializing", .{});
test_image = images.loadBytes(test_asset) catch {
debug.log("Could not load test texture", .{});
return;
};
texture = graphics.Texture.init(test_image);
graphics.setClearColor(colors.examples_bg_dark);
}
fn on_tick(delta: f32) void {
time += delta * 100.0;
if (input.isKeyJustPressed(.ESCAPE))
delve.platform.app.exit();
}
fn on_draw() void {
// Draw our debug cat image, but use the color override to tint it!
const r_ovr = std.math.sin(time * 0.006) + 0.5;
const g_ovr = std.math.sin(time * 0.008) + 0.5;
const b_ovr = std.math.sin(time * 0.01) + 0.5;
const a_ovr = std.math.sin(time * 0.02) - 0.5; // alpha channel controls how much tinting should occur
graphics.setDebugDrawColorOverride(colors.Color.new(r_ovr, g_ovr, b_ovr, a_ovr));
defer graphics.setDebugDrawColorOverride(colors.transparent); // reset when done!
graphics.drawDebugRectangle(texture, 120.0, 200.0, 100.0, 100.0, colors.white);
// Now draw some text
const scale = 1.5 + std.math.sin(time * 0.02) * 0.2;
graphics.setDebugTextScale(scale);
graphics.setDebugTextColor(colors.Color.new(1.0, std.math.sin(time * 0.02), 0.0, 1.0));
graphics.drawDebugText(4.0, 480.0, "This is from the debug draw module!");
}
fn on_cleanup() !void {
debug.log("Debug draw example module cleaning up", .{});
test_image.deinit();
}