From 969c4ae18e400e916135a8f49318a4ad5b901bfb Mon Sep 17 00:00:00 2001 From: kdx Date: Thu, 1 Jun 2023 11:26:58 +0200 Subject: draw stuff and get input --- src/main.zig | 24 +++++++++++- src/zxc.zig | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 137 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index ea12f00..0bfbe60 100644 --- a/src/main.zig +++ b/src/main.zig @@ -9,6 +9,26 @@ pub fn main() !void { }); defer z.deinit(); - try z.drawBegin(); - try z.drawEnd(); + var score: usize = 0; + while (!z.shouldQuit()) { + z.cycleEvents(); + + if (z.getKey(.space) == .pressed) { + score += 1; + std.log.info("score: {}", .{score}); + } + + try z.drawBegin(); + try z.drawSetColor(.{ .r = 1, .g = 0, .b = 1 }); + try z.drawClear(); + try z.drawSetColor(.{ .r = 0, .g = 0, .b = 0 }); + try z.drawRectangle(.{ + .x = 12, + .y = 25, + .w = 32, + .h = 16, + .fill = true, + }); + try z.drawEnd(); + } } diff --git a/src/zxc.zig b/src/zxc.zig index 599dfaa..d826585 100644 --- a/src/zxc.zig +++ b/src/zxc.zig @@ -2,11 +2,13 @@ const std = @import("std"); const sdl = @import("sdl2"); const Self = @This(); +pub const Scancode = sdl.Scancode; + const Config = struct { const RenderMode = enum { - Pixel, - PixelPerfect, - Hd, + pixel, + pixel_perfect, + hd, }; width: usize, @@ -14,10 +16,33 @@ const Config = struct { scale: usize = 1, target_fps: ?u31, show_cursor: bool = false, - render: RenderMode = .Pixel, + render: RenderMode = .pixel, title: [:0]const u8 = "ZXC", }; +const Keystate = enum { + up, + released, + down, + pressed, +}; + +const Color = struct { + r: ?f32 = null, + g: ?f32 = null, + b: ?f32 = null, + a: ?f32 = null, +}; + +const Rectangle = struct { + x: i32, + y: i32, + w: u16, + h: u16, + fill: bool = false, + center: bool = false, +}; + config: Config, window: sdl.Window, renderer: sdl.Renderer, @@ -25,6 +50,9 @@ target: ?sdl.Texture, min_dt: u64, next_time: u64, tick: u64, +should_quit: bool, +keystates: [sdl.c.SDL_NUM_SCANCODES]Keystate, +color: struct { r: u8, g: u8, b: u8, a: u8 }, pub fn init(config: Config) !Self { var self = Self{ @@ -35,6 +63,9 @@ pub fn init(config: Config) !Self { .min_dt = undefined, .next_time = undefined, .tick = 0, + .should_quit = false, + .keystates = [_]Keystate{.up} ** sdl.c.SDL_NUM_SCANCODES, + .color = .{ .r = 0, .g = 0, .b = 0, .a = 255 }, }; try sdl.init(.{ .video = true }); @@ -65,7 +96,7 @@ pub fn init(config: Config) !Self { errdefer self.renderer.destroy(); try self.renderer.setDrawBlendMode(.blend); - if (config.render != .Hd) { + if (config.render != .hd) { self.target = try sdl.createTexture( self.renderer, .rgb888, @@ -81,7 +112,7 @@ pub fn init(config: Config) !Self { _ = sdl.setHint( sdl.hint.render_scale_quality, - if (config.render == .Hd) "1" else "0", + if (config.render == .hd) "1" else "0", ); if (config.target_fps != null) { @@ -101,14 +132,14 @@ pub fn deinit(self: *const Self) void { } pub fn drawBegin(self: *const Self) !void { - if (self.config.render == .Hd) + if (self.config.render == .hd) return; try self.renderer.setTarget(self.target); } pub fn drawEnd(self: *Self) !void { - if (self.config.render != .Hd) { + if (self.config.render != .hd) { try self.renderer.setTarget(null); try self.renderer.setColorRGBA(0, 0, 0, 255); try self.renderer.clear(); @@ -125,8 +156,8 @@ pub fn drawEnd(self: *Self) !void { }; const dst_rect = sdl.Rectangle{ - .x = size.width - @floatToInt(c_int, scw * scale), - .y = size.height - @floatToInt(c_int, sch * scale), + .x = @divFloor(size.width - @floatToInt(c_int, scw * scale), 2), + .y = @divFloor(size.height - @floatToInt(c_int, sch * scale), 2), .width = @floatToInt(c_int, scw * scale), .height = @floatToInt(c_int, sch * scale), }; @@ -145,3 +176,77 @@ pub fn drawEnd(self: *Self) !void { self.renderer.present(); self.tick += 1; } + +pub fn cycleEvents(self: *Self) void { + for (&self.keystates) |*keystate| { + keystate.* = switch (keystate.*) { + .up, .released => .up, + .down, .pressed => .down, + }; + } + + while (sdl.pollEvent()) |e| switch (e) { + .quit => self.should_quit = true, + .key_down => |k| { + if (k.is_repeat) + continue; + self.keystates[@enumToInt(k.scancode)] = .pressed; + }, + .key_up => |k| self.keystates[@enumToInt(k.scancode)] = .released, + else => {}, + }; +} + +pub fn shouldQuit(self: *const Self) bool { + return self.should_quit; +} + +pub fn getKey(self: *const Self, scancode: Scancode) Keystate { + return self.keystates[@enumToInt(scancode)]; +} + +pub fn drawClear(self: *const Self) !void { + try self.renderer.clear(); +} + +pub fn drawSetColor(self: *Self, c: Color) !void { + const r = if (c.r != null) + @truncate(u8, @floatToInt(usize, c.r.? * 255)) + else + self.color.r; + const g = if (c.g != null) + @truncate(u8, @floatToInt(usize, c.g.? * 255)) + else + self.color.g; + const b = if (c.b != null) + @truncate(u8, @floatToInt(usize, c.b.? * 255)) + else + self.color.b; + const a = if (c.a != null) + @truncate(u8, @floatToInt(usize, c.a.? * 255)) + else + self.color.a; + self.color = .{ .r = r, .g = g, .b = b, .a = a }; + try self.renderer.setColorRGBA(r, g, b, a); +} + +pub fn drawPoint(self: *const Self, x: i32, y: i32) !void { + try self.renderer.drawPoint(x, y); +} + +pub fn drawRectangle(self: *const Self, rect: Rectangle) !void { + const srect = sdl.Rectangle{ + .x = if (rect.center) rect.x - @divFloor(rect.w, 2) else rect.x, + .y = if (rect.center) rect.y - @divFloor(rect.h, 2) else rect.y, + .width = rect.w, + .height = rect.h, + }; + if (rect.fill) + try self.renderer.fillRect(srect) + else + try self.renderer.drawRect(srect); +} + +pub fn drawLine(self: *const Self, x0: i32, y0: i32, x1: i32, y1: i32) !void { + try self.renderer.drawLine(x0, y0, x1, y1); +} -- cgit v1.2.3