Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Jan 5, 2025
1 parent 190a194 commit de59de7
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 41 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ else()
endif()

if (CMAKE_BUILD_TYPE MATCHES "Debug")
target_compile_definitions(${PROJECT_NAME} PRIVATE DEBUG)
if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /Od /Zi)
else()
Expand All @@ -47,7 +46,7 @@ elseif (CMAKE_BUILD_TYPE MATCHES "Release")
if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /O2 /GL)
else()
target_compile_options(${PROJECT_NAME} PRIVATE -O2 -flto)
target_compile_options(${PROJECT_NAME} PRIVATE -Os -flto)
endif()
endif()

Expand Down Expand Up @@ -89,6 +88,11 @@ if(SANDBOX)
target_compile_definitions(${PROJECT_NAME} PRIVATE SANDBOX)
endif()

option(HITBOX "Show hitboxes" OFF)
if(LOCAL)
target_compile_definitions(${PROJECT_NAME} PRIVATE HITBOX)
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE
physfs-static
SDL2::SDL2-static
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ build: ## Build

configure: clean ## Configure
conan remote update conancenter --url https://center2.conan.io
conan install . --output-folder=build --build=missing --profile=webassembly --settings compiler.cppstd=20 --settings build_type=Debug
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DSDL2_DIR=generators -DCMAKE_BUILD_TYPE=Debug -DLOCAL=ON -DSANDBOX=OFF -DENABLE_PROFILING=ON
conan install . --output-folder=build --build=missing --profile=webassembly --settings compiler.cppstd=20 --settings build_type=Release
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DSDL2_DIR=generators -DCMAKE_BUILD_TYPE=Release -DLOCAL=ON -DHITBOX=ON -DSANDBOX=OFF -DENABLE_PROFILING=ON
27 changes: 21 additions & 6 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,29 @@ void entity::draw() const noexcept {
const auto &animation = _props.animations.at(_props.action).keyframes.at(_props.frame);
const auto &source = animation.frame;
const auto &offset = animation.offset;
#ifdef DEBUG
#ifdef HITBOX
const auto &hitbox = _props.animations.at(_props.action).hitbox;
#endif

geometry::rect destination{_props.position + offset, source.size()};

destination.scale(_props.scale);

#ifdef HITBOX
const auto debug = hitbox
? std::make_optional(geometry::rect{_props.position + hitbox->position(), hitbox->size() * _props.scale})
: std::nullopt;
#else
const auto debug = std::nullopt;
#endif

_props.spritesheet->draw(
source,
destination,
_props.angle,
_props.reflection,
_props.alpha
#ifdef DEBUG
,
{_props.position + hitbox.position(), hitbox.size()}
#endif
_props.alpha,
debug
);
}

Expand Down Expand Up @@ -181,6 +186,16 @@ bool entity::visible() const noexcept {
return _props.visible;
}

bool entity::intersects(const std::shared_ptr<entity> &other) const noexcept {
const auto &hitbox = _props.animations.at(_props.action).hitbox;
const auto &other_hitbox = other->_props.animations.at(_props.action).hitbox;
if (!hitbox || !other_hitbox) [[likely]] {
return false;
}

return hitbox->intersects(*other_hitbox);
}

