view game_time.cc @ 122:5219bf3d5e71

title_draw
author e085768
date Mon, 27 Jun 2011 19:44:46 +0900
parents c534f339ee8b
children
line wrap: on
line source

/**
 * 2006/07/31(MON) Nobutaka OSHIRO
 */
#include "game_time.h"

int game_time_get_msec() {
	
  struct timeval now; 	 
  int time_msec;

 /** 
  *  *** man timeval ***
  *  struct timeval {
  *   time_t tv_sec // second
  *   suseconds_t tv_usec; // micro sec
  * }; 
  **/

  // get time micro second ( micro second = 1 / 1000,000 second) 
  gettimeofday(&now, NULL);

  // evaluation milli second ( milli second = 1 / 1000 second )
  time_msec = now.tv_sec * 1000 + now.tv_usec / 1000;
 
  return time_msec;
}


int game_time_conv_ms2cs (int msec)
{
  return msec / 10 % 100;  /** 1/100 sec **/ 
}

int game_time_conv_ms2sec (int msec)
{
  return msec / 1000 % 60;
}

int game_time_conv_ms2minute (int msec)
{
  return msec / 60000 % 60;  
}


/**
 *  NAME:
 *       game_time_set_raptime - Return RAP TIME ("XXX:XX:XX") 
 *
 *  DESCRIPTION:
 *       1. convert (int --> string)
 *             (int) argument2 (milli second)  --> "XXX:XX:XX"
 *  
 *       2. set ( string --> char * ) 
 *             "XXX:XX:XX" --> char *argument1  
 **/
void game_time_set_raptime (char *s,int msec)  
{
  char chminute[3]; 
  char chsec[2];
  char chcsec[2];
  char raptime[10];
  int minute  = game_time_conv_ms2minute (msec);
  int sec  = game_time_conv_ms2sec (msec); 
  int csec = game_time_conv_ms2cs (msec);
  int i;


  if (minute < 10 ){
    chminute[0] = ' ';
    chminute[1] = '0';
    chminute[2] = minute + 48;
  } else if (minute < 100 ) {
    chminute[0] = ' ';
    chminute[1] = (minute / 10) + 48;  
    chminute[2] = (minute % 10) + 48;
  } else {
    chminute[0] = (minute / 100) + 48;
    chminute[1] = (minute / 10) + 48;  
    chminute[2] = (minute % 10) + 48;
  }
  if (sec < 10 ){  
    chsec[0] = '0';
    chsec[1] = sec + 48;
  } else {
    chsec[0] = (sec / 10) + 48;  
    chsec[1] = (sec % 10) + 48;
  }

  if (csec < 10 ){  
    chcsec[0] = '0';
    chcsec[1] = csec + 48;
  } else {
    chcsec[0] = (csec / 10) + 48;  
    chcsec[1] = (csec % 10) + 48;
  }


  /** raptime[0-9] == "XXX:XX:XX\0" **/ 
  raptime[0] = chminute[0];
  raptime[1] = chminute[1];
  raptime[2] = chminute[2];
  raptime[3] = ':';
  raptime[4] = chsec[0];
  raptime[5] = chsec[1];
  raptime[6] = ':';
  raptime[7] = chcsec[0];
  raptime[8] = chcsec[1];
  raptime[9] = '\0';   // NULL


  for (i = 0; raptime[i] != '\0'; i++) {
    s[i] = raptime[i];    
  } 
}