load sqlite

This commit is contained in:
Fabien Freling 2023-10-27 14:23:40 +02:00 committed by Fabien Freling
parent 3abe6d39c4
commit 699fe2d48c

View file

@ -1,34 +1,68 @@
const std = @import("std"); const std = @import("std");
const raylib = @import("raylib.zig"); const raylib = @import("raylib.zig");
const sqlite = @cImport({ const c = @cImport({
@cInclude("sqlite3.h"); @cInclude("sqlite3.h");
}); });
pub fn main() !void { pub fn main() !void {
const alloc = std.heap.page_allocator;
const screen_width = 800; const screen_width = 800;
const screen_height = 450; const screen_height = 450;
raylib.InitWindow(screen_width, screen_height, "FabApp"); raylib.InitWindow(screen_width, screen_height, "FabApp");
raylib.SetTargetFPS(60); raylib.SetTargetFPS(60);
var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory()); 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()) { while (!raylib.WindowShouldClose()) {
raylib.BeginDrawing(); //
defer raylib.EndDrawing(); // Update
raylib.ClearBackground(raylib.RAYWHITE); //
{ {
if (file_dialog_state.windowActive) raylib.GuiLock(); if (file_dialog_state.SelectFilePressed) {
defer raylib.GuiUnlock(); // 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; // slices -> C-string (null-terminated)
if (raylib.GuiButton(.{ .x = (screen_width - button_size) / 2, .y = (screen_height - button_size) / 2, .width = 200, .height = 200 }, "Load db file") == 1) { const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file });
file_dialog_state.windowActive = true; 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(); raylib.CloseWindow();
} }