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 24, 2024
1 parent e32a0cf commit 798ab10
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 40 deletions.
8 changes: 8 additions & 0 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ std::shared_ptr<input::eventmanager> engine::eventmanager() const noexcept {
return _eventmanager;
}

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

std::shared_ptr<graphics::overlay> engine::overlay() const noexcept {
return _overlay;
}
Expand Down Expand Up @@ -82,6 +86,10 @@ void engine::set_eventmanager(std::shared_ptr<input::eventmanager> eventmanager)
_eventmanager = std::move(eventmanager);
}

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

void engine::set_overlay(std::shared_ptr<graphics::overlay> overlay) noexcept {
_overlay = std::move(overlay);
}
Expand Down
3 changes: 3 additions & 0 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class engine : public input::eventreceiver {
std::shared_ptr<audio::audiodevice> audiodevice() const noexcept;
std::shared_ptr<framework::entitymanager> entitymanager() const noexcept;
std::shared_ptr<input::eventmanager> eventmanager() const noexcept;
std::shared_ptr<graphics::fontfactory> fontfactory() const noexcept;
std::shared_ptr<graphics::overlay> overlay() const noexcept;
std::shared_ptr<framework::resourcemanager> resourcemanager() const noexcept;
std::shared_ptr<framework::scenemanager> scenemanager() const noexcept;
Expand All @@ -28,6 +29,7 @@ class engine : public input::eventreceiver {
void set_audiodevice(std::shared_ptr<audio::audiodevice> audiodevice) noexcept;
void set_entitymanager(std::shared_ptr<framework::entitymanager> entitymanager) noexcept;
void set_eventmanager(std::shared_ptr<input::eventmanager> eventmanager) noexcept;
void set_fontfactory(std::shared_ptr<graphics::fontfactory> fontfactory) noexcept;
void set_overlay(std::shared_ptr<graphics::overlay> overlay) noexcept;
void set_resourcemanager(std::shared_ptr<framework::resourcemanager> resourcemanager) noexcept;
void set_scenemanager(std::shared_ptr<framework::scenemanager> scenemanager) noexcept;
Expand Down Expand Up @@ -63,5 +65,6 @@ class engine : public input::eventreceiver {
std::shared_ptr<graphics::renderer> _renderer;
std::shared_ptr<graphics::window> _window;
std::shared_ptr<framework::world> _world;
std::shared_ptr<graphics::fontfactory> _fontfactory;
};
}
2 changes: 2 additions & 0 deletions src/enginefactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ std::shared_ptr<engine> enginefactory::create() {
const auto statemanager = std::make_shared<framework::statemanager>();
const auto world = std::make_shared<framework::world>(_gravity, renderer);
const auto entitymanager = std::make_shared<framework::entitymanager>(world, resourcemanager);
const auto fontfactory = std::make_shared<graphics::fontfactory>(renderer);

engine->set_audiodevice(std::move(audiodevice));
engine->set_entitymanager(std::move(entitymanager));
Expand All @@ -60,6 +61,7 @@ std::shared_ptr<engine> enginefactory::create() {
engine->set_statemanager(std::move(statemanager));
engine->set_window(std::move(window));
engine->set_world(std::move(world));
engine->set_fontfactory(fontfactory);

engine->eventmanager()->add_receiver(engine->entitymanager());
engine->eventmanager()->add_receiver(engine);
Expand Down
22 changes: 14 additions & 8 deletions src/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

using namespace graphics;

// label::label(/*std::shared_ptr<font> font, */ const std::string &text, const geometry::point &position)
// : /* _font(std::move(font)), */ _text(text), _position(position) {}
void label::set_font(const std::shared_ptr<font> &font) noexcept {
_font = font;
}

// std::shared_ptr<label> label::create(std::shared_ptr<font> font, const std::string &text, const geometry::point &position) noexcept {
// return std::make_shared<label>(std::move(font), text, position);
// }
void label::set_placement(const geometry::point &position) noexcept {
_position = position;
}

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

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

void label::update(float_t delta) noexcept {
UNUSED(delta);
}

void label::draw() const noexcept {
_font->draw(_text, _position);
}
25 changes: 13 additions & 12 deletions src/label.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#pragma once

#include "common.hpp"
#include "widget.hpp"

namespace graphics {

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

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

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

void draw() const noexcept;
void dispose() noexcept;
void update(float_t delta) noexcept override;
void draw() const noexcept override;

private:
// std::shared_ptr<font> _font;
std::string _text;
geometry::point _position;
std::shared_ptr<font> _font{};
std::string _text{};
geometry::point _position{};
};
}
32 changes: 22 additions & 10 deletions src/overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@

using namespace graphics;

void overlay::add(std::shared_ptr<widget> widget) noexcept {
_widgets.emplace_back(std::move(widget));
}

void overlay::remove(std::shared_ptr<widget> widget) noexcept {
_widgets.remove_if([&widget](const std::shared_ptr<::widget> &existing) {
return existing == widget;
});
}
overlay::overlay(std::shared_ptr<renderer> renderer)
: _renderer(std::move(renderer)) {}

void overlay::update(float_t delta) noexcept {
for (const auto &widget : _widgets) {
Expand All @@ -22,6 +15,25 @@ void overlay::draw() const noexcept {
for (const auto &widget : _widgets) {
widget->draw();
}
}

std::shared_ptr<widget> overlay::create(widgettype type) noexcept {
std::shared_ptr<widget> widget;

_font->draw("Rodrigo Delduca! :)", {10, 10});
switch (type) {
case widgettype::label:
widget = std::make_shared<label>();
break;
default:
return nullptr;
}

_widgets.emplace_back(widget);
return widget;
}

void overlay::destroy(std::shared_ptr<widget> widget) noexcept {
_widgets.remove_if([&widget](const std::shared_ptr<::widget> &existing) {
return existing == widget;
});
}
15 changes: 5 additions & 10 deletions src/overlay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@
namespace graphics {
class overlay {
public:
overlay(std::shared_ptr<renderer> renderer) : _renderer(std::move(renderer)) {
auto ff = fontfactory(_renderer);
_font = ff.get("fonts/fixedsys.json");
}

explicit overlay(std::shared_ptr<renderer> renderer);
~overlay() = default;

void add(std::shared_ptr<widget> widget) noexcept;

void remove(std::shared_ptr<widget> widget) noexcept;

void update(float_t delta) noexcept;

void draw() const noexcept;

std::shared_ptr<widget> create(widgettype type) noexcept;

void destroy(std::shared_ptr<widget> widget) noexcept;

private:
std::shared_ptr<renderer> _renderer;
std::list<std::shared_ptr<widget>> _widgets;
std::shared_ptr<font> _font;
};
}
28 changes: 28 additions & 0 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
#include "entitymanager.hpp"
#include "event.hpp"
#include "io.hpp"
#include "label.hpp"
#include "loopable.hpp"
#include "point.hpp"
#include "postalservice.hpp"
#include "soundmanager.hpp"
#include "ticks.hpp"
#include "vector2d.hpp"
#include "widget.hpp"
#include <sol/overload.hpp>

using namespace framework;

Expand Down Expand Up @@ -51,6 +54,11 @@ void scriptengine::run() {
"none", anchor::none
);

lua.new_enum(
"Widget",
"label", graphics::widgettype::label
);

lua.new_usertype<entity>(
"Entity",
"id", sol::property(&entity::id),
Expand Down Expand Up @@ -80,6 +88,7 @@ void scriptengine::run() {
"add_loopable", &engine::add_loopable,
"entitymanager", &engine::entitymanager,
"flush", &engine::flush,
"fontfactory", &engine::fontfactory,
"width", sol::property(&engine::width),
"height", sol::property(&engine::height),
"is_keydown", &engine::is_keydown,
Expand All @@ -98,6 +107,12 @@ void scriptengine::run() {
}
);

lua.new_usertype<graphics::overlay>(
"Overlay",
"create", &graphics::overlay::create,
"destroy", &graphics::overlay::destroy
);

lua.new_usertype<enginefactory>(
"EngineFactory",
sol::constructors<enginefactory()>(),
Expand Down Expand Up @@ -135,6 +150,7 @@ void scriptengine::run() {
);

lua.new_usertype<graphics::color>(
"Color",
"color", sol::constructors<graphics::color(const std::string &)>(),

"r", sol::property(&graphics::color::r, &graphics::color::set_r),
Expand Down Expand Up @@ -210,6 +226,18 @@ void scriptengine::run() {
"left", &vector2d::left
);

lua.new_usertype<graphics::label>(
"Label",
"set_font", &graphics::label::set_font,
"set_placement", &graphics::label::set_placement,
"set", sol::overload(&graphics::label::set_text, &graphics::label::set_text_with_position)
);

lua.new_usertype<graphics::fontfactory>(
"FontFactory",
"get", &graphics::fontfactory::get
);

const auto script = storage::io::read("scripts/main.lua");

lua.script(std::string_view(reinterpret_cast<const char *>(script.data()), script.size()));
Expand Down
4 changes: 4 additions & 0 deletions src/widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "common.hpp"

namespace graphics {
enum class widgettype : uint8_t {
label
};

class widget {
public:
widget() = default;
Expand Down

0 comments on commit 798ab10

Please sign in to comment.