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 30, 2024
1 parent c270343 commit e38c8ff
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void engine::run() {
void engine::_loop() {
static auto prior = SDL_GetTicks();
const auto now = SDL_GetTicks();
const auto delta = now - prior;
const auto delta = static_cast<float_t>(now - prior);

prior = now;

Expand Down
16 changes: 9 additions & 7 deletions src/entity.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "entity.hpp"
#include <box2d/box2d.h>

using namespace framework;

Expand All @@ -23,7 +24,7 @@ int32_t entity::x() const noexcept { return _props.position.x(); }

int32_t entity::y() const noexcept { return _props.position.y(); }

void entity::update(double_t delta) {
void entity::update() {
if (_onupdate) {
_onupdate(shared_from_this());
}
Expand Down Expand Up @@ -55,12 +56,13 @@ void entity::update(double_t delta) {
}
}

if (!_props.velocity.zero()) {
_props.position.set(
_props.position.x() + static_cast<int32_t>(_props.velocity.x() * delta),
_props.position.y() + static_cast<int32_t>(_props.velocity.y() * delta)
);
}
const auto position = b2Body_GetPosition(_props.body);
_props.position.set(
static_cast<int32_t>(std::round(position.x)),
static_cast<int32_t>(std::round(position.y))
);

_props.angle = b2Rot_GetAngle(b2Body_GetRotation(_props.body));
}

void entity::draw() const {
Expand Down
2 changes: 1 addition & 1 deletion src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class entity : public std::enable_shared_from_this<entity> {
uint64_t id() const noexcept;
std::string kind() const;

virtual void update(double_t delta);
virtual void update();
virtual void draw() const;

bool colliding_with(const entity &other) const noexcept;
Expand Down
68 changes: 39 additions & 29 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ using namespace framework;
using json = nlohmann::json;

entitymanager::entitymanager() {
auto def = b2DefaultWorldDef();
def.gravity = {0.0f, -10.0f};
auto worldDef = b2DefaultWorldDef();
worldDef.gravity = {0.0f, -10.0f};

_world = b2CreateWorld(&def);
_world = b2CreateWorld(&worldDef);
}

entitymanager::~entitymanager() {
Expand Down Expand Up @@ -78,37 +78,47 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
{"dynamic", b2_dynamicBody},
};

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

const auto margin = j["physics"]["margin"].get<geometry::margin>();
// const auto top = .value("top", 0);
// const auto left = j["physics"]["margin"].value("left", 0);
// const auto top = j["physics"]["margin"].value("top", 0);
// const auto top = j["physics"]["margin"].value("top", 0);

auto bodyDef = b2DefaultBodyDef();
bodyDef.position = {0.0f, -10.0f};
bodyDef.position = {.0f, .0f};
bodyDef.type = type;

const auto body = b2CreateBody(_world, &bodyDef);

b2Polygon shape = b2MakeBox(
(width - margin.left - margin.right) * .5f,
(height - margin.top - margin.bottom) * .5f
);

b2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = p.value("density", 1.f);
shapeDef.friction = 0.3f;
b2CreatePolygonShape(body, &shapeDef, &shape);

entityprops props{
_counter++,
kind,
spritesheet,
std::move(animations),
0,
SDL_GetTicks(),
0.0,
scale,
255,
true,
{},
{},
size,
0.0f,
scale,
graphics::flip::none,
255,
"",
0,
SDL_GetTicks(),
{},
true,
kind,
"",
graphics::flip::none,
spritesheet,
std::move(animations),
body
};

Expand All @@ -129,19 +139,19 @@ std::shared_ptr<entity> entitymanager::find(uint64_t id) const noexcept {
return (it != _entities.end()) ? *it : nullptr;
}

void entitymanager::update(double_t delta) {
b2World_Step(_world, delta, 1);
void entitymanager::update(float_t delta) {
b2World_Step(_world, delta, 4);

for (const auto &entity : _entities) {
entity->update(delta);
entity->update();
}

for (auto a = _entities.begin(); a != _entities.end(); ++a) {
for (auto b = std::next(a); b != _entities.end(); ++b) {
if ((*a)->colliding_with(**b)) {
}
}
}
// for (auto a = _entities.begin(); a != _entities.end(); ++a) {
// for (auto b = std::next(a); b != _entities.end(); ++b) {
// if ((*a)->colliding_with(**b)) {
// }
// }
// }
}

void entitymanager::draw() noexcept {
Expand Down
2 changes: 1 addition & 1 deletion src/entitymanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class entitymanager : public input::eventreceiver {
std::shared_ptr<entity> spawn(const std::string &kind);
void destroy(const std::weak_ptr<entity> entity) noexcept;
std::shared_ptr<entity> find(uint64_t id) const noexcept;
void update(double_t delta);
void update(float_t delta);
void draw() noexcept;

protected:
Expand Down
139 changes: 86 additions & 53 deletions src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,35 @@
#include "vector2d.hpp"

namespace framework {

struct keyframe {
geometry::rect frame;
geometry::point offset;
uint64_t duration{};
bool singleshoot{};
geometry::point offset;

keyframe() noexcept = default;
keyframe(const geometry::rect &rect, uint64_t duration, bool singleshoot, const geometry::point &offset) noexcept
: frame(rect), duration(duration), singleshoot(singleshoot), offset(offset) {}
: frame(rect), offset(offset), duration(duration), singleshoot(singleshoot) {}
};

struct entityprops {
uint64_t id{};
std::string kind;
std::shared_ptr<graphics::pixmap> spritesheet;
std::map<std::string, std::vector<keyframe>> animations;
uint32_t frame{};
uint32_t last_frame{};
double_t angle{};
double_t scale{1.0};
uint8_t alpha{255};
bool visible{true};
geometry::point position;
geometry::point pivot;
geometry::size size;
float_t angle{};
double_t scale{1.0};
graphics::flip flip{graphics::flip::none};
uint8_t alpha{255};
std::string action;
uint32_t frame{};
uint32_t last_frame{};
math::vector2d velocity;
bool visible{true};
std::string kind;
std::string action;
graphics::flip flip{graphics::flip::none};
std::shared_ptr<graphics::pixmap> spritesheet;
std::map<std::string, std::vector<keyframe>> animations;
b2BodyId body;

entityprops() noexcept = default;
Expand All @@ -58,49 +59,81 @@ struct entityprops {
b2BodyId>;

attribute_t get(const std::string &name) const {
static const std::map<std::string, std::function<attribute_t(const entityprops &)>> getters{
{"id", [](const entityprops &e) { return e.id; }},
{"kind", [](const entityprops &e) { return e.kind; }},
{"spritesheet", [](const entityprops &e) { return e.spritesheet; }},
{"animations", [](const entityprops &e) { return e.animations; }},
{"position", [](const entityprops &e) { return e.position; }},
{"pivot", [](const entityprops &e) { return e.pivot; }},
{"size", [](const entityprops &e) { return e.size; }},
{"angle", [](const entityprops &e) { return e.angle; }},
{"scale", [](const entityprops &e) { return e.scale; }},
{"flip", [](const entityprops &e) { return e.flip; }},
{"alpha", [](const entityprops &e) { return e.alpha; }},
{"action", [](const entityprops &e) { return e.action; }},
{"frame", [](const entityprops &e) { return e.frame; }},
{"last_frame", [](const entityprops &e) { return e.last_frame; }},
{"velocity", [](const entityprops &e) { return e.velocity; }},
{"visible", [](const entityprops &e) { return e.visible; }},
{"body", [](const entityprops &e) { return e.body; }},
};
return getters.at(name)(*this);
if (name == "id")
return id;
else if (name == "frame")
return frame;
else if (name == "last_frame")
return last_frame;
else if (name == "angle")
return angle;
else if (name == "scale")
return scale;
else if (name == "alpha")
return alpha;
else if (name == "visible")
return visible;
else if (name == "position")
return position;
else if (name == "pivot")
return pivot;
else if (name == "size")
return size;
else if (name == "velocity")
return velocity;
else if (name == "kind")
return kind;
else if (name == "action")
return action;
else if (name == "flip")
return flip;
else if (name == "spritesheet")
return spritesheet;
else if (name == "animations")
return animations;
else if (name == "body")
return body;
throw std::invalid_argument("Invalid property name");
}

void set(const std::string &name, const attribute_t &value) {
static const std::map<std::string, std::function<void(entityprops &, const attribute_t &)>> setters{
{"id", [](entityprops &e, const attribute_t &v) { e.id = std::get<uint64_t>(v); }},
{"kind", [](entityprops &e, const attribute_t &v) { e.kind = std::get<std::string>(v); }},
{"spritesheet", [](entityprops &e, const attribute_t &v) { e.spritesheet = std::get<std::shared_ptr<graphics::pixmap>>(v); }},
{"animations", [](entityprops &e, const attribute_t &v) { e.animations = std::get<std::map<std::string, std::vector<keyframe>>>(v); }},
{"position", [](entityprops &e, const attribute_t &v) { e.position = std::get<geometry::point>(v); }},
{"pivot", [](entityprops &e, const attribute_t &v) { e.pivot = std::get<geometry::point>(v); }},
{"size", [](entityprops &e, const attribute_t &v) { e.size = std::get<geometry::size>(v); }},
{"angle", [](entityprops &e, const attribute_t &v) { e.angle = std::get<float_t>(v); }},
{"scale", [](entityprops &e, const attribute_t &v) { e.scale = std::get<double_t>(v); }},
{"flip", [](entityprops &e, const attribute_t &v) { e.flip = std::get<graphics::flip>(v); }},
{"alpha", [](entityprops &e, const attribute_t &v) { e.alpha = std::get<uint8_t>(v); }},
{"action", [](entityprops &e, const attribute_t &v) { e.action = std::get<std::string>(v); }},
{"frame", [](entityprops &e, const attribute_t &v) { e.frame = std::get<uint32_t>(v); }},
{"last_frame", [](entityprops &e, const attribute_t &v) { e.last_frame = std::get<uint32_t>(v); }},
{"velocity", [](entityprops &e, const attribute_t &v) { e.velocity = std::get<math::vector2d>(v); }},
{"visible", [](entityprops &e, const attribute_t &v) { e.visible = std::get<bool>(v); }},
{"body", [](entityprops &e, const attribute_t &v) { e.body = std::get<b2BodyId>(v); }},
};
setters.at(name)(*this, value);
if (name == "id")
id = std::get<uint64_t>(value);
else if (name == "frame")
frame = std::get<uint32_t>(value);
else if (name == "last_frame")
last_frame = std::get<uint32_t>(value);
else if (name == "angle")
angle = std::get<double_t>(value);
else if (name == "scale")
scale = std::get<double_t>(value);
else if (name == "alpha")
alpha = std::get<uint8_t>(value);
else if (name == "visible")
visible = std::get<bool>(value);
else if (name == "position")
position = std::get<geometry::point>(value);
else if (name == "pivot")
pivot = std::get<geometry::point>(value);
else if (name == "size")
size = std::get<geometry::size>(value);
else if (name == "velocity")
velocity = std::get<math::vector2d>(value);
else if (name == "kind")
kind = std::get<std::string>(value);
else if (name == "action")
action = std::get<std::string>(value);
else if (name == "flip")
flip = std::get<graphics::flip>(value);
else if (name == "spritesheet")
spritesheet = std::get<std::shared_ptr<graphics::pixmap>>(value);
else if (name == "animations")
animations = std::get<std::map<std::string, std::vector<keyframe>>>(value);
else if (name == "body")
body = std::get<b2BodyId>(value);
else
throw std::invalid_argument("Invalid property name");
}
};

}
2 changes: 1 addition & 1 deletion src/eventmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ eventmanager::eventmanager() {
}
}

void eventmanager::update(double_t delta) {
void eventmanager::update(float_t delta) {
UNUSED(delta);

static const std::unordered_map<Uint8, SDL_Keycode> mapping = {
Expand Down
2 changes: 1 addition & 1 deletion src/eventmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class eventmanager : private framework::noncopyable {
eventmanager();
virtual ~eventmanager() = default;

void update(double_t delta);
void update(float_t delta);

void add_receiver(std::shared_ptr<eventreceiver> receiver) noexcept;

Expand Down
2 changes: 1 addition & 1 deletion src/framerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace framework;

uint64_t framerate::per_second() const noexcept { return _frames; }

void framerate::loop(uint32_t delta) noexcept {
void framerate::loop(float_t delta) noexcept {
UNUSED(delta);
_frames++;

Expand Down
2 changes: 1 addition & 1 deletion src/framerate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class framerate : public loopable {

uint64_t per_second() const noexcept;

void loop(uint32_t delta) noexcept override;
void loop(float_t delta) noexcept override;

private:
uint64_t _frames{0};
Expand Down
4 changes: 2 additions & 2 deletions src/glyphprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace graphics {
class glyphprops {
public:
glyphprops() noexcept = default;
explicit glyphprops(const geometry::rect &rect, const color &color, float_t angle = 0.0f) noexcept
explicit glyphprops(const geometry::rect &rect, const color &color, double_t angle = 0.0f) noexcept
: _rect(rect), _color(color), _angle(angle) {}

geometry::rect _rect;
color _color;
float_t _angle{0.0f};
double_t _angle{0.0f};
};
}
2 changes: 1 addition & 1 deletion src/loopable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class loopable {
loopable() = default;
virtual ~loopable() = default;

virtual void loop(uint32_t delta) noexcept = 0;
virtual void loop(float_t delta) noexcept = 0;
};
}
Loading

0 comments on commit e38c8ff

Please sign in to comment.