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 4, 2025
1 parent 43321c8 commit 190a194
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 86 deletions.
8 changes: 2 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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 Down Expand Up @@ -65,13 +66,8 @@ if (${CMAKE_SYSTEM_NAME} MATCHES Emscripten)
if (ENABLE_PROFILING)
list(APPEND EMSCRIPTEN_OPTIONS
-O0
-g
-gsource-map
-sDEMANGLE_SUPPORT=1
-sASSERTIONS=1
-sNO_DISABLE_EXCEPTION_CATCHING
--profiling-funcs
-sSAFE_HEAP=0
-sSAFE_HEAP=1
-sSTACK_OVERFLOW_CHECK=2
)
else()
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=Release
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DSDL2_DIR=generators -DCMAKE_BUILD_TYPE=Release -DLOCAL=ON -DSANDBOX=OFF -DENABLE_PROFILING=ON
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
24 changes: 18 additions & 6 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ void entity::update(float_t delta) noexcept {

const auto now = SDL_GetTicks();
const auto &animation = _props.animations.at(_props.action);
const auto &frame = animation[_props.frame];
const auto &frame = animation.keyframes.at(_props.frame);

if (frame.duration > 0 && now - _props.last_frame >= frame.duration) {
++_props.frame;
_props.last_frame = now;

if (_props.frame >= animation.size()) {
if (std::ranges::any_of(animation, [](const auto &keyframe) { return keyframe.singleshoot; })) {
if (_props.frame >= animation.keyframes.size()) {
if (std::ranges::any_of(animation.keyframes, [](const auto &keyframe) { return keyframe.singleshoot; })) {
_props.action.clear();

if (_onanimationfinished) {
Expand All @@ -99,9 +99,13 @@ void entity::draw() const noexcept {
return;
}

const auto &animation = _props.animations.at(_props.action)[_props.frame];
const auto &animation = _props.animations.at(_props.action).keyframes.at(_props.frame);
const auto &source = animation.frame;
const auto &offset = animation.offset;
#ifdef DEBUG
const auto &hitbox = _props.animations.at(_props.action).hitbox;
#endif

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

destination.scale(_props.scale);
Expand All @@ -112,6 +116,10 @@ void entity::draw() const noexcept {
_props.angle,
_props.reflection,
_props.alpha
#ifdef DEBUG
,
{_props.position + hitbox.position(), hitbox.size()}
#endif
);
}

Expand Down Expand Up @@ -143,15 +151,19 @@ void entity::set_reflection(graphics::reflection reflection) noexcept {
_props.reflection = reflection;
}

void entity::set_action(const std::string &action) {
void entity::set_action(const std::string &action) noexcept {
if (_props.action != action) {
_props.action.assign(action);
_props.frame = 0;
_props.last_frame = SDL_GetTicks();
}
}

void entity::unset_action() {
std::string entity::get_action() const noexcept {
return _props.action;
}

void entity::unset_action() noexcept {
_props.action.clear();
_props.frame = 0;
_props.last_frame = SDL_GetTicks();
Expand Down
5 changes: 3 additions & 2 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class entity : public std::enable_shared_from_this<entity> {
void set_oncollision(const std::string &kind, const std::function<void(std::shared_ptr<entity>, uint64_t)> &fn) noexcept;

void set_reflection(graphics::reflection reflection) noexcept;
void set_action(const std::string &action);
void unset_action();
void set_action(const std::string &action) noexcept;
std::string get_action() const noexcept;
void unset_action() noexcept;

std::string action() const noexcept;
geometry::size size() const noexcept;
Expand Down
26 changes: 14 additions & 12 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "entitymanager.hpp"
#include "common.hpp"
#include "entityprops.hpp"
#include <ostream>

using namespace framework;
Expand All @@ -21,20 +22,21 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
? _resourcemanager->pixmappool()->get(j["spritesheet"].get_ref<const std::string &>())
: nullptr;

std::map<std::string, std::vector<keyframe>> animations;
for (const auto &[key, frames] : j["animations"].items()) {
std::map<std::string, animation> animations;
for (const auto &[key, anim] : j["animations"].items()) {
const auto hitbox = anim.value("hitbox", geometry::rect{});

std::vector<keyframe> keyframes;
for (const auto &frame : frames) {
for (const auto &f : frame) {
keyframes.emplace_back(
f["rect"].get<geometry::rect>(),
f["duration"].get<uint64_t>(),
f.value("singleshoot", false),
f.value("offset", geometry::point{})
);
}
keyframes.reserve(16);
for (const auto &frame : anim["frames"]) {
keyframes.emplace_back(
frame["rect"].get<geometry::rect>(),
frame["duration"].get<uint64_t>(),
frame.value("singleshoot", false),
frame.value("offset", geometry::point{})
);
}
animations.emplace(key, std::move(keyframes));
animations.emplace(key, animation{hitbox, std::move(keyframes)});
}

entityprops props{
Expand Down
11 changes: 10 additions & 1 deletion src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ struct keyframe {
: frame(rect), offset(offset), duration(duration), singleshoot(singleshoot) {}
};

struct animation {
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 {
uint64_t id{};
uint32_t frame{};
Expand All @@ -37,6 +46,6 @@ struct entityprops {
std::string action{};
graphics::reflection reflection{graphics::reflection::none};
std::shared_ptr<graphics::pixmap> spritesheet{};
std::map<std::string, std::vector<keyframe>> animations{};
std::map<std::string, animation> animations{};
};
}
18 changes: 17 additions & 1 deletion src/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum player : uint8_t {
two
};

enum eventtype : Uint32 {
enum eventtype : uint32_t {
mail = SDL_USEREVENT + 1,
timer
};
Expand All @@ -32,6 +32,22 @@ enum class joystickevent : int32_t {
square = SDL_CONTROLLER_BUTTON_X,
};

struct joystickaxisevent {
enum class axis : uint8_t {
invalid = SDL_CONTROLLER_AXIS_INVALID,
leftx = SDL_CONTROLLER_AXIS_LEFTX,
lefty = SDL_CONTROLLER_AXIS_LEFTY,
rightx = SDL_CONTROLLER_AXIS_RIGHTX,
righty = SDL_CONTROLLER_AXIS_RIGHTY,
triggerleft = SDL_CONTROLLER_AXIS_TRIGGERLEFT,
triggerright = SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
max = SDL_CONTROLLER_AXIS_MAX
};

axis kind;
int16_t value;
};

enum class mouseevent : int32_t {};

class mailevent {
Expand Down
61 changes: 9 additions & 52 deletions src/eventmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ eventmanager::eventmanager() {
void eventmanager::update(float_t delta) {
UNUSED(delta);

// static constexpr std::array<std::pair<Uint8, SDL_Keycode>, 6> mapping = {{
// {SDL_CONTROLLER_BUTTON_DPAD_UP, SDLK_UP},
// {SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDLK_LEFT},
// {SDL_CONTROLLER_BUTTON_DPAD_DOWN, SDLK_DOWN},
// {SDL_CONTROLLER_BUTTON_DPAD_RIGHT, SDLK_RIGHT},
// {SDL_CONTROLLER_BUTTON_A, SDLK_SPACE},
// {SDL_CONTROLLER_BUTTON_B, SDLK_SPACE},
// }};

SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
Expand Down Expand Up @@ -71,52 +62,18 @@ void eventmanager::update(float_t delta) {
for (const auto &receiver : _receivers) {
receiver->on_joystickbuttonup(event.cbutton.which, joystickevent(event.cbutton.button));
}

// const auto it = std::find_if(mapping.begin(), mapping.end(), [&event](const auto &pair) { return pair.first == event.cbutton.button; });

// if (it != mapping.end()) {
// const auto action = (event.type == SDL_CONTROLLERBUTTONDOWN) ? &eventreceiver::on_keydown : &eventreceiver::on_keyup;
// for (const auto &receiver : _receivers) {
// (receiver.get()->*action)(keyevent(it->second));
// }
// }
} break;

case SDL_CONTROLLERAXISMOTION: {
// static constexpr auto threshold = 8000;
// static constexpr auto deadzone = 4000;

// const auto axis = event.caxis.axis;
// const auto value = event.caxis.value;
// const auto process = [&](SDL_Keycode negative, SDL_Keycode positive) {
// if (value < -threshold) {
// for (const auto &receiver : _receivers) {
// receiver->on_keydown(keyevent(negative));
// }
// } else if (value > threshold) {
// for (const auto &receiver : _receivers) {
// receiver->on_keydown(keyevent(positive));
// }
// } else if (std::abs(value) < deadzone) {
// for (const auto &receiver : _receivers) {
// receiver->on_keyup(keyevent(negative));
// receiver->on_keyup(keyevent(positive));
// }
// }
// };

// switch (axis) {
// case SDL_CONTROLLER_AXIS_LEFTY:
// // process(SDLK_UP, SDLK_DOWN);
// break;

// case SDL_CONTROLLER_AXIS_LEFTX:
// // process(SDLK_LEFT, SDLK_RIGHT);
// break;

// default:
// break;
// }
const auto who = event.caxis.which;
const auto axis = static_cast<input::joystickaxisevent::axis>(event.caxis.axis);
const auto value = event.caxis.value;

joystickaxisevent event = {axis, value};

for (const auto &receiver : _receivers) {
receiver->on_joystickaxismotion(who, event);
}
} break;

case input::eventtype::mail: {
Expand Down
5 changes: 5 additions & 0 deletions src/eventreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ void eventreceiver::on_joystickbuttonup(int who, const joystickevent &event) noe
UNUSED(event);
}

void eventreceiver::on_joystickaxismotion(int who, const joystickaxisevent &event) noexcept {
UNUSED(who);
UNUSED(event);
}

void eventreceiver::on_mail(const mailevent &event) noexcept {
UNUSED(event);
}
2 changes: 2 additions & 0 deletions src/eventreceiver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class eventreceiver {
virtual void on_keyup(const keyevent &event) noexcept;
virtual void on_joystickbuttondown(int who, const joystickevent &event) noexcept;
virtual void on_joystickbuttonup(int who, const joystickevent &event) noexcept;
virtual void on_joystickaxismotion(int who, const joystickaxisevent &event) noexcept;

virtual void on_mail(const mailevent &event) noexcept;
};
}
19 changes: 18 additions & 1 deletion src/pixmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,29 @@ pixmap::pixmap(const std::shared_ptr<renderer> &renderer, std::unique_ptr<SDL_Su
}
}

void pixmap::draw(const geometry::rect &source, const geometry::rect &destination, const double_t angle, reflection reflection, const uint8_t alpha) const noexcept {
void pixmap::draw(
const geometry::rect &source,
const geometry::rect &destination,
const double_t angle,
reflection reflection,
const uint8_t alpha
#ifdef DEBUG
,
const geometry::rect &outline
#endif
) const noexcept {
const SDL_Rect &src = source;
const SDL_Rect &dst = destination;

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;

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

geometry::size pixmap::size() const noexcept {
Expand Down
4 changes: 4 additions & 0 deletions src/pixmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class pixmap {
double_t angle = 0.0f,
reflection reflection = reflection::none,
uint8_t alpha = 255
#ifdef DEBUG
,
const geometry::rect &outline = {}
#endif
) const noexcept;

geometry::size size() const noexcept;
Expand Down
5 changes: 5 additions & 0 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ void framework::scriptengine::run() {
e.set_action(value);
}

std::string get() const {
return e.get_action();
}

void unset() {
e.unset_action();
}
Expand All @@ -200,6 +204,7 @@ void framework::scriptengine::run() {
lua.new_usertype<actionproxy>(
"ActionProxy",
"set", &actionproxy::set,
"get", &actionproxy::get,
"unset", &actionproxy::unset
);

Expand Down
Loading

0 comments on commit 190a194

Please sign in to comment.