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 12, 2024
1 parent e310bf5 commit dd57c59
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#define NOMINMAX
#endif

#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif

#include <AL/al.h>

Check failure on line 15 in src/common.hpp

View workflow job for this annotation

GitHub Actions / lint

src/common.hpp:15:10 [clang-diagnostic-error]

'AL/al.h' file not found
#include <AL/alc.h>
#include <SDL2/SDL.h>
Expand Down Expand Up @@ -43,10 +47,6 @@
#include <variant>
#include <vector>

#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif

#include "constants.hpp"
#include "deleters.hpp"
#include "helpers.hpp"
Expand Down
42 changes: 34 additions & 8 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ using namespace framework;

using json = nlohmann::json;

entitymanager::entitymanager() {
// worldDef.gravity = {0.0f, 9.8f};
entitymanager::entitymanager()

Check warning on line 16 in src/entitymanager.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entitymanager.cpp:16:1 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: _space, _entities
: _space(cpSpaceNew(), &cpSpaceFree) {
const auto gravity = cpv(0, -9.8);
cpSpaceSetGravity(_space.get(), gravity);
}

entitymanager::~entitymanager() {

Check warning on line 22 in src/entitymanager.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entitymanager.cpp:22:16 [modernize-use-equals-default]

use '= default' to define a trivial destructor
Expand All @@ -29,10 +31,21 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
const auto j = json::parse(buffer);
const auto spritesheet = _resourcemanager->pixmappool()->get(j["spritesheet"].get<std::string>());
const auto scale = j.value("scale", 1.0);
const auto size = geometry::size{
static_cast<int32_t>(j.value("width", 0) * scale),
static_cast<int32_t>(j.value("height", 0) * scale)
};

const auto position = j["position"].get<geometry::point>();

// const auto position = geometry::point{
// static_cast<int32_t>(j.value("x", 0) * scale),
// static_cast<int32_t>(j.value("y", 0) * scale),
// };

const auto size = j["size"].get<geometry::size>();

UNUSED(size);
// const auto size = geometry::size{
// static_cast<int32_t>(j.value("width", 0) * scale),
// static_cast<int32_t>(j.value("height", 0) * scale)
// };

std::map<std::string, std::vector<keyframe>> animations;

Check warning on line 50 in src/entitymanager.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entitymanager.cpp:50:48 [cppcoreguidelines-init-variables]

variable 'animations' is not initialized
for (const auto &[key, frames] : j["animations"].items()) {
Expand All @@ -58,12 +71,24 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
// {"dynamic", b2_dynamicBody},
// };

std::unordered_map<std::string, std::function<void()>> cases = {

Check warning on line 74 in src/entitymanager.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entitymanager.cpp:74:58 [cppcoreguidelines-init-variables]

variable 'cases' is not initialized
{"static", []() {

}},
{"dynamic", []() {

}}
};

const auto p = j["physics"];
const auto width = p["size"]["width"].get<int32_t>();
const auto height = p["size"]["height"].get<int32_t>();
// const auto type = mapping[p["type"].get<std::string>()];
const auto type = p["type"].get<std::string>();
const auto margin = p["margin"].get<geometry::margin>();

cases[type]();

UNUSED(position);
UNUSED(width);
UNUSED(height);
UNUSED(margin);
Expand Down Expand Up @@ -105,7 +130,8 @@ std::shared_ptr<entity> entitymanager::find(uint64_t id) const noexcept {

void entitymanager::update(float_t delta) {

Check warning on line 131 in src/entitymanager.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entitymanager.cpp:131:21 [readability-convert-member-functions-to-static]

method 'update' can be made static
UNUSED(delta);
// b2World_Step(_world, delta, 4);

cpSpaceStep(_space.get(), 1.0 / 60.0);

for (const auto &entity : _entities) {
entity->update();
Expand Down
5 changes: 3 additions & 2 deletions src/entitymanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include "eventreceiver.hpp"

namespace framework {
class entity;
class resourcemanager;
using space_ptr = std::unique_ptr<cpSpace, decltype(&cpSpaceFree)>;

class entitymanager : public input::eventreceiver {
public:
Expand All @@ -24,6 +23,8 @@ class entitymanager : public input::eventreceiver {
virtual void on_mail(const input::mailevent &event) noexcept override;

private:
space_ptr _space;

std::shared_ptr<resourcemanager> _resourcemanager;
std::list<std::shared_ptr<entity>> _entities;
std::atomic<uint64_t> _counter{0};
Expand Down
38 changes: 31 additions & 7 deletions src/margin.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@

#include "margin.hpp"

namespace geometry {
void from_json(const nlohmann::json &j, margin &m) {
m.top = j.at("top").get<int32_t>();
m.left = j.at("left").get<int32_t>();
m.bottom = j.at("bottom").get<int32_t>();
m.right = j.at("right").get<int32_t>();
using namespace geometry;

int32_t margin::top() const noexcept {
return _top;
}

void margin::set_top(int32_t value) noexcept {
_top = value;
}

int32_t margin::left() const noexcept {
return _left;
}

void margin::set_left(int32_t value) noexcept {
_left = value;
}

int32_t margin::bottom() const noexcept {
return _bottom;
}

void margin::set_bottom(int32_t value) noexcept {
_bottom = value;
}

int32_t margin::right() const noexcept {
return _right;
}

void margin::set_right(int32_t value) noexcept {
_right = value;
}
30 changes: 25 additions & 5 deletions src/margin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,34 @@
namespace geometry {
class margin {
public:
int32_t top;
int32_t left;
int32_t bottom;
int32_t right;

margin() noexcept = default;
~margin() noexcept = default;

int32_t top() const noexcept;
void set_top(int32_t value) noexcept;

int32_t left() const noexcept;
void set_left(int32_t value) noexcept;

int32_t bottom() const noexcept;
void set_bottom(int32_t value) noexcept;

int32_t right() const noexcept;
void set_right(int32_t value) noexcept;

friend void from_json(const nlohmann::json &j, margin &m);

private:
int32_t _top{0};
int32_t _left{0};
int32_t _bottom{0};
int32_t _right{0};
};

inline void from_json(const nlohmann::json &j, margin &m) {
j.at("top").get_to(m._top);
j.at("left").get_to(m._left);
j.at("bottom").get_to(m._bottom);
j.at("right").get_to(m._right);
}
}
7 changes: 7 additions & 0 deletions src/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ class point {
point operator+(const point &other) const noexcept;
point &operator+=(const point &other) noexcept;

friend void from_json(const nlohmann::json &j, point &m) noexcept;

private:
int32_t _x{0};
int32_t _y{0};
};

inline void from_json(const nlohmann::json &j, point &m) noexcept {
j.at("x").get_to(m._x);
j.at("y").get_to(m._y);
}
}
7 changes: 7 additions & 0 deletions src/size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ class size {
bool operator==(const size &rhs) const noexcept;
bool operator!=(const size &rhs) const noexcept;

friend void from_json(const nlohmann::json &j, size &s) noexcept;

private:
int32_t _width;
int32_t _height;
};

inline void from_json(const nlohmann::json &j, size &s) noexcept {
j.at("width").get_to(s._width);
j.at("height").get_to(s._height);
}
}

0 comments on commit dd57c59

Please sign in to comment.