Compare commits
1 Commits
bca66e3815
...
sdl-multiw
| Author | SHA1 | Date | |
|---|---|---|---|
| 03ae8eccb6 |
11
src/WindowData.zig
Normal file
11
src/WindowData.zig
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const WindowData = @This();
|
||||||
|
|
||||||
|
frame: u64,
|
||||||
|
|
||||||
|
pub fn init() WindowData {
|
||||||
|
return WindowData{
|
||||||
|
.frame = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
260
src/main.zig
260
src/main.zig
@@ -3,112 +3,224 @@ const builtin = @import("builtin");
|
|||||||
const dvui = @import("dvui");
|
const dvui = @import("dvui");
|
||||||
const SDLBackend = @import("sdl-backend");
|
const SDLBackend = @import("sdl-backend");
|
||||||
const sdl_c = SDLBackend.c;
|
const sdl_c = SDLBackend.c;
|
||||||
const Allocator = std.mem.Allocator;
|
const WindowData = @import("WindowData.zig");
|
||||||
const Color = dvui.Color;
|
const dbprint = std.debug.print;
|
||||||
|
|
||||||
pub fn main() !void {
|
const WindowContext = struct {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
backend: SDLBackend,
|
||||||
const allocator = gpa.allocator();
|
backend_id: u32,
|
||||||
|
window: dvui.Window,
|
||||||
|
title: [:0]u8,
|
||||||
|
id: usize,
|
||||||
|
data: WindowData,
|
||||||
|
|
||||||
var backend = try SDLBackend.initWindow(.{
|
fn init(allocator: std.mem.Allocator, id: usize) !*WindowContext {
|
||||||
|
var ctx = try allocator.create(WindowContext);
|
||||||
|
errdefer allocator.destroy(ctx);
|
||||||
|
|
||||||
|
ctx.id = id;
|
||||||
|
|
||||||
|
const title_bytes = try std.fmt.allocPrint(allocator, "My DVUI App #{d}", .{id});
|
||||||
|
defer allocator.free(title_bytes);
|
||||||
|
|
||||||
|
ctx.title = try allocator.allocSentinel(u8, title_bytes.len, 0);
|
||||||
|
@memcpy(ctx.title[0..title_bytes.len], title_bytes);
|
||||||
|
|
||||||
|
ctx.backend = try SDLBackend.initWindow(.{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.size = .{ .w = 800.0, .h = 600.0 },
|
.size = .{ .w = 800.0, .h = 600.0 },
|
||||||
.title = "My DVUI App",
|
.title = ctx.title,
|
||||||
.vsync = true,
|
.vsync = true,
|
||||||
});
|
});
|
||||||
defer backend.deinit();
|
errdefer destroyBackendKeepingSDL(&ctx.backend);
|
||||||
|
|
||||||
var win = try dvui.Window.init(@src(), allocator, backend.backend(), .{
|
ctx.backend_id = @intCast(sdl_c.SDL_GetWindowID(ctx.backend.window));
|
||||||
.theme = switch (backend.preferredColorScheme() orelse .light) {
|
|
||||||
|
const theme = switch (ctx.backend.preferredColorScheme() orelse .light) {
|
||||||
.light => dvui.Theme.builtin.adwaita_light,
|
.light => dvui.Theme.builtin.adwaita_light,
|
||||||
.dark => dvui.Theme.builtin.adwaita_dark,
|
.dark => dvui.Theme.builtin.adwaita_dark,
|
||||||
},
|
};
|
||||||
});
|
|
||||||
|
|
||||||
defer win.deinit();
|
ctx.window = try dvui.Window.init(@src(), allocator, ctx.backend.backend(), .{ .theme = theme });
|
||||||
|
errdefer ctx.window.deinit();
|
||||||
|
|
||||||
var interrupted = false;
|
ctx.data = WindowData.init();
|
||||||
|
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deinit(self: *WindowContext, allocator: std.mem.Allocator, last_backend: bool) void {
|
||||||
|
self.window.deinit();
|
||||||
|
|
||||||
|
if (last_backend) {
|
||||||
|
self.backend.deinit();
|
||||||
|
} else {
|
||||||
|
destroyBackendKeepingSDL(&self.backend);
|
||||||
|
}
|
||||||
|
|
||||||
|
allocator.free(self.title);
|
||||||
|
self.* = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn destroyBackendKeepingSDL(backend: *SDLBackend) void {
|
||||||
|
sdl_c.SDL_DestroyRenderer(backend.renderer);
|
||||||
|
sdl_c.SDL_DestroyWindow(backend.window);
|
||||||
|
backend.we_own_window = false;
|
||||||
|
backend.deinit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
if (@import("builtin").os.tag == .windows) {
|
||||||
|
// on windows graphical apps have no console, so output goes to nowhere - attach it manually. related: https://github.com/ziglang/zig/issues/4196
|
||||||
|
dvui.Backend.Common.windowsAttachConsole() catch {};
|
||||||
|
}
|
||||||
|
SDLBackend.enableSDLLogging();
|
||||||
|
std.log.info("SDL version: {f}", .{SDLBackend.getSDLVersion()});
|
||||||
|
|
||||||
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
defer _ = gpa.deinit();
|
||||||
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
|
var windows = try std.ArrayList(*WindowContext).initCapacity(allocator, 0);
|
||||||
|
defer {
|
||||||
|
for (windows.items, 0..) |ctx, i| {
|
||||||
|
const last = i + 1 == windows.items.len;
|
||||||
|
ctx.deinit(allocator, last);
|
||||||
|
allocator.destroy(ctx);
|
||||||
|
}
|
||||||
|
windows.deinit(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
var next_window_id: usize = 1;
|
||||||
|
const first_ctx = try WindowContext.init(allocator, next_window_id);
|
||||||
|
next_window_id += 1;
|
||||||
|
try windows.append(allocator, first_ctx);
|
||||||
|
|
||||||
main_loop: while (true) {
|
main_loop: while (true) {
|
||||||
// beginWait coordinates with waitTime below to run frames only when needed
|
if (windows.items.len == 0) break :main_loop;
|
||||||
const nstime = win.beginWait(interrupted);
|
|
||||||
|
|
||||||
// marks the beginning of a frame for dvui, can call dvui functions after this
|
const saw_events = try pumpEvents(&windows);
|
||||||
try win.begin(nstime);
|
|
||||||
|
|
||||||
// send all SDL events to dvui for processing
|
var min_wait_event_micros: u32 = std.math.maxInt(u32);
|
||||||
try backend.addAllEvents(&win);
|
var idx: usize = 0;
|
||||||
|
while (saw_events and idx < windows.items.len) {
|
||||||
|
const ctx = windows.items[idx];
|
||||||
|
|
||||||
// if dvui widgets might not cover the whole window, then need to clear
|
const flags = sdl_c.SDL_GetWindowFlags(ctx.backend.window);
|
||||||
// the previous frame's render
|
const occluded = (flags & (sdl_c.SDL_WINDOW_HIDDEN | sdl_c.SDL_WINDOW_MINIMIZED | sdl_c.SDL_WINDOW_OCCLUDED)) != 0;
|
||||||
_ = SDLBackend.c.SDL_SetRenderDrawColor(backend.renderer, 0, 0, 0, 255);
|
if (occluded) {
|
||||||
_ = SDLBackend.c.SDL_RenderClear(backend.renderer);
|
idx += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const keep_running = gui_frame();
|
const nstime = @max(ctx.window.frame_time_ns, ctx.backend.nanoTime());
|
||||||
if (!keep_running) break :main_loop;
|
|
||||||
|
|
||||||
// marks end of dvui frame, don't call dvui functions after this
|
try ctx.window.begin(nstime);
|
||||||
// - sends all dvui stuff to backend for rendering, must be called before renderPresent()
|
|
||||||
const end_micros = try win.end(.{});
|
_ = sdl_c.SDL_SetRenderDrawColor(ctx.backend.renderer, 0, 0, 0, 255);
|
||||||
|
_ = sdl_c.SDL_RenderClear(ctx.backend.renderer);
|
||||||
|
|
||||||
|
const keep_open = try gui_frame(ctx, allocator, &windows, &next_window_id);
|
||||||
|
if (!keep_open) {
|
||||||
|
closeWindow(&windows, allocator, idx);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const end = try ctx.window.end(.{});
|
||||||
|
|
||||||
// cursor management
|
// cursor management
|
||||||
try backend.setCursor(win.cursorRequested());
|
try ctx.backend.setCursor(ctx.window.cursorRequested());
|
||||||
try backend.textInputRect(win.textInputRequested());
|
try ctx.backend.textInputRect(ctx.window.textInputRequested());
|
||||||
|
|
||||||
// render frame to OS
|
// render frame to OS
|
||||||
try backend.renderPresent();
|
try ctx.backend.renderPresent();
|
||||||
|
|
||||||
// waitTime and beginWait combine to achieve variable framerates
|
const sleep_time = ctx.window.waitTime(end);
|
||||||
const wait_event_micros = win.waitTime(end_micros);
|
min_wait_event_micros = @min(min_wait_event_micros, sleep_time);
|
||||||
interrupted = try backend.waitEventTimeout(wait_event_micros);
|
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (min_wait_event_micros != std.math.maxInt(u32)) {
|
||||||
|
min_wait_event_micros = @max(min_wait_event_micros, 1 / 120 * std.time.us_per_s);
|
||||||
|
std.Thread.sleep(min_wait_event_micros * std.time.ns_per_us);
|
||||||
|
} else {
|
||||||
|
std.Thread.sleep(1 / 60 * std.time.ns_per_s);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (windows.items.len == 0) break :main_loop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gui_frame() bool {
|
fn closeWindow(windows: *std.ArrayList(*WindowContext), allocator: std.mem.Allocator, idx: usize) void {
|
||||||
for (dvui.events()) |*e| {
|
const last = windows.items.len == 1;
|
||||||
if (e.evt == .window and e.evt.window.action == .close) return false;
|
const ctx = windows.swapRemove(idx);
|
||||||
if (e.evt == .app and e.evt.app.action == .quit) return false;
|
ctx.deinit(allocator, last);
|
||||||
|
allocator.destroy(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eventWindowId(event: sdl_c.SDL_Event) ?u32 {
|
||||||
|
return switch (event.type) {
|
||||||
|
sdl_c.SDL_EVENT_KEY_DOWN => @intCast(event.key.windowID),
|
||||||
|
sdl_c.SDL_EVENT_KEY_UP => @intCast(event.key.windowID),
|
||||||
|
sdl_c.SDL_EVENT_TEXT_INPUT => @intCast(event.text.windowID),
|
||||||
|
sdl_c.SDL_EVENT_TEXT_EDITING => @intCast(event.edit.windowID),
|
||||||
|
sdl_c.SDL_EVENT_MOUSE_MOTION => @intCast(event.motion.windowID),
|
||||||
|
sdl_c.SDL_EVENT_MOUSE_BUTTON_DOWN => @intCast(event.button.windowID),
|
||||||
|
sdl_c.SDL_EVENT_MOUSE_BUTTON_UP => @intCast(event.button.windowID),
|
||||||
|
sdl_c.SDL_EVENT_MOUSE_WHEEL => @intCast(event.wheel.windowID),
|
||||||
|
sdl_c.SDL_EVENT_WINDOW_FIRST...sdl_c.SDL_EVENT_WINDOW_LAST => @intCast(event.window.windowID),
|
||||||
|
else => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pumpEvents(windows: *std.ArrayList(*WindowContext)) !bool {
|
||||||
|
var event: sdl_c.SDL_Event = undefined;
|
||||||
|
const poll_got_event = if (SDLBackend.sdl3) true else 1;
|
||||||
|
var saw_event = false;
|
||||||
|
|
||||||
|
while (sdl_c.SDL_PollEvent(&event) == poll_got_event) {
|
||||||
|
saw_event = true;
|
||||||
|
|
||||||
|
if (eventWindowId(event)) |wid| {
|
||||||
|
for (windows.items) |ctx| {
|
||||||
|
if (ctx.backend_id == wid) {
|
||||||
|
_ = try ctx.backend.addEvent(&ctx.window, event);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//std.debug.print("null with: {any}\n", .{event.type});
|
||||||
|
// broadcast events without a window target (like SDL_QUIT)
|
||||||
|
for (windows.items) |ctx| {
|
||||||
|
_ = try ctx.backend.addEvent(&ctx.window, event);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const root = dvui.box(
|
return saw_event;
|
||||||
@src(),
|
}
|
||||||
.{ .dir = .horizontal },
|
|
||||||
.{ .expand = .both, .background = true, .style = .window },
|
fn gui_frame(ctx: *WindowContext, allocator: std.mem.Allocator, windows: *std.ArrayList(*WindowContext), next_window_id: *usize) !bool {
|
||||||
);
|
for (ctx.window.events.items) |*e| {
|
||||||
|
if (e.evt == .window and e.evt.window.action == .close) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var root = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .both, .padding = dvui.Rect.all(12), .background = true, .style = .window });
|
||||||
defer root.deinit();
|
defer root.deinit();
|
||||||
|
|
||||||
// Левая панель с фиксированной шириной
|
dvui.label(@src(), "Window #{d}", .{ctx.id}, .{ .font_style = .title_2 });
|
||||||
{
|
dvui.label(@src(), "Open windows: {d}", .{windows.items.len}, .{});
|
||||||
var left_panel = dvui.box(@src(), .{ .dir = .vertical }, .{ .expand = .vertical, .min_size_content = .{ .w = 200 }, .background = true });
|
dvui.label(@src(), "Frame {d}, fps: {d}", .{ ctx.data.frame, dvui.FPS() }, .{});
|
||||||
defer left_panel.deinit();
|
|
||||||
|
|
||||||
dvui.label(@src(), "Tools", .{}, .{});
|
ctx.data.frame += 1;
|
||||||
_ = dvui.button(@src(), "Button", .{}, .{});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Правая панель - занимает оставшееся пространство
|
if (dvui.button(@src(), "New window", .{}, .{})) {
|
||||||
{
|
const id = next_window_id.*;
|
||||||
const back = dvui.box(
|
next_window_id.* += 1;
|
||||||
@src(),
|
const new_ctx = try WindowContext.init(allocator, id);
|
||||||
.{ .dir = .horizontal },
|
try windows.append(allocator, new_ctx);
|
||||||
.{ .expand = .both, .padding = dvui.Rect.all(12), .background = true },
|
|
||||||
);
|
|
||||||
defer back.deinit();
|
|
||||||
|
|
||||||
const fill_color = Color.white.opacity(0.5);
|
|
||||||
var right_panel = dvui.box(
|
|
||||||
@src(),
|
|
||||||
.{ .dir = .vertical },
|
|
||||||
.{
|
|
||||||
.expand = .both,
|
|
||||||
.background = true,
|
|
||||||
.corner_radius = dvui.Rect.all(10),
|
|
||||||
.color_fill = fill_color,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
defer right_panel.deinit();
|
|
||||||
|
|
||||||
dvui.label(@src(), "Canvas", .{}, .{ .gravity_x = 0.5 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user