mirror of
https://github.com/Maschell/SDL2_Playground.git
synced 2024-12-26 05:11:48 +01:00
GuiImage: Rewrite as GuiTexture
This commit is contained in:
parent
0ca699b4e7
commit
4f1e6c6fac
@ -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)
|
||||
|
@ -18,70 +18,8 @@
|
||||
#include <iostream>
|
||||
#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;
|
||||
|
@ -18,9 +18,10 @@
|
||||
|
||||
#include <SDL2/SDL_render.h>
|
||||
#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;
|
||||
};
|
||||
|
71
src/gui/GuiTexture.cpp
Normal file
71
src/gui/GuiTexture.cpp
Normal 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
19
src/gui/GuiTexture.h
Normal 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;
|
||||
};
|
Loading…
Reference in New Issue
Block a user