1 /* ---------------------------------------------------------------------- *
2  * timer.c
3  * This file is part of lincity.
4  * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
5  * ---------------------------------------------------------------------- */
6 #if defined (HAVE_CONFIG_H)
7 #include "config.h"
8 #elif defined (WIN32)
9 #include "confw32.h"
10 #endif
11 
12 #if defined (TIME_WITH_SYS_TIME)
13 #include <time.h>
14 #include <sys/time.h>
15 #else
16 #if defined (HAVE_SYS_TIME_H)
17 #include <sys/time.h>
18 #else
19 #include <time.h>
20 #endif
21 #endif
22 #if defined(AIX) || defined(__EMX__)
23 #include <sys/select.h>
24 #endif
25 
26 #include "cliglobs.h"
27 #include "mouse.h"
28 #include "geometry.h"
29 #include "lchelp.h"
30 #include "timer.h"
31 
32 
33 /* ---------------------------------------------------------------------- *
34  * Private Global Variables
35  * ---------------------------------------------------------------------- */
36 #if defined (WIN32)
37 int usleep_counter = 0;
38 #else
39 struct timeval lc_timeval;
40 int real_start_time;
41 #endif
42 
43 Mouse_Handle * pause_handle;
44 Mouse_Handle * slow_handle;
45 Mouse_Handle * medium_handle;
46 Mouse_Handle * fast_handle;
47 
48 /* ---------------------------------------------------------------------- *
49  * Public Global Variables
50  * ---------------------------------------------------------------------- */
51 long real_time = 0;    /* In milliseconds */
52 
53 
54 /* ---------------------------------------------------------------------- *
55  * Private Function Declarations
56  * ---------------------------------------------------------------------- */
57 
58 void pause_handler(int x, int y, int button);
59 void slow_handler(int x, int y, int button);
60 void medium_handler(int x, int y, int button);
61 void fast_handler(int x, int y, int button);
62 
63 /* ---------------------------------------------------------------------- *
64  * Function Definitions
65  * ---------------------------------------------------------------------- */
66 void
lc_usleep(unsigned long t)67 lc_usleep (unsigned long t)
68 {
69 #if defined (WIN32)
70   /* This function will usually sleep too long.  For example, if t == 0
71      (e.g. 1 usec), thread will sleep for the remainder of its timeslice,
72      which might be 20 ms.
73    */
74   SleepEx (t / 1000, FALSE);
75 #else
76   struct timeval timeout;
77   timeout.tv_sec = t / 1000000;
78   timeout.tv_usec = t - 1000000 * timeout.tv_sec;
79   select (1, NULL, NULL, NULL, &timeout);
80 #endif
81 }
82 
83 void
reset_start_time(void)84 reset_start_time (void)
85 {
86 #if !defined (WIN32)
87   if (gettimeofday (&lc_timeval, 0) != 0)
88     do_error ("Can't get timeofday");
89   real_start_time = lc_timeval.tv_sec;
90 #endif
91 }
92 
93 
94 void
get_real_time(void)95 get_real_time (void)
96 {
97 #if defined (WIN32)
98   const int CLOCKS_PER_MILLISECOND = CLOCKS_PER_SEC / 1000;
99   real_time = (long) (clock () / CLOCKS_PER_MILLISECOND);
100 #else
101   gettimeofday (&lc_timeval, 0);
102   real_time = (lc_timeval.tv_sec - real_start_time) * 1000
103     + (lc_timeval.tv_usec / 1000);
104 #endif
105 }
106 
107 /* Game speed functions */
108 
109 void
init_timer_buttons(void)110 init_timer_buttons (void)
111 {
112     pause_handle = mouse_register(&scr.pause_button, &pause_handler);
113     slow_handle = mouse_register(&scr.slow_button, &slow_handler);
114     medium_handle = mouse_register(&scr.med_button, &medium_handler);
115     fast_handle = mouse_register(&scr.fast_button, &fast_handler);
116 }
117 
118 /* Mouse handlers */
119 
120 void
pause_handler(int x,int y,int button)121 pause_handler(int x, int y, int button)
122 {
123     if (button == LC_MOUSE_RIGHTBUTTON) {
124 	activate_help ("pause.hlp");
125     } else {
126 	select_pause ();
127     }
128 }
129 
130 void
slow_handler(int x,int y,int button)131 slow_handler(int x, int y, int button)
132 {
133     if (button == LC_MOUSE_RIGHTBUTTON) {
134 	activate_help ("slow.hlp");
135     } else {
136 	select_slow ();
137     }
138 }
139 
140 void
medium_handler(int x,int y,int button)141 medium_handler(int x, int y, int button)
142 {
143     if (button == LC_MOUSE_RIGHTBUTTON) {
144 	activate_help ("medium.hlp");
145     } else {
146 	select_medium ();
147     }
148 }
149 
150 void
fast_handler(int x,int y,int button)151 fast_handler(int x, int y, int button)
152 {
153     if (button == LC_MOUSE_RIGHTBUTTON) {
154 	activate_help ("fast.hlp");
155     } else {
156 	select_fast ();
157     }
158 }
159 
160 void
select_fast(void)161 select_fast (void)
162 {
163     hide_mouse ();
164     pause_flag = 0;
165     draw_pause (0);
166     slow_flag = 0;
167     draw_slow (0);
168     med_flag = 0;
169     draw_med (0);
170     fast_flag = 1;
171     draw_fast (1);
172     redraw_mouse ();
173 }
174 
175 void
select_medium(void)176 select_medium (void)
177 {
178     hide_mouse ();
179     pause_flag = 0;
180     draw_pause (0);
181     slow_flag = 0;
182     draw_slow (0);
183     med_flag = 1;
184     draw_med (1);
185     fast_flag = 0;
186     draw_fast (0);
187     redraw_mouse ();
188 }
189 
190 void
select_slow(void)191 select_slow (void)
192 {
193     hide_mouse ();
194     pause_flag = 0;
195     draw_pause (0);
196     slow_flag = 1;
197     draw_slow (1);
198     med_flag = 0;
199     draw_med (0);
200     fast_flag = 0;
201     draw_fast (0);
202     redraw_mouse ();
203 }
204 
205 void
select_pause(void)206 select_pause (void)
207 {
208     if (pause_flag) {
209 	/* unpause it */
210 	if (fast_flag)
211 	    select_fast ();
212 	else if (med_flag)
213 	    select_medium ();
214 	else if (slow_flag)
215 	    select_slow ();
216 	else
217 	    select_medium ();
218     } else {
219 	/* pause it */
220 	hide_mouse ();
221 	pause_flag = 1;
222 	draw_pause (1);
223 	draw_slow (0);
224 	draw_med (0);
225 	draw_fast (0);
226 	redraw_mouse ();
227     }
228 }
229