mirror of
https://github.com/Maschell/SDL2_Playground.git
synced 2024-06-03 07:58:49 +02:00
104 lines
2.6 KiB
C++
104 lines
2.6 KiB
C++
#include <SDL2/SDL_image.h>
|
|
#include "GuiTexture.h"
|
|
#include "../CVideo.h"
|
|
#include "../logger.h"
|
|
|
|
GuiTexture::GuiTexture(const std::string& path) {
|
|
imgSurface = IMG_Load( path.c_str() );
|
|
|
|
if(!imgSurface){
|
|
return;
|
|
}
|
|
this->width = imgSurface->w;
|
|
this->height = imgSurface->h;
|
|
}
|
|
|
|
GuiTexture::GuiTexture(SDL_Texture *texture) {
|
|
if (this->texture) {
|
|
SDL_DestroyTexture(this->texture);
|
|
this->texture = NULL;
|
|
}
|
|
this->texture = texture;
|
|
int w, h;
|
|
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
|
|
this->setSize(w, h);
|
|
|
|
}
|
|
|
|
GuiTexture::GuiTexture(SDL_Surface *pSurface) {
|
|
if(!pSurface){
|
|
return;
|
|
}
|
|
imgSurface = pSurface;
|
|
|
|
this->width = imgSurface->w;
|
|
this->height = imgSurface->h;
|
|
}
|
|
|
|
/**
|
|
* Destructor for the GuiImage class.
|
|
*/
|
|
GuiTexture::~GuiTexture() {
|
|
if (this->imgSurface) {
|
|
SDL_FreeSurface(this->imgSurface);
|
|
this->imgSurface = nullptr;
|
|
}
|
|
if (this->texture) {
|
|
SDL_DestroyTexture(this->texture);
|
|
this->texture = nullptr;
|
|
}
|
|
}
|
|
|
|
void GuiTexture::draw(CVideo *pVideo) {
|
|
if (!this->isVisible()) {
|
|
DEBUG_FUNCTION_LINE("not visible!");
|
|
return;
|
|
}
|
|
|
|
if (texture == NULL && imgSurface) {
|
|
SDL_Surface *optimizedSurface = SDL_ConvertSurfaceFormat(imgSurface, pVideo->getPixelFormat(), 0);
|
|
if (optimizedSurface != NULL) {
|
|
SDL_FreeSurface(imgSurface);
|
|
imgSurface = optimizedSurface;
|
|
DEBUG_FUNCTION_LINE("Optimized surface");
|
|
}
|
|
texture = SDL_CreateTextureFromSurface(pVideo->getRenderer(), imgSurface);
|
|
}
|
|
if (!texture) {
|
|
DEBUG_FUNCTION_LINE("no texture!");
|
|
return;
|
|
}
|
|
|
|
float currScaleX = getScaleX();
|
|
float currScaleY = getScaleY();
|
|
|
|
SDL_Rect rect;
|
|
rect.x = getLeft();
|
|
rect.y = getTop();
|
|
rect.w = currScaleX * getWidth();
|
|
rect.h = currScaleY * getHeight();
|
|
|
|
// copy the texture to the rendering context
|
|
SDL_BlendMode mode;
|
|
SDL_GetRenderDrawBlendMode(pVideo->getRenderer(), &mode);
|
|
|
|
// adjust blend mode
|
|
if(blendMode != mode){
|
|
SDL_SetRenderDrawBlendMode(pVideo->getRenderer(), blendMode);
|
|
}
|
|
if (getAngle() == 0) {
|
|
SDL_RenderCopy(pVideo->getRenderer(), texture, NULL, &rect);
|
|
} else {
|
|
SDL_RenderCopyEx(pVideo->getRenderer(), texture, NULL, &rect, getAngle(), NULL, SDL_FLIP_NONE);
|
|
}
|
|
// Restore blend mode
|
|
if(blendMode != mode) {
|
|
SDL_SetRenderDrawBlendMode(pVideo->getRenderer(), mode);
|
|
}
|
|
}
|
|
|
|
void GuiTexture::setBlend(SDL_BlendMode blendmode) {
|
|
this->blendMode = blendMode;
|
|
}
|
|
|