From 699fe2d48cd468281eff480f7b4bf581f3ce4fd1 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Fri, 27 Oct 2023 14:23:40 +0200 Subject: [PATCH] load sqlite --- src/main.zig | 58 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index 732e699..6963fb9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,34 +1,68 @@ const std = @import("std"); const raylib = @import("raylib.zig"); -const sqlite = @cImport({ +const c = @cImport({ @cInclude("sqlite3.h"); }); pub fn main() !void { + const alloc = std.heap.page_allocator; + const screen_width = 800; const screen_height = 450; raylib.InitWindow(screen_width, screen_height, "FabApp"); raylib.SetTargetFPS(60); var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory()); + // const ext = ".sqlite3"; + // @memcpy(file_dialog_state.filterExt[0..ext.len], ext); + + var db: ?*c.sqlite3 = undefined; while (!raylib.WindowShouldClose()) { - raylib.BeginDrawing(); - defer raylib.EndDrawing(); - raylib.ClearBackground(raylib.RAYWHITE); - + // + // Update + // { - if (file_dialog_state.windowActive) raylib.GuiLock(); - defer raylib.GuiUnlock(); + 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); - 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; + // slices -> C-string (null-terminated) + const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file }); + defer alloc.free(db_path); + + const result = c.sqlite3_open(db_path.ptr, &db); + if (result != c.SQLITE_OK or db == null) { + std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db) }); + _ = c.sqlite3_close(db); + } else { + std.debug.print("[SQLite] Success\n", .{}); + } } + file_dialog_state.SelectFilePressed = false; } - raylib.GuiWindowFileDialog(&file_dialog_state); - // // if (GuiButton((Rectangle){ 20, 20, 140, 30 }, GuiIconText(ICON_FILE_OPEN, "Open Image"))) fileDialogState.windowActive = true; + // + // Render + // + { + raylib.BeginDrawing(); + defer raylib.EndDrawing(); + raylib.ClearBackground(raylib.RAYWHITE); + + { + 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; + } + } + + raylib.GuiWindowFileDialog(&file_dialog_state); + } } raylib.CloseWindow(); }