diff --git a/src/font.cpp b/src/font.cpp index 26d3a18..3649621 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -1,8 +1,36 @@ #include "font.hpp" +#include "resourcemanager.hpp" using namespace graphics; -font::font(const std::string_view filename, const std::string &alphabet) { +font::font( + const std::shared_ptr resourcemanager, + const std::string &filename, + const std::string &alphabet +) { + UNUSED(filename); + UNUSED(alphabet); + + const auto texture = resourcemanager->pixmappool()->get(filename); + int32_t width, height; + SDL_QueryTexture(*texture, NULL, NULL, &width, &height); + + SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); + + if (SDL_RenderReadPixels(*resourcemanager->renderer(), NULL, surface->format->format, surface->pixels, surface->pitch) != 0) { + printf("Error reading pixels: %s\n", SDL_GetError()); + } + + uint32_t *pixels = (uint32_t *)surface->pixels; + for (int32_t y = 0; y < height; ++y) { + for (int32_t x = 0; x < width; ++x) { + uint32_t pixel = pixels[(y * width) + x]; + uint8_t r, g, b, a; + SDL_GetRGBA(pixel, surface->format, &r, &g, &b, &a); + } + } + + SDL_FreeSurface(surface); } // void font::update(double_t delta) { diff --git a/src/font.hpp b/src/font.hpp index cd89013..c22952f 100644 --- a/src/font.hpp +++ b/src/font.hpp @@ -8,7 +8,7 @@ namespace graphics { class font { font() = delete; - font(const std::string_view filename, const std::string &alphabet); + font(const std::shared_ptr pixmappool, const std::string &filename, const std::string &alphabet); ~font() = default; // void update(double_t delta); diff --git a/src/glyph.cpp b/src/glyph.cpp index cddaeef..81d220f 100644 --- a/src/glyph.cpp +++ b/src/glyph.cpp @@ -2,8 +2,8 @@ using namespace graphics; -void glyph::update(double_t delta) { -} +// void glyph::update(double_t delta) { +// } -void glyph::draw() const { -} +// void glyph::draw() const { +// } diff --git a/src/pixmap.cpp b/src/pixmap.cpp index 099d1c0..16ae63d 100644 --- a/src/pixmap.cpp +++ b/src/pixmap.cpp @@ -1,7 +1,6 @@ #include "pixmap.hpp" #include "deleters.hpp" -#include "helpers.hpp" #include "io.hpp" #include "rect.hpp" #include "renderer.hpp" @@ -20,17 +19,20 @@ pixmap::pixmap(const std::shared_ptr renderer, std::string_view filena if (ptr) { spng_ctx_free(ptr); } - }); + } + ); if (const auto error = spng_set_png_buffer(ctx.get(), buffer.data(), buffer.size()); error != SPNG_OK) { throw std::runtime_error( - fmt::format("[spng_set_png_buffer] error while parsing image: {}, error: {}", filename, spng_strerror(error))); + fmt::format("[spng_set_png_buffer] error while parsing image: {}, error: {}", filename, spng_strerror(error)) + ); } spng_ihdr ihdr{}; if (const auto error = spng_get_ihdr(ctx.get(), &ihdr); error != SPNG_OK) { throw std::runtime_error( - fmt::format("[spng_get_ihdr] error while getting image information: {}, error: {}", filename, spng_strerror(error))); + fmt::format("[spng_get_ihdr] error while getting image information: {}, error: {}", filename, spng_strerror(error)) + ); } const int format = SPNG_FMT_RGBA8; @@ -38,18 +40,21 @@ pixmap::pixmap(const std::shared_ptr renderer, std::string_view filena if (const auto error = spng_decoded_image_size(ctx.get(), format, &length); error != SPNG_OK) { throw std::runtime_error( - fmt::format("[spng_decoded_image_size] error while getting image size: {}, error: {}", filename, spng_strerror(error))); + fmt::format("[spng_decoded_image_size] error while getting image size: {}, error: {}", filename, spng_strerror(error)) + ); } std::vector output(length); if (const auto error = spng_decode_image(ctx.get(), output.data(), length, format, SPNG_DECODE_TRNS); error != SPNG_OK) { throw std::runtime_error( - fmt::format("[spng_decode_image] error while decoding image: {}, error: {}", filename, spng_strerror(error))); + fmt::format("[spng_decode_image] error while decoding image: {}, error: {}", filename, spng_strerror(error)) + ); } _size = geometry::size{ static_cast(ihdr.width), - static_cast(ihdr.height)}; + static_cast(ihdr.height) + }; std::unique_ptr surface{ SDL_CreateRGBSurfaceWithFormat( @@ -57,21 +62,24 @@ pixmap::pixmap(const std::shared_ptr renderer, std::string_view filena _size.width(), _size.height(), 0, - SDL_PIXELFORMAT_ABGR8888), - SDL_FreeSurface}; + SDL_PIXELFORMAT_ABGR8888 + ), + SDL_FreeSurface + }; if (!surface) { throw std::runtime_error( - fmt::format("[SDL_CreateRGBSurfaceWithFormat] error while creating surface with format: {}, error {}", filename, SDL_GetError())); + fmt::format("[SDL_CreateRGBSurfaceWithFormat] error while creating surface with format: {}, error {}", filename, SDL_GetError()) + ); } std::memcpy(surface->pixels, output.data(), length); ctx.reset(); - _texture = texture_ptr(SDL_CreateTextureFromSurface(*renderer, surface.get()), - SDL_Deleter()); + _texture = texture_ptr(SDL_CreateTextureFromSurface(*renderer, surface.get()), SDL_Deleter()); if (!_texture) { throw std::runtime_error( - fmt::format("[SDL_CreateTextureFromSurface] error while creating texture from surface: {}", filename)); + fmt::format("[SDL_CreateTextureFromSurface] error while creating texture from surface: {}", filename) + ); } } @@ -80,15 +88,27 @@ void pixmap::draw( const geometry::rect &destination, const double_t angle, flip flip, - const uint8_t alpha) const { + const uint8_t alpha +) const { const SDL_Rect &src = source; const SDL_Rect &dst = destination; SDL_SetTextureAlphaMod(_texture.get(), alpha); SDL_RenderCopyEx( - *_renderer, _texture.get(), &src, &dst, angle, nullptr, static_cast(flip)); + *_renderer, + _texture.get(), + &src, + &dst, + angle, + nullptr, + static_cast(flip) + ); } const geometry::size pixmap::size() const { return _size; } void pixmap::set_size(const geometry::size &size) { _size = size; } + +pixmap::operator SDL_Texture *() const { + return _texture.get(); +} diff --git a/src/pixmap.hpp b/src/pixmap.hpp index f3fbe2e..d6fbe5d 100644 --- a/src/pixmap.hpp +++ b/src/pixmap.hpp @@ -23,12 +23,15 @@ class pixmap { const geometry::rect &destination, const double angle = 0.0, flip flip = flip::none, - const uint8_t alpha = 255) const; + const uint8_t alpha = 255 + ) const; const geometry::size size() const; void set_size(const geometry::size &size); + operator SDL_Texture *() const; + private: std::shared_ptr _renderer; diff --git a/src/resourcemanager.cpp b/src/resourcemanager.cpp index cc2dfa7..edd35fd 100644 --- a/src/resourcemanager.cpp +++ b/src/resourcemanager.cpp @@ -12,14 +12,16 @@ resourcemanager::resourcemanager( const std::shared_ptr renderer, const std::shared_ptr audiodevice ) - : _pixmappool(std::make_shared(renderer)), - _soundmanager(std::make_shared(audiodevice)) { + : _renderer(renderer), + _audiodevice(audiodevice), + _pixmappool(renderer), + _soundmanager(audiodevice) { _handlers[".png"] = [this](const std::string &filename) { - _pixmappool->get(filename); + return _pixmappool->get(filename); }; _handlers[".ogg"] = [this](const std::string &filename) { - _soundmanager->get(filename); + return _soundmanager->get(filename); }; } @@ -52,6 +54,10 @@ bool resourcemanager::busy() const { return !_filenames.empty(); } +std::shared_ptr resourcemanager::renderer() const { + return _renderer; +} + std::shared_ptr resourcemanager::pixmappool() { return _pixmappool; } diff --git a/src/resourcemanager.hpp b/src/resourcemanager.hpp index 9897e16..2d40be7 100644 --- a/src/resourcemanager.hpp +++ b/src/resourcemanager.hpp @@ -18,15 +18,18 @@ class resourcemanager { bool busy() const; + std::shared_ptr renderer() const; + std::shared_ptr pixmappool(); std::shared_ptr soundmanager(); private: - std::map> - _handlers; + std::map> _handlers; std::list _filenames; + std::shared_ptr _renderer; + std::shared_ptr _audiodevice; std::shared_ptr _pixmappool; std::shared_ptr _soundmanager; };