mirror of
https://github.com/SFML/SFML.git
synced 2025-01-19 15:55:13 +08:00
Manage memory with std::unique_ptr
This commit is contained in:
parent
4ec85b932e
commit
7e927c488b
@ -40,6 +40,7 @@
|
|||||||
#include <stb_image_write.h>
|
#include <stb_image_write.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <memory>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
@ -75,6 +76,16 @@ void bufferFromCallback(void* context, void* data, int size)
|
|||||||
auto* dest = static_cast<std::vector<std::uint8_t>*>(context);
|
auto* dest = static_cast<std::vector<std::uint8_t>*>(context);
|
||||||
std::copy(source, source + size, std::back_inserter(*dest));
|
std::copy(source, source + size, std::back_inserter(*dest));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deleter for STB pointers
|
||||||
|
struct StbDeleter
|
||||||
|
{
|
||||||
|
void operator()(stbi_uc* image) const
|
||||||
|
{
|
||||||
|
stbi_image_free(image);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
using StbPtr = std::unique_ptr<stbi_uc, StbDeleter>;
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
@ -155,7 +166,7 @@ bool Image::loadFromFile(const std::filesystem::path& filename)
|
|||||||
int width = 0;
|
int width = 0;
|
||||||
int height = 0;
|
int height = 0;
|
||||||
int channels = 0;
|
int channels = 0;
|
||||||
auto* ptr = stbi_load(filename.string().c_str(), &width, &height, &channels, STBI_rgb_alpha);
|
const auto ptr = StbPtr(stbi_load(filename.string().c_str(), &width, &height, &channels, STBI_rgb_alpha));
|
||||||
|
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
@ -163,10 +174,7 @@ bool Image::loadFromFile(const std::filesystem::path& filename)
|
|||||||
m_size = Vector2u(Vector2i(width, height));
|
m_size = Vector2u(Vector2i(width, height));
|
||||||
|
|
||||||
// Copy the loaded pixels to the pixel buffer
|
// Copy the loaded pixels to the pixel buffer
|
||||||
m_pixels.assign(ptr, ptr + width * height * 4);
|
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
|
||||||
|
|
||||||
// Free the loaded pixels (they are now in our own pixel buffer)
|
|
||||||
stbi_image_free(ptr);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -202,7 +210,8 @@ bool Image::loadFromMemory(const void* data, std::size_t size)
|
|||||||
int height = 0;
|
int height = 0;
|
||||||
int channels = 0;
|
int channels = 0;
|
||||||
const auto* buffer = static_cast<const unsigned char*>(data);
|
const auto* buffer = static_cast<const unsigned char*>(data);
|
||||||
auto* ptr = stbi_load_from_memory(buffer, static_cast<int>(size), &width, &height, &channels, STBI_rgb_alpha);
|
const auto ptr = StbPtr(
|
||||||
|
stbi_load_from_memory(buffer, static_cast<int>(size), &width, &height, &channels, STBI_rgb_alpha));
|
||||||
|
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
@ -210,10 +219,7 @@ bool Image::loadFromMemory(const void* data, std::size_t size)
|
|||||||
m_size = Vector2u(Vector2i(width, height));
|
m_size = Vector2u(Vector2i(width, height));
|
||||||
|
|
||||||
// Copy the loaded pixels to the pixel buffer
|
// Copy the loaded pixels to the pixel buffer
|
||||||
m_pixels.assign(ptr, ptr + width * height * 4);
|
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
|
||||||
|
|
||||||
// Free the loaded pixels (they are now in our own pixel buffer)
|
|
||||||
stbi_image_free(ptr);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -256,7 +262,7 @@ bool Image::loadFromStream(InputStream& stream)
|
|||||||
int width = 0;
|
int width = 0;
|
||||||
int height = 0;
|
int height = 0;
|
||||||
int channels = 0;
|
int channels = 0;
|
||||||
auto* ptr = stbi_load_from_callbacks(&callbacks, &stream, &width, &height, &channels, STBI_rgb_alpha);
|
const auto ptr = StbPtr(stbi_load_from_callbacks(&callbacks, &stream, &width, &height, &channels, STBI_rgb_alpha));
|
||||||
|
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
@ -264,10 +270,7 @@ bool Image::loadFromStream(InputStream& stream)
|
|||||||
m_size = Vector2u(Vector2i(width, height));
|
m_size = Vector2u(Vector2i(width, height));
|
||||||
|
|
||||||
// Copy the loaded pixels to the pixel buffer
|
// Copy the loaded pixels to the pixel buffer
|
||||||
m_pixels.assign(ptr, ptr + width * height * 4);
|
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
|
||||||
|
|
||||||
// Free the loaded pixels (they are now in our own pixel buffer)
|
|
||||||
stbi_image_free(ptr);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user