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 e7b2afe commit c270343
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(box2d CONFIG REQUIRED)
find_package(CURL CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_package(Ogg CONFIG REQUIRED)
Expand Down Expand Up @@ -61,6 +62,7 @@ if(SANDBOX)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE
box2d::box2d
CURL::libcurl
fmt::fmt
Ogg::ogg
Expand Down
1 change: 1 addition & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[requires]
box2d/3.0.0
fmt/11.0.2
libcurl/8.10.1
libspng/0.7.4
Expand Down
4 changes: 4 additions & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern "C" {
#include <cmath>
#include <cstdint>
#include <fmt/core.h>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
Expand All @@ -40,12 +41,15 @@ extern "C" {
#include <string>
#include <string_view>
#include <unordered_map>
#include <variant>
#include <vector>

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

#include <box2d/box2d.h>

#include "constants.hpp"
#include "deleters.hpp"
#include "helpers.hpp"
Expand Down
41 changes: 41 additions & 0 deletions src/deleters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,44 @@ struct SDL_Deleter {
}
}
};

struct Box2D_Deleter {
void operator()(b2WorldId *ptr) const noexcept {
if (ptr) {
delete ptr;
}
}

// void operator()(b2BodyId *ptr) const noexcept {
// if (ptr) {
// const auto world = ptr->world0
// if (world) {
// world->DestroyBody(ptr);
// }
// }
// }

// void operator()(b2Fixture *ptr) const noexcept {
// if (ptr) {
// b2Body *body = ptr->GetBody();
// if (body) {
// body->DestroyFixture(ptr);
// }
// }
// }

// void operator()(b2Joint *ptr) const noexcept {
// if (ptr) {
// b2World *world = ptr->GetBodyA()->GetWorld(); // ou ptr->GetBodyB()->GetWorld()
// if (world) {
// world->DestroyJoint(ptr);
// }
// }
// }

// void operator()(b2Shape *ptr) const noexcept {
// if (ptr) {
// delete ptr;
// }
// }
};
2 changes: 1 addition & 1 deletion src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void entity::update(double_t delta) {
}
}

if (_props.gravitic || !_props.velocity.zero()) {
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)
Expand Down
56 changes: 53 additions & 3 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@
#include "entity.hpp"
#include "entityprops.hpp"
#include "io.hpp"
#include "margin.hpp"
#include "point.hpp"
#include "rect.hpp"
#include "resourcemanager.hpp"
#include "size.hpp"

using namespace framework;

using json = nlohmann::json;

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

_world = b2CreateWorld(&def);
}

entitymanager::~entitymanager() {
b2DestroyWorld(_world);
}

void entitymanager::set_resourcemanager(std::shared_ptr<resourcemanager> resourcemanager) noexcept {
_resourcemanager = std::move(resourcemanager);
}
Expand All @@ -19,7 +32,6 @@ 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"].get<std::string>());
const auto gravitic = j.value("gravitic", false);
const auto scale = j.value("scale", 1.0);
const auto size = geometry::size{
static_cast<int32_t>(j.value("width", 0) * scale),
Expand All @@ -44,6 +56,42 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
animations.emplace(key, std::move(keyframes));
}

/*
"physics": {
"dynamic": true,
"margin": {
"top": 0,
"left": 0,
"bottom": 0,
"right": 0
},
"size": {
"width": 32,
"height": 32
}
}
*/

static auto mapping = std::unordered_map<std::string_view, b2BodyType>{
{"static", b2_staticBody},
{"kinematic", b2_kinematicBody},
{"dynamic", b2_dynamicBody},
};

const auto type = mapping[j["physics"]["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.type = type;

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

entityprops props{
_counter++,
kind,
Expand All @@ -56,12 +104,12 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
scale,
graphics::flip::none,
255,
gravitic,
"",
0,
SDL_GetTicks(),
{},
true
true,
body
};

const auto e = entity::create(std::move(props));
Expand All @@ -82,6 +130,8 @@ std::shared_ptr<entity> entitymanager::find(uint64_t id) const noexcept {
}

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

for (const auto &entity : _entities) {
entity->update(delta);
}
Expand Down
6 changes: 4 additions & 2 deletions src/entitymanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class resourcemanager;

class entitymanager : public input::eventreceiver {
public:
entitymanager() = default;
~entitymanager() = default;
entitymanager();
~entitymanager();

void set_resourcemanager(std::shared_ptr<resourcemanager> resourcemanager) noexcept;
std::shared_ptr<entity> spawn(const std::string &kind);
Expand All @@ -24,6 +24,8 @@ class entitymanager : public input::eventreceiver {
virtual void on_mail(const input::mailevent &event) noexcept override;

private:
b2WorldId _world;

std::shared_ptr<resourcemanager> _resourcemanager;
std::list<std::shared_ptr<entity>> _entities;
std::atomic<uint64_t> _counter{0};
Expand Down
64 changes: 63 additions & 1 deletion src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,75 @@ struct entityprops {
double_t scale{1.0};
graphics::flip flip{graphics::flip::none};
uint8_t alpha{255};
bool gravitic{};
std::string action;
uint32_t frame{};
uint32_t last_frame{};
math::vector2d velocity;
bool visible{true};
b2BodyId body;

entityprops() noexcept = default;

using attribute_t = std::variant<
uint64_t,
std::string,
std::shared_ptr<graphics::pixmap>,
std::map<std::string, std::vector<keyframe>>,
geometry::point,
geometry::size,
float_t,
double_t,
graphics::flip,
uint8_t,
uint32_t,
math::vector2d,
bool,
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);
}

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);
}
};
}
11 changes: 11 additions & 0 deletions src/margin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include "margin.hpp"

using 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>();
}
18 changes: 18 additions & 0 deletions src/margin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "common.hpp"

namespace geometry {
class margin {
public:
int32_t top;
int32_t left;
int32_t bottom;
int32_t right;

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

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

0 comments on commit c270343

Please sign in to comment.