Main Page | Data Structures | Directories | File List | Data Fields | Globals

timer.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------- *
00002  * timer.c
00003  * This file is part of lincity.
00004  * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
00005  * ---------------------------------------------------------------------- */
00006 #if defined (HAVE_CONFIG_H)
00007 #include "config.h"
00008 #elif defined (WIN32)
00009 #include "confw32.h"
00010 #endif
00011 
00012 #if defined (TIME_WITH_SYS_TIME)
00013 #include <time.h>
00014 #include <sys/time.h>
00015 #else
00016 #if defined (HAVE_SYS_TIME_H)
00017 #include <sys/time.h>
00018 #else
00019 #include <time.h>
00020 #endif
00021 #endif
00022 #if defined(AIX) || defined(__EMX__)
00023 #include <sys/select.h>
00024 #endif
00025 
00026 #include "cliglobs.h"
00027 #include "mouse.h"
00028 #include "geometry.h"
00029 #include "lchelp.h"
00030 #include "timer.h"
00031 
00032 
00033 /* ---------------------------------------------------------------------- *
00034  * Private Global Variables
00035  * ---------------------------------------------------------------------- */
00036 #if defined (WIN32)
00037 int usleep_counter = 0;
00038 #else
00039 struct timeval lc_timeval;
00040 int real_start_time;
00041 #endif
00042 
00043 Mouse_Handle * pause_handle;
00044 Mouse_Handle * slow_handle;
00045 Mouse_Handle * medium_handle;
00046 Mouse_Handle * fast_handle;
00047 
00048 /* ---------------------------------------------------------------------- *
00049  * Public Global Variables
00050  * ---------------------------------------------------------------------- */
00051 long real_time = 0;    /* In milliseconds */
00052 
00053 
00054 /* ---------------------------------------------------------------------- *
00055  * Private Function Declarations
00056  * ---------------------------------------------------------------------- */
00057 
00058 void pause_handler(int x, int y, int button);
00059 void slow_handler(int x, int y, int button);
00060 void medium_handler(int x, int y, int button);
00061 void fast_handler(int x, int y, int button);
00062 
00063 /* ---------------------------------------------------------------------- *
00064  * Function Definitions
00065  * ---------------------------------------------------------------------- */
00066 void
00067 lc_usleep (unsigned long t)
00068 {
00069 #if defined (WIN32)
00070   /* This function will usually sleep too long.  For example, if t == 0 
00071      (e.g. 1 usec), thread will sleep for the remainder of its timeslice, 
00072      which might be 20 ms.
00073    */
00074   SleepEx (t / 1000, FALSE);
00075 #else
00076   struct timeval timeout;
00077   timeout.tv_sec = t / 1000000;
00078   timeout.tv_usec = t - 1000000 * timeout.tv_sec;
00079   select (1, NULL, NULL, NULL, &timeout);
00080 #endif
00081 }
00082 
00083 void 
00084 reset_start_time (void)
00085 {
00086 #if !defined (WIN32)
00087   if (gettimeofday (&lc_timeval, 0) != 0)
00088     do_error ("Can't get timeofday");
00089   real_start_time = lc_timeval.tv_sec;
00090 #endif
00091 }
00092 
00093 
00094 void
00095 get_real_time (void)
00096 {
00097 #if defined (WIN32)
00098   const int CLOCKS_PER_MILLISECOND = CLOCKS_PER_SEC / 1000;
00099   real_time = (long) (clock () / CLOCKS_PER_MILLISECOND);
00100 #else
00101   gettimeofday (&lc_timeval, 0);
00102   real_time = (lc_timeval.tv_sec - real_start_time) * 1000
00103     + (lc_timeval.tv_usec / 1000);
00104 #endif
00105 }
00106 
00107 /* Game speed functions */
00108 
00109 void
00110 init_timer_buttons (void)
00111 {
00112     pause_handle = mouse_register(&scr.pause_button, &pause_handler);
00113     slow_handle = mouse_register(&scr.slow_button, &slow_handler);
00114     medium_handle = mouse_register(&scr.med_button, &medium_handler);
00115     fast_handle = mouse_register(&scr.fast_button, &fast_handler);
00116 }
00117 
00118 /* Mouse handlers */
00119 
00120 void
00121 pause_handler(int x, int y, int button)
00122 {
00123     if (button == LC_MOUSE_RIGHTBUTTON) {
00124         activate_help ("pause.hlp");
00125     } else {
00126         select_pause ();
00127     }
00128 }
00129 
00130 void
00131 slow_handler(int x, int y, int button)
00132 {
00133     if (button == LC_MOUSE_RIGHTBUTTON) {
00134         activate_help ("slow.hlp");
00135     } else {
00136         select_slow ();
00137     }
00138 }
00139 
00140 void
00141 medium_handler(int x, int y, int button)
00142 {
00143     if (button == LC_MOUSE_RIGHTBUTTON) {
00144         activate_help ("medium.hlp");
00145     } else {
00146         select_medium ();
00147     }
00148 }
00149 
00150 void
00151 fast_handler(int x, int y, int button)
00152 {
00153     if (button == LC_MOUSE_RIGHTBUTTON) {
00154         activate_help ("fast.hlp");
00155     } else {
00156         select_fast ();
00157     }
00158 }
00159 
00160 void
00161 select_fast (void)
00162 {
00163     hide_mouse ();
00164     pause_flag = 0;
00165     draw_pause (0);
00166     slow_flag = 0;
00167     draw_slow (0);
00168     med_flag = 0;
00169     draw_med (0);
00170     fast_flag = 1;
00171     draw_fast (1);
00172     redraw_mouse ();
00173 }
00174 
00175 void
00176 select_medium (void)
00177 {
00178     hide_mouse ();
00179     pause_flag = 0;
00180     draw_pause (0);
00181     slow_flag = 0;
00182     draw_slow (0);
00183     med_flag = 1;
00184     draw_med (1);
00185     fast_flag = 0;
00186     draw_fast (0);
00187     redraw_mouse ();
00188 }
00189 
00190 void
00191 select_slow (void)
00192 {
00193     hide_mouse ();
00194     pause_flag = 0;
00195     draw_pause (0);
00196     slow_flag = 1;
00197     draw_slow (1);
00198     med_flag = 0;
00199     draw_med (0);
00200     fast_flag = 0;
00201     draw_fast (0);
00202     redraw_mouse ();
00203 }
00204 
00205 void
00206 select_pause (void)
00207 {
00208     if (pause_flag) {
00209         /* unpause it */
00210         if (fast_flag)
00211             select_fast ();
00212         else if (med_flag)
00213             select_medium ();
00214         else if (slow_flag)
00215             select_slow ();
00216         else
00217             select_medium ();
00218     } else {
00219         /* pause it */
00220         hide_mouse ();
00221         pause_flag = 1;
00222         draw_pause (1);
00223         draw_slow (0);
00224         draw_med (0);
00225         draw_fast (0);
00226         redraw_mouse ();
00227     }
00228 }

Generated on Sun Dec 26 11:23:26 2004 for lincity by  doxygen 1.3.9.1