Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Dec 14, 2024
1 parent 3843f5a commit 7e36afb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 75 deletions.
6 changes: 3 additions & 3 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ void entity::draw() const noexcept {
geometry::rect destination{_props.position + offset, source.size()};
destination.scale(_props.size.scale());

if (!_props.spritesheet) {
return;
}
// if (!_props.spritesheet) {
// return;
// }

_props.spritesheet->draw(
source,
Expand Down
117 changes: 59 additions & 58 deletions src/fontfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,76 @@ fontfactory::fontfactory(const std::shared_ptr<graphics::renderer> renderer) noe
: _renderer(std::move(renderer)) {}

std::shared_ptr<font> fontfactory::get(const std::string &family) {
auto [it, added] = _pool.insert_or_assign(family, nullptr);

if (added) [[unlikely]] {
fmt::println("[fontfactory] cache miss {}", family);

const auto &buffer = storage::io::read(fmt::format("fonts/{}.json", family));
const auto &j = json::parse(buffer);
const auto &alphabet = j["alphabet"].get<std::string>();
const auto spacing = j["spacing"].get<int16_t>();

std::vector<uint8_t> output;
geometry::size size;
std::tie(output, size) = _load_png(j["spritesheet"].get_ref<const std::string &>());

auto surface = std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)>{
SDL_CreateRGBSurfaceWithFormatFrom(
output.data(),
size.width(),
size.height(),
32,
size.width() * 4,
SDL_PIXELFORMAT_ABGR8888
),
SDL_FreeSurface
};

if (!surface) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError()));
}
if (auto it = _pool.find(family); it != _pool.end()) [[likely]] {
return it->second;
}

const auto pixels = static_cast<uint32_t *>(surface->pixels);
const auto separator = color(pixels[0], surface->format);
fmt::println("[fontfactory] cache miss {}", family);

const auto &buffer = storage::io::read(fmt::format("fonts/{}.json", family));
const auto &j = json::parse(buffer);
const auto &alphabet = j["alphabet"].get<std::string>();
const auto spacing = j["spacing"].get<int16_t>();

std::vector<uint8_t> output;
geometry::size size;
std::tie(output, size) = _load_png(j["spritesheet"].get_ref<const std::string &>());

auto surface = std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)>{
SDL_CreateRGBSurfaceWithFormatFrom(
output.data(),
size.width(),
size.height(),
32,
size.width() * 4,
SDL_PIXELFORMAT_ABGR8888
),
SDL_FreeSurface
};

if (!surface) [[unlikely]] {
throw std::runtime_error(fmt::format("[SDL_CreateRGBSurfaceWithFormatFrom] Error: {}", SDL_GetError()));
}

glyphmap map;
int x = 0, y = 0, width = 0, height = 0;
const auto pixels = static_cast<uint32_t *>(surface->pixels);
const auto separator = color(pixels[0], surface->format);

for (const char letter : alphabet) {
while (x < size.width() && color(pixels[y * size.width() + x], surface->format) == separator) {
++x;
}
glyphmap map;
int x = 0, y = 0, width = 0, height = 0;

if (x >= size.width()) [[unlikely]] {
throw std::runtime_error(fmt::format("Error: Missing glyph for '{}'", letter));
}
for (const char letter : alphabet) {
while (x < size.width() && color(pixels[y * size.width() + x], surface->format) == separator) {
++x;
}

width = 0;
while (x + width < size.width() &&
color(pixels[y * size.width() + x + width], surface->format) != separator) {
++width;
}
if (x >= size.width()) [[unlikely]] {
throw std::runtime_error(fmt::format("Error: Missing glyph for '{}'", letter));
}

height = 0;
while (y + height < size.height() &&
color(pixels[(y + height) * size.width() + x], surface->format) != separator) {
++height;
}
width = 0;
while (x + width < size.width() &&
color(pixels[y * size.width() + x + width], surface->format) != separator) {
++width;
}

map[letter] = {{x, y}, {width, height}};
x += width;
height = 0;
while (y + height < size.height() &&
color(pixels[(y + height) * size.width() + x], surface->format) != separator) {
++height;
}

it->second = std::make_shared<font>(
std::move(map),
std::make_shared<pixmap>(_renderer, std::move(surface)),
spacing
);
map[letter] = {{x, y}, {width, height}};
x += width;
}

return it->second;
auto ptr = std::make_shared<font>(
std::move(map),
std::make_shared<pixmap>(_renderer, std::move(surface)),
spacing
);

_pool.emplace(family, ptr);
return ptr;
}

void fontfactory::flush() noexcept {
Expand Down
15 changes: 8 additions & 7 deletions src/pixmappool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ pixmappool::pixmappool(const std::shared_ptr<renderer> renderer) noexcept
: _renderer(std::move(renderer)) {}

const std::shared_ptr<pixmap> pixmappool::get(const std::string &filename) {
auto [it, added] = _pool.insert_or_assign(filename, nullptr);
if (auto it = _pool.find(filename); it != _pool.end()) [[likely]] {
return it->second;
}

if (added) [[unlikely]] {
fmt::println("[pixmappool] cache miss {}", filename);
fmt::println("[pixmappool] cache miss {}", filename);

assert(_renderer);
assert(_renderer);

it->second = std::make_shared<pixmap>(_renderer, filename);
}
auto ptr = std::make_shared<pixmap>(_renderer, filename);
_pool.emplace(filename, ptr);

return it->second;
return ptr;
}

void pixmappool::flush() noexcept {
Expand Down
15 changes: 8 additions & 7 deletions src/soundmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ soundmanager::soundmanager(const std::shared_ptr<audiodevice> audiodevice) noexc
: _audiodevice(std::move(audiodevice)) {}

std::shared_ptr<soundfx> soundmanager::get(const std::string &filename) noexcept {
auto [it, added] = _pool.insert_or_assign(filename, nullptr);
if (auto it = _pool.find(filename); it != _pool.end()) [[likely]] {
return it->second;
}

if (added) [[unlikely]] {
fmt::println("[soundmanager] cache miss {}", filename);
fmt::println("[soundmanager] cache miss {}", filename);

assert(_audiodevice);
assert(_audiodevice);

it->second = std::make_shared<soundfx>(_audiodevice, filename);
}
auto ptr = std::make_shared<soundfx>(_audiodevice, filename);
_pool.emplace(filename, ptr);

return it->second;
return ptr;
}

void soundmanager::play(const std::string &filename) noexcept {
Expand Down

0 comments on commit 7e36afb

Please sign in to comment.