sqlite database setup
This commit is contained in:
70
src/main.cpp
70
src/main.cpp
@@ -5,6 +5,8 @@
|
||||
#include <RmlUi/Core/Math.h>
|
||||
#include <RmlUi/Lua/Interpreter.h>
|
||||
#include <RmlUi/Lua/Lua.h>
|
||||
#include <SQLiteCpp/Database.h>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <RmlUi/Core.h>
|
||||
#include <RmlUi_Backend.h>
|
||||
@@ -13,15 +15,17 @@
|
||||
#include <lua.h>
|
||||
#include <RmlUi/Debugger/Debugger.h>
|
||||
#include "critter.hpp"
|
||||
#include <sqlite3.h>
|
||||
#include <ostream>
|
||||
#include <SQLiteCpp/SQLiteCpp.h>
|
||||
#include <map>
|
||||
|
||||
static bool is_initalized = false;
|
||||
static bool is_running = false;
|
||||
static Rml::Context *ctx;
|
||||
static std::map<int, Critter> 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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user