Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Nov 20, 2024
1 parent bb69b88 commit 10f6aed
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 25 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(CURL CONFIG REQUIRED)
find_package(chipmunk2d CONFIG REQUIRED)
find_package(freetype CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(Ogg CONFIG REQUIRED)
find_package(OpenAL CONFIG REQUIRED)
Expand Down Expand Up @@ -65,7 +64,6 @@ endif()
target_link_libraries(${PROJECT_NAME} PRIVATE
CURL::libcurl
chipmunk2d::chipmunk2d
Freetype::Freetype
fmt::fmt
Ogg::ogg
physfs-static
Expand Down
1 change: 0 additions & 1 deletion conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[requires]
chipmunk2d/7.0.3
freetype/2.13.3
fmt/11.0.2
libcurl/8.10.1
libspng/0.7.4
Expand Down
1 change: 1 addition & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class size;
namespace graphics {
class effect;
class fontfactory;
class font;
class glyph;
class pixmap;
class pixmappool;
Expand Down
19 changes: 6 additions & 13 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ std::shared_ptr<framework::scenemanager> engine::scenemanager() const {
return _scenemanager;
}

// void engine::set_fontfactory(std::shared_ptr<graphics::fontfactory> fontfactory) {
// _fontfactory = std::move(fontfactory);
// }
void engine::set_fontfactory(std::shared_ptr<graphics::fontfactory> fontfactory) {
_fontfactory = std::move(fontfactory);
}

// std::shared_ptr<graphics::fontfactory> engine::fontfactory() const {
// return _fontfactory;
// }
std::shared_ptr<graphics::fontfactory> engine::fontfactory() const {
return _fontfactory;
}

void engine::prefetch(const std::vector<std::string> &filenames) {
_resourcemanager->prefetch(filenames);
Expand Down Expand Up @@ -141,14 +141,7 @@ inline void run(void *arg) {
}
#endif

#include "fontfactory.hpp"

void engine::run() {

graphics::fontfactory ff(_renderer);
// TODO XXX
/*const auto font = */ ff.get("fixedsys");

#ifdef EMSCRIPTEN
emscripten_set_main_loop_arg(::run<engine>, this, 0, true);
#else
Expand Down
6 changes: 3 additions & 3 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class engine : public input::eventreceiver {
void set_scenemanager(std::shared_ptr<framework::scenemanager> scenemanager);
std::shared_ptr<framework::scenemanager> scenemanager() const;

// void set_fontfactory(std::shared_ptr<graphics::fontfactory> fontfactory);
// std::shared_ptr<graphics::fontfactory> fontfactory() const;
void set_fontfactory(std::shared_ptr<graphics::fontfactory> fontfactory);
std::shared_ptr<graphics::fontfactory> fontfactory() const;

void prefetch(const std::vector<std::string> &filenames);
void flush() const;
Expand Down Expand Up @@ -75,6 +75,6 @@ class engine : public input::eventreceiver {
std::shared_ptr<framework::resourcemanager> _resourcemanager;
std::shared_ptr<framework::statemanager> _statemanager;
std::shared_ptr<framework::scenemanager> _scenemanager;
// std::shared_ptr<graphics::fontfactory> _fontfactory;
std::shared_ptr<graphics::fontfactory> _fontfactory;
};
}
4 changes: 2 additions & 2 deletions src/enginefactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ std::shared_ptr<engine> enginefactory::create() {
const auto statemanager = std::make_shared<framework::statemanager>();
const auto resourcemanager = std::make_shared<framework::resourcemanager>(renderer, audiodevice);
const auto scenemanager = std::make_shared<framework::scenemanager>(resourcemanager->pixmappool());
// const auto fontfactory = std::make_shared<graphics::fontfactory>(resourcemanager);
const auto fontfactory = std::make_shared<graphics::fontfactory>(renderer);
const auto engine = std::make_shared<framework::engine>();

engine->set_window(std::move(window));
Expand All @@ -58,7 +58,7 @@ std::shared_ptr<engine> enginefactory::create() {
engine->set_statemanager(std::move(statemanager));
engine->set_resourcemanager(std::move(resourcemanager));
engine->set_scenemanager(std::move(scenemanager));
// engine->set_fontfactory(std::move(fontfactory));
engine->set_fontfactory(std::move(fontfactory));

engine->eventmanager()->add_receiver(engine->entitymanager());
engine->eventmanager()->add_receiver(engine);
Expand Down
16 changes: 16 additions & 0 deletions src/font.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "font.hpp"

using namespace graphics;

font::font(const glyphmap &glyphs, std::shared_ptr<pixmap> pixmap)
: _glyphs(glyphs), _pixmap(std::move(pixmap)) {}

void font::draw(const std::string &text, const geometry::point &position) const noexcept {
geometry::point cursor = position;
for (const auto character : text) {
const auto &glyph = _glyphs.at(character);
const auto &size = glyph.size();
_pixmap->draw(glyph, {cursor, size});
cursor += std::make_pair('x', size.width());
}
}
4 changes: 3 additions & 1 deletion src/font.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#pragma once

#include "common.hpp"
#include "point.hpp"

namespace graphics {
using glyphmap = std::map<uint8_t, geometry::rect>;

class font {
public:
font() = delete;
font(const glyphmap &glyphs, std::shared_ptr<pixmap> pixmap);
~font() = default;

void draw(std::string_view text) const noexcept;
void draw(const std::string &text, const geometry::point &position) const noexcept;

private:
glyphmap _glyphs;
Expand Down
4 changes: 2 additions & 2 deletions src/fontfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ std::ostream &operator<<(std::ostream &os, const color &c) {
fontfactory::fontfactory(std::shared_ptr<graphics::renderer> renderer) noexcept
: _renderer(std::move(renderer)) {}

/* std::shared_ptr<font> */ void fontfactory::get(const std::string &face) {
std::shared_ptr<font> fontfactory::get(const std::string &face) {
const auto buffer = storage::io::read(fmt::format("fonts/{}.json", face));
const auto j = json::parse(buffer);
const auto alphabet = j["alphabet"].get<std::string>();
Expand Down Expand Up @@ -93,5 +93,5 @@ fontfactory::fontfactory(std::shared_ptr<graphics::renderer> renderer) noexcept
fmt::print("glyph '{}' -> x: {}, y: {}, w: {}, h: {}\n", static_cast<char>(letter), rect.position().x(), rect.position().y(), rect.size().width(), rect.size().height());
}

auto _pixmap = pixmap(_renderer, std::move(surface));
return std::make_shared<font>(map, std::make_shared<pixmap>(_renderer, std::move(surface)));
}
2 changes: 1 addition & 1 deletion src/fontfactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class fontfactory {
explicit fontfactory(std::shared_ptr<graphics::renderer> renderer) noexcept;
~fontfactory() noexcept = default;

/* std::shared_ptr<font> */ void get(const std::string &face);
std::shared_ptr<font> get(const std::string &face);

private:
std::shared_ptr<graphics::renderer> _renderer;
Expand Down
9 changes: 9 additions & 0 deletions src/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ point &point::operator+=(const point &other) noexcept {
_y += other._y;
return *this;
}

point &point::operator+=(std::pair<char, int32_t> offset) noexcept {
if (offset.first == 'x') {
_x += offset.second;
} else if (offset.first == 'y') {
_y += offset.second;
}
return *this;
}
1 change: 1 addition & 0 deletions src/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class point {

point operator+(const point &other) const noexcept;
point &operator+=(const point &other) noexcept;
point &operator+=(std::pair<char, int32_t> axis_offset) noexcept;

friend void from_json(const nlohmann::json &j, point &m) noexcept;

Expand Down

0 comments on commit 10f6aed

Please sign in to comment.