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 20, 2024
1 parent 6f2fe54 commit e769186
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <AL/alc.h>
#include <SDL2/SDL.h>
#include <chipmunk/chipmunk.h>
#include <chipmunk/chipmunk_structs.h>
#include <curl/curl.h>
#include <fmt/core.h>
#include <nlohmann/json.hpp>
Expand Down
7 changes: 7 additions & 0 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@ inline void run(void *arg) {
}
#endif

#include "fontfactory.hpp"

void engine::run() {

graphics::fontfactory ff(_resourcemanager);
// TODO XXX
/*const auto font = */ ff.get("fixedsys");

#ifdef EMSCRIPTEN
emscripten_set_main_loop_arg(::run<engine>, this, 0, true);
#else
Expand Down
11 changes: 8 additions & 3 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {

body_ptr body{nullptr, cpBodyFree};
std::unordered_map<bodytype, std::function<void()>> mapping = {
{bodytype::stationary, [&body]() {
{bodytype::stationary, [&]() {
body = body_ptr(cpBodyNewStatic(), [](cpBody *body) { cpBodyFree(body); });
}},
{bodytype::kinematic, [&body]() {
{bodytype::kinematic, [&]() {
body = body_ptr(cpBodyNewKinematic(), [](cpBody *body) { cpBodyFree(body); });
}},
{bodytype::dynamic, [&body, &size]() {
{bodytype::dynamic, [&]() {
body = body_ptr(cpBodyNew(1.0, cpMomentForBox(1.0, size.width(), size.height())), [](cpBody *body) { cpBodyFree(body); });
}}
};
Expand All @@ -75,6 +75,11 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {

auto shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });

shape.get()->filter = CP_SHAPE_FILTER_ALL;

// cpShapeSetCollisionType(shape.get(), p["collision"].get<collision>().type);
cpShapeSetCollisionType(shape.get(), p["collision"].get<collision>().type);

cpShapeSetFriction(shape.get(), p.value("friction", 0.5f));
cpShapeSetElasticity(shape.get(), p.value("elasticity", 0.3f));
cpSpaceAddShape(_world->space().get(), shape.get());
Expand Down
15 changes: 15 additions & 0 deletions src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ enum bodytype : int8_t {

NLOHMANN_JSON_SERIALIZE_ENUM(bodytype, {{stationary, "stationary"}, {dynamic, "dynamic"}, {kinematic, "kinematic"}})

enum collisiontype : uint8_t {
player = 1,
enemy = 2,
playerbullet = 3,
enemybullet = 4,
wall = 5
};

struct collision {
collisiontype type;
std::optional<std::string> from;
};

void from_json(const nlohmann::json &j, collision &c);

struct keyframe {
geometry::rect frame;
geometry::point offset;
Expand Down
8 changes: 6 additions & 2 deletions src/font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
#include "common.hpp"

namespace graphics {
using glyphmap = std::map<uint8_t, geometry::rect>;

class font {
public:
font() = default;
font(const glyphmap &glyphs, std::shared_ptr<pixmap> pixmap);
~font() = default;

void draw(std::string_view text) const noexcept;

private:
std::map<uint8_t, glyph> _glyphs;
glyphmap _glyphs;
std::shared_ptr<pixmap> _pixmap;
};
}
4 changes: 2 additions & 2 deletions src/fontfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using json = nlohmann::json;
fontfactory::fontfactory(std::shared_ptr<framework::resourcemanager> resourcemanager) noexcept
: _resourcemanager(std::move(resourcemanager)) {}

std::shared_ptr<font> fontfactory::get(const std::string &face) {
/* std::shared_ptr<font> */ void fontfactory::get(const std::string &face) {
const auto buffer = storage::io::read(fmt::format("fonts/{}.json", face));
const auto j = json::parse(buffer);
const auto alphabet = j["alphabet"].get<std::string>();
Expand Down Expand Up @@ -49,5 +49,5 @@ std::shared_ptr<font> fontfactory::get(const std::string &face) {
}
}

return std::make_shared<font>();
// return std::make_shared<font>();
}
2 changes: 1 addition & 1 deletion src/fontfactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class fontfactory {
explicit fontfactory(const std::shared_ptr<framework::resourcemanager> resourcemanager) noexcept;
~fontfactory() noexcept = default;

std::shared_ptr<font> get(const std::string &face);
/* std::shared_ptr<font> */ void get(const std::string &face);

private:
std::shared_ptr<framework::resourcemanager> _resourcemanager;
Expand Down
9 changes: 4 additions & 5 deletions src/glyph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

using namespace graphics;

glyph::glyph(uint32_t code) noexcept
: _code(code) {}

void glyph::draw(const glyphprops &props) const noexcept {
UNUSED(props);
glyph::glyph(const geometry::rect &rect) noexcept {
UNUSED(rect);
}

void glyph::draw() const noexcept {}
7 changes: 4 additions & 3 deletions src/glyph.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#pragma once

#include "common.hpp"
#include "glyphprops.hpp"

namespace graphics {
class glyph {
public:
explicit glyph(uint32_t code) noexcept;
explicit glyph(const geometry::rect &rect) noexcept;

void draw(const glyphprops &props) const noexcept;
void draw() const noexcept;

private:
uint32_t _code{0};
geometry::rect _rect{};
double_t _angle{0.0f};
};
}
25 changes: 25 additions & 0 deletions src/unmarshalling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,28 @@ void from_json(const nlohmann::json &j, rect &r) noexcept {
r._size = geometry::size{j.at("width").get<int>(), j.at("height").get<int>()};
}
}

namespace framework {
void from_json(const nlohmann::json &j, collision &c) {
const auto type = j.at("type").get<std::string>();

if (type == "bullet") {
const auto from = j.at("from").get<std::string>();
if (from == "player") {
c.type = playerbullet;
} else if (from == "enemy") {
c.type = enemybullet;
} else {
throw std::invalid_argument("[collision] Invalid 'from' value for bullet");
}
} else if (type == "player") {
c.type = player;
} else if (type == "enemy") {
c.type = enemy;
} else if (type == "wall") {
c.type = wall;
} else {
throw std::invalid_argument("[collision] Invalid 'type' value");
}
}
}
37 changes: 36 additions & 1 deletion src/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,42 @@ void world::draw() noexcept {
return cpPolyShapeGetVert(shape, i++);
});

SDL_SetRenderDrawColor(*_renderer.get(), 0, 255, 0, 255);
uint8_t r, g, b, a = 255;
switch (cpShapeGetCollisionType(shape)) {
case 1:
r = 255;
g = 0;
b = 0;
break;
case 2:
r = 0;
g = 0;
b = 255;
break;
case 3:
r = 255;
g = 255;
b = 0;
break;
case 4:
r = 0;
g = 255;
b = 255;
break;
case 5:
r = 0;
g = 255;
b = 0;
break;
default:
r = 255;
g = 255;
b = 255;
break;
}

SDL_SetRenderDrawColor(*_renderer.get(), r, g, b, a);

for (int i = 0; i < count; ++i) {
int next = (i + 1) % count;

Expand Down

0 comments on commit e769186

Please sign in to comment.