2dkg/2dkg_zad5/2dgk_zad5/KTexture.cpp
2022-01-20 08:52:14 +01:00

156 lines
3.4 KiB
C++

#include "KTexture.h"
#include <SDL_image.h>
#include <SDL_render.h>
namespace KapitanGame {
KTexture::KTexture() : Texture(nullptr), Width(0), Height(0)
{
}
KTexture::KTexture(KTexture&& other) noexcept
: Texture(other.Texture),
Width(other.Width),
Height(other.Height) {
other.Texture = nullptr;
other.Width = 0;
other.Height = 0;
}
KTexture& KTexture::operator=(KTexture&& other) noexcept {
if (this == &other)
return *this;
Free();
Texture = other.Texture;
Width = other.Width;
Height = other.Height;
other.Texture = nullptr;
other.Width = 0;
other.Height = 0;
return *this;
}
KTexture::~KTexture()
{
//Deallocate
Free();
}
bool KTexture::LoadFromFile(const std::string& path, SDL_Renderer* renderer)
{
//Get rid of preexisting texture
Free();
//The final texture
SDL_Texture* newTexture = nullptr;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == nullptr)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{
//Color key image
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0xFF, 0, 0xFF));
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == nullptr)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
//Get image dimensions
Width = loadedSurface->w;
Height = loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
//Return success
Texture = newTexture;
return Texture != nullptr;
}
bool KTexture::LoadFromSurface(SDL_Surface* surface, SDL_Renderer* renderer)
{
Free();
Texture = SDL_CreateTextureFromSurface(renderer, surface);
if (Texture == nullptr)
{
printf("Unable to create texture from surface! SDL Error: %s\n", SDL_GetError());
}
else
{
Width = surface->w;
Height = surface->h;
}
return Texture != nullptr;
}
void KTexture::Free()
{
//Free texture if it exists
if (Texture != nullptr)
{
SDL_DestroyTexture(Texture);
Texture = nullptr;
Width = 0;
Height = 0;
}
}
void KTexture::Render(SDL_Renderer* renderer, const float x, const float y, SDL_Rect* clip, const float scale) const
{
if (Texture == nullptr) return;
//Set rendering space and render to screen
SDL_FRect renderQuad = {
x * scale, y * scale,
static_cast<float>(Width) * scale, static_cast<float>(Height) * scale
};
//Set clip rendering dimensions
if (clip != nullptr)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopyF(renderer, Texture, clip, &renderQuad);
}
void KTexture::RenderEx(SDL_Renderer* renderer, const float x, const float y, const double degrees, SDL_Rect* clip, const float scale, const SDL_RendererFlip flip) const
{
if (Texture == nullptr) return;
//Set rendering space and render to screen
SDL_FRect renderQuad = {
x * scale, y * scale,
static_cast<float>(Width) * scale, static_cast<float>(Height) * scale
};
//Set clip rendering dimensions
if (clip != nullptr)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopyExF(renderer, Texture, clip, &renderQuad, degrees, nullptr, flip);
}
int KTexture::GetWidth() const
{
return Width;
}
int KTexture::GetHeight() const
{
return Height;
}
}