Skip to content

Commit 06aec42

Browse files
foxnneemidoots
authored andcommitted
core: windows: Get triangle showing, still freezing on run. Comment out more windows, set value of window back on presentFrame.
1 parent e15cbe6 commit 06aec42

File tree

3 files changed

+142
-158
lines changed

3 files changed

+142
-158
lines changed

examples/core-triangle/App.zig

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,44 @@ pub fn init(
2626
core.on_exit = app_mod.id.deinit;
2727

2828
const main_window = core.windows.getValue(core.main_window);
29-
if (main_window.native != null) {
30-
// if window.native is not null, the window is initialized
3129

32-
// Create our shader module
33-
const shader_module = main_window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
34-
defer shader_module.release();
35-
36-
// Blend state describes how rendered colors get blended
37-
const blend = gpu.BlendState{};
38-
39-
// Color target describes e.g. the pixel format of the window we are rendering to.
40-
const color_target = gpu.ColorTargetState{
41-
.format = core.windows.get(core.main_window, .framebuffer_format),
42-
.blend = &blend,
43-
};
44-
45-
// Fragment state describes which shader and entrypoint to use for rendering fragments.
46-
const fragment = gpu.FragmentState.init(.{
30+
// Create our shader module
31+
const shader_module = main_window.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
32+
defer shader_module.release();
33+
34+
// Blend state describes how rendered colors get blended
35+
const blend = gpu.BlendState{};
36+
37+
// Color target describes e.g. the pixel format of the window we are rendering to.
38+
const color_target = gpu.ColorTargetState{
39+
.format = main_window.framebuffer_format,
40+
.blend = &blend,
41+
};
42+
43+
// Fragment state describes which shader and entrypoint to use for rendering fragments.
44+
const fragment = gpu.FragmentState.init(.{
45+
.module = shader_module,
46+
.entry_point = "frag_main",
47+
.targets = &.{color_target},
48+
});
49+
50+
// Create our render pipeline that will ultimately get pixels onto the screen.
51+
const label = @tagName(mach_module) ++ ".init";
52+
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
53+
.label = label,
54+
.fragment = &fragment,
55+
.vertex = gpu.VertexState{
4756
.module = shader_module,
48-
.entry_point = "frag_main",
49-
.targets = &.{color_target},
50-
});
51-
52-
// Create our render pipeline that will ultimately get pixels onto the screen.
53-
const label = @tagName(mach_module) ++ ".init";
54-
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
55-
.label = label,
56-
.fragment = &fragment,
57-
.vertex = gpu.VertexState{
58-
.module = shader_module,
59-
.entry_point = "vertex_main",
60-
},
61-
};
62-
const pipeline = main_window.device.createRenderPipeline(&pipeline_descriptor);
63-
64-
// Store our render pipeline in our module's state, so we can access it later on.
65-
app.* = .{
66-
.title_timer = try mach.time.Timer.start(),
67-
.pipeline = pipeline,
68-
};
69-
}
57+
.entry_point = "vertex_main",
58+
},
59+
};
60+
const pipeline = main_window.device.createRenderPipeline(&pipeline_descriptor);
61+
62+
// Store our render pipeline in our module's state, so we can access it later on.
63+
app.* = .{
64+
.title_timer = try mach.time.Timer.start(),
65+
.pipeline = pipeline,
66+
};
7067
}
7168

7269
// TODO(object): window-title
@@ -101,9 +98,7 @@ pub fn tick(app: *App, core: *mach.Core) void {
10198
}
10299
}
103100

104-
var main_window = core.windows.getValue(core.main_window);
105-
106-
// Window is ready when device is not null
101+
const main_window = core.windows.getValue(core.main_window);
107102

108103
// Grab the back buffer of the swapchain
109104
// TODO(Core)

