scummvm/engines/chewy/timer.cpp

146 lines
3.6 KiB
C++
Raw 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"
#include "common/textconsole.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;
2019-11-04 22:24:37 +01:00
set_all_status(TIMER_STOP);
}
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-12 19:23:13 -08:00
void Timer::calc_timer() {
2019-11-04 22:24:37 +01:00
int16 i;
int16 count;
int ak_time;
float freq;
2022-02-10 21:52:20 -08:00
ak_time = _G(timer_count);
2022-02-10 20:58:38 -08:00
for (i = 0; i < _timerMax; i++) {
if (_timerBlk[i].TimeStatus != TIMER_STOP) {
freq = timer_freq[_timerBlk[i].TimeMode];
2019-11-04 22:24:37 +01:00
2022-02-10 20:58:38 -08:00
_timerBlk[i].TimeLast = ((float)ak_time - (float)_timerBlk[i].TimeLast);
2019-11-04 22:24:37 +01:00
2022-02-10 20:58:38 -08:00
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-10 20:58:38 -08: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-10 20:58:38 -08: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-12 19:23:13 -08:00
int16 Timer::set_new_timer(int16 timer_nr, int16 timer_end_wert, int16 timer_mode) {
2019-11-04 22:24:37 +01:00
int16 ret;
2022-02-10 20:58:38 -08:00
if (timer_nr < _timerMax) {
2019-11-04 22:24:37 +01:00
ret = true;
2022-02-10 20:58:38 -08:00
_timerBlk[timer_nr].TimeCount = 0;
_timerBlk[timer_nr].TimeEnd = timer_end_wert;
_timerBlk[timer_nr].TimeFlag = 0;
2022-02-10 21:52:20 -08:00
_timerBlk[timer_nr].TimeLast = _G(timer_count);
2022-02-10 20:58:38 -08:00
_timerBlk[timer_nr].TimeMode = timer_mode;
_timerBlk[timer_nr].TimeStatus = true;
2019-11-04 22:24:37 +01:00
} else
ret = -1;
2021-10-25 20:29:21 -07:00
return ret;
2019-11-04 22:24:37 +01:00
}
2022-02-12 19:23:13 -08:00
void Timer::reset_timer(int16 timer_nr, int16 timer_wert) {
2022-02-10 20:58:38 -08:00
if (timer_nr < _timerMax) {
_timerBlk[timer_nr].TimeCount = 0;
_timerBlk[timer_nr].TimeFlag = 0;
2022-02-10 21:52:20 -08:00
_timerBlk[timer_nr].TimeLast = _G(timer_count);
2019-11-04 22:24:37 +01:00
if (timer_wert)
2022-02-10 20:58:38 -08:00
_timerBlk[timer_nr].TimeEnd = timer_wert;
2019-11-04 22:24:37 +01:00
}
}
2022-02-12 19:23:13 -08:00
void Timer::reset_all_timer() {
2019-11-04 22:24:37 +01:00
int16 i;
2022-02-10 20:58:38 -08:00
for (i = 0; i < _timerMax; i++) {
_timerBlk[i].TimeCount = 0;
_timerBlk[i].TimeFlag = 0;
2022-02-10 21:52:20 -08:00
_timerBlk[i].TimeLast = _G(timer_count);
2019-11-04 22:24:37 +01:00
}
}
2022-02-12 19:23:13 -08:00
void Timer::set_status(int16 timer_nr, int16 status) {
2022-02-10 20:58:38 -08:00
if (timer_nr < _timerMax) {
_timerBlk[timer_nr].TimeStatus = status;
2019-11-04 22:24:37 +01:00
}
}
2022-02-12 19:23:13 -08:00
void Timer::set_all_status(int16 status) {
2019-11-04 22:24:37 +01:00
int16 i;
if (status == TIMER_FREEZE) {
2022-02-10 20:58:38 -08:00
for (i = 0; i < _timerMax; i++) {
if (_timerBlk[i].TimeStatus != TIMER_STOP)
_timerBlk[i].TimeStatus = TIMER_FREEZE;
2019-11-04 22:24:37 +01:00
}
}
else if (status == TIMER_UNFREEZE) {
2022-02-10 20:58:38 -08:00
for (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-10 20:58:38 -08:00
for (i = 0; i < _timerMax; i++)
_timerBlk[i].TimeStatus = status;
2019-11-04 22:24:37 +01:00
}
}
2022-02-12 19:23:13 -08:00
void Timer::disable_timer() {
2022-02-10 21:52:20 -08:00
_G(timer_suspend) = true;
2019-11-04 22:24:37 +01:00
}
2022-02-12 19:23:13 -08:00
void Timer::enable_timer() {
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