From 9fd81c0b38b2b843c24fb61bf8cb5b7873deaa72 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 12 Feb 2025 12:48:11 +0100 Subject: graphics: add line --- src/screen/drm/buffer/raw.zig | 2 +- src/screen/drm/buffer/root.zig | 2 +- src/screen/drm/pixel.zig | 18 ------------ src/screen/drm/request/frame-buffer.zig | 13 ++++++++- src/screen/drm/root.zig | 1 - src/screen/main.zig | 51 ++++++++++++++++++--------------- 6 files changed, 42 insertions(+), 45 deletions(-) (limited to 'src/screen') diff --git a/src/screen/drm/buffer/raw.zig b/src/screen/drm/buffer/raw.zig index c5a04f3..12784de 100644 --- a/src/screen/drm/buffer/raw.zig +++ b/src/screen/drm/buffer/raw.zig @@ -40,7 +40,7 @@ pub const Raw = struct { .id = 0, .width = fb.width, .height = fb.height, - .pixel_format = drm.Pixel.Format.xrgb8888, + .pixel_format = .xrgb8888, .flags = 0, .handles = [_] u32 { fb.handle, 0, 0, 0 }, .pitches = [_] u32 { fb.stride, 0, 0, 0 }, diff --git a/src/screen/drm/buffer/root.zig b/src/screen/drm/buffer/root.zig index f14ed66..30734f2 100644 --- a/src/screen/drm/buffer/root.zig +++ b/src/screen/drm/buffer/root.zig @@ -35,7 +35,7 @@ pub const Buffer = struct { .width = self.current.width, .height = self.current.height, .buffer = @as( - [*]volatile graphics.Pixel, + [*]volatile graphics.Color, @ptrCast(@alignCast(self.current.bytes)) )[0..self.current.bytes.len / 4], }; diff --git a/src/screen/drm/pixel.zig b/src/screen/drm/pixel.zig index 7c99e52..8b13789 100644 --- a/src/screen/drm/pixel.zig +++ b/src/screen/drm/pixel.zig @@ -1,19 +1 @@ -fn pixel_format_code(comptime a: u8, comptime b: u8, comptime c: u8, comptime d: u8) u32 { - return @as(u32, @intCast(a)) | - (@as(u32, @intCast(b)) << 8) | - (@as(u32, @intCast(c)) << 16) | - (@as(u32, @intCast(d)) << 24); -} - -pub const Pixel = packed struct(u32) { - blue: u8, - green: u8, - red: u8, - _padding: u8 = 0x0, - - pub const Format = enum(u32) { - xrgb8888 = pixel_format_code('X', 'R', '2', '4'), - }; -}; - diff --git a/src/screen/drm/request/frame-buffer.zig b/src/screen/drm/request/frame-buffer.zig index 8f93862..c9077e8 100644 --- a/src/screen/drm/request/frame-buffer.zig +++ b/src/screen/drm/request/frame-buffer.zig @@ -10,11 +10,22 @@ pub const CreateDumb = extern struct { size: u32, }; +fn pixel_format_code(comptime a: u8, comptime b: u8, comptime c: u8, comptime d: u8) u32 { + return @as(u32, @intCast(a)) | + (@as(u32, @intCast(b)) << 8) | + (@as(u32, @intCast(c)) << 16) | + (@as(u32, @intCast(d)) << 24); +} + +pub const PixelFormat = enum(u32) { + xrgb8888 = pixel_format_code('X', 'R', '2', '4'), +}; + pub const FrameBufferCmd2 = extern struct { id: u32, width: u32, height: u32, - pixel_format: drm.Pixel.Format, + pixel_format: PixelFormat, flags: u32, handles: [4]u32, pitches: [4]u32, diff --git a/src/screen/drm/root.zig b/src/screen/drm/root.zig index 5012743..2417463 100644 --- a/src/screen/drm/root.zig +++ b/src/screen/drm/root.zig @@ -6,4 +6,3 @@ pub const Event = @import("event.zig").Event; pub const Connector = @import("connector/root.zig").Connector; pub const Crtc = @import("crtc.zig").Crtc; pub const Buffer = @import("buffer/root.zig").Buffer; -pub const Pixel = @import("pixel.zig").Pixel; diff --git a/src/screen/main.zig b/src/screen/main.zig index 7b73450..2cb8c03 100644 --- a/src/screen/main.zig +++ b/src/screen/main.zig @@ -28,39 +28,44 @@ pub fn main() !void { try crtc.attach(&buffer, &connector, mode); - const center_x: i64 = @intCast(buffer.current.width / 2 - 100); - const center_y: i64 = @intCast(buffer.current.height / 2 - 100); - - var box = graphics.Box { - .x = @intCast(center_x), - .y = @intCast(center_y), - .width = 200, - .height = 200, - .color = .{ .red = 0xff, .green = 0x05, .blue = 0x07 }, - .radius = 0, + const center_x = (buffer.current.width / 2); + const center_y = (buffer.current.height / 2); + + var line = graphics.Line { + .start = .{ .x = center_x, .y = center_y }, + .end = .{ .x = center_x + 110 , .y = center_y + 100 }, + .color = .{ .red = 0xff, .green = 0, .blue = 0 }, }; var last_duration: f64 = 0; - var dir: f64 = 1; var delta: f64 = 0; - var radius: f64 = 0; + var angle: f64 = 0; while (true) { const start = try std.time.Instant.now(); const canvas = buffer.canvas(); + canvas.fill(.{ .red = 0x25, .green = 0x25, .blue = 0x25 }); - box.render(&canvas); - - radius += delta * dir; - if (radius > 100) { - std.debug.print("HERE\n", .{}); - radius = 100; - dir = -1; - } else if (radius < 0) { - radius = 0; - dir = 1; + + (graphics.Box { + .x = center_x - 100, + .y = center_y - 100, + .width = 200, + .height = 200, + .radius = 100, + .color = .{ .red = 0xff, .green = 0xff, .blue = 0xff }, + }).render(&canvas); + + line.render(&canvas); + const cos = @cos(std.math.degreesToRadians(angle)); + const sin = @sin(std.math.degreesToRadians(angle)); + line.end.x = @intCast(@as(i64, @intCast(center_x)) + @as(i64, @intFromFloat(100 * cos))); + line.end.y = @intCast(@as(i64, @intCast(center_y)) + @as(i64, @intFromFloat(100 * sin))); + + angle += delta; + if (angle > 360) { + angle = 0; } - box.radius = @intFromFloat(radius); const end = try std.time.Instant.now(); const duration: f64 = @floatFromInt(end.since(start)); -- cgit v1.2.3-70-g09d2