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 20, 2024
1 parent f3d9d76 commit 56de319
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class engine : public input::eventreceiver {

void prefetch(const std::vector<std::string> &filenames);

void load_font(const std::vector<std::string> &filename);

void flush() const;

bool is_keydown(const input::keyevent &event) const;
Expand Down
77 changes: 64 additions & 13 deletions src/fontfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@

using namespace graphics;

fontfactory::fontfactory(const std::weak_ptr<framework::resourcemanager> resourcemanager)
: _resourcemanager(resourcemanager) {}
using json = nlohmann::json;
/*
std::shared_ptr<font> fontfactory::get(const std::string &filename, const std::string &alphabet) {
UNUSED(alphabet);
using json = nlohmann::json;
const auto ptr = _resourcemanager.lock();
if (!ptr) {
throw std::runtime_error("bad resourcemanager_shared");
}
void entitymanager::set_resourcemanager(std::shared_ptr<resourcemanager> resourcemanager) {
_resourcemanager = resourcemanager;
}
std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
const auto buffer = storage::io::read(fmt::format("entities/{}.json", kind));
const auto j = json::parse(buffer);
const auto spritesheet = _resourcemanager->pixmappool()->get(j["spritesheet"].template get<std::string>());
const auto gravitic = j.value("gravitic", false);
const auto scale = j.value("scale", 1.);*/

fontfactory::fontfactory(const std::shared_ptr<framework::resourcemanager> resourcemanager)
: _resourcemanager(resourcemanager) {}

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"].template get<std::string>();
const auto spritesheet = _resourcemanager->pixmappool()->get(j["spritesheet"].template get<std::string>());

const auto texture = ptr->pixmappool()->get(filename);
int32_t width, height;
SDL_QueryTexture(*texture, NULL, NULL, &width, &height);
SDL_QueryTexture(*spritesheet, NULL, NULL, &width, &height);

std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> surface{
SDL_CreateRGBSurfaceWithFormat(
Expand All @@ -29,12 +42,12 @@ std::shared_ptr<font> fontfactory::get(const std::string &filename, const std::s
};
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: {}", SDL_GetError())
);
}

if (SDL_RenderReadPixels(*ptr->renderer(), NULL, surface->format->format, surface->pixels, surface->pitch) != 0) {
throw std::runtime_error(fmt::format("[SDL_RenderReadPixels] error reading pixels: {}, error {}", filename, SDL_GetError()));
if (SDL_RenderReadPixels(*_resourcemanager->renderer(), NULL, surface->format->format, surface->pixels, surface->pitch) != 0) {
throw std::runtime_error(fmt::format("[SDL_RenderReadPixels] error reading pixels: {}", SDL_GetError()));
}

uint32_t *pixels = reinterpret_cast<uint32_t *>(surface->pixels);
Expand All @@ -46,6 +59,44 @@ std::shared_ptr<font> fontfactory::get(const std::string &filename, const std::s
}
}

// const auto ptr = _resourcemanager.lock();
// if (!ptr) {
// throw std::runtime_error("bad resourcemanager_shared");
// }

// const auto texture = ptr->pixmappool()->get(filename);
// int32_t width, height;
// SDL_QueryTexture(*texture, NULL, NULL, &width, &height);

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

// if (SDL_RenderReadPixels(*ptr->renderer(), NULL, surface->format->format, surface->pixels, surface->pitch) != 0) {
// throw std::runtime_error(fmt::format("[SDL_RenderReadPixels] error reading pixels: {}, error {}", filename, SDL_GetError()));
// }

// uint32_t *pixels = reinterpret_cast<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);
// }
// }

return std::shared_ptr<font>(); // TEMP
}

Expand Down
6 changes: 3 additions & 3 deletions src/fontfactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ namespace graphics {
class fontfactory {
public:
fontfactory() = delete;
fontfactory(const std::weak_ptr<framework::resourcemanager> _resourcemanager);
fontfactory(const std::shared_ptr<framework::resourcemanager> _resourcemanager);
~fontfactory() = default;

std::shared_ptr<font> get(const std::string &filename, const std::string &alphabet);
std::shared_ptr<font> get(const std::string &face);

// void update(double_t delta);

// void draw() const;

private:
std::weak_ptr<framework::resourcemanager> _resourcemanager;
std::shared_ptr<framework::resourcemanager> _resourcemanager;

// std::map<uint8_t, glyph> _glyphs;
// std::shared_ptr<pixmap> _pixmap;
Expand Down
23 changes: 23 additions & 0 deletions src/label.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include "label.hpp"

using namespace graphics;

label::label(std::shared_ptr<font> font, const std::string &text, const geometry::point &position)
: _font(font), _text(text), _position(position) {}

std::shared_ptr<label> label::create(std::shared_ptr<font> font, const std::string &text, const geometry::point &position) {
return std::make_shared<label>(font, text, position);
}

void label::set(const std::string &text) {
_text = text;
}

void label::set(const std::string &text, const geometry::point &position) {
_text = text;
_position = position;
}

void label::draw() const {
}
30 changes: 30 additions & 0 deletions src/label.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "common.hpp"

#include "font.hpp"
#include "point.hpp"

namespace graphics {
class label : protected std::enable_shared_from_this<label> {
public:
label() = delete;
label(std::shared_ptr<font> font, const std::string &text, const geometry::point &position);
~label() = default;

static std::shared_ptr<label> create(std::shared_ptr<font> font, const std::string &text, const geometry::point &position);

void set(const std::string &text);

void set(const std::string &text, const geometry::point &position);

void draw() const;

void dispose(); // ???

private:
std::shared_ptr<font> _font;
std::string _text;
geometry::point _position;
};
}
4 changes: 0 additions & 4 deletions src/resourcemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,3 @@ std::shared_ptr<graphics::pixmappool> resourcemanager::pixmappool() {
std::shared_ptr<audio::soundmanager> resourcemanager::soundmanager() {
return _soundmanager;
}

std::shared_ptr<graphics::fontfactory> resourcemanager::fontfactory() {
return _fontfactory;
}
1 change: 1 addition & 0 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void scriptengine::run() {
"soundmanager", &engine::soundmanager,
"add_loopable", &engine::add_loopable,
"set_scene", &engine::set_scene,
//"load_font", &engine::load_font,
"prefetch", [](engine &engine, sol::table table) {
std::vector<std::string> filenames;
filenames.reserve(table.size());
Expand Down

0 comments on commit 56de319

Please sign in to comment.