Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Dec 27, 2024
1 parent f1bba57 commit c8b182f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
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=default --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=OFF -DSANDBOX=ON -DENABLE_PROFILING=ON
1 change: 1 addition & 0 deletions src/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace input {
enum eventtype : Uint32 {
mail = SDL_USEREVENT + 1,
timer
};

enum class keyevent : int32_t {
Expand Down
10 changes: 9 additions & 1 deletion src/eventmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "eventmanager.hpp"
#include <SDL_keycode.h>
#include "event.hpp"

using namespace input;

Expand Down Expand Up @@ -120,6 +120,14 @@ void eventmanager::update(float_t delta) {
}
} break;

case input::eventtype::timer: {
auto *ptr = static_cast<std::function<void()> *>(event.user.data1);
if (ptr) {
(*ptr)();
delete ptr;
}
} break;

default:
break;
}
Expand Down
19 changes: 13 additions & 6 deletions src/timermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ using namespace framework;

uint32_t generic_wrapper(uint32_t interval, void *param, bool repeat) {
auto fn = static_cast<std::function<void()> *>(param);
(*fn)();
SDL_Event event{};
event.type = input::eventtype::timer;
event.user.data1 = fn;

SDL_PushEvent(&event);

return repeat ? interval : 0;
}

Expand All @@ -25,11 +30,11 @@ timermanager::~timermanager() noexcept {
}

void timermanager::set(int32_t interval, std::function<void()> fn) {
add_timer(interval, std::move(fn), true);
add_timer(interval, fn, true);
}

void timermanager::singleshot(int32_t interval, std::function<void()> fn) {
add_timer(interval, std::move(fn), false);
add_timer(interval, fn, false);
}

void timermanager::clear(int32_t id) noexcept {
Expand All @@ -40,13 +45,15 @@ void timermanager::clear(int32_t id) noexcept {
}

void timermanager::add_timer(int32_t interval, std::function<void()> fn, bool repeat) {
const auto ptr = std::make_shared<std::function<void()>>(fn);
const auto id = SDL_AddTimer(interval, repeat ? wrapper : singleshot_wrapper, ptr.get());
// const auto ptr = std::make_shared<std::function<void()>>(fn);
const auto ptr = new std::function<void()>(fn);
const auto id = SDL_AddTimer(interval, repeat ? wrapper : singleshot_wrapper, ptr);
if (!id) [[unlikely]] {
delete ptr;
std::ostringstream oss;
oss << "[SDL_AddTimer] failed to set timer. reason: " << SDL_GetError();
throw std::runtime_error(oss.str());
}

_timers.emplace(id, ptr);
// _timers.emplace(id, ptr);
}

0 comments on commit c8b182f

Please sign in to comment.