scummvm/engines/chewy/timer.cpp

139 lines
3.5 KiB
C++
Raw Permalink Normal View History

2019-11-04 22:24:37 +01:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
2021-12-26 15:54:17 -08:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
2019-11-04 22:24:37 +01:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-12-26 15:54:17 -08:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2019-11-04 22:24:37 +01:00
*
*/
#include "common/system.h"
2021-09-11 20:13:53 -07:00
#include "chewy/chewy.h"
#include "chewy/defines.h"
#include "chewy/globals.h"
2022-02-10 21:52:20 -08:00
#include "chewy/timer.h"
2019-11-04 22:24:37 +01:00
2021-09-12 15:41:09 -07:00
namespace Chewy {
2022-02-12 19:23:13 -08:00
Timer::Timer(int16 max_t, TimerBlk *t) {
2022-02-10 20:58:38 -08:00
_timerBlk = t;
_timerMax = max_t;
2022-02-10 21:52:20 -08:00
_G(timer_int) = false;
_G(timer_suspend) = false;
2022-02-27 23:22:24 +01:00
setAllStatus(TIMER_STOP);
2019-11-04 22:24:37 +01:00
}
2022-02-12 19:23:13 -08:00
Timer::~Timer() {
2019-11-04 22:24:37 +01:00
}
float timer_freq[6] = {
0.0182f,
0.182f,
1.82f,
18.2f,
1092.0f,
65520.0f
};
2022-02-27 23:22:24 +01:00
void Timer::calcTimer() {
int ak_time = _G(timer_count);
for (int16 i = 0; i < _timerMax; i++) {
if (_timerBlk[i]._timeStatus != TIMER_STOP) {
float freq = timer_freq[_timerBlk[i]._timeMode];
2019-11-04 22:24:37 +01:00
2022-02-27 23:22:24 +01:00
_timerBlk[i]._timeLast = ((float)ak_time - (float)_timerBlk[i]._timeLast);
2019-11-04 22:24:37 +01:00
2022-02-27 23:22:24 +01:00
int16 count = (int16)((float)_timerBlk[i]._timeLast / (float)freq);
if (_timerBlk[i]._timeStatus == TIMER_START)
_timerBlk[i]._timeCount += count;
2019-11-04 22:24:37 +01:00
2022-02-27 23:22:24 +01:00
_timerBlk[i]._timeLast = (float)ak_time - (float)(_timerBlk[i]._timeLast -
2019-11-04 22:24:37 +01:00
(float)((float)count * (float)freq));
2022-02-27 23:22:24 +01:00
if (_timerBlk[i]._timeCount >= _timerBlk[i]._timeEnd) {
++_timerBlk[i]._timeFlag;
_timerBlk[i]._timeCount = 0;
2019-11-04 22:24:37 +01:00
}
}
}
}
2022-02-27 23:22:24 +01:00
int16 Timer::setNewTimer(int16 timerNr, int16 timerEndValue, int16 timerMode) {
2022-03-01 00:17:25 +01:00
int16 ret = -1;
2022-02-27 23:22:24 +01:00
if (timerNr < _timerMax) {
ret = 1;
_timerBlk[timerNr]._timeCount = 0;
_timerBlk[timerNr]._timeEnd = timerEndValue;
_timerBlk[timerNr]._timeFlag = 0;
_timerBlk[timerNr]._timeLast = _G(timer_count);
_timerBlk[timerNr]._timeMode = timerMode;
_timerBlk[timerNr]._timeStatus = true;
2022-03-01 00:17:25 +01:00
}
2021-10-25 20:29:21 -07:00
return ret;
2019-11-04 22:24:37 +01:00
}
2022-02-27 23:22:24 +01:00
void Timer::resetTimer(int16 timerNr, int16 timerValue) {
if (timerNr < _timerMax) {
_timerBlk[timerNr]._timeCount = 0;
_timerBlk[timerNr]._timeFlag = 0;
_timerBlk[timerNr]._timeLast = _G(timer_count);
if (timerValue)
_timerBlk[timerNr]._timeEnd = timerValue;
2019-11-04 22:24:37 +01:00
}
}
2022-02-27 23:22:24 +01:00
void Timer::resetAllTimer() {
for (int16 i = 0; i < _timerMax; i++) {
_timerBlk[i]._timeCount = 0;
_timerBlk[i]._timeFlag = 0;
_timerBlk[i]._timeLast = _G(timer_count);
2019-11-04 22:24:37 +01:00
}
}
2022-02-27 23:22:24 +01:00
void Timer::setStatus(int16 timerNr, int16 status) {
if (timerNr < _timerMax) {
_timerBlk[timerNr]._timeStatus = status;
2019-11-04 22:24:37 +01:00
}
}
2022-02-27 23:22:24 +01:00
void Timer::setAllStatus(int16 status) {
2019-11-04 22:24:37 +01:00
if (status == TIMER_FREEZE) {
2022-02-27 23:22:24 +01:00
for (int16 i = 0; i < _timerMax; i++) {
if (_timerBlk[i]._timeStatus != TIMER_STOP)
_timerBlk[i]._timeStatus = TIMER_FREEZE;
2019-11-04 22:24:37 +01:00
}
2022-02-27 23:22:24 +01:00
} else if (status == TIMER_UNFREEZE) {
for (int16 i = 0; i < _timerMax; i++) {
if (_timerBlk[i]._timeStatus != TIMER_STOP)
_timerBlk[i]._timeStatus = TIMER_START;
2019-11-04 22:24:37 +01:00
}
2021-10-19 20:40:45 -07:00
} else {
2022-02-27 23:22:24 +01:00
for (int16 i = 0; i < _timerMax; i++)
_timerBlk[i]._timeStatus = status;
2019-11-04 22:24:37 +01:00
}
}
2022-02-27 23:22:24 +01:00
void Timer::disableTimer() {
2022-02-10 21:52:20 -08:00
_G(timer_suspend) = true;
2019-11-04 22:24:37 +01:00
}
2022-02-27 23:22:24 +01:00
void Timer::enableTimer() {
2022-02-10 21:52:20 -08:00
_G(timer_suspend) = false;
2019-11-04 22:24:37 +01:00
}
2021-09-12 15:41:09 -07:00
} // namespace Chewy