Skip to content

Commit

Permalink
Improved: FontAtlas generation
Browse files Browse the repository at this point in the history
  • Loading branch information
WillisMedwell committed Mar 11, 2024
1 parent 104bdf0 commit 32522d0
Show file tree
Hide file tree
Showing 8 changed files with 312 additions and 655 deletions.
21 changes: 4 additions & 17 deletions code/Demos/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,8 @@ struct IsoData {
struct IsoLogic {
void init(AppRenderer& renderer, Core::AudioManager& audio, IsoData& data) {

std::cout << std::thread::hardware_concurrency() << '\n';

/*Media::Sound sound {};*/

Media::Sound sound = Media::Sound::create("assets/background_sound.wav").on_error_panic().value_move();


auto res = audio.load_sound_into_buffer(sound).on_error(print_then_quit);

data.sound_buffer = res.value();
Expand All @@ -433,25 +428,17 @@ struct IsoLogic {
data.spinning.angle = 0;
data.spinning.rotations_per_second = 1;

auto model_data = Utily::FileReader::load_entire_file("assets/teapot.obj")
.on_error_panic()
.value_move();
auto model = Model::decode_as_static_model(model_data, ".obj")
.on_error_panic()
.value_move();
auto image = Media::Image::create("assets/texture.png")
.on_error_panic()
.value_move();
auto model_data = Utily::FileReader::load_entire_file("assets/teapot.obj").on_error_panic().value_move();
auto model = Model::decode_as_static_model(model_data, ".obj").on_error_panic().value_move();
auto image = Media::Image::create("assets/texture.png").on_error_panic().value_move();

data.font_batch_renderer.emplace(Renderer::FontBatchRenderer::create(data.resource_manager, "assets/RobotoMono.ttf")
.on_error_panic()
.value_move());

auto font_atlas = Media::FontAtlas::create("assets/RobotoMono.ttf", 500).on_error_panic().value_move();
font_atlas.atlas_image().save_to_disk("RobotoMonoAtlas.png");
Media::FontAtlas::create("assets/RobotoMono.ttf", 500).on_error_panic().value().atlas_image().save_to_disk("RobotoMonoAtlas.png");

data.instance_renderer.init(data.resource_manager, model, image);

data.source_handle = audio.play_sound(data.sound_buffer, { 5, 0, 0 }).on_error(print_then_quit).value();
data.start_time = std::chrono::high_resolution_clock::now();
}
Expand Down
129 changes: 0 additions & 129 deletions code/Engine/include/Media/Font.hpp

This file was deleted.

46 changes: 46 additions & 0 deletions code/Engine/include/Media/FontAtlas.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <Utily/Utily.hpp>

#include "Media/Image.hpp"
#include "Model/Static.hpp"
#include <array>
#include <glm/vec2.hpp>
#include <limits>
#include <ranges>
#include <utility>

namespace Media {

class FontAtlas
{
public:
/// @brief Load .ttf font from disk. Generate a font-atlas image. Can fail.
[[nodiscard]] static auto create(std::filesystem::path path, uint32_t char_height_px) noexcept -> Utily::Result<FontAtlas, Utily::Error>;

FontAtlas(FontAtlas&& other)
: _m(std::move(other._m)) { }

struct UvCoord {
float min_x;
float max_x;
float min_y;
float max_y;
};
[[nodiscard]] auto uv_for(char a) const noexcept -> FontAtlas::UvCoord;

[[nodiscard]] auto atlas_image() const noexcept -> const Media::Image& { return _m.atlas_image; }
[[nodiscard]] auto atlas_layout() const noexcept { return _m.atlas_layout; }
[[nodiscard]] auto glyph_dimensions() const noexcept { return _m.glyph_dimensions; }

private:
struct M {
Media::Image atlas_image;
glm::vec2 atlas_layout;
glm::vec2 glyph_dimensions;
} _m;

explicit FontAtlas(M&& m)
: _m(std::move(m)) { }
};
}
6 changes: 6 additions & 0 deletions code/Engine/include/Media/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ namespace Media {
/// @brief Load png image from disk and decode it. Can fail.
[[nodiscard]] static auto create(std::filesystem::path path)
-> Utily::Result<Image, Utily::Error>;

/// @brief Take decoded-raw image data and copy it. Can fail.
[[nodiscard]] static auto create(std::span<const uint8_t> raw_bytes, glm::uvec2 dimensions, InternalFormat format)
-> Utily::Result<Image, Utily::Error>;

/// @brief Take ownership of decoded-raw image data. Can fail.
[[nodiscard]] static auto create(std::unique_ptr<uint8_t[]>&& data, size_t data_size_bytes, glm::uvec2 dimensions, InternalFormat format)
-> Utily::Result<Image, Utily::Error>;

[[nodiscard]] inline auto raw_bytes() const noexcept { return std::span { _m.data.get(), _m.data_size_bytes }; }
[[nodsicard]] inline auto dimensions() const noexcept { return _m.dimensions; }
[[nodiscard]] auto format() const { return _m.format; }
Expand All @@ -90,6 +95,7 @@ namespace Media {
Image(const Image&) = delete;

auto save_to_disk(std::filesystem::path path) const noexcept -> Utily::Result<void, Utily::Error>;

private:
struct M {
std::unique_ptr<uint8_t[]> data = {};
Expand Down
2 changes: 1 addition & 1 deletion code/Engine/include/Media/Media.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

#include "Media/Image.hpp"
#include "Media/Font.hpp"
#include "Media/FontAtlas.hpp"
#include "Media/Sound.hpp"
Loading

0 comments on commit 32522d0

Please sign in to comment.