Refactored stuff into new files, added a test modal for adding critters
This commit is contained in:
190
src/main.cpp
190
src/main.cpp
@@ -1,162 +1,96 @@
|
||||
#include <RmlUi/Config/Config.h>
|
||||
#include <RmlUi/Core/Core.h>
|
||||
#include <RmlUi/Core/DataModelHandle.h>
|
||||
#include <RmlUi/Core/ElementDocument.h>
|
||||
#include <RmlUi/Core/Math.h>
|
||||
#include <RmlUi/Lua/Interpreter.h>
|
||||
#include <RmlUi/Lua/Lua.h>
|
||||
#include <SQLiteCpp/Database.h>
|
||||
#include <iostream>
|
||||
#include <RmlUi/Core.h>
|
||||
#include <RmlUi_Backend.h>
|
||||
#include <RmlUi/Lua/Lua.h>
|
||||
#include <RmlUi/Core/Context.h>
|
||||
#include <lua.h>
|
||||
#include <RmlUi/Debugger/Debugger.h>
|
||||
#include "critter.hpp"
|
||||
#include <SQLiteCpp/SQLiteCpp.h>
|
||||
#include "database.hpp"
|
||||
#include "ui.hpp"
|
||||
|
||||
#include <RmlUi/Core.h>
|
||||
#include <RmlUi/Debugger/Debugger.h>
|
||||
#include <RmlUi/Lua/Lua.h>
|
||||
#include <RmlUi_Backend.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include "database.hpp"
|
||||
|
||||
static bool is_initalized = false;
|
||||
static bool is_running = false;
|
||||
static Rml::Context *ctx;
|
||||
static std::map<int, Critter> critters = {};
|
||||
static std::vector<CritterEntry> critter_list = {};
|
||||
static Rml::Context *ctx = nullptr;
|
||||
static std::map<int, Critter> critters;
|
||||
static std::vector<CritterEntry> critter_list;
|
||||
static AppData app_data;
|
||||
|
||||
struct AppData {
|
||||
Rml::String page_title = "Main";
|
||||
} app_data;
|
||||
|
||||
void SyncMapToList() {
|
||||
critter_list.clear();
|
||||
for (const auto &[id, critter] : critters) {
|
||||
critter_list.push_back({id, critter});
|
||||
}
|
||||
void shutdown() {
|
||||
Rml::Shutdown();
|
||||
Backend::Shutdown();
|
||||
}
|
||||
|
||||
void Shutdown() {
|
||||
Rml::Shutdown();
|
||||
Backend::Shutdown();
|
||||
}
|
||||
bool initialize() {
|
||||
if (!Backend::Initialize("CritterFolio", 800, 600, true)) {
|
||||
std::cout << "[ERROR] Failed to initialize backend.\n";
|
||||
return false;
|
||||
}
|
||||
std::cout << "[INFO] RmlUI Backend initialized.\n";
|
||||
|
||||
bool SetupDataBinding(Rml::Context *context, Rml::DataModelHandle &model_handle) {
|
||||
Rml::DataModelConstructor dmc = ctx->CreateDataModel("app_data");
|
||||
if (!dmc) {
|
||||
std::cout << "[ERROR] could not create 'app_data' data binding.";
|
||||
return false;
|
||||
}
|
||||
Rml::SetSystemInterface(Backend::GetSystemInterface());
|
||||
Rml::SetRenderInterface(Backend::GetRenderInterface());
|
||||
Rml::Initialise();
|
||||
Rml::Lua::Initialise();
|
||||
|
||||
if (auto handle = dmc.RegisterStruct<Critter>()) {
|
||||
handle.RegisterMember("id", &Critter::id);
|
||||
handle.RegisterMember("name", &Critter::name);
|
||||
handle.RegisterMember("gender", &Critter::gender);
|
||||
handle.RegisterMember("species", &Critter::species);
|
||||
handle.RegisterMember("notes", &Critter::notes);
|
||||
handle.RegisterMember("mother_id", &Critter::mother_id);
|
||||
handle.RegisterMember("father_id", &Critter::father_id);
|
||||
}
|
||||
std::cout << "[INFO] Loading local DB...\n";
|
||||
if (!db_get_all_critters(critters)) {
|
||||
std::cout << "[ERROR] Local DB failed to initialize.\n";
|
||||
return false;
|
||||
}
|
||||
std::cout << "[INFO] Local DB initialized.\n";
|
||||
|
||||
if (auto handle = dmc.RegisterStruct<CritterEntry>()) {
|
||||
handle.RegisterMember("key", &CritterEntry::key);
|
||||
handle.RegisterMember("value", &CritterEntry::value);
|
||||
}
|
||||
sync_map_to_list(critters, critter_list);
|
||||
|
||||
dmc.RegisterArray<std::vector<CritterEntry>>();
|
||||
dmc.Bind("critters", &critter_list);
|
||||
ctx = Rml::CreateContext("main", Rml::Vector2i(800, 600));
|
||||
if (!ctx) {
|
||||
std::cout << "[ERROR] Failed to create context.\n";
|
||||
return false;
|
||||
}
|
||||
std::cout << "[INFO] Main context initialized.\n";
|
||||
|
||||
dmc.Bind("page_title", &app_data.page_title);
|
||||
|
||||
model_handle = dmc.GetModelHandle();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Initalize() {
|
||||
if (!Backend::Initialize("CritterFolio", 800, 600, true)) {
|
||||
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";
|
||||
bool db_result = db_get_all_critters(critters);
|
||||
if (!db_result) {
|
||||
std::cout << "[ERROR] Local DB failed to initalize.\n";
|
||||
return false;
|
||||
}
|
||||
std::cout << "[INFO] Local DB initalized.\n";
|
||||
|
||||
SyncMapToList();
|
||||
|
||||
// ------- DB TESTING
|
||||
// db_print_all_critters(critters);
|
||||
// ------- END DB TESTING
|
||||
|
||||
ctx = Rml::CreateContext("main", Rml::Vector2i(800, 600));
|
||||
if (!ctx) {
|
||||
std::cout << "[ERROR] Failed to create context.";
|
||||
return false;
|
||||
}
|
||||
std::cout << "[INFO] Main context initalized.\n";
|
||||
|
||||
Rml::Debugger::Initialise(ctx);
|
||||
|
||||
// lua_State *L = Rml::Lua::Interpreter::GetLuaState();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Update(Rml::DataModelHandle model_handle) {
|
||||
|
||||
Rml::Debugger::Initialise(ctx);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "[INFO] Starting critterfolio...\n";
|
||||
std::cout << "[INFO] Starting CritterFolio...\n";
|
||||
|
||||
is_initalized = Initalize();
|
||||
is_initalized = initialize();
|
||||
if (!is_initalized) {
|
||||
std::cout << "[ERROR] Failed to initalize, shutting down.";
|
||||
Shutdown();
|
||||
return 1;
|
||||
std::cout << "[ERROR] Failed to initialize, shutting down.\n";
|
||||
shutdown();
|
||||
return 1;
|
||||
}
|
||||
std::cout << "[INFO] Application initalized, all systems running.\n";
|
||||
|
||||
is_running = true;
|
||||
|
||||
Rml::DataModelHandle app_data_handle;
|
||||
if (!setup_data_binding(ctx, app_data_handle, app_data, critter_list, critters)) {
|
||||
shutdown();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SetupDataBinding(ctx, app_data_handle);
|
||||
|
||||
Rml::ElementDocument *doc;
|
||||
doc = ctx->LoadDocument("./assets/main.rml");
|
||||
Rml::ElementDocument *doc = ctx->LoadDocument("./assets/main.rml");
|
||||
if (!doc) {
|
||||
std::cout << "[ERROR] Could no load assets/main.rml document, shutting down.";
|
||||
Shutdown();
|
||||
return 1;
|
||||
std::cout << "[ERROR] Could not load assets/main.rml document, shutting down.\n";
|
||||
shutdown();
|
||||
return 1;
|
||||
}
|
||||
std::cout << "[INFO] Main page loaded.\n";
|
||||
|
||||
doc->Show();
|
||||
|
||||
while(is_running) {
|
||||
is_running = Backend::ProcessEvents(ctx);
|
||||
|
||||
Update(app_data_handle);
|
||||
|
||||
ctx->Update();
|
||||
Backend::BeginFrame();
|
||||
ctx->Render();
|
||||
Backend::PresentFrame();
|
||||
while (is_running) {
|
||||
is_running = Backend::ProcessEvents(ctx);
|
||||
ctx->Update();
|
||||
Backend::BeginFrame();
|
||||
ctx->Render();
|
||||
Backend::PresentFrame();
|
||||
}
|
||||
|
||||
Shutdown();
|
||||
shutdown();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user