GuiImage: Rewrite as GuiTexture

This commit is contained in:
Maschell 2020-08-30 21:32:48 +02:00
parent 0ca699b4e7
commit 4f1e6c6fac
5 changed files with 95 additions and 73 deletions

View File

@ -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/SDLController.h src/MainWindow.cpp src/MainWindow.h src/gui/SDLControllerJoystick.h src/gui/SDLControllerMouse.h
src/gui/SDLControllerWiiUGamepad.h src/gui/SDLControllerWiiUGamepad.h
src/gui/SDLControllerWiiUProContoller.h src/gui/SDLControllerWiiUProContoller.h
src/gui/GuiTexture.cpp src/gui/GuiTexture.h
) )
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)

View File

@ -18,70 +18,8 @@
#include <iostream> #include <iostream>
#include "GuiImage.h" #include "GuiImage.h"
#include "../CVideo.h" #include "../CVideo.h"
#include "../logger.h"
GuiImage::GuiImage(const std::string& path) { GuiImage::GuiImage(const std::string& path) : GuiTexture(path){
imgSurface = IMG_Load( path.c_str() );
this->width = imgSurface->w;
this->height = imgSurface->h;
} }
/** GuiImage::~GuiImage() = default;
* 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);
}
}

View File

@ -18,9 +18,10 @@
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include "GuiElement.h" #include "GuiElement.h"
#include "GuiTexture.h"
//!Display, manage, and manipulate images in the GUI //!Display, manage, and manipulate images in the GUI
class GuiImage : public GuiElement { class GuiImage : public GuiTexture {
public: public:
//!\overload //!\overload
//!\param img Pointer to GuiImageData element //!\param img Pointer to GuiImageData element
@ -28,12 +29,4 @@ public:
//!Destructor //!Destructor
~GuiImage() override; ~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;
}; };

71
src/gui/GuiTexture.cpp Normal file
View File

@ -0,0 +1,71 @@
#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_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);
}
}

19
src/gui/GuiTexture.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <SDL2/SDL_render.h>
#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;
};