src/Core.zig

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ pub fn init(core: *Core) !void {
162162
pub fn initWindow(core: *Core, window_id: mach.ObjectID) !void {
163163
var core_window = core.windows.getValue(window_id);
164164
defer core.windows.setValueRaw(window_id, core_window);
165-
166165
core_window.instance = gpu.createInstance(null) orelse {
167166
log.err("failed to create GPU instance", .{});
168167
std.process.exit(1);
@@ -247,7 +246,6 @@ pub fn main(core: *Core, core_mod: mach.Mod(Core)) !void {
247246
if (core.on_exit == null) @panic("core.on_exit callback must be set");
248247

249248
try Platform.tick(core);
250-
251249
core_mod.run(core.on_tick.?);
252250
core_mod.call(.presentFrame);
253251

@@ -526,6 +524,7 @@ pub fn presentFrame(core: *Core, core_mod: mach.Mod(Core)) !void {
526524
var windows = core.windows.slice();
527525
while (windows.next()) |window_id| {
528526
var core_window = core.windows.getValue(window_id);
527+
defer core.windows.setValueRaw(window_id, core_window);
529528

530529
mach.sysgpu.Impl.deviceTick(core_window.device);
531530

@@ -556,16 +555,6 @@ pub fn presentFrame(core: *Core, core_mod: mach.Mod(Core)) !void {
556555
}
557556
}
558557

559-
// TODO(important): update this information in response to resize events rather than
560-
// after frame submission
561-
// var win = core.windows.getAll(core.main_window).?;
562-
// win.framebuffer_format = core.descriptor.format;
563-
// win.framebuffer_width = core.descriptor.width;
564-
// win.framebuffer_height = core.descriptor.height;
565-
// win.width = core.platform.size.width;
566-
// win.height = core.platform.size.height;
567-
// core.windows.setAll(core.main_window, win);
568-
569558
// Record to frame rate frequency monitor that a frame was finished.
570559
core.frame.tick();
571560

src/core/Windows.zig

Lines changed: 104 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,6 @@ fn initWindow(
238238

239239
_ = w.SetWindowLongPtrW(native_window, w.GWLP_USERDATA, @bitCast(@intFromPtr(&context)));
240240

241-
_ = w.ShowWindow(native_window, w.SW_SHOW);
242-
243241
restoreWindowPosition(core, window_id);
244242

245243
const size = getClientRect(core, window_id);
@@ -251,6 +249,7 @@ fn initWindow(
251249
core_window.native = native;
252250
core.windows.setValueRaw(window_id, core_window);
253251
try core.initWindow(window_id);
252+
_ = w.ShowWindow(native_window, w.SW_SHOW);
254253
}
255254

256255
// pub fn update(self: *Win32) !void {
@@ -262,116 +261,116 @@ fn initWindow(
262261
// }
263262
// }
264263

265-
pub fn setTitle(self: *Win32, title: [:0]const u8) void {
266-
const wtitle = std.unicode.utf8ToUtf16LeAllocZ(self.allocator, title) catch {
267-
self.state.oom.set();
268-
return;
269-
};
270-
defer self.allocator.free(wtitle);
271-
_ = w.SetWindowTextW(self.window, wtitle);
272-
}
273-
274-
pub fn setDisplayMode(self: *Win32, mode: DisplayMode) void {
275-
self.display_mode = mode;
276-
277-
switch (mode) {
278-
.windowed => {
279-
const window_style: w.WINDOW_STYLE = if (self.border) w.WS_OVERLAPPEDWINDOW else w.WS_POPUPWINDOW;
280-
const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 };
281-
282-
_ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style));
283-
_ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style));
284-
285-
restoreWindowPosition(self);
286-
},
287-
.fullscreen => {
288-
// TODO (win32) - change to use exclusive fullscreen using ChangeDisplaySetting
289-
290-
_ = w.GetWindowRect(self.window, &self.saved_window_rect);
291-
292-
const window_style = w.WINDOW_STYLE{ .POPUP = 1, .VISIBLE = 1 };
293-
const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 };
294-
295-
_ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style));
296-
_ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style));
297-
298-
const monitor = w.MonitorFromWindow(self.window, w.MONITOR_DEFAULTTONEAREST);
299-
var monitor_info: w.MONITORINFO = undefined;
300-
monitor_info.cbSize = @sizeOf(w.MONITORINFO);
301-
if (w.GetMonitorInfoW(monitor, &monitor_info) == w.TRUE) {
302-
_ = w.SetWindowPos(self.window, null, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, w.SWP_NOZORDER);
303-
}
304-
},
305-
.borderless => {
306-
_ = w.GetWindowRect(self.window, &self.saved_window_rect);
307-
308-
const window_style = w.WINDOW_STYLE{ .POPUP = 1, .VISIBLE = 1 };
309-
const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 };
310-
311-
_ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style));
312-
_ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style));
264+
// pub fn setTitle(self: *Win32, title: [:0]const u8) void {
265+
// const wtitle = std.unicode.utf8ToUtf16LeAllocZ(self.allocator, title) catch {
266+
// self.state.oom.set();
267+
// return;
268+
// };
269+
// defer self.allocator.free(wtitle);
270+
// _ = w.SetWindowTextW(self.window, wtitle);
271+
// }
313272

