const std = @import("std"); const raylib = @import("raylib.zig"); const c = @cImport({ @cInclude("sqlite3.h"); }); const StateTag = enum { unloaded, loaded, }; const State = union(StateTag) { unloaded: void, loaded: *c.sqlite3, }; fn loadSqlite(path: [:0]const u8) State { var db_opt: ?*c.sqlite3 = undefined; const result = c.sqlite3_open(path.ptr, &db_opt); if (result != c.SQLITE_OK) { std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db_opt) }); _ = c.sqlite3_close(db_opt); return State{ .unloaded = {} }; } if (db_opt) |db| { std.debug.print("[SQLite] Success\n", .{}); return State{ .loaded = db }; } return State{ .unloaded = {} }; } pub fn main() !void { var state = State{ .unloaded = {} }; const alloc = std.heap.c_allocator; const args = try std.process.argsAlloc(alloc); defer std.process.argsFree(alloc, args); std.debug.print("Arguments: {s}\n", .{args}); if (args.len > 1) { state = loadSqlite(args[1]); } const screen_width = 800; const screen_height = 450; raylib.InitWindow(screen_width, screen_height, "Loggy"); raylib.SetTargetFPS(60); var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory()); // const ext = ".sqlite3"; // @memcpy(file_dialog_state.filterExt[0..ext.len], ext); var checked = false; while (!raylib.WindowShouldClose()) { // // Update // { if (file_dialog_state.SelectFilePressed) { // C-strings -> slices const dir = std.mem.sliceTo(&file_dialog_state.dirPathText, 0); const file = std.mem.sliceTo(&file_dialog_state.fileNameText, 0); // slices -> C-string (null-terminated) const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file }); defer alloc.free(db_path); state = loadSqlite(db_path); } file_dialog_state.SelectFilePressed = false; } // // Render // { raylib.BeginDrawing(); defer raylib.EndDrawing(); raylib.ClearBackground(raylib.RAYWHITE); switch (state) { .unloaded => { if (file_dialog_state.windowActive) raylib.GuiLock(); defer raylib.GuiUnlock(); const button_size = 200; if (raylib.GuiButton(.{ .x = (screen_width - button_size) / 2, .y = (screen_height - button_size) / 2, .width = 200, .height = 200 }, "Load db file") == 1) { file_dialog_state.windowActive = true; } }, .loaded => |*db| { _ = db; const label_rect: raylib.Rectangle = .{ .x = 10, .y = 10, .width = 40, .height = 40 }; _ = raylib.GuiCheckBox(label_rect, "Eat well", &checked); }, } raylib.GuiWindowFileDialog(&file_dialog_state); } } raylib.CloseWindow(); } test "simple test" { var list = std.ArrayList(i32).init(std.testing.allocator); defer list.deinit(); // try commenting this out and see if zig detects the memory leak! try list.append(42); try std.testing.expectEqual(@as(i32, 42), list.pop()); }