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 12, 2024
1 parent 41e1f58 commit 5c826a5
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 41 deletions.
8 changes: 6 additions & 2 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ void engine::prefetch(const std::vector<std::string> &filenames) {
_resourcemanager->prefetch(filenames);
}

void engine::flush() const {
_resourcemanager->pixmappool()->flush();
_resourcemanager->soundmanager()->flush();
}

bool engine::is_keydown(const input::keyevent &event) const {
return _statemanager->is_keydown(event);
}
Expand Down Expand Up @@ -141,8 +146,7 @@ void engine::_loop() {
_eventmanager->update(delta);
_entitymanager->update(delta);

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

_renderer->begin();
_scenemanager->draw();
Expand Down
2 changes: 2 additions & 0 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class engine : public input::eventreceiver {

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

void flush() const;

bool is_keydown(const input::keyevent &event) const;

const std::shared_ptr<entity> spawn(const std::string &kind);
Expand Down
19 changes: 10 additions & 9 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "entity.hpp"
#include "entityprops.hpp"
#include "io.hpp"
#include "pixmap.hpp"
#include "point.hpp"
#include "rect.hpp"
#include "resourcemanager.hpp"
Expand All @@ -15,19 +14,21 @@ using namespace framework;
using json = nlohmann::json;

void entitymanager::set_resourcemanager(
std::shared_ptr<resourcemanager> 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_view>());
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.);
const auto size = geometry::size{
static_cast<int32_t>(j.value("width", 0) * scale),
static_cast<int32_t>(j.value("height", 0) * scale)};
static_cast<int32_t>(j.value("height", 0) * scale)
};

std::map<std::string, std::vector<keyframe>> animations;
for (const auto &[key, a] : j["animations"].get<json::object_t>()) {
Expand Down Expand Up @@ -83,7 +84,8 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
frame,
last_frame,
velocity,
visible};
visible
};

const auto e = entity::create(std::move(props));
std::cout << "[entitymanager] spawn: " << e->id() << std::endl;
Expand All @@ -98,10 +100,9 @@ void entitymanager::destroy(const std::weak_ptr<entity> entity) {
}

std::shared_ptr<entity> entitymanager::find(uint64_t id) const {
const auto it = std::find_if(_entities.begin(), _entities.end(),
[id](const std::shared_ptr<entity> &entity) {
return entity->id() == id;
});
const auto it = std::find_if(_entities.begin(), _entities.end(), [id](const std::shared_ptr<entity> &entity) {
return entity->id() == id;
});

return (it != _entities.end()) ? *it : nullptr;
}
Expand Down
5 changes: 3 additions & 2 deletions src/pixmappool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ using namespace graphics;
pixmappool::pixmappool(const std::shared_ptr<renderer> renderer)
: _renderer(renderer) {}

