mirror of
https://github.com/Maschell/SDL2_Playground.git
synced 2024-11-27 07:04:16 +01:00
Update GuiImage to allow to be set by color instead of a texture
This commit is contained in:
parent
27c96611b5
commit
122f6f991c
@ -22,6 +22,11 @@ GuiImage::GuiImage(GuiTextureData *texture) {
|
||||
setTexture(texture);
|
||||
}
|
||||
|
||||
GuiImage::GuiImage(SDL_Color color, float width, float height) {
|
||||
this->color = color;
|
||||
this->setSize(width, height);
|
||||
}
|
||||
|
||||
GuiImage::~GuiImage() {
|
||||
if (this->texture && freeTextureData) {
|
||||
delete this->texture;
|
||||
@ -33,13 +38,20 @@ void GuiImage::draw(Renderer *renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (texture) {
|
||||
SDL_Rect rect;
|
||||
rect.x = (int) getLeft();
|
||||
rect.y = (int) getTop();
|
||||
rect.w = (int) (getScaleX() * getWidth());
|
||||
rect.h = (int) (getScaleY() * getHeight());
|
||||
|
||||
if (texture) {
|
||||
texture->draw(renderer, rect, getAngle());
|
||||
} else {
|
||||
SDL_SetRenderDrawColor(renderer->getRenderer(), color.r, color.g, color.b, color.a);
|
||||
SDL_RenderFillRect(renderer->getRenderer(), &rect);
|
||||
if(getAngle() != 0.0f){
|
||||
DEBUG_FUNCTION_LINE("Drawing a rotated rect is not supported yet");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,19 +23,28 @@
|
||||
//!Display, manage, and manipulate images in the GUI
|
||||
class GuiImage : public GuiElement {
|
||||
public:
|
||||
//!\overload
|
||||
//!\param img Pointer to GuiImageData element
|
||||
|
||||
GuiImage() = default;
|
||||
|
||||
//! Draws an image from an existing texture
|
||||
//!\param texture Pointer to GuiTextureData element
|
||||
explicit GuiImage(GuiTextureData *texture);
|
||||
|
||||
//! Draws a colored rectangle
|
||||
//!\param texture Pointer to GuiTextureData element
|
||||
explicit GuiImage(SDL_Color color, float width, float height);
|
||||
|
||||
//!Destructor
|
||||
~GuiImage() override;
|
||||
|
||||
void draw(Renderer *r) override;
|
||||
|
||||
void setTexture(GuiTextureData *tex);
|
||||
|
||||
private:
|
||||
GuiTextureData *texture = nullptr;
|
||||
bool freeTextureData = false;
|
||||
|
||||
// Color of the rect that's drawn if the picture has no texture.
|
||||
SDL_Color color = {0, 0, 0, 0};
|
||||
};
|
||||
|
@ -76,10 +76,18 @@ void GuiText::updateTexture(Renderer *renderer) {
|
||||
textureData->setBlendMode(SDL_BLENDMODE_BLEND);
|
||||
texture.setTexture(textureData);
|
||||
|
||||
// Set render target to texture
|
||||
SDL_SetRenderTarget(renderer->getRenderer(), temp);
|
||||
|
||||
// Clear texture.
|
||||
SDL_SetRenderDrawColor(renderer->getRenderer(), 0, 0, 0, 0);
|
||||
SDL_RenderClear(renderer->getRenderer());
|
||||
|
||||
// Draw text to texture
|
||||
FC_DrawColumn(fc_font, renderer->getRenderer(), 0, 0, maxWidth, text.c_str());
|
||||
SDL_SetRenderTarget(renderer->getRenderer(), NULL);
|
||||
|
||||
// Restore render target
|
||||
SDL_SetRenderTarget(renderer->getRenderer(), nullptr);
|
||||
} else {
|
||||
DEBUG_FUNCTION_LINE("Failed to create texture");
|
||||
}
|
||||
|
@ -59,11 +59,6 @@ GuiTextureData::~GuiTextureData() {
|
||||
|
||||
void GuiTextureData::draw(Renderer *renderer, const SDL_Rect& dest, float angle) {
|
||||
if (texture == nullptr && imgSurface) {
|
||||
SDL_Surface *optimizedSurface = SDL_ConvertSurfaceFormat(imgSurface, renderer->getPixelFormat(), 0);
|
||||
if (optimizedSurface != nullptr) {
|
||||
SDL_FreeSurface(imgSurface);
|
||||
imgSurface = optimizedSurface;
|
||||
}
|
||||
texture = SDL_CreateTextureFromSurface(renderer->getRenderer(), imgSurface);
|
||||
}
|
||||
if (!texture) {
|
||||
|
@ -17,12 +17,14 @@ MainWindow::~MainWindow() {
|
||||
delete bgMusic;
|
||||
}
|
||||
|
||||
MainWindow::MainWindow(int32_t w, int32_t h, Renderer* renderer) : GuiFrame(w, h) {
|
||||
MainWindow::MainWindow(int32_t w, int32_t h, Renderer* renderer) : GuiFrame(w, h), bgImage({100, 0, 0, 255}, w, h) {
|
||||
auto picture_path = "button.png";
|
||||
auto font_path = "FreeSans.ttf";
|
||||
auto bgMusic_path = "bgMusic.ogg";
|
||||
auto music_click = "button_click.mp3";
|
||||
|
||||
append(&bgImage);
|
||||
|
||||
TTF_Font *font;
|
||||
|
||||
SDL_RWops *rw = SDL_RWFromMem((void *) Resources::GetFile(font_path), Resources::GetFileSize(font_path));
|
||||
|
@ -25,5 +25,6 @@ private:
|
||||
GuiImage *image4 = nullptr;
|
||||
GuiImage *image5 = nullptr;
|
||||
GuiButton *button = nullptr;
|
||||
GuiImage bgImage;
|
||||
GuiSound *bgMusic = nullptr;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user