Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Oct 19, 2024
1 parent 63ef34f commit 3693905
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 28 deletions.
30 changes: 29 additions & 1 deletion src/font.cpp
Original file line number Diff line number Diff line change
@@ -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(

Check warning on line 6 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:6:1 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: _glyphs
const std::shared_ptr<framework::resourcemanager> resourcemanager,
const std::string &filename,
const std::string &alphabet
) {
UNUSED(filename);
UNUSED(alphabet);

const auto texture = resourcemanager->pixmappool()->get(filename);
int32_t width, height;

Check warning on line 15 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:15:3 [readability-isolate-declaration]

multiple declarations in a single statement reduces readability

Check warning on line 15 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:15:11 [cppcoreguidelines-init-variables]

variable 'width' is not initialized

Check warning on line 15 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:15:18 [cppcoreguidelines-init-variables]

variable 'height' is not initialized
SDL_QueryTexture(*texture, NULL, NULL, &width, &height);

SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);

Check warning on line 18 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:18:16 [cppcoreguidelines-init-variables]

variable 'surface' is not initialized

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;

Check warning on line 24 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:24:13 [cppcoreguidelines-init-variables]

variable 'pixels' is not initialized
for (int32_t y = 0; y < height; ++y) {
for (int32_t x = 0; x < width; ++x) {
uint32_t pixel = pixels[(y * width) + x];

Check warning on line 27 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:27:24 [cppcoreguidelines-pro-bounds-pointer-arithmetic]

do not use pointer arithmetic
uint8_t r, g, b, a;

Check warning on line 28 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:28:7 [readability-isolate-declaration]

multiple declarations in a single statement reduces readability

Check warning on line 28 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:28:15 [cppcoreguidelines-init-variables]

variable 'r' is not initialized

Check warning on line 28 in src/font.cpp

View workflow job for this annotation

GitHub Actions / lint

src/font.cpp:28:18 [cppcoreguidelines-init-variables]

variable 'g' is not initialized
SDL_GetRGBA(pixel, surface->format, &r, &g, &b, &a);
}
}

SDL_FreeSurface(surface);
}

// void font::update(double_t delta) {
Expand Down
2 changes: 1 addition & 1 deletion src/font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> pixmappool, const std::string &filename, const std::string &alphabet);
~font() = default;

// void update(double_t delta);
Expand Down
8 changes: 4 additions & 4 deletions src/glyph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
// }
50 changes: 35 additions & 15 deletions src/pixmap.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "pixmap.hpp"

#include "deleters.hpp"
#include "helpers.hpp"
#include "io.hpp"
#include "rect.hpp"
#include "renderer.hpp"
Expand All @@ -20,58 +19,67 @@ pixmap::pixmap(const std::shared_ptr<renderer> 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;
size_t length = 0;
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<uint8_t> 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<int32_t>(ihdr.width),
static_cast<int32_t>(ihdr.height)};
static_cast<int32_t>(ihdr.height)
};

std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> surface{
SDL_CreateRGBSurfaceWithFormat(
0,
_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)
);
}
}

Expand All @@ -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<SDL_RendererFlip>(flip));
*_renderer,
_texture.get(),
&src,
&dst,
angle,
nullptr,
static_cast<SDL_RendererFlip>(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();
}
5 changes: 4 additions & 1 deletion src/pixmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> _renderer;

Expand Down
14 changes: 10 additions & 4 deletions src/resourcemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ resourcemanager::resourcemanager(
const std::shared_ptr<graphics::renderer> renderer,
const std::shared_ptr<audio::audiodevice> audiodevice
)
: _pixmappool(std::make_shared<graphics::pixmappool>(renderer)),
_soundmanager(std::make_shared<audio::soundmanager>(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);
};
}

Expand Down Expand Up @@ -52,6 +54,10 @@ bool resourcemanager::busy() const {
return !_filenames.empty();
}

std::shared_ptr<graphics::renderer> resourcemanager::renderer() const {
return _renderer;
}

std::shared_ptr<graphics::pixmappool> resourcemanager::pixmappool() {
return _pixmappool;
}
Expand Down
7 changes: 5 additions & 2 deletions src/resourcemanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ class resourcemanager {

bool busy() const;

std::shared_ptr<graphics::renderer> renderer() const;

std::shared_ptr<graphics::pixmappool> pixmappool();

std::shared_ptr<audio::soundmanager> soundmanager();

private:
std::map<std::string, std::function<void(const std::string &)>>
_handlers;
std::map<std::string, std::function<void(const std::string &)>> _handlers;

std::list<std::string> _filenames;
std::shared_ptr<graphics::renderer> _renderer;
std::shared_ptr<audio::audiodevice> _audiodevice;
std::shared_ptr<graphics::pixmappool> _pixmappool;
std::shared_ptr<audio::soundmanager> _soundmanager;
};
Expand Down

0 comments on commit 3693905

Please sign in to comment.