File: sge_misc.cpp

package info (click to toggle)
ruby-sdl 2.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,344 kB
  • ctags: 4,200
  • sloc: cpp: 7,598; ansic: 4,475; ruby: 2,258; sh: 102; makefile: 97
file content (92 lines) | stat: -rw-r--r-- 2,642 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
*	SDL Graphics Extension
*	Misc functions
*
*	Started 990819
*
*	License: LGPL v2+ (see the file LICENSE)
*	(c)1999-2003 Anders Lindstrm
*/

/*********************************************************************
 *  This library is free software; you can redistribute it and/or    *
 *  modify it under the terms of the GNU Library General Public      *
 *  License as published by the Free Software Foundation; either     *
 *  version 2 of the License, or (at your option) any later version. *
 *********************************************************************/

#include "SDL.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "sge_misc.h"


Uint32 delay_res=10;

//==================================================================================
// Returns a random integer between min and max
//==================================================================================
int sge_Random(int min, int max)
{
	return min+(rand()%(max-min+1));
}


//==================================================================================
// Seed the random number generator with a number from the system clock.
// Should be called once before the first use of sge_Random.
//==================================================================================
void sge_Randomize(void)
{
	srand(time(NULL));
}


//==================================================================================
// Test the resolution of SDL_Delay()
//==================================================================================
Uint32 sge_CalibrateDelay(void)
{
  SDL_Delay(10);
  delay_res=SDL_GetTicks();
  SDL_Delay(1);
  delay_res=SDL_GetTicks()-delay_res;

  return delay_res;
}


//==================================================================================
// Get the resolution of SDL_Delay()
//==================================================================================
Uint32 sge_DelayRes(void)
{
	return delay_res;
}


//==================================================================================
// Delay 'ticks' ms.
// Tries to burn time in SDL_Delay() if possible
// Returns the exact delay time
//==================================================================================
Uint32 sge_Delay(Uint32 ticks)
{
	Uint32 start = SDL_GetTicks();
	Sint32 time_left = (Sint32)ticks;
	Uint32 tmp;
	
	if(ticks >= delay_res){
		tmp=ticks-(ticks%delay_res);
		SDL_Delay(tmp);
		time_left=(Sint32)(ticks-(SDL_GetTicks()-start));	//Possible error for large ticks... nah	
	}
	
	while(time_left>0){
		time_left=ticks-(SDL_GetTicks()-start);	
	}
	
	return SDL_GetTicks()-start;	
}