const std = @import("std"); const sdl = @cImport({ @cInclude("SDL3/SDL.h"); }); const Action = @import("../actions.zig").Action; const IOInterface = @import("io_interface.zig").IOInterface; pub const IO = struct { allocator: std.mem.Allocator, screen: ?*sdl.SDL_Window, sprites: std.ArrayList([*c]sdl.SDL_Surface), fn loadSprite(self: *IO) !void { const sprite = sdl.SDL_LoadBMP("assets/image.bmp"); if (sprite == null) return error.FailedToLoadSprite; try self.sprites.append(sprite); } pub fn displayMessage(self: *IO, str: []const u8) anyerror!void { _ = self; _ = str; // TODO } pub fn waitForTick(self: *IO) anyerror!Action { _ = self; var quit = false; var event: sdl.SDL_Event = undefined; while (!quit) { _ = sdl.SDL_WaitEvent(&event); switch (event.type) { sdl.SDL_EVENT_QUIT => quit = true, else => {}, } } return Action.exit; } pub fn init(allocator: std.mem.Allocator) !IOInterface { var io = try allocator.create(IO); io.allocator = allocator; io.sprites = std.ArrayList([*c]sdl.SDL_Surface).init(allocator); if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) return error.SDL_Failed; io.screen = sdl.SDL_CreateWindow( "urlg", 640, 480, 0, ); if (io.screen == null) return error.SDL_Failed; io.loadSprite() catch |err| { io.deinit(); return err; }; const screen_surface = sdl.SDL_GetWindowSurface(io.screen); _ = sdl.SDL_BlitSurface(io.sprites.items[0], null, screen_surface, null); _ = sdl.SDL_UpdateWindowSurface(io.screen); return IOInterface.init(io); } pub fn deinit(self: *IO) void { for (self.sprites.items) |sprite| { _ = sdl.SDL_DestroySurface(sprite); } self.sprites.deinit(); _ = sdl.SDL_DestroyWindow(self.screen); _ = sdl.SDL_Quit(); self.allocator.destroy(self); } };