Debian Codegraph @

src/gengine/TimerAgent.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "TimerAgent.h"
10
11 #include "OptionAgent.h"
12
13 //-----------------------------------------------------------------
14 void
15 TimerAgent::own_init()
16 {
17 m_timeinterval = OptionAgent::agent()->getAsInt("timeinterval", 100);
18 m_lastTime = SDL_GetTicks();
19 m_nextTime = m_lastTime;
20 m_deltaTime = 1;
21 m_count = 0;
22 }
23 //-----------------------------------------------------------------
24 /**
25 * Game is faster with pressed Shift.
26 */
27 int
28 TimerAgent::getTimeInterval()
29 {
30 int result = m_timeinterval;
31
32 if (SDL_GetModState() & KMOD_SHIFT) {
33 result = m_timeinterval / 4;
34 }
35 return result;
36 }
37 //-----------------------------------------------------------------
38 /**
39 * Sleep fixed number miliseconds.
40 */
41 void
42 TimerAgent::own_update()
43 {
44 m_count++;
45
46 Uint32 now = SDL_GetTicks();
47 if (now < m_nextTime) {
48 SDL_Delay(m_nextTime - now);
49 }
50
51 now = SDL_GetTicks();
52 //NOTE: every cycle have fixed time interval
53 m_nextTime = now + getTimeInterval();
54
55 m_deltaTime = now - m_lastTime;
56 m_lastTime = now;
57 }