Initial image implementation
This commit is contained in:
84
Widgets/WidgetImage.cpp
Normal file
84
Widgets/WidgetImage.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "WidgetImage.h"
|
||||
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
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<Widget> 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<WidgetImage>(x, y, width, height, filename,
|
||||
resize_type, halign, valign, bg_color);
|
||||
}
|
||||
47
Widgets/WidgetImage.h
Normal file
47
Widgets/WidgetImage.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef WIDGET_IMAGE_H_
|
||||
#define WIDGET_IMAGE_H_
|
||||
|
||||
#include "Widget.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../sdl_helpers.h"
|
||||
|
||||
class WidgetImage : public Widget
|
||||
{
|
||||
protected:
|
||||
// Image filepath
|
||||
std::string m_filename;
|
||||
|
||||
// Image surface
|
||||
SDL_Surface* m_image_surface;
|
||||
|
||||
// Whether to resize the image and how
|
||||
// RESIZE_NONE - Do not resize
|
||||
// RESIZE_FIT - Scale the image to fit the rectangle
|
||||
// RESIZE_STRETCH - Scale and stretch the image to fit the box perfectly
|
||||
ImageResize m_resize_type;
|
||||
|
||||
// Default - center
|
||||
HorizontalAlign m_halign;
|
||||
|
||||
// Default - center
|
||||
VerticalAlign m_valign;
|
||||
|
||||
// Background color to be used
|
||||
// Default - transparent white
|
||||
SDL_Color m_bg_color;
|
||||
|
||||
public:
|
||||
WidgetImage(int x, int y, int width, int height, std::string filename,
|
||||
ImageResize resize_type = RESIZE_FIT,
|
||||
HorizontalAlign halign = HALIGN_CENTER,
|
||||
VerticalAlign valign = VALIGN_CENTER,
|
||||
SDL_Color bg_color = SDL_Color{.r = 255, .g = 255, .b = 255, .a = SDL_ALPHA_TRANSPARENT});
|
||||
|
||||
virtual void draw() override;
|
||||
|
||||
static std::unique_ptr<Widget> builder(const nlohmann::json& j);
|
||||
};
|
||||
|
||||
#endif // WIDGET_IMAGE_H_
|
||||
Reference in New Issue
Block a user