diff --git a/CMakeLists.txt b/CMakeLists.txt index e7b3f09..9353726 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,10 @@ find_package(Lua REQUIRED) pkg_check_modules(LUNASVG REQUIRED IMPORTED_TARGET lunasvg) add_library(lunasvg::lunasvg ALIAS PkgConfig::LUNASVG) -pkg_check_modules(SQLITE3 REQUIRED IMPORTED_TARGET sqlite3) -add_library(sqlite3::sqlite3 ALIAS PkgConfig::SQLITE3) +#pkg_check_modules(SQLITE3 REQUIRED IMPORTED_TARGET sqlite3) +#add_library(sqlite3::sqlite3 ALIAS PkgConfig::SQLITE3) + +find_package(SQLiteCpp REQUIRED) find_package(toml11 CONFIG REQUIRED) @@ -43,8 +45,8 @@ target_link_libraries(${PROJECT_NAME} PRIVATE Freetype::Freetype lunasvg::lunasvg glfw - sqlite3::sqlite3 toml11::toml11 + SQLiteCpp ) install(TARGETS ${PROJECT_NAME} DESTINATION bin) diff --git a/assets/local.db b/assets/local.db new file mode 100644 index 0000000..0eadb6b Binary files /dev/null and b/assets/local.db differ diff --git a/flake.nix b/flake.nix index 917dfdc..7909984 100644 --- a/flake.nix +++ b/flake.nix @@ -67,6 +67,7 @@ lunasvg lua sqlite.dev + sqlitecpp toml11 ]; in diff --git a/src/critter.hpp b/src/critter.hpp index cbf11b1..6b385a6 100644 --- a/src/critter.hpp +++ b/src/critter.hpp @@ -1,10 +1,11 @@ #pragma once #include -#include class CritterEvent { public: + int id; + int critter_id; std::string name; std::string date; std::string time; @@ -13,23 +14,21 @@ class CritterEvent { class CritterDocument { public: + int id; + int critter_id; std::string name; std::string doc_type; std::string cached_path; }; class Critter { - - enum Sex {Male, Female}; - public: + int id; std::string name; - Sex sex; std::string birthdate; - Critter *mother; - Critter *father; - std::vector children; std::string notes; - std::vector events; - std::vector documents; -}; \ No newline at end of file + int mother_id; + int father_id; + std::string gender; + std::string species; +}; diff --git a/src/main.cpp b/src/main.cpp index 0d2fe14..1a98982 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -13,15 +15,17 @@ #include #include #include "critter.hpp" -#include +#include +#include +#include static bool is_initalized = false; static bool is_running = false; static Rml::Context *ctx; +static std::map critters = {}; struct AppData { Rml::String page_title = "Main"; - sqlite3 *db; } app_data; void Shutdown() { @@ -48,22 +52,74 @@ bool Initalize() { std::cout << "[ERROR] Failed to initalize the backend."; return false; } + std::cout << "[INFO] RmlUI Backend initalized.\n"; Rml::SetSystemInterface(Backend::GetSystemInterface()); Rml::SetRenderInterface(Backend::GetRenderInterface()); Rml::Initialise(); Rml::Lua::Initialise(); + + // TODO: Add more error handling for initalization + + std::cout << "[INFO] Loading local DB...\n"; + try { + SQLite::Database db = SQLite::Database("./assets/local.db", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); + + SQLite::Statement q(db, "SELECT * FROM critters"); + + while(q.executeStep()) { + int id = q.getColumn(0); + std::string name = q.getColumn(1); + std::string gender = q.getColumn(2); + int mother_id = q.getColumn(3); + int father_id = q.getColumn(4); + std::string notes = q.getColumn(5); + std::string species = q.getColumn(6); + + Critter c; + c.id = id; + c.name = name; + c.gender = gender; + c.mother_id = mother_id; + c.father_id = father_id; + c.notes = notes; + c.species = species; + + critters[c.id] = c; + } + + std::cout << "[INFO] Loaded " << critters.size() << " critters from local db.\n"; + } + catch (std::exception &e){ + std::cout << "[ERROR] DB exception: " << e.what() << std::endl; + return false; + } + std::cout << "[INFO] Local DB initalized.\n"; + + // ------- DB TESTING + std::cout << "\n"; + for (auto c : critters) { + std::cout << "\nCritter " << c.first << "\n"; + std::cout << "ID: " << c.second.id << std::endl; + std::cout << "Name:" << c.second.name << std::endl; + std::cout << "Gender: " << c.second.gender << std::endl; + std::cout << "Mother ID: " << c.second.mother_id << std::endl; + std::cout << "Father ID: " << c.second.father_id << std::endl; + std::cout << "Notes: " << c.second.notes << std::endl; + std::cout << "Species: " << c.second.species << std::endl; + } + std::cout << "\n"; + // ------- END DB TESTING ctx = Rml::CreateContext("main", Rml::Vector2i(800, 600)); if (!ctx) { std::cout << "[ERROR] Failed to create context."; - Shutdown(); + return false; } + std::cout << "[INFO] Main context initalized.\n"; Rml::Debugger::Initialise(ctx); - - // lua_State *L = Rml::Lua::Interpreter::GetLuaState(); return true; @@ -74,7 +130,7 @@ void Update(Rml::DataModelHandle model_handle) { } int main() { - std::cout << "Hello world!\n"; + std::cout << "[INFO] Starting critterfolio...\n"; is_initalized = Initalize(); if (!is_initalized) { @@ -82,6 +138,7 @@ int main() { Shutdown(); return 1; } + std::cout << "[INFO] Application initalized, all systems running.\n"; is_running = true; @@ -96,6 +153,7 @@ int main() { Shutdown(); return 1; } + std::cout << "[INFO] Main page loaded.\n"; doc->Show();