sdl2_impl 0.1.0
Vendor SDL2 2.32 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SdlTexture.hpp
Go to the documentation of this file.
1
11#ifndef SDLTEXTURE_HPP_
12#define SDLTEXTURE_HPP_
13
14//Sdl
15#include <SDL.h>
16#include <SDL_image.h>
17
18//Interface
19#include "ITexture.hpp"
20
21#include <iostream>
22#include <string>
23
38class SdlTexture : public graphic::ITexture {
39
40 public:
41 SdlTexture(std::string path) {
42 _surface = IMG_Load(path.c_str());
43 if (!_surface)
44 std::cerr << "Failed to load texture: " << IMG_GetError() << std::endl;
45 }
46
48 drop();
49 if (_surface)
50 SDL_FreeSurface(_surface);
51 }
52
53 bool isReady() const override {
54 return _surface != nullptr;
55 }
56
57 Vector2f getSize() const override {
58 if (!_surface)
59 return {};
60 return {static_cast<double>(_surface->w), static_cast<double>(_surface->h)};
61 }
62
64 SDL_Texture *handle(SDL_Renderer *renderer) const {
65 if (!_surface || !renderer)
66 return nullptr;
67 if (_texture && _owner == renderer)
68 return _texture;
69
70 drop();
71 _texture = SDL_CreateTextureFromSurface(renderer, _surface);
72 _owner = renderer;
73 return _texture;
74 }
75
76 private:
77 void drop() const {
78 if (_texture)
79 SDL_DestroyTexture(_texture);
80 _texture = nullptr;
81 _owner = nullptr;
82 }
83
84 SDL_Surface *_surface = nullptr;
85
86 /* mutable : le cache n'est pas l'etat de l'image, juste sa forme
87 * televersee pour un renderer donne. */
88 mutable SDL_Texture *_texture = nullptr;
89 mutable SDL_Renderer *_owner = nullptr;
90};
91
94#endif /* !SDLTEXTURE_HPP_ */
Une image en memoire vive, televersee a la demande.
Definition SdlTexture.hpp:38
Vector2f getSize() const override
Definition SdlTexture.hpp:57
SDL_Texture * handle(SDL_Renderer *renderer) const
La texture pour CE renderer, fabriquee au besoin.
Definition SdlTexture.hpp:64
bool isReady() const override
Definition SdlTexture.hpp:53
SdlTexture(std::string path)
Definition SdlTexture.hpp:41
~SdlTexture()
Definition SdlTexture.hpp:47