void entity::on_email(const std::string &message) {
if (_onmail) {
_onmail(shared_from_this(), message);
Expand Down
2 changes: 2 additions & 0 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class entity : public std::enable_shared_from_this<entity> {
geometry::size size() const noexcept;
bool visible() const noexcept;

bool intersects(const std::shared_ptr<entity> &other) const noexcept;

void on_email(const std::string &message);

memory::kv &kv() noexcept;
Expand Down
37 changes: 24 additions & 13 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {

std::map<std::string, animation> animations;
for (const auto &[key, anim] : j["animations"].items()) {
const auto hitbox = anim.value("hitbox", geometry::rect{});
const auto hitbox = anim.contains("hitbox")
? std::make_optional(anim["hitbox"].get<geometry::rect>())
: std::nullopt;

std::vector<keyframe> keyframes;
keyframes.reserve(16);
for (const auto &frame : anim["frames"]) {
keyframes.emplace_back(
frame["rect"].get<geometry::rect>(),
frame.value("offset", geometry::point{}),
frame["duration"].get<uint64_t>(),
frame.value("singleshoot", false),
frame.value("offset", geometry::point{})
frame.value("singleshoot", false)
);
}
animations.emplace(key, animation{hitbox, std::move(keyframes)});
Expand Down Expand Up @@ -95,8 +97,8 @@ void entitymanager::update(float_t delta) noexcept {
continue;
}

const auto &pos1 = entity1->position();
const auto &size1 = entity1->size() * entity1->props().scale;
// const auto &pos1 = entity1->position();
// const auto &size1 = entity1->size() * entity1->props().scale;

for (const auto &[kind, callback] : entity1->_collisionmapping) {
if (auto it = mapping.find(kind); it != mapping.end()) [[likely]] {
Expand All @@ -106,17 +108,26 @@ void entitymanager::update(float_t delta) noexcept {
if (entity1 == entity2) [[unlikely]]
continue;

const auto &pos2 = entity2->position();
const auto &size2 = entity2->size() * entity2->props().scale;

if (pos1.x() < pos2.x() + size2.width() &&
pos1.x() + size1.width() > pos2.x() &&
pos1.y() < pos2.y() + size2.height() &&
pos1.y() + size1.height() > pos2.y()) [[likely]] {

if (entity1->intersects(entity2)) {
callback(entity1, entity2->id());
}
}

// for (const auto &entity2 : mapping) {
// if (entity1 == entity2) [[unlikely]]
// continue;

// const auto &pos2 = entity2->position();
// const auto &size2 = entity2->size() * entity2->props().scale;

// if (pos1.x() < pos2.x() + size2.width() &&
// pos1.x() + size1.width() > pos2.x() &&
// pos1.y() < pos2.y() + size2.height() &&
// pos1.y() + size1.height() > pos2.y()) [[likely]] {

// callback(entity1, entity2->id());
// }
// }
}
}
}
Expand Down
10 changes: 1 addition & 9 deletions src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@ struct keyframe {
geometry::point offset;
uint64_t duration{};
bool singleshoot{};

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

struct animation {
geometry::rect hitbox;
std::optional<geometry::rect> hitbox;
std::vector<keyframe> keyframes;

animation() = default;
animation(const geometry::rect &hitbox, std::vector<keyframe> keyframes)
: hitbox(hitbox), keyframes(std::move(keyframes)) {}
};

struct entityprops {
Expand Down
2 changes: 1 addition & 1 deletion src/fontfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using namespace graphics;
using json = nlohmann::json;

fontfactory::fontfactory(const std::shared_ptr<graphics::renderer> renderer) noexcept
: _renderer(std::move(renderer)) {}
: _renderer(renderer) {}

std::shared_ptr<font> fontfactory::get(const std::string &family) {
if (auto it = _pool.find(family); it != _pool.end()) [[likely]] {
Expand Down
14 changes: 8 additions & 6 deletions src/pixmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ void pixmap::draw(
const double_t angle,
reflection reflection,
const uint8_t alpha
#ifdef DEBUG
#ifdef HITBOX
,
const geometry::rect &outline
const std::optional<geometry::rect> &outline
#endif
) const noexcept {
const SDL_Rect &src = source;
Expand All @@ -64,11 +64,13 @@ void pixmap::draw(
SDL_SetTextureAlphaMod(_texture.get(), alpha);
SDL_RenderCopyEx(*_renderer, _texture.get(), &src, &dst, angle, nullptr, static_cast<SDL_RendererFlip>(reflection));

#ifdef DEBUG
const SDL_Rect &debug = outline;
#ifdef HITBOX
if (outline) {
const SDL_Rect &debug = *outline;

SDL_SetRenderDrawColor(*_renderer, 0, 255, 0, 255);
SDL_RenderDrawRect(*_renderer, &debug);
SDL_SetRenderDrawColor(*_renderer, 0, 255, 0, 255);
SDL_RenderDrawRect(*_renderer, &debug);
}
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions src/pixmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class pixmap {
double_t angle = 0.0f,
reflection reflection = reflection::none,
uint8_t alpha = 255
#ifdef DEBUG
#ifdef HITBOX
,
const geometry::rect &outline = {}
const std::optional<geometry::rect> &outline = std::nullopt
#endif
) const noexcept;

Expand Down
9 changes: 9 additions & 0 deletions src/rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ void rect::scale(float_t factor) noexcept {
_size.set_height(static_cast<int>(_size.height() * factor));
}

bool rect::intersects(const rect &other) const noexcept {
return !(
_position.x() + _size.width() <= other._position.x() ||
_position.x() >= other._position.x() + other._size.width() ||
_position.y() + _size.height() <= other._position.y() ||
_position.y() >= other._position.y() + other._size.height()
);
}

rect::operator SDL_Rect() const noexcept {
return SDL_Rect{
.x = static_cast<int>(_position.x()),
Expand Down
2 changes: 2 additions & 0 deletions src/rect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class rect {

void scale(float_t factor) noexcept;

bool intersects(const rect &other) const noexcept;

operator SDL_Rect() const noexcept;

rect operator+(const point &offset) const noexcept;
Expand Down

0 comments on commit de59de7

Please sign in to comment.