forked from david-vanderson/dvui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandalone-sdl.zig
132 lines (107 loc) · 4.81 KB
/
standalone-sdl.zig
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
const std = @import("std");
const dvui = @import("dvui");
const Backend = @import("SDLBackend");
var gpa_instance = std.heap.GeneralPurposeAllocator(.{}){};
const gpa = gpa_instance.allocator();
const vsync = true;
var show_dialog_outside_frame: bool = false;
/// This example shows how to use the dvui for a normal application:
/// - dvui renders the whole application
/// - render frames only when needed
pub fn main() !void {
// init SDL backend (creates OS window)
var backend = try Backend.init(.{
.width = 500,
.height = 600,
.vsync = vsync,
.title = "DVUI Standalone Example",
});
defer backend.deinit();
// init dvui Window (maps onto a single OS window)
var win = try dvui.Window.init(@src(), 0, gpa, backend.backend());
defer win.deinit();
main_loop: while (true) {
// beginWait coordinates with waitTime below to run frames only when needed
var nstime = win.beginWait(backend.hasEvent());
// marks the beginning of a frame for dvui, can call dvui functions after this
try win.begin(nstime);
// send all SDL events to dvui for processing
const quit = try backend.addAllEvents(&win);
if (quit) break :main_loop;
try dvui_frame();
// marks end of dvui frame, don't call dvui functions after this
// - sends all dvui stuff to backend for rendering, must be called before renderPresent()
const end_micros = try win.end(.{});
// cursor management
backend.setCursor(win.cursorRequested());
// render frame to OS
backend.renderPresent();
// waitTime and beginWait combine to achieve variable framerates
const wait_event_micros = win.waitTime(end_micros, null);
backend.waitEventTimeout(wait_event_micros);
// Example of how to show a dialog from another thread (outside of win.begin/win.end)
if (show_dialog_outside_frame) {
show_dialog_outside_frame = false;
try dvui.dialog(@src(), .{ .window = &win, .modal = false, .title = "Dialog from Outside", .message = "This is a non modal dialog that was created outside win.begin()/win.end(), usually from another thread." });
}
}
}
fn dvui_frame() !void {
{
var m = try dvui.menu(@src(), .horizontal, .{ .background = true, .expand = .horizontal });
defer m.deinit();
if (try dvui.menuItemLabel(@src(), "File", .{ .submenu = true }, .{ .expand = .none })) |r| {
var fw = try dvui.popup(@src(), dvui.Rect.fromPoint(dvui.Point{ .x = r.x, .y = r.y + r.h }), .{});
defer fw.deinit();
if (try dvui.menuItemLabel(@src(), "Close Menu", .{}, .{}) != null) {
dvui.menuGet().?.close();
}
}
if (try dvui.menuItemLabel(@src(), "Edit", .{ .submenu = true }, .{ .expand = .none })) |r| {
var fw = try dvui.popup(@src(), dvui.Rect.fromPoint(dvui.Point{ .x = r.x, .y = r.y + r.h }), .{});
defer fw.deinit();
_ = try dvui.menuItemLabel(@src(), "Cut", .{}, .{});
_ = try dvui.menuItemLabel(@src(), "Copy", .{}, .{});
_ = try dvui.menuItemLabel(@src(), "Paste", .{}, .{});
}
}
var scroll = try dvui.scrollArea(@src(), .{}, .{ .expand = .both, .color_style = .window });
defer scroll.deinit();
var tl = try dvui.textLayout(@src(), .{}, .{ .expand = .horizontal, .font_style = .title_4 });
const lorem = "This example shows how to use dvui in a normal application.";
try tl.addText(lorem, .{});
tl.deinit();
var tl2 = try dvui.textLayout(@src(), .{}, .{ .expand = .horizontal });
try tl2.addText(
\\DVUI
\\- paints the entire window
\\- can show floating windows and dialogs
\\- example menu at the top of the window
\\- rest of the window is a scroll area
, .{});
try tl2.addText("\n\n", .{});
try tl2.addText("Framerate is variable and adjusts as needed for input events and animations.", .{});
try tl2.addText("\n\n", .{});
if (vsync) {
try tl2.addText("Framerate is capped by vsync.", .{});
} else {
try tl2.addText("Framerate is uncapped.", .{});
}
try tl2.addText("\n\n", .{});
try tl2.addText("Cursor is always being set by dvui.", .{});
tl2.deinit();
if (dvui.Examples.show_demo_window) {
if (try dvui.button(@src(), "Hide Demo Window", .{})) {
dvui.Examples.show_demo_window = false;
}
} else {
if (try dvui.button(@src(), "Show Demo Window", .{})) {
dvui.Examples.show_demo_window = true;
}
}
if (try dvui.button(@src(), "Show Dialog From\nOutside Frame", .{})) {
show_dialog_outside_frame = true;
}
// look at demo() for examples of dvui widgets, shows in a floating window
try dvui.Examples.demo();
}