add main state
This commit is contained in:
parent
699fe2d48c
commit
f3911ead38
48
src/main.zig
48
src/main.zig
|
@ -4,6 +4,16 @@ const c = @cImport({
|
||||||
@cInclude("sqlite3.h");
|
@cInclude("sqlite3.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const StateTag = enum {
|
||||||
|
unloaded,
|
||||||
|
loaded,
|
||||||
|
};
|
||||||
|
|
||||||
|
const State = union(StateTag) {
|
||||||
|
unloaded: void,
|
||||||
|
loaded: *c.sqlite3,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const alloc = std.heap.page_allocator;
|
const alloc = std.heap.page_allocator;
|
||||||
|
|
||||||
|
@ -12,12 +22,12 @@ pub fn main() !void {
|
||||||
raylib.InitWindow(screen_width, screen_height, "FabApp");
|
raylib.InitWindow(screen_width, screen_height, "FabApp");
|
||||||
raylib.SetTargetFPS(60);
|
raylib.SetTargetFPS(60);
|
||||||
|
|
||||||
|
var state = State{ .unloaded = {} };
|
||||||
|
|
||||||
var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory());
|
var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory());
|
||||||
// const ext = ".sqlite3";
|
// const ext = ".sqlite3";
|
||||||
// @memcpy(file_dialog_state.filterExt[0..ext.len], ext);
|
// @memcpy(file_dialog_state.filterExt[0..ext.len], ext);
|
||||||
|
|
||||||
var db: ?*c.sqlite3 = undefined;
|
|
||||||
|
|
||||||
while (!raylib.WindowShouldClose()) {
|
while (!raylib.WindowShouldClose()) {
|
||||||
//
|
//
|
||||||
// Update
|
// Update
|
||||||
|
@ -32,12 +42,16 @@ pub fn main() !void {
|
||||||
const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file });
|
const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file });
|
||||||
defer alloc.free(db_path);
|
defer alloc.free(db_path);
|
||||||
|
|
||||||
const result = c.sqlite3_open(db_path.ptr, &db);
|
var db_opt: ?*c.sqlite3 = undefined;
|
||||||
if (result != c.SQLITE_OK or db == null) {
|
|
||||||
std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db) });
|
const result = c.sqlite3_open(db_path.ptr, &db_opt);
|
||||||
_ = c.sqlite3_close(db);
|
if (result != c.SQLITE_OK) {
|
||||||
} else {
|
std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db_opt) });
|
||||||
|
_ = c.sqlite3_close(db_opt);
|
||||||
|
}
|
||||||
|
if (db_opt) |db| {
|
||||||
std.debug.print("[SQLite] Success\n", .{});
|
std.debug.print("[SQLite] Success\n", .{});
|
||||||
|
state = State{ .loaded = db };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file_dialog_state.SelectFilePressed = false;
|
file_dialog_state.SelectFilePressed = false;
|
||||||
|
@ -51,14 +65,20 @@ pub fn main() !void {
|
||||||
defer raylib.EndDrawing();
|
defer raylib.EndDrawing();
|
||||||
raylib.ClearBackground(raylib.RAYWHITE);
|
raylib.ClearBackground(raylib.RAYWHITE);
|
||||||
|
|
||||||
{
|
switch (state) {
|
||||||
if (file_dialog_state.windowActive) raylib.GuiLock();
|
.unloaded => {
|
||||||
defer raylib.GuiUnlock();
|
if (file_dialog_state.windowActive) raylib.GuiLock();
|
||||||
|
defer raylib.GuiUnlock();
|
||||||
|
|
||||||
const button_size = 200;
|
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) {
|
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;
|
file_dialog_state.windowActive = true;
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
.loaded => |*db| {
|
||||||
|
_ = db;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
raylib.GuiWindowFileDialog(&file_dialog_state);
|
raylib.GuiWindowFileDialog(&file_dialog_state);
|
||||||
|
|
Loading…
Reference in a new issue