From 4f1e6c6fac3e679fae0949fc0adca6c8c6990fcf Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 30 Aug 2020 21:32:48 +0200 Subject: [PATCH] GuiImage: Rewrite as GuiTexture --- CMakeLists.txt | 1 + src/gui/GuiImage.cpp | 66 ++------------------------------------- src/gui/GuiImage.h | 11 ++----- src/gui/GuiTexture.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++ src/gui/GuiTexture.h | 19 +++++++++++ 5 files changed, 95 insertions(+), 73 deletions(-) create mode 100644 src/gui/GuiTexture.cpp create mode 100644 src/gui/GuiTexture.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ab03aa..e411438 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(${PROJECT_NAME} src/gui/SDLController.h src/MainWindow.cpp src/MainWindow.h src/gui/SDLControllerJoystick.h src/gui/SDLControllerMouse.h src/gui/SDLControllerWiiUGamepad.h src/gui/SDLControllerWiiUProContoller.h + src/gui/GuiTexture.cpp src/gui/GuiTexture.h ) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2) diff --git a/src/gui/GuiImage.cpp b/src/gui/GuiImage.cpp index 122ce07..038a30e 100644 --- a/src/gui/GuiImage.cpp +++ b/src/gui/GuiImage.cpp @@ -18,70 +18,8 @@ #include #include "GuiImage.h" #include "../CVideo.h" -#include "../logger.h" -GuiImage::GuiImage(const std::string& path) { - imgSurface = IMG_Load( path.c_str() ); - - this->width = imgSurface->w; - this->height = imgSurface->h; +GuiImage::GuiImage(const std::string& path) : GuiTexture(path){ } -/** - * Destructor for the GuiImage class. - */ -GuiImage::~GuiImage() { - if(imgSurface){ - SDL_FreeSurface(imgSurface); - imgSurface = NULL; - } - if(texture){ - SDL_DestroyTexture(texture); - texture = NULL; - } -} - -void GuiImage::process(){ - auto res = this->getAngle() + 1; - if(res > 360){ - res =0; - } - - setAngle(res); - -} - -void GuiImage::draw(CVideo *pVideo) { - if (!this->isVisible()) { - return; - } - - if(texture == NULL){ - 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); - } - - 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 - if(getAngle() == 0){ - SDL_RenderCopy(pVideo->getRenderer(), texture, NULL, &rect); - }else{ - SDL_RenderCopyEx(pVideo->getRenderer(), texture, NULL, &rect, getAngle(), NULL, SDL_FLIP_NONE); - } - - -} +GuiImage::~GuiImage() = default; diff --git a/src/gui/GuiImage.h b/src/gui/GuiImage.h index f4f3654..36bad71 100644 --- a/src/gui/GuiImage.h +++ b/src/gui/GuiImage.h @@ -18,9 +18,10 @@ #include #include "GuiElement.h" +#include "GuiTexture.h" //!Display, manage, and manipulate images in the GUI -class GuiImage : public GuiElement { +class GuiImage : public GuiTexture { public: //!\overload //!\param img Pointer to GuiImageData element @@ -28,12 +29,4 @@ public: //!Destructor ~GuiImage() override; - - //!Constantly called to draw the image - void draw(CVideo *pVideo) override; -protected: - SDL_Surface *imgSurface = nullptr; - SDL_Texture *texture = nullptr; - - void process() override; }; diff --git a/src/gui/GuiTexture.cpp b/src/gui/GuiTexture.cpp new file mode 100644 index 0000000..a727dff --- /dev/null +++ b/src/gui/GuiTexture.cpp @@ -0,0 +1,71 @@ +#include +#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_Surface *pSurface) { + if(!pSurface){ + return; + } + imgSurface = pSurface; + + this->width = imgSurface->w; + this->height = imgSurface->h; +} + +/** + * Destructor for the GuiImage class. + */ +GuiTexture::~GuiTexture() { + if(imgSurface){ + SDL_FreeSurface(imgSurface); + imgSurface = NULL; + } + if(texture){ + SDL_DestroyTexture(texture); + texture = NULL; + } +} + +void GuiTexture::draw(CVideo *pVideo) { + if (!this->isVisible()) { + return; + } + + if(texture == NULL){ + 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); + } + + 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 + if(getAngle() == 0){ + SDL_RenderCopy(pVideo->getRenderer(), texture, NULL, &rect); + }else{ + SDL_RenderCopyEx(pVideo->getRenderer(), texture, NULL, &rect, getAngle(), NULL, SDL_FLIP_NONE); + } +} diff --git a/src/gui/GuiTexture.h b/src/gui/GuiTexture.h new file mode 100644 index 0000000..e229715 --- /dev/null +++ b/src/gui/GuiTexture.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include "GuiElement.h" + +class GuiTexture : public GuiElement { +public: + explicit GuiTexture(const std::string &path); + + GuiTexture(SDL_Surface *pSurface); + +//!Destructor + ~GuiTexture() override; + + //!Constantly called to draw the image + void draw(CVideo *pVideo) override; +protected: + SDL_Surface *imgSurface = nullptr; + SDL_Texture *texture = nullptr; +};