1
0
Fork 0
mirror of https://github.com/k4zmu2a/SpaceCadetPinball.git synced 2024-11-17 23:20:19 +01:00
SpaceCadetPinball/SpaceCadetPinball/timer.cpp

144 lines
2.7 KiB
C++
Raw Normal View History

#include "pch.h"
#include "timer.h"
#include "pb.h"
2020-12-06 14:21:56 +01:00
int timer::SetCount;
timer_struct* timer::ActiveList;
int timer::MaxCount;
int timer::Count;
timer_struct* timer::FreeList;
timer_struct* timer::TimerBuffer;
int timer::init(int count)
{
auto buf = new timer_struct[count];
2020-12-06 14:21:56 +01:00
TimerBuffer = buf;
if (!buf)
return 1;
2020-12-06 14:21:56 +01:00
Count = 0;
MaxCount = count;
SetCount = 1;
for (int index = 0; index < count - 1; index++)
buf[index].NextTimer = &buf[index + 1];
buf[count - 1].NextTimer = nullptr;
2020-12-06 14:21:56 +01:00
ActiveList = nullptr;
FreeList = buf;
return 0;
}
void timer::uninit()
{
delete[] TimerBuffer;
2020-12-06 14:21:56 +01:00
TimerBuffer = nullptr;
}
int timer::kill(int timerId)
{
2020-12-06 14:21:56 +01:00
timer_struct* current = ActiveList;
int index = 0;
2020-12-06 14:21:56 +01:00
timer_struct* prev = nullptr;
if (Count <= 0)
return 0;
2020-12-06 14:21:56 +01:00
while (timerId != current->TimerId)
{
++index;
2020-12-06 14:21:56 +01:00
prev = current;
current = current->NextTimer;
if (index >= Count)
return 0;
}
2020-12-06 14:21:56 +01:00
if (prev)
prev->NextTimer = current->NextTimer;
else
2020-12-06 14:21:56 +01:00
ActiveList = current->NextTimer;
2020-12-06 14:21:56 +01:00
--Count;
current->NextTimer = FreeList;
FreeList = current;
return timerId;
}
int timer::set(float time, void* caller, void (* callback)(int, void*))
{
2020-12-06 14:21:56 +01:00
if (Count >= MaxCount)
return 0;
2020-12-06 14:21:56 +01:00
auto timer = FreeList;
FreeList = timer->NextTimer;
timer->NextTimer = nullptr;
2020-12-06 14:21:56 +01:00
auto prev = ActiveList;
auto current = ActiveList;
auto targetTime = pb::time_ticks + static_cast<int>(time * 1000.0f);
2020-12-06 14:21:56 +01:00
for (int index = 0; index < Count && targetTime >= current->TargetTime; ++index)
{
prev = current;
current = current->NextTimer;
}
if (current != prev)
{
2020-12-06 14:21:56 +01:00
timer->NextTimer = prev->NextTimer;
prev->NextTimer = timer;
}
else
{
2020-12-06 14:21:56 +01:00
timer->NextTimer = ActiveList;
ActiveList = timer;
}
2020-12-06 14:21:56 +01:00
timer->Caller = caller;
timer->Callback = callback;
timer->TimerId = SetCount;
timer->TargetTime = targetTime;
Count++;
2020-12-06 14:21:56 +01:00
SetCount++;
if (SetCount <= 0)
SetCount = 1;
return timer->TimerId;
}
int timer::check()
{
2020-12-06 14:21:56 +01:00
timer_struct* current = ActiveList;
int index = 0;
2020-12-06 14:21:56 +01:00
if (ActiveList)
{
while (pb::time_ticks >= current->TargetTime)
{
2020-12-06 14:21:56 +01:00
--Count;
// Advance active list, move current to free
ActiveList = current->NextTimer;
current->NextTimer = FreeList;
FreeList = current;
if (current->Callback != nullptr)
current->Callback(current->TimerId, current->Caller);
current = ActiveList;
++index;
if (index > 1)
break;
if (!current)
return index;
}
while (current && pb::time_ticks >= current->TargetTime + 100)
{
2020-12-06 14:21:56 +01:00
--Count;
ActiveList = current->NextTimer;
current->NextTimer = FreeList;
FreeList = current;
if (current->Callback != nullptr)
current->Callback(current->TimerId, current->Caller);
current = ActiveList;
++index;
}
}
return index;
}