Added initial JSON configuration support
This commit is contained in:
parent
34ce515521
commit
6cad6428a4
@ -13,3 +13,6 @@ A utility which can be used to create custom images for TRMNL devices via code
|
||||
|
||||
### Convert image with dithering
|
||||
`magick input.png -dither FloydSteinberg -remap pattern:gray50 -depth 1 -strip png:output.png`
|
||||
|
||||
# Notes
|
||||
json.hpp from nlohmann/json v3.11.2
|
||||
|
||||
@ -14,7 +14,7 @@ private:
|
||||
|
||||
public:
|
||||
Widget(int width, int height);
|
||||
~Widget();
|
||||
virtual ~Widget();
|
||||
|
||||
// Method which updates the internal surface with the latest image
|
||||
virtual void draw() = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -3,12 +3,14 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace std;
|
||||
using nlohmann::json;
|
||||
|
||||
map<pair<string, int>, TTF_Font*> font_map;
|
||||
|
||||
@ -74,3 +76,49 @@ TTF_Font* get_font(const string& filename, int size)
|
||||
return font;
|
||||
}
|
||||
}
|
||||
|
||||
bool read_config_json(json& cfg, const string& filename, ostream* log)
|
||||
{
|
||||
ifstream cfg_file(filename);
|
||||
|
||||
if (!cfg_file.is_open())
|
||||
{
|
||||
if (nullptr != log)
|
||||
{
|
||||
*log << "Could not open config.json" << endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse with comments
|
||||
try
|
||||
{
|
||||
cfg = json::parse(cfg_file, nullptr, true, true);
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
if (nullptr != log)
|
||||
{
|
||||
*log << e.what() << endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void json_extract(const json& j, const string& key, string& out)
|
||||
{
|
||||
if (j.contains(key) && j[key].is_string())
|
||||
{
|
||||
out = j[key];
|
||||
}
|
||||
}
|
||||
|
||||
void json_extract(const nlohmann::json& j, const string& key, int& out)
|
||||
{
|
||||
if (j.contains(key) && j[key].is_number_integer())
|
||||
{
|
||||
out = j[key];
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
#ifndef SDL_HELPERS_H_
|
||||
#define SDL_HELPERS_H_
|
||||
|
||||
#include <string>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "json.hpp"
|
||||
|
||||
// Call this before everything
|
||||
// Prints its messages
|
||||
bool init_sdl();
|
||||
@ -15,4 +19,15 @@ void clean_sdl();
|
||||
// Can return NULL
|
||||
TTF_Font* get_font(const std::string& filename, int size);
|
||||
|
||||
// Reads the file and tries to parse a JSON file with comments
|
||||
// cfg - output json struct
|
||||
// filename - filepath to open and read
|
||||
// log - output errors to this ostream, silent if NULL
|
||||
bool read_config_json(nlohmann::json& cfg, const std::string& filename, std::ostream* log = &std::cout);
|
||||
|
||||
// JSON Extractors - They do not override already set values if key is not present
|
||||
|
||||
void json_extract(const nlohmann::json& j, const std::string& key, std::string& out);
|
||||
void json_extract(const nlohmann::json& j, const std::string& key, int& out);
|
||||
|
||||
#endif // SDL_HELPERS_H_
|
||||
|
||||
Loading…
Reference in New Issue
Block a user