#include "WidgetImage.h" #include using std::string; WidgetImage::WidgetImage(int x, int y, int width, int height, string filename, ImageResize resize_type, HorizontalAlign halign, VerticalAlign valign, SDL_Color bg_color) : Widget(x, y, width, height), m_filename(filename), m_image_surface(nullptr), m_resize_type(resize_type), m_halign(halign), m_valign(valign), m_bg_color(bg_color) { m_image_surface = IMG_Load(m_filename.c_str()); if (nullptr == m_image_surface) { // TODO: Print errors } } void WidgetImage::draw() { if (nullptr == m_surface) { return; } // Clear surface with BG color SDL_FillRect(m_surface, nullptr, SDL_MapRGBA(m_surface->format, m_bg_color.r, m_bg_color.g, m_bg_color.b, m_bg_color.a)); if (nullptr == m_image_surface) { return; } SDL_Rect align = surface_align(m_surface, m_image_surface, m_halign, m_valign); switch (m_resize_type) { case RESIZE_NONE: SDL_BlitSurface(m_image_surface, nullptr, m_surface, &align); break; case RESIZE_FIT: // TODO: This currently stretches SDL_BlitScaled(m_image_surface, nullptr, m_surface, nullptr); break; case RESIZE_STRETCH: SDL_BlitScaled(m_image_surface, nullptr, m_surface, nullptr); break; } } std::unique_ptr WidgetImage::builder(const nlohmann::json& j) { int x = 0; int y = 0; int width = 0; int height = 0; string filename; ImageResize resize_type = RESIZE_FIT; HorizontalAlign halign = HALIGN_CENTER; VerticalAlign valign = VALIGN_CENTER; SDL_Color bg_color = {.r = 255, .g = 255, .b = 255, .a = SDL_ALPHA_TRANSPARENT}; json_extract(j, "x", x); json_extract(j, "y", y); json_extract(j, "width", width); json_extract(j, "height", height); json_extract(j, "filename", filename); json_extract(j, "resize_type", resize_type); json_extract(j, "halign", halign); json_extract(j, "valign", valign); json_extract(j, "bg_color", bg_color); return std::make_unique(x, y, width, height, filename, resize_type, halign, valign, bg_color); }