Skip to content

Commit

Permalink
timer.tick, clamp functions
Browse files Browse the repository at this point in the history
* bool timer.tick(int64 units) is a convenience method that alleviates the need for calling timer.restart() all the time. It calls timer.has_elapsed() internally and if that returns true, restarts the timer before returning true itself.

* Fixes with timers and negative units, it can be useful to force a timer to a negative value sometimes and previously that was broken.

* Registers float clamp(float value, float min, float max) and integer equivalent.
  • Loading branch information
samtupy committed Nov 9, 2024
1 parent 8f76ea1 commit c961e34
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/reactphysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ template <class T, typename... A> void rp_construct(void* mem, A... args) { new
template <class T> void rp_copy_construct(void* mem, const T& obj) { new(mem) T(obj); }
template <class T> void rp_destruct(T* obj) { obj->~T(); }

// Some functions require manual wrapping, especially anything dealing with arrays.
// Some functions require manual wrapping, especially anything dealing with arrays or force inline.
CScriptArray* transform_get_opengl_matrix(const Transform& t) {
CScriptArray* array = CScriptArray::Create(get_array_type("array<float>"), 16);
t.getOpenGLMatrix(reinterpret_cast<float*>(array->GetBuffer()));
Expand All @@ -51,6 +51,8 @@ template <class T> void RegisterCollisionShape() {
}

void RegisterReactphysics(asIScriptEngine* engine) {
engine->RegisterGlobalFunction("int clamp(int value, int min, int max)", asFUNCTIONPR(clamp, (int, int, int), int), asCALL_CDECL);
engine->RegisterGlobalFunction("float clamp(float value, float min, float max)", asFUNCTIONPR(clamp, (decimal, decimal, decimal), decimal), asCALL_CDECL);
engine->RegisterEnum("physics_shape_type");
engine->RegisterEnumValue("physics_shape_type", "SHAPE_TYPE_SPHERE", int(CollisionShapeType::SPHERE));
engine->RegisterEnumValue("physics_shape_type", "SHAPE_TYPE_CAPSULE", int(CollisionShapeType::CAPSULE));
Expand Down
2 changes: 0 additions & 2 deletions src/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,6 @@ BOOL mixer::add_mixer(mixer* m) {
if (ret) {
m->parent_mixer = this;
mixers.insert(m);
AddRef();
}
return ret;
}
Expand All @@ -1526,7 +1525,6 @@ BOOL mixer::remove_mixer(mixer* m, BOOL internal) {
return FALSE;
mixers.erase(i);
BASS_Mixer_ChannelRemove(m->channel);
Release();
if (internal)
return TRUE;
m->parent_mixer = NULL;
Expand Down
16 changes: 11 additions & 5 deletions src/timestuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,22 +270,27 @@ asINT64 system_running_milliseconds() {
uint64_t timer_default_accuracy = Timespan::MILLISECONDS;
timer::timer() : value(microticks()), accuracy(timer_default_accuracy), paused(false), secure(speedhack_protection) {}
timer::timer(bool secure) : value(microticks(secure)), accuracy(timer_default_accuracy), paused(false), secure(secure) {}
timer::timer(int64_t initial_value, bool secure) : value(microticks(secure) + initial_value * timer_default_accuracy), accuracy(timer_default_accuracy), paused(false), secure(secure) {}
timer::timer(int64_t initial_value, uint64_t initial_accuracy, bool secure) : value(microticks(secure) + initial_value * initial_accuracy), accuracy(initial_accuracy), paused(false), secure(secure) {}
int64_t timer::get_elapsed() const { return (paused ? value : microticks(secure) - value) / accuracy; }
timer::timer(int64_t initial_value, bool secure) : value(int64_t(microticks(secure)) - initial_value * timer_default_accuracy), accuracy(timer_default_accuracy), paused(false), secure(secure) {}
timer::timer(int64_t initial_value, uint64_t initial_accuracy, bool secure) : value(int64_t(microticks(secure)) - initial_value * initial_accuracy), accuracy(initial_accuracy), paused(false), secure(secure) {}
int64_t timer::get_elapsed() const { return int64_t(paused ? value : microticks(secure) - value) / int64_t(accuracy); }
bool timer::has_elapsed(int64_t value) const { return get_elapsed() >= value; }
void timer::force(int64_t new_value) { paused ? value = new_value * accuracy : value = microticks(secure) - new_value * accuracy; }
void timer::adjust(int64_t new_value) { paused ? value += new_value * accuracy : value -= new_value * accuracy; }
void timer::restart() { value = microticks(secure); paused = false; }
void timer::restart() { value = int64_t(microticks(secure)); paused = false; }
bool timer::get_secure() const { return secure; }
bool timer::get_paused() const { return paused; }
bool timer::get_running() const { return !paused; }
bool timer::pause() { return paused ? false : set_paused(true); }
bool timer::resume() { return !paused ? false : set_paused(false); }
void timer::toggle_pause() { value = microticks(secure) - value; paused = !paused; }
bool timer::tick(int64_t value) {
if (!has_elapsed(value)) return false;
restart();
return true;
}
bool timer::set_paused(bool new_paused) {
if (paused == new_paused) return false;
value = microticks(secure) - value;
value = int64_t(microticks(secure)) - value;
paused = new_paused;
return true;
}
Expand Down Expand Up @@ -370,6 +375,7 @@ void RegisterScriptTimestuff(asIScriptEngine* engine) {
engine->RegisterObjectMethod(_O("timer"), _O("int64 get_elapsed() const property"), asMETHOD(timer, get_elapsed), asCALL_THISCALL);
engine->RegisterObjectMethod(_O("timer"), _O("void set_elapsed(int64 time_units) property"), asMETHOD(timer, force), asCALL_THISCALL);
engine->RegisterObjectMethod(_O("timer"), _O("bool has_elapsed(int64 time_units) const"), asMETHOD(timer, has_elapsed), asCALL_THISCALL);
engine->RegisterObjectMethod(_O("timer"), _O("bool tick(int64 time_units)"), asMETHOD(timer, tick), asCALL_THISCALL);
engine->RegisterObjectMethod(_O("timer"), _O("void force(int64 elapsed)"), asMETHOD(timer, force), asCALL_THISCALL);
engine->RegisterObjectMethod(_O("timer"), _O("void adjust(int64 mod_elapsed)"), asMETHOD(timer, adjust), asCALL_THISCALL);
engine->RegisterObjectMethod(_O("timer"), _O("void restart()"), asMETHOD(timer, restart), asCALL_THISCALL);
Expand Down
1 change: 1 addition & 0 deletions src/timestuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class timer : public Poco::RefCountedObject {
timer(int64_t initial_value, uint64_t initial_accuracy, bool secure);
int64_t get_elapsed() const;
bool has_elapsed(int64_t value) const;
bool tick(int64_t value);
void force(int64_t value);
void adjust(int64_t value);
void restart();
Expand Down
2 changes: 1 addition & 1 deletion test/quick/timer.nvgt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

void main() {
timer t_millis, t_dec;
timer t_secs(0, SECONDS);
timer t_secs(-5, SECONDS);
timer t_micros(0, 1);
show_window("test");
t_dec.accuracy = SECONDS / 10;
Expand Down

0 comments on commit c961e34

Please sign in to comment.