Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilKleistGao committed Aug 21, 2021
1 parent bd9b2da commit 87aca98
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 185 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ A game engine for 2D pixel game powered by C++ (自家用).
+ ...

## TODO List
### 0.9
+ [x] Saving Game Files
+ [ ] Prefab

### 1.0
+ [ ] Package Tools
+ [ ] Screen Adaptor
Expand Down
4 changes: 2 additions & 2 deletions kernel/animation/aseprite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ namespace ngind::animation {
Aseprite::Aseprite(const std::string& name) : _current_index(), _current_tag(nullptr) {
_config = resources::ResourcesManager::getInstance()->load<resources::ConfigResource>("animations/" + name + ".json");

auto frames = _config->getDocument()["frames"].GetArray();
auto frames = (*_config)["frames"].GetArray();
for (const auto& frame : frames) {
_frames.emplace_back(frame);
}

auto meta = _config->getDocument()["meta"].GetObject();
auto meta = (*_config)["meta"].GetObject();
_image_path = meta["image"].GetString();

auto tags = meta["frameTags"].GetArray();
Expand Down
2 changes: 1 addition & 1 deletion kernel/audio/audio_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void AudioManager::playMusic(resources::MusicResource* music) {
return;
}

_handles[name] = _engine.play(*music->getStream());
_handles[name] = _engine.play(**music);
}

void AudioManager::stopMusic(resources::MusicResource* music) {
Expand Down
2 changes: 1 addition & 1 deletion kernel/audio/audio_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class AudioManager {
* @param effect: given effect audio.
*/
inline void playEffect(resources::EffectResource* effect) {
_engine.play(*effect->getEffect());
_engine.play(**effect);
}

private:
Expand Down
10 changes: 4 additions & 6 deletions kernel/components/animation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ void Animation::update(const float& delta) {
auto duration = _frame.getDuration().count();
if (_timer >= duration) {
_timer -= duration;
auto ase = _anim->getAseprite();
if (ase->isEnd()) {
if ((*_anim)->isEnd()) {
if (_loop) {
this->play(_tag);
}
Expand All @@ -68,7 +67,7 @@ void Animation::update(const float& delta) {
}
}
else {
_frame = ase->next();
_frame = (*_anim)->next();
}
auto bound = _frame.getRect();
_sprite->setBound({bound.x, bound.y}, {bound.z, bound.w});
Expand All @@ -92,14 +91,13 @@ Animation* Animation::create(const typename resources::ConfigResource::JsonObjec
}

void Animation::play(const std::string& name) {
auto ase = _anim->getAseprite();
_tag = name;
_frame = ase->play(name);
_frame = (*_anim)->play(name);
_playing = true;
_timer = 0.0f;

auto bound = _frame.getRect();
_sprite->setImage(ase->getFilename(), {bound.x, bound.y}, {bound.z, bound.w});
_sprite->setImage((*_anim)->getFilename(), {bound.x, bound.y}, {bound.z, bound.w});
}

void Animation::stop() {
Expand Down
18 changes: 9 additions & 9 deletions kernel/components/label.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void Label::parseText() {
continue;
}

auto ch = _font->getCharacter(c);
auto ch = (*_font)->generateCharacterData(c);
current_width += (ch.advance.x >> 6) * scale;
}

Expand All @@ -157,7 +157,7 @@ void Label::parseText() {
if (_text[i] == '\n') {
j++;
current_width = 0;
max_height += static_cast<float>(_font->getMaxHeight()) * scale * 2 + _line_space;
max_height += static_cast<float>((*_font)->getMaxHeight()) * scale * 2 + _line_space;
model = getModelMatrix(max_width, widths[j], max_height);
}

Expand All @@ -170,7 +170,7 @@ void Label::parseText() {
->create<rendering::BatchQuadRenderingCommand>(_quads.back(), 24);
command->addReference();
_commands.push_back(command);
_commands.back()->setProgram(_program->getProgram());
_commands.back()->setProgram(_program->get());
_commands.back()->setZ(temp->getZOrder());
_commands.back()->setModel(model);

Expand All @@ -193,7 +193,7 @@ void Label::parseText() {
}

auto& cmd = _commands.back();
rendering::Character ch = _font->getCharacter(_text[i]);
rendering::Character ch = (*_font)->generateCharacterData(_text[i]);

auto x = current_width + ch.bearing.x * scale;
auto y = -max_height - (ch.size.y - ch.bearing.y) * scale;
Expand Down Expand Up @@ -231,7 +231,7 @@ glm::mat4 Label::getModelMatrix(const float& max_width, const float& width, cons
auto anchor = temp->getAnchor();

float scale = static_cast<float>(_size) / rendering::TrueTypeFont::DEFAULT_FONT_SIZE;
pos.y -= static_cast<float>(_font->getMaxHeight()) * scale;
pos.y -= static_cast<float>((*_font)->getMaxHeight()) * scale;

glm::mat4 model{1.0f};
if (_alignment == ALIGNMENT_LEFT) {
Expand Down Expand Up @@ -282,7 +282,7 @@ void Label::parseUTF8Text(const std::wstring& text) {
continue;
}

auto ch = _font->getCharacter(c);
auto ch = (*_font)->generateCharacterData(c);
current_width += (ch.advance.x >> 6) * scale;
}

Expand All @@ -295,7 +295,7 @@ void Label::parseUTF8Text(const std::wstring& text) {
if (text[i] == L'\n') {
j++;
current_width = 0;
max_height += static_cast<float>(_font->getMaxHeight()) * scale * 2 + _line_space;
max_height += static_cast<float>((*_font)->getMaxHeight()) * scale * 2 + _line_space;
model = getModelMatrix(max_width, widths[j], max_height);
}

Expand All @@ -308,7 +308,7 @@ void Label::parseUTF8Text(const std::wstring& text) {
->create<rendering::BatchQuadRenderingCommand>(_quads.back(), 24);
command->addReference();
_commands.push_back(command);
_commands.back()->setProgram(_program->getProgram());
_commands.back()->setProgram(_program->get());
_commands.back()->setZ(temp->getZOrder());
_commands.back()->setModel(model);

Expand All @@ -331,7 +331,7 @@ void Label::parseUTF8Text(const std::wstring& text) {
}

auto& cmd = _commands.back();
rendering::Character ch = _font->getCharacter(text[i]);
rendering::Character ch = (*_font)->generateCharacterData(text[i]);

auto x = current_width + ch.bearing.x * scale;
auto y = -max_height - (ch.size.y - ch.bearing.y) * scale;
Expand Down
10 changes: 5 additions & 5 deletions kernel/components/sprite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void Sprite::setImage(const std::string& filename) {
resources::ResourcesManager::getInstance()->release(_texture->getResourcePath());
}
_texture = resources::ResourcesManager::getInstance()->load<resources::TextureResource>(filename);
this->setBound({0, 0}, _texture->getTextureSize());
this->setBound({0, 0}, (*_texture)->getSize());
}

void Sprite::setImage(const std::string& filename, const glm::vec2& lb, const glm::vec2& rt) {
Expand All @@ -91,7 +91,7 @@ void Sprite::draw() {
_command = nullptr;
}

auto texture_size = _texture->getTextureSize();
auto texture_size = (*_texture)->getSize();
_quad = memory::MemoryPool::getInstance()->create<rendering::Quad, std::initializer_list<GLfloat>>(
{
_rt.x - _lb.x, _rt.y - _lb.y, static_cast<float>(_rt.x) / texture_size.x, static_cast<float>(_lb.y) / texture_size.y, // Top Right
Expand All @@ -102,13 +102,13 @@ void Sprite::draw() {
);
_quad->addReference();
_command = memory::MemoryPool::getInstance()
->create<rendering::QuadRenderingCommand>(_quad, _texture->getTextureID());
->create<rendering::QuadRenderingCommand>(_quad, (*_texture)->getTextureID());
_command->addReference();

_command->setModel(getModelMatrix());
_command->setColor(_color);
_command->setZ(temp->getZOrder());
_command->setProgram(_program->getProgram());
_command->setProgram(_program->get());

_dirty = false;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ glm::mat4 Sprite::getModelMatrix() {
auto rotate = temp->getRotation();
auto scale = temp->getScale();
auto anchor = temp->getAnchor();
auto texture_size = _texture->getTextureSize();
auto texture_size = (*_texture)->getSize();

glm::mat4 model{1.0f};
model = glm::translate(model, glm::vec3{pos.x - texture_size.x * scale.x * anchor.x,
Expand Down
5 changes: 5 additions & 0 deletions kernel/components/state_machine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ StateMachine* StateMachine::create(const typename resources::ConfigResource::Jso
void StateMachine::halt() {
luabridge::LuaRef state_exit = _instance["exit"];
state_exit();

auto instance = script::Observer::getInstance();
for (const auto& [name, _] : _subscribe) {
instance->cancel(this, name);
}
}

void StateMachine::update(const float& dlt) {
Expand Down
22 changes: 12 additions & 10 deletions kernel/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "game.h"

#include <utility>

#include "rendering/renderer.h"
#include "resources/resources_manager.h"
#include "log/visual_logger.h"
Expand Down Expand Up @@ -65,25 +67,25 @@ void Game::destroyInstance() {

void Game::start() {
auto render = rendering::Renderer::getInstance();
auto height = _global_settings->getDocument()["window-height"].GetInt();
render->createWindow(_global_settings->getDocument()["window-width"].GetInt(),
auto height = (*_global_settings)["window-height"].GetInt();
render->createWindow((*_global_settings)["window-width"].GetInt(),
height,
_global_settings->getDocument()["window-title"].GetString(),
_global_settings->getDocument()["window-icon"].GetString(),
_global_settings->getDocument()["window-full-screen"].GetBool());
(*_global_settings)["window-title"].GetString(),
(*_global_settings)["window-icon"].GetString(),
(*_global_settings)["window-full-screen"].GetBool());

ui::EventSystem::getInstance()->init(height);

script::LuaState::getInstance()->preload("kernel");

this->loadWorld(_global_settings->getDocument()["welcome-world"].GetString());
this->loadWorld((*(*_global_settings))["welcome-world"].GetString());

auto logger = log::VisualLogger::getInstance();
if (_global_settings->getDocument()["enable-visual-debug"].GetBool()) {
if ((*_global_settings)["enable-visual-debug"].GetBool()) {
logger->enable();
}

const float MAX_FRAME_RATE = _global_settings->getDocument()["max-frame-rate"].GetFloat();
const float MAX_FRAME_RATE = (*(*_global_settings))["max-frame-rate"].GetFloat();
const float MIN_DURATION = 1.0f / MAX_FRAME_RATE;
float duration = 1.0f / 60.0f;
logger->registerVariable("frame rate", "0");
Expand Down Expand Up @@ -131,7 +133,7 @@ void Game::loadWorld(const std::string& name) {
}

void Game::loadWorld(resources::ConfigResource* config) {
std::string name = config->getDocument()["world-name"].GetString();
std::string name = (*config)["world-name"].GetString();
if (this->_worlds.find(name) == this->_worlds.end()) {
this->_worlds[name] = memory::MemoryPool::getInstance()->create<objects::World>(config);
this->_worlds[name]->addReference();
Expand Down Expand Up @@ -173,7 +175,7 @@ void Game::popAndLoadWorld(const bool& has_destroy_current = true) {
}

void Game::destroyAndLoadWorld(std::string name) {
_next_world = name;
_next_world = std::move(name);
_transition = [&]() {
auto destroy_name = this->_current_world->getName();
loadWorld(_next_world);
Expand Down
75 changes: 23 additions & 52 deletions kernel/memory/memory_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,74 +45,45 @@ void MemoryPool::destroyInstance() {
}

void MemoryPool::clear() {
for (auto& [size, list] : _open_pool) {
for (auto* item : list) {
::operator delete(item);
auto tbm = _pool.end();
for (auto it = _pool.begin(); it != _pool.end(); ++it) {
if (tbm != _pool.end()) {
_pool.erase(tbm);
tbm = _pool.end();
}

list.clear();
auto temp = *it;
if (temp->getSustain() == 0) {
temp->~AutoCollectionObject();
::operator delete(temp);
*it = nullptr;
tbm = it;
}
}

for (auto& [size, list] : _closed_pool) {
for (auto it = list.begin(); it != list.end();) {
auto temp = *it;
if (temp->getSustain() == 0) {
temp->~AutoCollectionObject();
_open_pool[size].push_back(temp);
auto prev = it;
++it;
list.erase(prev);
}
else {
++it;
}
}
if (tbm != _pool.end()) {
_pool.erase(tbm);
}
}

MemoryPool::MemoryPool() : _dirty(false) {
}

MemoryPool::~MemoryPool() {
for (auto& [size, list] : _open_pool) {
for (auto* item : list) {
::operator delete(item);
}

list.clear();
}

for (auto& [size, list] : _closed_pool) {
for (auto* item : list) {
delete item;
}

list.clear();
for (auto* item : _pool) {
item->~AutoCollectionObject();
::operator delete(item);
}

_open_pool.clear();
_closed_pool.clear();
_pool.clear();
}

AutoCollectionObject* MemoryPool::allocate(const size_t& size) {
if (_closed_pool.find(size) == _closed_pool.end()) {
_closed_pool[size] = std::list<AutoCollectionObject*>{};
_open_pool[size] = std::list<AutoCollectionObject*>{};
}

if (_open_pool[size].empty()) {
auto p = reinterpret_cast<AutoCollectionObject*>(::operator new(size));
std::memset(p, 0, size);
_closed_pool[size].push_back(p);
return p;
}
else {
auto p = _open_pool[size].front();
_open_pool[size].pop_front();
std::memset(p, 0, size);
_closed_pool[size].push_back(p);
return p;
}
auto p = ::operator new(size);
std::memset(p, 0, size);
auto ao = reinterpret_cast<AutoCollectionObject*>(p);
_pool.push_back(ao);
return ao;
}

} // namespace ngind
3 changes: 1 addition & 2 deletions kernel/memory/memory_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <set>
#include <memory>
#include <list>
#include <map>

#include "auto_collection_object.h"

Expand Down Expand Up @@ -103,7 +102,7 @@ class MemoryPool {
/**
* A RB-Tree to manage objects
*/
std::map<size_t, std::list<AutoCollectionObject*>> _open_pool, _closed_pool;
std::list<AutoCollectionObject*> _pool;

MemoryPool();

Expand Down
Loading

0 comments on commit 87aca98

Please sign in to comment.