Finished JSON system. Cleaned up some things

This commit is contained in:
nedko 2025-12-04 15:24:12 +02:00
parent 6cad6428a4
commit 6931d80e04
2 changed files with 48 additions and 11 deletions

View File

@ -2,20 +2,28 @@
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <stdio.h>
#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<string, Widget*(const json&)>& 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<json> widgets_cfg;
vector<Widget*> widgets;
map<string, Widget*(*)(const json&)> 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();

View File

@ -7,8 +7,6 @@
#include <map>
#include <utility>
#include <stdio.h>
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;
}