314-
const monitor = w.MonitorFromWindow(self.window, w.MONITOR_DEFAULTTONEAREST);
315-
var monitor_info: w.MONITORINFO = undefined;
316-
monitor_info.cbSize = @sizeOf(w.MONITORINFO);
317-
if (w.GetMonitorInfoW(monitor, &monitor_info) == w.TRUE) {
318-
_ = w.SetWindowPos(self.window, null, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, w.SWP_NOZORDER);
319-
}
320-
},
321-
}
322-
}
273+
// pub fn setDisplayMode(self: *Win32, mode: DisplayMode) void {
274+
// self.display_mode = mode;
275+
276+
// switch (mode) {
277+
// .windowed => {
278+
// const window_style: w.WINDOW_STYLE = if (self.border) w.WS_OVERLAPPEDWINDOW else w.WS_POPUPWINDOW;
279+
// const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 };
280+
281+
// _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style));
282+
// _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style));
283+
284+
// restoreWindowPosition(self);
285+
// },
286+
// .fullscreen => {
287+
// // TODO (win32) - change to use exclusive fullscreen using ChangeDisplaySetting
288+
289+
// _ = w.GetWindowRect(self.window, &self.saved_window_rect);
290+
291+
// const window_style = w.WINDOW_STYLE{ .POPUP = 1, .VISIBLE = 1 };
292+
// const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 };
293+
294+
// _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style));
295+
// _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style));
296+
297+
// const monitor = w.MonitorFromWindow(self.window, w.MONITOR_DEFAULTTONEAREST);
298+
// var monitor_info: w.MONITORINFO = undefined;
299+
// monitor_info.cbSize = @sizeOf(w.MONITORINFO);
300+
// if (w.GetMonitorInfoW(monitor, &monitor_info) == w.TRUE) {
301+
// _ = w.SetWindowPos(self.window, null, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, w.SWP_NOZORDER);
302+
// }
303+
// },
304+
// .borderless => {
305+
// _ = w.GetWindowRect(self.window, &self.saved_window_rect);
306+
307+
// const window_style = w.WINDOW_STYLE{ .POPUP = 1, .VISIBLE = 1 };
308+
// const window_ex_style = w.WINDOW_EX_STYLE{ .APPWINDOW = 1 };
309+
310+
// _ = w.SetWindowLongW(self.window, w.GWL_STYLE, @bitCast(window_style));
311+
// _ = w.SetWindowLongW(self.window, w.GWL_EXSTYLE, @bitCast(window_ex_style));
312+
313+
// const monitor = w.MonitorFromWindow(self.window, w.MONITOR_DEFAULTTONEAREST);
314+
// var monitor_info: w.MONITORINFO = undefined;
315+
// monitor_info.cbSize = @sizeOf(w.MONITORINFO);
316+
// if (w.GetMonitorInfoW(monitor, &monitor_info) == w.TRUE) {
317+
// _ = w.SetWindowPos(self.window, null, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top, monitor_info.rcMonitor.right - monitor_info.rcMonitor.left, monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top, w.SWP_NOZORDER);
318+
// }
319+
// },
320+
// }
321+
// }
323322

324-
pub fn setBorder(self: *Win32, value: bool) void {
325-
const overlappedwindow: i32 = @bitCast(w.WS_OVERLAPPEDWINDOW);
326-
const popupwindow: i32 = @bitCast(w.WS_POPUPWINDOW);
327-
_ = w.SetWindowLongW(self.window, w.GWL_STYLE, if (value) overlappedwindow else popupwindow);
328-
self.border = value;
329-
}
323+
// pub fn setBorder(self: *Win32, value: bool) void {
324+
// const overlappedwindow: i32 = @bitCast(w.WS_OVERLAPPEDWINDOW);
325+
// const popupwindow: i32 = @bitCast(w.WS_POPUPWINDOW);
326+
// _ = w.SetWindowLongW(self.window, w.GWL_STYLE, if (value) overlappedwindow else popupwindow);
327+
// self.border = value;
328+
// }
330329

331-
pub fn setHeadless(self: *Win32, value: bool) void {
332-
_ = w.ShowWindow(self.window, if (value) w.SW_HIDE else w.SW_SHOW);
333-
self.headless = value;
334-
}
330+
// pub fn setHeadless(self: *Win32, value: bool) void {
331+
// _ = w.ShowWindow(self.window, if (value) w.SW_HIDE else w.SW_SHOW);
332+
// self.headless = value;
333+
// }
335334

