Debian Codegraph @

netgames/dmfc/dmfctimer.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/Dmfc/dmfctimer.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003/08/26 03:57:21 $
23 * $Author: kevinb $
24 *
25 * DMFC timer API functions
26 *
27 * $Log: dmfctimer.cpp,v $
28 * Revision 1.1.1.1 2003/08/26 03:57:21 kevinb
29 * initial 1.5 import
30 *
31 *
32 * 8 5/23/99 2:21a Jeff
33 * fixed timer bugs with intervals
34 *
35 * 7 5/13/99 4:56p Ardussi
36 * changes for compiling on the Mac
37 *
38 * 6 3/17/99 12:25p Jeff
39 * converted DMFC to be COM interface
40 *
41 * 5 2/11/99 12:51a Jeff
42 * changed names of exported variables
43 *
44 * 4 9/21/98 7:11p Jeff
45 * made InputCommand interface API and moved existing input commands to
46 * the interface. Changed mprintf/ASSERT so they are valid in DMFC
47 *
48 * $NoKeywords: $
49 */
50
51 #include "gamedll_header.h"
52 #include "DMFC.h"
53 #include "dmfcinternal.h"
54
55 // DMFCBase::InitTimers
56 //
57 // Inializes the timers
58 void DMFCBase::InitTimers(void) {
59 for (int i = 0; i < MAX_DMFC_TIMERS; i++) {
60 Timers[i].inuse = false;
61 }
62 }
63
64 // DMFCBase::KillAllTimers
65 //
66 // Kills all running timers
67 void DMFCBase::KillAllTimers(void) {
68 for (int i = 0; i < MAX_DMFC_TIMERS; i++) {
69 KillTimer(i);
70 }
71 }
72
73 // DMFCBase::SetTimerInterval
74 //
75 // Starts a timer event running
76 int DMFCBase::SetTimerInterval(void (*func)(void), float intval, float longevity, void (*onkill)()) {
77 int i;
78
79 // Find a free timer
80 for (i = 0; i < MAX_DMFC_TIMERS; i++) {
81 if (!Timers[i].inuse)
82 break;
83 }
84
85 if (i >= MAX_DMFC_TIMERS) {
86 return -1; // no free timers
87 }
88
89 // insert this timer information
90 Timers[i].inuse = true;
91 Timers[i].handler = func;
92 Timers[i].starttime = *Gametime;
93 Timers[i].longevity = longevity;
94 Timers[i].intval = intval;
95 Timers[i].nexttime = Timers[i].starttime + Timers[i].intval;
96 Timers[i].onkill = onkill;
97 return i;
98 }
99
100 // DMFCBase::KillTimer
101 //
102 // Kills a timer
103 void DMFCBase::KillTimer(int handle) {
104 if ((handle >= 0) && (handle < MAX_DMFC_TIMERS)) {
105 if (Timers[handle].inuse) {
106 Timers[handle].inuse = false;
107 if (Timers[handle].onkill)
108 (*Timers[handle].onkill)();
109 }
110 }
111 }
112
113 // DMFCBase::ProcessTimers
114 //
115 // Goes through all the timers and updates them if they need to be updated
116 void DMFCBase::ProcessTimers(void) {
117 for (int i = 0; i < MAX_DMFC_TIMERS; i++) {
118 if (Timers[i].inuse) {
119 // we have a running timer here
120 float currtime = *Gametime;
121 if (Timers[i].nexttime <= currtime) {
122 // time to call
123 Timers[i].nexttime += Timers[i].intval;
124 if (Timers[i].handler)
125 (*Timers[i].handler)();
126 }
127 if (Timers[i].longevity != -1) {
128 if (Timers[i].longevity + Timers[i].starttime < currtime) {
129 // this timer is over with
130 KillTimer(i);
131 }
132 }
133 }
134 } // end for
135 }