timeUtils.cpp

00001 /*
00002   時間管理
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "timeUtils.h"
00008 #include "detect_os.h"
00009 #ifdef Linux
00010 #include <unistd.h>
00011 #include <sys/time.h>
00012 #else
00013 #include <windows.h>
00014 #include <time.h>
00015 #endif
00016 
00017 #ifdef HAVE_CONFIG_H
00018 #include <config.h>
00019 #endif
00020 #if !HAVE_CONFIG_H || HAVE_LIBSDL
00021 #include <SDL.h>
00022 #endif
00023 
00024 
00025 static bool is_realDevice = true;
00026 static double delay_magnify = 1.0;
00027 static unsigned long delay_const = 1000;
00028 
00029 
00030 void VXV::set_DelayTimeMagnify(double magnify) {
00031   delay_magnify = magnify;
00032   delay_const = static_cast<unsigned long>(1000.0 / delay_magnify);
00033 }
00034 
00035 
00036 void VXV::set_MonitorMode(bool realDevice) {
00037   is_realDevice = realDevice;
00038 }
00039 
00040 
00041 void VXV::Delay(unsigned long msec) {
00042   unsigned long delay = delay_const * msec;
00043   if (is_realDevice || (delay > 1000)) {
00044 #ifdef Linux
00045     usleep(delay);
00046 #else
00047     Sleep(delay / 1000);
00048 #endif
00049   }
00050 }
00051 
00052 
00053 unsigned long VXV::GetTicks(void) {
00054   unsigned long ticks = 0;
00055 
00056 #if !HAVE_CONFIG_H || HAVE_LIBSDL
00057   ticks = SDL_GetTicks();
00058 
00059 #elif defined Linux
00060   // Linux で SDL がない場合の実装。最初の呼び出しは 0 を返す
00061   static unsigned long first_ticks = 0;
00062   struct timeval tvp;
00063   gettimeofday(&tvp, NULL);
00064   unsigned long global_ticks = tvp.tv_sec * 1000 + tvp.tv_usec / 1000;
00065   if (first_ticks == 0) {
00066     first_ticks = global_ticks;
00067   }
00068   ticks = global_ticks - first_ticks;
00069 
00070 #else
00071   ticks = static_cast<unsigned long>(clock() / (CLOCKS_PER_SEC / 1000.0));
00072 #endif
00073   return ticks;
00074 }
00075 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1