trmnl_sdl/main.cpp

93 lines
1.7 KiB
C++

#include <SDL2/SDL.h>
#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 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;
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
main_surface = SDL_CreateRGBSurfaceWithFormat(0, screen_width, screen_height, 32, SDL_PIXELFORMAT_RGBA8888);
if (nullptr == main_surface)
{
printf("Could not allocate main surface\n");
result = -1;
goto cleanup;
}
// TODO: Add Widgets From JSON
// Clear screen with white
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
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;
}