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 5, 2024
1 parent 1bceef1 commit cec7c89
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class garbagecollector;
class loopable;
class postalservice;
class resourcemanager;
class scenemanager;
class scriptengine;
class statemanager;
class timermanager;
Expand Down
18 changes: 14 additions & 4 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
#include "entity.hpp"
#include "entitymanager.hpp"
#include "eventmanager.hpp"
#include "eventreceiver.hpp"
#include "framerate.hpp"
#include "garbagecollector.hpp"
#include "loopable.hpp"
#include "noncopyable.hpp"
#include "renderer.hpp"
#include "resourcemanager.hpp"
#include "singleton.hpp"
#include "statemanager.hpp"
#include "window.hpp"

Expand Down Expand Up @@ -81,6 +77,14 @@ const std::shared_ptr<framework::statemanager> engine::statemanager() const {
return _statemanager;
}

void engine::set_scenemanager(const std::shared_ptr<framework::scenemanager> scenemanager) {
_scenemanager = scenemanager;
}

const std::shared_ptr<framework::scenemanager> engine::scenemanager() const {
return _scenemanager;
}

void engine::prefetch(const std::vector<std::string> &filenames) {
_resourcemanager->prefetch(filenames);
}
Expand All @@ -104,6 +108,10 @@ void engine::add_loopable(std::shared_ptr<loopable> loopable) {
_loopables.emplace_back(std::move(loopable));
}

void engine::set_scene(const std::string_view name) {
_scenemanager->load(name);
}

