Added initial JSON configuration support
This commit is contained in:
50
main.cpp
50
main.cpp
@@ -2,30 +2,59 @@
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "json.hpp"
|
||||
|
||||
#include "sdl_helpers.h"
|
||||
#include "Widgets/Widget.h"
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using nlohmann::json;
|
||||
|
||||
int main(int argd, char **argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int result = 0;
|
||||
bool ok;
|
||||
|
||||
int screen_width = 800;
|
||||
int screen_height = 600;
|
||||
string output_filename = "trmnl.png";
|
||||
string cfg_filename = "config.json";
|
||||
json cfg;
|
||||
|
||||
SDL_Surface* main_surface = nullptr;
|
||||
vector<Widget*> widgets;
|
||||
|
||||
if (!init_sdl())
|
||||
ok = init_sdl();
|
||||
if (!ok)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (argc > 1)
|
||||
{
|
||||
cfg_filename = argv[1];
|
||||
}
|
||||
|
||||
// Read JSON CFG
|
||||
ok = read_config_json(cfg, cfg_filename);
|
||||
if (!ok)
|
||||
{
|
||||
result = -1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// Change screen size from JSON
|
||||
json_extract(cfg, "width", screen_width);
|
||||
json_extract(cfg, "height", screen_height);
|
||||
|
||||
// Create surface
|
||||
SDL_Surface* main_surface = SDL_CreateRGBSurfaceWithFormat(0, screen_width, screen_height, 32, SDL_PIXELFORMAT_RGBA8888);
|
||||
main_surface = SDL_CreateRGBSurfaceWithFormat(0, screen_width, screen_height, 32, SDL_PIXELFORMAT_RGBA8888);
|
||||
if (nullptr == main_surface)
|
||||
{
|
||||
printf("Could not allocate main surface\n");
|
||||
@@ -33,20 +62,31 @@ int main(int argd, char **argv)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// TODO: Add Widgets From JSON
|
||||
|
||||
// Clear screen with white
|
||||
SDL_FillRect(main_surface, nullptr, SDL_MapRGBA(main_surface->format, 255, 0, 255, 255));
|
||||
SDL_FillRect(main_surface, nullptr, SDL_MapRGBA(main_surface->format, 255, 255, 255, 255));
|
||||
|
||||
// Apply all widgets
|
||||
for (Widget* widget : widgets)
|
||||
{
|
||||
widget->draw();
|
||||
SDL_Rect rect = widget->get_rect();
|
||||
SDL_BlitSurface(widget->get_internal_surface(), nullptr, main_surface, &rect);
|
||||
}
|
||||
|
||||
// Save image
|
||||
IMG_SavePNG(main_surface, "asdf.png");
|
||||
json_extract(cfg, "output", output_filename);
|
||||
IMG_SavePNG(main_surface, output_filename.c_str());
|
||||
|
||||
cleanup:
|
||||
// Destroy All Widgets
|
||||
for (Widget* widget : widgets)
|
||||
{
|
||||
delete widget;
|
||||
}
|
||||
widgets.clear();
|
||||
|
||||
clean_sdl();
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user