openmohaa/code/qcommon/con_timer.cpp

84 lines
1.6 KiB
C++
Raw Permalink Normal View History

2023-01-29 20:59:31 +01:00
#include "con_timer.h"
2023-08-19 03:02:30 +02:00
#if defined(ARCHIVE_SUPPORTED)
# include "../fgame/archive.h"
2023-01-29 20:59:31 +01:00
#endif
con_timer::con_timer(void)
{
2023-08-19 03:02:30 +02:00
m_inttime = 0;
m_bDirty = false;
2023-01-29 20:59:31 +01:00
}
2023-08-19 03:02:30 +02:00
void con_timer::AddElement(Class *e, int inttime)
2023-01-29 20:59:31 +01:00
{
2023-08-19 03:02:30 +02:00
Element element;
2023-01-29 20:59:31 +01:00
2023-08-19 03:02:30 +02:00
element.obj = e;
element.inttime = inttime;
2023-01-29 20:59:31 +01:00
2023-08-19 03:02:30 +02:00
m_Elements.AddObject(element);
2023-01-29 20:59:31 +01:00
2023-08-19 03:02:30 +02:00
if (inttime <= m_inttime) {
SetDirty();
}
2023-01-29 20:59:31 +01:00
}
2023-08-19 03:02:30 +02:00
void con_timer::RemoveElement(Class *e)
2023-01-29 20:59:31 +01:00
{
2023-08-19 03:02:30 +02:00
for (int i = m_Elements.NumObjects(); i > 0; i--) {
Element *index = &m_Elements.ObjectAt(i);
if (index->obj == e) {
m_Elements.RemoveObjectAt(i);
return;
}
}
2023-01-29 20:59:31 +01:00
}
2023-08-19 03:02:30 +02:00
Class *con_timer::GetNextElement(int& foundtime)
2023-01-29 20:59:31 +01:00
{
2023-08-19 03:02:30 +02:00
int best_inttime;
int i;
int foundIndex;
Class *result;
foundIndex = 0;
best_inttime = m_inttime;
for (i = m_Elements.NumObjects(); i > 0; i--) {
if (m_Elements.ObjectAt(i).inttime <= best_inttime) {
best_inttime = m_Elements.ObjectAt(i).inttime;
foundIndex = i;
}
}
if (foundIndex) {
result = m_Elements.ObjectAt(foundIndex).obj;
m_Elements.RemoveObjectAt(foundIndex);
foundtime = best_inttime;
} else {
result = NULL;
m_bDirty = false;
}
return result;
2023-01-29 20:59:31 +01:00
}
#if defined(ARCHIVE_SUPPORTED)
2023-08-19 03:02:30 +02:00
void con_timer::ArchiveElement(Archiver& arc, Element *e)
2023-01-29 20:59:31 +01:00
{
2023-08-19 03:02:30 +02:00
arc.ArchiveObjectPointer(&e->obj);
arc.ArchiveInteger(&e->inttime);
2023-01-29 20:59:31 +01:00
}
void con_timer::Archive(Archiver& arc)
{
2023-08-19 03:02:30 +02:00
arc.ArchiveBool(&m_bDirty);
arc.ArchiveInteger(&m_inttime);
2023-01-29 20:59:31 +01:00
2023-08-19 03:02:30 +02:00
m_Elements.Archive(arc, con_timer::ArchiveElement);
2023-01-29 20:59:31 +01:00
}
#endif