void pixmappool::preload(const std::vector<std::string_view> &filenames) {
void pixmappool::preload(const std::vector<std::string> &filenames) {
for (const auto &filename : filenames) {
get(filename);
}
}

const std::shared_ptr<pixmap> pixmappool::get(const std::string_view filename) {
const std::shared_ptr<pixmap> pixmappool::get(const std::string &filename) {
auto [it, added] = _pool.try_emplace(filename, nullptr);

if (added) {
std::cout << "[pixmappool] cache miss: " << filename << std::endl;
assert(_renderer);
it->second = std::make_shared<pixmap>(_renderer, filename);
}

Expand Down
8 changes: 3 additions & 5 deletions src/pixmappool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ class pixmappool {
explicit pixmappool(const std::shared_ptr<renderer> renderer);
~pixmappool() = default;

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

const std::shared_ptr<pixmap> get(const std::string_view filename);
const std::shared_ptr<pixmap> get(const std::string &filename);

void flush();

private:
std::shared_ptr<renderer> _renderer;

std::unordered_map<std::string_view, std::shared_ptr<pixmap>, std::hash<std::string_view>>
_pool;
std::map<std::string, std::shared_ptr<pixmap>> _pool;
};
}
7 changes: 4 additions & 3 deletions src/resourcemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ using namespace framework;

resourcemanager::resourcemanager(
const std::shared_ptr<graphics::renderer> renderer,
const std::shared_ptr<audio::audiodevice> audiodevice)
const std::shared_ptr<audio::audiodevice> audiodevice
)
: _pixmappool(std::make_shared<graphics::pixmappool>(renderer)),
_soundmanager(std::make_shared<audio::soundmanager>(audiodevice)) {
_handlers[".png"] = [this](const std::string_view filename) {
_handlers[".png"] = [this](const std::string &filename) {
_pixmappool->get(filename);
};

_handlers[".ogg"] = [this](const std::string_view filename) {
_handlers[".ogg"] = [this](const std::string &filename) {
_soundmanager->get(filename);
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/resourcemanager.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#pragma once

#include "common.hpp"
#include <string_view>

namespace framework {
class resourcemanager {
public:
resourcemanager(
const std::shared_ptr<graphics::renderer> renderer,
const std::shared_ptr<audio::audiodevice> audiodevice);
const std::shared_ptr<audio::audiodevice> audiodevice
);

~resourcemanager() = default;

Expand All @@ -23,7 +23,7 @@ class resourcemanager {
std::shared_ptr<audio::soundmanager> soundmanager();

private:
std::unordered_map<std::string, std::function<void(const std::string_view)>>
std::map<std::string, std::function<void(const std::string &)>>
_handlers;

std::list<std::string> _filenames;
Expand Down
2 changes: 1 addition & 1 deletion src/scenemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ scenemanager::scenemanager(const std::shared_ptr<graphics::pixmappool> pixmappoo
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>());
_background = _pixmappool->get(j["background"].template get<std::string>());
_size = {j.at("width").get<int32_t>(), j.at("height").get<int32_t>()};
}

Expand Down
11 changes: 7 additions & 4 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "soundmanager.hpp"
#include "ticks.hpp"
#include "vector2d.hpp"
#include <string>

using namespace framework;

Expand Down Expand Up @@ -93,12 +94,14 @@ void scriptengine::run() {
"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) {
filenames.push_back(item.second.as<std::string>());
std::vector<std::string> filenames;
filenames.reserve(table.size());
for (const auto &item : table) {
filenames.emplace_back(item.second.as<std::string>());
}
engine.prefetch(filenames);
}
},
"flush", &engine::flush
);

lua.new_usertype<audio::soundmanager>(
Expand Down
8 changes: 4 additions & 4 deletions src/soundmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ using namespace audio;
soundmanager::soundmanager(std::shared_ptr<audiodevice> audiodevice)
: _audiodevice(audiodevice) {}

void soundmanager::prefetch(const std::vector<std::string_view> &filenames) {
void soundmanager::prefetch(const std::vector<std::string> &filenames) {
for (const auto &filename : filenames) {
get(filename);
}
}

const std::shared_ptr<soundfx> soundmanager::get(const std::string_view filename) {
const std::shared_ptr<soundfx> soundmanager::get(const std::string &filename) {
auto [it, added] = _soundmap.try_emplace(filename, nullptr);

if (added) {
Expand All @@ -27,11 +27,11 @@ const std::shared_ptr<soundfx> soundmanager::get(const std::string_view filename
return it->second;
}

void soundmanager::play(const std::string_view filename, bool loop = false) {
void soundmanager::play(const std::string &filename, bool loop = false) {
get(fmt::format("blobs/{}.ogg", filename))->play(loop);
}

void soundmanager::stop(const std::string_view filename) {
void soundmanager::stop(const std::string &filename) {
get(filename)->stop();
}

Expand Down
13 changes: 5 additions & 8 deletions src/soundmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ class soundmanager {
soundmanager(std::shared_ptr<audiodevice> audiodevice);
~soundmanager() = default;

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

const std::shared_ptr<soundfx> get(const std::string_view filename);
const std::shared_ptr<soundfx> get(const std::string &filename);

void play(const std::string_view filename, bool loop);
void play(const std::string &filename, bool loop);

void stop(const std::string_view filename);
void stop(const std::string &filename);

void flush();

private:
std::shared_ptr<audiodevice> _audiodevice;

std::unordered_map<std::string_view, std::shared_ptr<soundfx>,
std::hash<std::string_view>>
_soundmap;
std::map<std::string_view, std::shared_ptr<soundfx>> _soundmap;
};
}

0 comments on commit 5c826a5

Please sign in to comment.