Mercurial > malikania
changeset 9:35852c7d422a
Client: load SDL_Texture later to avoid using Window in Image constructor, #442
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 29 Mar 2016 20:18:32 +0200 |
parents | e33b246ac2d3 |
children | cff1d99eff11 |
files | libclient/malikania/backend/sdl/image-backend.cpp libclient/malikania/backend/sdl/image-backend.h libclient/malikania/client-resources-loader.cpp libclient/malikania/client-resources-loader.h libclient/malikania/image.cpp libclient/malikania/image.h tests/libclient/animation/main.cpp tests/libclient/image/main.cpp tests/libclient/sprite/main.cpp |
diffstat | 9 files changed, 38 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/libclient/malikania/backend/sdl/image-backend.cpp Wed Mar 23 20:36:23 2016 +0100 +++ b/libclient/malikania/backend/sdl/image-backend.cpp Tue Mar 29 20:18:32 2016 +0200 @@ -27,8 +27,18 @@ namespace malikania { -Image::Backend::Backend(Image &, Window &window, std::string data) - : m_texture(nullptr, nullptr) +void Image::Backend::createTexture(Window &window) +{ + m_texture = Texture(SDL_CreateTextureFromSurface(window.backend().renderer(), m_surface.get()), SDL_DestroyTexture); + + if (m_texture == nullptr) { + throw std::runtime_error(SDL_GetError()); + } +} + +Image::Backend::Backend(Image &, std::string data) + : m_surface(nullptr, nullptr) + , m_texture(nullptr, nullptr) { /* Initialize the texture */ auto rw = sdl::RWFromBinary(std::move(data)); @@ -37,24 +47,21 @@ throw std::runtime_error(SDL_GetError()); } - m_texture = Handle(IMG_LoadTexture_RW(window.backend().renderer(), rw, true), SDL_DestroyTexture); + m_surface = Surface(IMG_Load_RW(rw, true), SDL_FreeSurface); - if (m_texture == nullptr) { + if (!m_surface) { throw std::runtime_error(SDL_GetError()); } - /* Store the size */ - int width, height; - - if (SDL_QueryTexture(m_texture.get(), nullptr, nullptr, &width, &height) < 0) { - throw std::runtime_error(SDL_GetError()); - } - - m_size = Size((unsigned)width, (unsigned)height); + m_size = Size((unsigned)m_surface->w, (unsigned)m_surface->h); } void Image::Backend::draw(Window &window, const Point &point) { + if (!m_texture) { + createTexture(window); + } + SDL_Rect target; target.x = (int)point.x(); @@ -69,6 +76,10 @@ void Image::Backend::draw(Window &window, const Rectangle &source, const Rectangle &target) { + if (!m_texture) { + createTexture(window); + } + SDL_Rect sr, st; sr.x = source.x();
--- a/libclient/malikania/backend/sdl/image-backend.h Wed Mar 23 20:36:23 2016 +0100 +++ b/libclient/malikania/backend/sdl/image-backend.h Tue Mar 29 20:18:32 2016 +0200 @@ -35,13 +35,17 @@ class Image::Backend { private: - using Handle = std::unique_ptr<SDL_Texture, void (*)(SDL_Texture *)>; + using Surface = std::unique_ptr<SDL_Surface, void (*)(SDL_Surface *)>; + using Texture = std::unique_ptr<SDL_Texture, void (*)(SDL_Texture *)>; - Handle m_texture; + Surface m_surface; + Texture m_texture; Size m_size; + void createTexture(Window &window); + public: - Backend(Image &self, Window &window, std::string data); + Backend(Image &self, std::string data); inline SDL_Texture *texture() noexcept {
--- a/libclient/malikania/client-resources-loader.cpp Wed Mar 23 20:36:23 2016 +0100 +++ b/libclient/malikania/client-resources-loader.cpp Tue Mar 29 20:18:32 2016 +0200 @@ -59,7 +59,7 @@ Image ClientResourcesLoader::loadImage(const std::string &id) { - return Image(m_window, locator().read(id)); + return Image(locator().read(id)); } Sprite ClientResourcesLoader::loadSprite(const std::string &id)
--- a/libclient/malikania/client-resources-loader.h Wed Mar 23 20:36:23 2016 +0100 +++ b/libclient/malikania/client-resources-loader.h Tue Mar 29 20:18:32 2016 +0200 @@ -28,16 +28,11 @@ namespace malikania { -class Window; - /** * @class ClientResourcesLoader * @brief Load client resources. */ class ClientResourcesLoader : public ResourcesLoader { -private: - Window &m_window; - protected: /** * Require a size object from an object property. @@ -73,9 +68,8 @@ * @param window the window * @param locator the resources locator */ - inline ClientResourcesLoader(Window &window, ResourcesLocator &locator) + inline ClientResourcesLoader(ResourcesLocator &locator) : ResourcesLoader(locator) - , m_window(window) { }
--- a/libclient/malikania/image.cpp Wed Mar 23 20:36:23 2016 +0100 +++ b/libclient/malikania/image.cpp Tue Mar 29 20:18:32 2016 +0200 @@ -20,8 +20,8 @@ namespace malikania { -Image::Image(Window &window, std::string data) - : m_backend(std::make_unique<Backend>(*this, window, std::move(data))) +Image::Image(std::string data) + : m_backend(std::make_unique<Backend>(*this, std::move(data))) { }
--- a/libclient/malikania/image.h Wed Mar 23 20:36:23 2016 +0100 +++ b/libclient/malikania/image.h Tue Mar 29 20:18:32 2016 +0200 @@ -52,7 +52,7 @@ * @param window the window * @param data the data */ - Image(Window &window, std::string data); + Image(std::string data); /** * Default move constructor.
--- a/tests/libclient/animation/main.cpp Wed Mar 23 20:36:23 2016 +0100 +++ b/tests/libclient/animation/main.cpp Tue Mar 29 20:18:32 2016 +0200 @@ -46,7 +46,7 @@ public: TestAnimation() : m_locator(SOURCE_DIRECTORY "/resources") - , m_loader(window, m_locator) + , m_loader(m_locator) { } };