diff --git a/src/game.zig b/src/game.zig index 117bd9b..15fa085 100644 --- a/src/game.zig +++ b/src/game.zig @@ -55,8 +55,8 @@ pub fn run() void { } else err.sdl(); Game.processEvents(); - Game.mouse.x_norm = (Game.mouse.x_screen / @as(f32, @floatFromInt(Graphics.getWidth()))) * 2 - 1; - Game.mouse.y_norm = (Game.mouse.y_screen / @as(f32, @floatFromInt(Graphics.getHeight()))) * -2 + 1; + Game.mouse.x_norm = (Game.mouse.x_screen / @as(f32, @floatFromInt(Graphics.window_width))) * 2 - 1; + Game.mouse.y_norm = (Game.mouse.y_screen / @as(f32, @floatFromInt(Graphics.window_height))) * -2 + 1; World.update(Game.time.delta); if (Game.beginDraw()) { World.draw(); @@ -90,10 +90,6 @@ fn processEvents() void { sdl.EVENT_QUIT => { Game.running = false; }, - sdl.EVENT_WINDOW_RESIZED => { - if (event.window.windowID != Graphics.windowId()) continue; - Graphics.resize(@intCast(event.window.data1), @intCast(event.window.data2)); - }, sdl.EVENT_MOUSE_MOTION => { if (event.motion.windowID != Graphics.windowId()) continue; Game.mouse.x_screen = event.motion.x; diff --git a/src/graphics.zig b/src/graphics.zig index db06acf..b377556 100644 --- a/src/graphics.zig +++ b/src/graphics.zig @@ -33,14 +33,13 @@ var depth_texture: *sdl.GPUTexture = undefined; var fsaa_target: *sdl.GPUTexture = undefined; var pipeline: *sdl.GPUGraphicsPipeline = undefined; -var window_size: [2]u32 = undefined; +pub var window_width: u32 = undefined; +pub var window_height: u32 = undefined; var fsaa_scale: u32 = 4; var fsaa_level: u32 = 3; pub var camera: Camera = undefined; -var to_resize: ?[2]u32 = null; - const VERTEX_BUFFER_DEFAULT_CAPACITY = 1024; const VERTEX_BUFFER_GROWTH_MULTIPLIER = 2; const BYTES_PER_VERTEX = 5 * 4; @@ -62,7 +61,6 @@ pub fn create() void { 900, sdl.WINDOW_VULKAN | sdl.WINDOW_RESIZABLE, ) orelse err.sdl(); - Graphics.window_size = .{ 1600, 900 }; // Device Graphics.device = sdl.CreateGPUDevice( @@ -111,20 +109,18 @@ pub fn create() void { if (target_format == sdl.GPU_TEXTUREFORMAT_INVALID) err.sdl(); // TODO: Clean - var window_width: c_int = 1; - var window_height: c_int = 1; - if (!sdl.GetWindowSizeInPixels(Graphics.window, &window_width, &window_height)) err.sdl(); + if (!sdl.GetWindowSizeInPixels(Graphics.window, @ptrCast(&Graphics.window_width), @ptrCast(&Graphics.window_height))) err.sdl(); Graphics.depth_texture = createTexture( - @as(u32, @intCast(window_width)) * Graphics.fsaa_scale, - @as(u32, @intCast(window_height)) * Graphics.fsaa_scale, + @as(u32, @intCast(Graphics.window_width)) * Graphics.fsaa_scale, + @as(u32, @intCast(Graphics.window_height)) * Graphics.fsaa_scale, DEPTH_FORMAT, sdl.GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET, 1, ); Graphics.fsaa_target = createTexture( - @as(u32, @intCast(window_width)) * Graphics.fsaa_scale, - @as(u32, @intCast(window_height)) * Graphics.fsaa_scale, + @as(u32, @intCast(Graphics.window_width)) * Graphics.fsaa_scale, + @as(u32, @intCast(Graphics.window_height)) * Graphics.fsaa_scale, target_format, sdl.GPU_TEXTUREUSAGE_COLOR_TARGET | sdl.GPU_TEXTUREUSAGE_SAMPLER, fsaa_level, @@ -296,18 +292,19 @@ fn growVertexBuffer(new_size: usize) void { /// Otherwise `command_buffer` and `render_pass` are both set pub fn beginDraw() bool { Graphics.command_buffer = sdl.AcquireGPUCommandBuffer(Graphics.device) orelse err.sdl(); - if (Graphics.to_resize) |new_size| { - Graphics.resetTextures(new_size[0], new_size[1]); - Graphics.camera.aspect = @as(f32, @floatFromInt(new_size[0])) / @as(f32, @floatFromInt(new_size[1])); - Graphics.window_size = new_size; - Graphics.to_resize = null; - } var width: u32 = 0; var height: u32 = 0; if (!sdl.WaitAndAcquireGPUSwapchainTexture(Graphics.command_buffer, Graphics.window, &Graphics.render_target, &width, &height)) err.sdl(); // Window is probably hidden - if (Graphics.render_target == null) return false; + if (Graphics.render_target == null or width == 0 or height == 0) return false; + + if (width != Graphics.window_width or height != Graphics.window_height) { + Graphics.resetTextures(width, height); + Graphics.camera.aspect = @as(f32, @floatFromInt(width)) / @as(f32, @floatFromInt(height)); + Graphics.window_width = width; + Graphics.window_height = height; + } Graphics.render_pass = sdl.BeginGPURenderPass(Graphics.command_buffer.?, &.{ .clear_color = .{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 1.0 }, @@ -379,14 +376,14 @@ pub fn endDraw() void { sdl.BlitGPUTexture(Graphics.command_buffer, &.{ .source = .{ .texture = Graphics.fsaa_target, - .w = Graphics.window_size[0], - .h = Graphics.window_size[1], + .w = Graphics.window_width, + .h = Graphics.window_height, .mip_level = fsaa_level - 1, }, .destination = .{ .texture = Graphics.render_target, - .w = Graphics.window_size[0], - .h = Graphics.window_size[1], + .w = Graphics.window_width, + .h = Graphics.window_height, }, .load_op = sdl.GPU_LOADOP_DONT_CARE, .filter = sdl.GPU_FILTER_NEAREST, @@ -452,7 +449,7 @@ fn resetTextures(width: u32, height: u32) void { 1, ); - const target_format = sdl.SDL_GetGPUSwapchainTextureFormat(Graphics.device, Graphics.window); + const target_format = sdl.GetGPUSwapchainTextureFormat(Graphics.device, Graphics.window); sdl.ReleaseGPUTexture(Graphics.device, Graphics.fsaa_target); Graphics.fsaa_target = createTexture( @@ -464,21 +461,10 @@ fn resetTextures(width: u32, height: u32) void { ); } -pub fn resize(width: u32, height: u32) void { - Graphics.to_resize = .{ width, height }; -} - pub fn windowId() sdl.WindowID { return sdl.GetWindowID(Graphics.window); } -pub fn getWidth() u32 { - return @max(1, Graphics.window_size[0]); -} -pub fn getHeight() u32 { - return @max(1, Graphics.window_size[1]); -} - pub fn generatePlane(x0: f32, y0: f32, x1: f32, y1: f32, w: f32, h: f32) [30]f32 { const hw = w * 0.5; const hh = h * 0.5; diff --git a/src/world.zig b/src/world.zig index 4fd7db8..fc564c3 100644 --- a/src/world.zig +++ b/src/world.zig @@ -350,7 +350,7 @@ pub fn tryRelease() bool { }; object.target_transform.position = World.hand_transform.position; World.bringToTop(object); - if (!Game.keyboard.keys.is_pressed(sdl.SDL_SCANCODE_LSHIFT)) { + if (object.type == .card and !Game.keyboard.keys.is_pressed(sdl.SDL_SCANCODE_LSHIFT)) { if (World.getHover()) |hover_object| { if (hover_object.type == .deck) { object.reparent(.{ .deck = hover_object.id }); @@ -510,8 +510,8 @@ pub fn updateCamera(delta: f32) void { if (Game.mouse.buttons.is_pressed(sdl.BUTTON_LEFT)) { if (World.panning) { - World.camera_position[0] += zoom_factor * Game.mouse.dx / @as(f32, @floatFromInt(Graphics.getWidth())) * -15; - World.camera_position[1] += zoom_factor * Game.mouse.dy / @as(f32, @floatFromInt(Graphics.getWidth())) * 15; + World.camera_position[0] += zoom_factor * Game.mouse.dx / @as(f32, @floatFromInt(Graphics.window_width)) * -15; + World.camera_position[1] += zoom_factor * Game.mouse.dy / @as(f32, @floatFromInt(Graphics.window_height)) * 15; } }