2020-11-29 16:50:49 +01:00
|
|
|
#include "pch.h"
|
|
|
|
#include "timer.h"
|
|
|
|
|
2020-12-04 16:35:47 +01:00
|
|
|
#include "pb.h"
|
2020-11-29 16:50:49 +01:00
|
|
|
|
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;
|
2020-11-29 16:50:49 +01:00
|
|
|
|
|
|
|
int timer::init(int count)
|
|
|
|
{
|
2021-10-02 16:45:31 +02:00
|
|
|
auto buf = new timer_struct[count];
|
2020-12-06 14:21:56 +01:00
|
|
|
TimerBuffer = buf;
|
2020-11-29 16:50:49 +01:00
|
|
|
if (!buf)
|
|
|
|
return 1;
|
2020-12-06 14:21:56 +01:00
|
|
|
Count = 0;
|
|
|
|
MaxCount = count;
|
|
|
|
SetCount = 1;
|
2020-12-04 16:35:47 +01:00
|
|
|
|
|
|
|
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;
|
2020-12-04 16:35:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void timer::uninit()
|
|
|
|
{
|
2021-10-02 16:45:31 +02:00
|
|
|
delete[] TimerBuffer;
|
2020-12-06 14:21:56 +01:00
|
|
|
TimerBuffer = nullptr;
|
2020-12-04 16:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int timer::kill(int timerId)
|
|
|
|
{
|
2020-12-06 14:21:56 +01:00
|
|
|
timer_struct* current = ActiveList;
|
2020-12-04 16:35:47 +01:00
|
|
|
int index = 0;
|
2020-12-06 14:21:56 +01:00
|
|
|
timer_struct* prev = nullptr;
|
|
|
|
if (Count <= 0)
|
2020-12-04 16:35:47 +01:00
|
|
|
return 0;
|
2020-12-06 14:21:56 +01:00
|
|
|
while (timerId != current->TimerId)
|
2020-11-29 16:50:49 +01:00
|
|
|
{
|
2020-12-04 16:35:47 +01:00
|
|
|
++index;
|
2020-12-06 14:21:56 +01:00
|
|
|
prev = current;
|
|
|
|
current = current->NextTimer;
|
|
|
|
if (index >= Count)
|
2020-12-04 16:35:47 +01:00
|
|
|
return 0;
|
2020-11-29 16:50:49 +01:00
|
|
|
}
|
2020-12-06 14:21:56 +01:00
|
|
|
if (prev)
|
|
|
|
prev->NextTimer = current->NextTimer;
|
2020-12-04 16:35:47 +01:00
|
|
|
else
|
2020-12-06 14:21:56 +01:00
|
|
|
ActiveList = current->NextTimer;
|
2020-12-04 16:35:47 +01:00
|
|
|
|
2020-12-06 14:21:56 +01:00
|
|
|
--Count;
|
|
|
|
current->NextTimer = FreeList;
|
|
|
|
FreeList = current;
|
2020-12-04 16:35:47 +01:00
|
|
|
return timerId;
|
2020-11-29 16:50:49 +01:00
|
|
|
}
|
2020-12-03 15:47:36 +01:00
|
|
|
|
2020-12-04 16:35:47 +01:00
|
|
|
int timer::set(float time, void* caller, void (* callback)(int, void*))
|
2020-12-03 15:47:36 +01:00
|
|
|
{
|
2020-12-06 14:21:56 +01:00
|
|
|
if (Count >= MaxCount)
|
2020-12-04 16:35:47 +01:00
|
|
|
return 0;
|
|
|
|
|
2020-12-06 14:21:56 +01:00
|
|
|
auto timer = FreeList;
|
|
|
|
FreeList = timer->NextTimer;
|
|
|
|
timer->NextTimer = nullptr;
|
2020-12-04 16:35:47 +01:00
|
|
|
|
2020-12-06 14:21:56 +01:00
|
|
|
auto prev = ActiveList;
|
|
|
|
auto current = ActiveList;
|
2020-12-04 16:35:47 +01:00
|
|
|
|
|
|
|
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)
|
2020-12-04 16:35:47 +01:00
|
|
|
{
|
|
|
|
prev = current;
|
|
|
|
current = current->NextTimer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current != prev)
|
|
|
|
{
|
2020-12-06 14:21:56 +01:00
|
|
|
timer->NextTimer = prev->NextTimer;
|
|
|
|
prev->NextTimer = timer;
|
2020-12-04 16:35:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-06 14:21:56 +01:00
|
|
|
timer->NextTimer = ActiveList;
|
|
|
|
ActiveList = timer;
|
2020-12-04 16:35:47 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 14:21:56 +01:00
|
|
|
timer->Caller = caller;
|
|
|
|
timer->Callback = callback;
|
|
|
|
timer->TimerId = SetCount;
|
|
|
|
timer->TargetTime = targetTime;
|
|
|
|
Count++;
|
2020-12-04 16:35:47 +01:00
|
|
|
|
2020-12-06 14:21:56 +01:00
|
|
|
SetCount++;
|
|
|
|
if (SetCount <= 0)
|
|
|
|
SetCount = 1;
|
|
|
|
return timer->TimerId;
|
2020-12-03 15:47:36 +01:00
|
|
|
}
|
|
|
|
|
2020-12-04 16:35:47 +01:00
|
|
|
int timer::check()
|
2020-12-03 15:47:36 +01:00
|
|
|
{
|
2020-12-06 14:21:56 +01:00
|
|
|
timer_struct* current = ActiveList;
|
2020-12-04 16:35:47 +01:00
|
|
|
int index = 0;
|
2020-12-06 14:21:56 +01:00
|
|
|
if (ActiveList)
|
2020-12-04 16:35:47 +01:00
|
|
|
{
|
|
|
|
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;
|
2020-12-04 16:35:47 +01:00
|
|
|
++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;
|
2020-12-04 16:35:47 +01:00
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
2020-12-03 15:47:36 +01:00
|
|
|
}
|