336-
pub fn setVSync(self: *Win32, mode: VSyncMode) void {
337-
self.vsync_mode = mode;
338-
}
335+
// pub fn setVSync(self: *Win32, mode: VSyncMode) void {
336+
// self.vsync_mode = mode;
337+
// }
339338

340-
pub fn setSize(self: *Win32, value: Size) void {
341-
// TODO (win32) - use AdjustClientRect to get correct client rect.
342-
_ = w.SetWindowPos(self.window, null, 0, 0, @as(i32, @intCast(value.width)), @as(i32, @intCast(value.height)), w.SET_WINDOW_POS_FLAGS{ .NOMOVE = 1, .NOZORDER = 1, .NOACTIVATE = 1 });
343-
self.size = value;
344-
}
339+
// pub fn setSize(self: *Win32, value: Size) void {
340+
// // TODO (win32) - use AdjustClientRect to get correct client rect.
341+
// _ = w.SetWindowPos(self.window, null, 0, 0, @as(i32, @intCast(value.width)), @as(i32, @intCast(value.height)), w.SET_WINDOW_POS_FLAGS{ .NOMOVE = 1, .NOZORDER = 1, .NOACTIVATE = 1 });
342+
// self.size = value;
343+
// }
345344

346-
pub fn setCursorMode(self: *Win32, mode: CursorMode) void {
347-
switch (mode) {
348-
.normal => while (w.ShowCursor(w.TRUE) < 0) {},
349-
.hidden => while (w.ShowCursor(w.FALSE) >= 0) {},
350-
.disabled => {},
351-
}
352-
self.cursor_mode = mode;
353-
}
345+
// pub fn setCursorMode(self: *Win32, mode: CursorMode) void {
346+
// switch (mode) {
347+
// .normal => while (w.ShowCursor(w.TRUE) < 0) {},
348+
// .hidden => while (w.ShowCursor(w.FALSE) >= 0) {},
349+
// .disabled => {},
350+
// }
351+
// self.cursor_mode = mode;
352+
// }
354353

355-
pub fn setCursorShape(self: *Win32, shape: CursorShape) void {
356-
const name: i32 = switch (shape) {
357-
.arrow => w.IDC_ARROW,
358-
.ibeam => w.IDC_IBEAM,
359-
.crosshair => w.IDC_CROSS,
360-
.pointing_hand => w.IDC_HAND,
361-
.resize_ew => w.IDC_SIZEWE,
362-
.resize_ns => w.IDC_SIZENS,
363-
.resize_nwse => w.IDC_SIZENWSE,
364-
.resize_nesw => w.IDC_SIZENESW,
365-
.resize_all => w.IDC_SIZEALL,
366-
.not_allowed => w.IDC_NO,
367-
};
368-
_ = w.SetCursor(w.LoadCursorW(null, @ptrFromInt(@as(usize, @intCast(name)))));
369-
self.cursor_shape = shape;
370-
}
354+
// pub fn setCursorShape(self: *Win32, shape: CursorShape) void {
355+
// const name: i32 = switch (shape) {
356+
// .arrow => w.IDC_ARROW,
357+
// .ibeam => w.IDC_IBEAM,
358+
// .crosshair => w.IDC_CROSS,
359+
// .pointing_hand => w.IDC_HAND,
360+
// .resize_ew => w.IDC_SIZEWE,
361+
// .resize_ns => w.IDC_SIZENS,
362+
// .resize_nwse => w.IDC_SIZENWSE,
363+
// .resize_nesw => w.IDC_SIZENESW,
364+
// .resize_all => w.IDC_SIZEALL,
365+
// .not_allowed => w.IDC_NO,
366+
// };
367+
// _ = w.SetCursor(w.LoadCursorW(null, @ptrFromInt(@as(usize, @intCast(name)))));
368+
// self.cursor_shape = shape;
369+
// }
371370

372-
pub fn nativeWindowWin32(self: *Win32) w.HWND {
373-
return self.window;
374-
}
371+
// pub fn nativeWindowWin32(self: *Win32) w.HWND {
372+
// return self.window;
373+
// }
375374

376375
// -----------------------------
377376
// Internal functions
@@ -615,6 +614,7 @@ fn wndProc(wnd: w.HWND, msg: u32, wParam: w.WPARAM, lParam: w.LPARAM) callconv(w
615614
},
616615
else => return w.DefWindowProcW(wnd, msg, wParam, lParam),
617616
}
617+
618618
return w.DefWindowProcW(wnd, msg, wParam, lParam);
619619
}
620620

0 commit comments

Comments
 (0)