trmnl_sdl/Widgets/Widget.cpp

52 lines
946 B
C++

#include "Widget.h"
Widget::Widget(int x, int y, int width, int height)
: m_surface(nullptr), m_rect{.x = x, .y = y, .w = width, .h = height}
{
m_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_RGBA8888);
if (nullptr != m_surface)
{
SDL_SetSurfaceBlendMode(m_surface, SDL_BLENDMODE_BLEND);
}
}
Widget::~Widget()
{
if (nullptr != m_surface)
{
SDL_FreeSurface(m_surface);
}
}
SDL_Surface* Widget::get_internal_surface()
{
return m_surface;
}
SDL_Rect Widget::get_rect()
{
return m_rect;
}
void Widget::set_position(int x, int y)
{
m_rect.x = x;
m_rect.y = y;
}
void Widget::resize(int width, int height)
{
if (nullptr != m_surface)
{
SDL_FreeSurface(m_surface);
}
m_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_RGBA8888);
m_rect.w = width;
m_rect.h = height;
if (nullptr != m_surface)
{
SDL_SetSurfaceBlendMode(m_surface, SDL_BLENDMODE_BLEND);
}
}