-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureWrap.cpp
More file actions
86 lines (71 loc) · 2.39 KB
/
Copy pathTextureWrap.cpp
File metadata and controls
86 lines (71 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// Created by luniminex on 10/7/23.
//
#include "TextureWrap.h"
TextureWrap::TextureWrap() {
texture_ = nullptr;
width_ = 0;
height_ = 0;
}
TextureWrap::TextureWrap(const std::string &path, SDL_Renderer *renderer) {
texture_ = nullptr;
width_ = 0;
height_ = 0;
LoadFromFile(path, renderer);
}
bool TextureWrap::LoadFromFile(const std::string &path, SDL_Renderer* renderer) {
if(path.empty()){
std::cout << "Path is empty" << std::endl;
return false;
}
if(texture_ != nullptr){
SDL_DestroyTexture(texture_);
}
SDL_Surface* surf = IMG_Load(path.c_str());
if(surf == nullptr){
std::cout << "Could not create a surface" << std::endl;
return false;
}
texture_ = SDL_CreateTextureFromSurface(renderer, surf);
if(texture_ == nullptr){
std::cout << "Could not create texture from surface" << std::endl;
SDL_FreeSurface(surf);
return false;
}
if(SDL_QueryTexture(texture_, nullptr, nullptr, &width_, &height_) != 0){
std::cout << "Error in texture" << std::endl;
}
SDL_FreeSurface(surf);
return true;
}
void TextureWrap::SetAsRenderTarget(SDL_Renderer *renderer) {
SDL_SetRenderTarget(renderer, texture_);
}
void TextureWrap::SetBlendMode(SDL_BlendMode blendMode) {
SDL_SetTextureBlendMode(texture_, blendMode);
}
bool TextureWrap::CreateBlankTexture(int width, int height, SDL_Renderer* renderer, SDL_TextureAccess textureAccess) {
texture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, textureAccess, width, height);
if(texture_ == nullptr){
std::cout << "Could not create a blank texture" << std::endl;
return false;
}
width_ = width;
height_ = height;
return true;
}
void TextureWrap::RenderTextureAt(int x, int y, int w, int h, SDL_Renderer *renderer) {
SDL_Rect destRect = { .x = x, .y = y, .w = w, .h = h};
SDL_RenderCopy(renderer, texture_, nullptr, &destRect);
}
void TextureWrap::RenderTextureAt(int x, int y, int w, int h, SDL_Renderer* renderer, SDL_Rect *clip, double angle, SDL_Point* center, SDL_RendererFlip flip) {
SDL_Rect renderDest = {x, y, w, h};
if (clip != nullptr) {
renderDest.w = clip->w;
renderDest.h = clip->h;
}
SDL_RenderCopyEx(renderer, texture_, clip, &renderDest, angle, center, flip);
}
TextureWrap::~TextureWrap() {
SDL_DestroyTexture(texture_);
}