11#include < SDL_image.h>
22#include < memory>
3+ #include < System/BinaryStreamReader.hpp>
34
45#include " SpriteResource.hpp"
56#include " System/Logger.hpp"
67#include " Renderer/Texture.hpp"
78
89struct TextureManager
910{
10- void AddTexture (Texture* texture)
11+ Texture* AddTexture (std::unique_ptr< Texture> texture)
1112 {
12- textures.emplace_back (texture);
13+ auto ptr = texture.get ();
14+ textures.emplace_back (std::move (texture));
15+ return ptr;
1316 }
1417
1518 std::vector<std::unique_ptr<Texture>> textures;
@@ -32,11 +35,28 @@ bool SpriteResource::LoadFromFile(const ResourceSettings& settings)
3235 return false ;
3336 }
3437
35- auto texture = new Texture (surface);
36- g_textureManager.AddTexture (texture);
38+ auto texture = g_textureManager.AddTexture (std::make_unique<Texture>(surface));
3739 SDL_FreeSurface (surface);
3840
39- sprite = Sprite (texture, Rectangle (Vector2 (0 , 0 ), texture->Size ()));
41+ _resource. emplace (texture, Rectangle (Vector2 (0 , 0 ), texture->Size ()));
4042
4143 return true ;
4244}
45+
46+ bool SpriteResource::WriteToBinary (const ResourceSettings& settings, BinaryStreamWriter& writer)
47+ {
48+ return writer.TryWriteFile (settings.path );
49+ }
50+
51+ bool SpriteResource::LoadFromBinary (BinaryStreamReader& reader)
52+ {
53+ const unsigned char * data = reader.Data ();
54+ auto ops = SDL_RWFromMem (const_cast <void *>(reinterpret_cast <const void *>(data)), reader.TotalSize ());
55+ SDL_Surface* loadedSurface = IMG_LoadPNG_RW (ops);
56+ auto texture = g_textureManager.AddTexture (std::make_unique<Texture>(loadedSurface));
57+ SDL_FreeSurface (loadedSurface);
58+
59+ _resource.emplace (texture, Rectangle (Vector2 (0 , 0 ), texture->Size ()));
60+
61+ return true ;
62+ }
0 commit comments