From 6931d80e04d8f2db05bba3c7b4ea125d3a20d1b1 Mon Sep 17 00:00:00 2001 From: nedko Date: Thu, 4 Dec 2025 15:24:12 +0200 Subject: [PATCH] Finished JSON system. Cleaned up some things --- main.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++----- sdl_helpers.cpp | 10 ++++------ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index d09f9d8..cb4d7f8 100644 --- a/main.cpp +++ b/main.cpp @@ -2,20 +2,28 @@ #include #include +#include +#include #include #include -#include - #include "json.hpp" #include "sdl_helpers.h" #include "Widgets/Widget.h" +using std::cout; +using std::endl; +using std::map; using std::string; using std::vector; using nlohmann::json; +void init_builders(map& widget_builders) +{ + // widget_builders["name"] = &WidgetName::builder; +} + int main(int argc, char **argv) { int result = 0; @@ -28,7 +36,10 @@ int main(int argc, char **argv) json cfg; SDL_Surface* main_surface = nullptr; + + vector widgets_cfg; vector widgets; + map widget_builders; ok = init_sdl(); if (!ok) @@ -57,17 +68,45 @@ int main(int argc, char **argv) main_surface = SDL_CreateRGBSurfaceWithFormat(0, screen_width, screen_height, 32, SDL_PIXELFORMAT_RGBA8888); if (nullptr == main_surface) { - printf("Could not allocate main surface\n"); + cout << "Could not allocate main surface" << endl; result = -1; goto cleanup; } - // TODO: Add Widgets From JSON + // Add Widgets From JSON + if (cfg.contains("widgets") && cfg["widgets"].is_array()) + { + widgets_cfg = cfg["widgets"]; + } + + for (const json& j : widgets_cfg) + { + string widget_name; + json_extract(j, "name", widget_name); + if (widget_name.empty()) + { + continue; + } + + // Check if name exists in known builders + if (0 == widget_builders.count(widget_name)) + { + cout << "Unknown widget '" << widget_name << "'. Skipping ..." << endl; + continue; + } + + // Construct and add widget to vector + Widget* widget = widget_builders[widget_name](j); + if (nullptr != widget) + { + widgets.push_back(widget); + } + } // Clear screen with white SDL_FillRect(main_surface, nullptr, SDL_MapRGBA(main_surface->format, 255, 255, 255, 255)); - // Apply all widgets + // Draw and apply all widgets for (Widget* widget : widgets) { widget->draw(); diff --git a/sdl_helpers.cpp b/sdl_helpers.cpp index 0f8d01a..bfe4399 100644 --- a/sdl_helpers.cpp +++ b/sdl_helpers.cpp @@ -7,8 +7,6 @@ #include #include -#include - using namespace std; using nlohmann::json; @@ -18,13 +16,13 @@ bool init_sdl() { if (SDL_Init(SDL_INIT_VIDEO) < 0) { - printf("SDL could not initialize! SDL - %s\n", SDL_GetError()); + cout << "SDL could not initialize! SDL - " << SDL_GetError() << endl; return false; } if (0 != TTF_Init()) { - printf("Could not init TTF! SDL - %s\n", SDL_GetError()); + cout << "Could not init TTF! SDL - " << SDL_GetError() << endl; SDL_Quit(); return false; } @@ -32,7 +30,7 @@ bool init_sdl() int img_flags = IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF); if (img_flags != (IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF)) { - printf("Could not init IMG! SDL - %s\n", SDL_GetError()); + cout << "Could not init IMG! SDL - " << SDL_GetError() << endl; TTF_Quit(); SDL_Quit(); return false; @@ -71,7 +69,7 @@ TTF_Font* get_font(const string& filename, int size) } else { - printf("Could not open font '%s' with size %d\n", filename.c_str(), size); + cout << "Could not open font '"<< filename << "' with size " << size << endl; } return font; }