#ifdef EMSCRIPTEN
template <class T>
inline void run(void *arg) {
Expand All @@ -129,13 +137,15 @@ void engine::_loop() {
prior = now;

_resourcemanager->update(delta);
_scenemanager->update(delta);
_eventmanager->update(delta);
_entitymanager->update(delta);

std::for_each(_loopables.begin(), _loopables.end(),
[delta](auto &loopable) { loopable->loop(delta); });

_renderer->begin();
_scenemanager->draw();
_entitymanager->draw();
// _scenegraph->render();
_renderer->end();
Expand Down
8 changes: 8 additions & 0 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "common.hpp"

#include "eventreceiver.hpp"
#include <memory>

namespace framework {
class engine : public input::eventreceiver {
Expand Down Expand Up @@ -41,6 +42,10 @@ class engine : public input::eventreceiver {

const std::shared_ptr<framework::statemanager> statemanager() const;

void set_scenemanager(const std::shared_ptr<framework::scenemanager> scenemanager);

const std::shared_ptr<framework::scenemanager> scenemanager() const;

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

bool is_keydown(const input::keyevent &event) const;
Expand All @@ -51,6 +56,8 @@ class engine : public input::eventreceiver {

void add_loopable(std::shared_ptr<loopable> loopable);

void set_scene(const std::string_view name);

int32_t width() const;

int32_t height() const;
Expand All @@ -73,5 +80,6 @@ class engine : public input::eventreceiver {
std::shared_ptr<framework::entitymanager> _entitymanager;
std::shared_ptr<framework::resourcemanager> _resourcemanager;
std::shared_ptr<framework::statemanager> _statemanager;
std::shared_ptr<framework::scenemanager> _scenemanager;
};
}
3 changes: 3 additions & 0 deletions src/enginefactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "entitymanager.hpp"
#include "eventmanager.hpp"
#include "resourcemanager.hpp"
#include "scenemanager.hpp"
#include "window.hpp"

using namespace framework;
Expand Down Expand Up @@ -37,6 +38,7 @@ std::shared_ptr<engine> enginefactory::create() {
const auto em2 = std::make_shared<framework::entitymanager>();
const auto em3 = std::make_shared<framework::statemanager>();
const auto rm = std::make_shared<framework::resourcemanager>(r, ad);
const auto sm = std::make_shared<framework::scenemanager>(rm->pixmappool());

const auto e = std::make_shared<framework::engine>();

Expand All @@ -47,6 +49,7 @@ std::shared_ptr<engine> enginefactory::create() {
e->set_entitymanager(std::move(em2));
e->set_statemanager(std::move(em3));
e->set_resourcemanager(std::move(rm));
e->set_scenemanager(std::move(sm));

em1->add_receiver(std::move(em2));
em1->add_receiver(std::move(e));
Expand Down
5 changes: 1 addition & 4 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void entity::update(double delta) noexcept {
}

void entity::draw() const noexcept {
if (_props.action.empty()) {
if (_props.action.empty() || !_props.visible) {
return;
}

Expand All @@ -76,9 +76,6 @@ void entity::draw() const noexcept {
geometry::rect destination{_props.position + offset, source.size()};
destination.scale(_props.scale);

if (_props.action == "dead") {
std::cout << ">>> offset x" << offset.x() << std::endl;
}
_props.spritesheet->draw(
source,
destination,
Expand Down
2 changes: 2 additions & 0 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class entity : public std::enable_shared_from_this<entity> {

std::string action() const noexcept;

void set_visible(bool visible) noexcept;

void dispatch(const std::string &message);

private:
Expand Down
14 changes: 7 additions & 7 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
#include "resourcemanager.hpp"
#include "size.hpp"
#include "vector2d.hpp"
#include <cstddef>
#include <cstdint>
#include <string_view>
#include <vector>

using namespace framework;

Expand Down Expand Up @@ -64,6 +60,7 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
uint32_t frame{0};
uint32_t last_frame{0};
vector2d velocity;
bool visible{true};

entityprops props{
id,
Expand All @@ -80,16 +77,19 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
action,
frame,
last_frame,
velocity};
velocity,
visible};

const auto e = entity::create(std::move(props));
std::cout << "[entitymanager] spawn: " << e->id() << std::endl;
_entities.emplace_back(e);
return e;
}

void entitymanager::destroy(const std::shared_ptr<entity> entity) {
_entities.remove(entity);
void entitymanager::destroy(const std::weak_ptr<entity> entity) {
if (auto e = entity.lock()) {
_entities.remove(e);
}
}

std::shared_ptr<entity> entitymanager::find(uint64_t id) const {
Expand Down
2 changes: 1 addition & 1 deletion src/entitymanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class entitymanager : public input::eventreceiver {

std::shared_ptr<entity> spawn(const std::string &kind);

void destroy(const std::shared_ptr<entity> entity);
void destroy(const std::weak_ptr<entity> entity);

std::shared_ptr<entity> find(uint64_t id) const;

Expand Down
1 change: 1 addition & 0 deletions src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct entityprops {
uint32_t frame;
uint32_t last_frame;
vector2d velocity;
bool visible;

entityprops() = default;
};
Expand Down
1 change: 1 addition & 0 deletions src/pixmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum class flip : int32_t {

class pixmap {
public:
pixmap() = default;
pixmap(const std::shared_ptr<renderer> renderer, std::string_view filename);
~pixmap() = default;

Expand Down
29 changes: 29 additions & 0 deletions src/scenemanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "scenemanager.hpp"
#include "point.hpp"

using namespace framework;

using json = nlohmann::json;

scenemanager::scenemanager(const std::shared_ptr<graphics::pixmappool> pixmappool)
: _pixmappool(pixmappool) {}

void scenemanager::load(const std::string_view name) {
const auto buffer = storage::io::read(fmt::format("scenes/{}.json", name));
const auto j = json::parse(buffer);
_background = _pixmappool->get(j["background"].template get<std::string_view>());
_size = {j.at("width").get<int32_t>(), j.at("height").get<int32_t>()};
}

void scenemanager::update(double_t delta) noexcept {
UNUSED(delta);
}

void scenemanager::draw() noexcept {
if (!_background) {
return;
}

static geometry::point point{0, 0};
_background->draw({point, _size}, {point, _size});
}
24 changes: 24 additions & 0 deletions src/scenemanager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "common.hpp"
#include "pixmap.hpp"
#include "pixmappool.hpp"
#include "rect.hpp"

namespace framework {
class scenemanager {
public:
scenemanager(const std::shared_ptr<graphics::pixmappool> pixmappool);

void load(const std::string_view name);

void update(double_t delta) noexcept;

void draw() noexcept;

private:
std::shared_ptr<graphics::pixmappool> _pixmappool;
std::shared_ptr<graphics::pixmap> _background;
geometry::size _size;
};
}
1 change: 1 addition & 0 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void scriptengine::run() {
"height", sol::property(&engine::height),
"soundmanager", &engine::soundmanager,
"add_loopable", &engine::add_loopable,
"set_scene", &engine::set_scene,
"prefetch", [](engine &engine, sol::table table) {
std::vector<std::string> filenames{table.size()};
for (auto &item : table) {
Expand Down

0 comments on commit cec7c89

Please sign in to comment.