src/timer/riscos/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 29 May 2003 04:44:13 +0000
changeset 630 550bccdf04bd
child 769 b8d311d90021
permissions -rw-r--r--
Added initial support for RISC OS (thanks Peter Naulls!)
slouken@630
     1
/*
slouken@630
     2
    SDL - Simple DirectMedia Layer
slouken@630
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@630
     4
slouken@630
     5
    This library is free software; you can redistribute it and/or
slouken@630
     6
    modify it under the terms of the GNU Library General Public
slouken@630
     7
    License as published by the Free Software Foundation; either
slouken@630
     8
    version 2 of the License, or (at your option) any later version.
slouken@630
     9
slouken@630
    10
    This library is distributed in the hope that it will be useful,
slouken@630
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@630
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@630
    13
    Library General Public License for more details.
slouken@630
    14
slouken@630
    15
    You should have received a copy of the GNU Library General Public
slouken@630
    16
    License along with this library; if not, write to the Free
slouken@630
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@630
    18
slouken@630
    19
    Sam Lantinga
slouken@630
    20
    slouken@devolution.com
slouken@630
    21
*/
slouken@630
    22
slouken@630
    23
#ifdef SAVE_RCSID
slouken@630
    24
static char rcsid =
slouken@630
    25
 "@(#) $Id$";
slouken@630
    26
#endif
slouken@630
    27
slouken@630
    28
#include <stdio.h>
slouken@630
    29
#include <time.h>
slouken@630
    30
#include <sys/time.h>
slouken@630
    31
#include <unistd.h>
slouken@630
    32
#include <string.h>
slouken@630
    33
#include <errno.h>
slouken@630
    34
slouken@630
    35
#include "SDL_error.h"
slouken@630
    36
#include "SDL_timer.h"
slouken@630
    37
#include "SDL_timer_c.h"
slouken@630
    38
slouken@630
    39
/* Timer start/reset time */
slouken@630
    40
static Uint32 timerStart;
slouken@630
    41
/* Timer running function */
slouken@630
    42
void RISCOS_CheckTimer();
slouken@630
    43
slouken@630
    44
extern void RISCOS_BackgroundTasks(void);
slouken@630
    45
slouken@630
    46
/* The first ticks value of the application */
slouken@630
    47
clock_t start;
slouken@630
    48
slouken@630
    49
void SDL_StartTicks(void)
slouken@630
    50
{
slouken@630
    51
	/* Set first ticks value */
slouken@630
    52
	start = clock();
slouken@630
    53
}
slouken@630
    54
slouken@630
    55
Uint32 SDL_GetTicks (void)
slouken@630
    56
{
slouken@630
    57
	clock_t ticks;
slouken@630
    58
slouken@630
    59
	ticks=clock()-start;
slouken@630
    60
slouken@630
    61
slouken@630
    62
#if CLOCKS_PER_SEC == 1000
slouken@630
    63
slouken@630
    64
	return(ticks);
slouken@630
    65
slouken@630
    66
#elif CLOCKS_PER_SEC == 100
slouken@630
    67
slouken@630
    68
	return (ticks * 10);
slouken@630
    69
slouken@630
    70
#else
slouken@630
    71
slouken@630
    72
	return ticks*(1000/CLOCKS_PER_SEC);
slouken@630
    73
slouken@630
    74
#endif
slouken@630
    75
slouken@630
    76
}
slouken@630
    77
slouken@630
    78
extern void DRenderer_FillBuffers();
slouken@630
    79
slouken@630
    80
void SDL_Delay (Uint32 ms)
slouken@630
    81
{
slouken@630
    82
    Uint32 now,then,elapsed;
slouken@630
    83
slouken@630
    84
	/* Set the timeout interval - Linux only needs to do this once */
slouken@630
    85
	then = SDL_GetTicks();
slouken@630
    86
slouken@630
    87
	do {
slouken@630
    88
		/* Do background tasks required while sleeping as we are not multithreaded */
slouken@630
    89
		RISCOS_BackgroundTasks();
slouken@630
    90
		/* Calculate the time interval left (in case of interrupt) */
slouken@630
    91
		now = SDL_GetTicks();
slouken@630
    92
		elapsed = (now-then);
slouken@630
    93
		then = now;
slouken@630
    94
		if ( elapsed >= ms ) {
slouken@630
    95
			break;
slouken@630
    96
		}
slouken@630
    97
		ms -= elapsed;
slouken@630
    98
slouken@630
    99
	} while ( 1 );
slouken@630
   100
}
slouken@630
   101
slouken@630
   102
int SDL_SYS_TimerInit(void)
slouken@630
   103
{
slouken@630
   104
	return(0);
slouken@630
   105
}
slouken@630
   106
slouken@630
   107
void SDL_SYS_TimerQuit(void)
slouken@630
   108
{
slouken@630
   109
	SDL_SetTimer(0, NULL);
slouken@630
   110
}
slouken@630
   111
slouken@630
   112
int SDL_SYS_StartTimer(void)
slouken@630
   113
{
slouken@630
   114
	timerStart = SDL_GetTicks();
slouken@630
   115
slouken@630
   116
	return(0);
slouken@630
   117
}
slouken@630
   118
slouken@630
   119
void SDL_SYS_StopTimer(void)
slouken@630
   120
{
slouken@630
   121
	/* Don't need to do anything as we use SDL_timer_running
slouken@630
   122
	   to detect if we need to check the timer */
slouken@630
   123
}
slouken@630
   124
slouken@630
   125
slouken@630
   126
void RISCOS_CheckTimer()
slouken@630
   127
{
slouken@630
   128
	if (SDL_timer_running && SDL_GetTicks() - timerStart >= SDL_alarm_interval)
slouken@630
   129
	{
slouken@630
   130
		Uint32 ms;
slouken@630
   131
slouken@630
   132
		ms = SDL_alarm_callback(SDL_alarm_interval);
slouken@630
   133
		if ( ms != SDL_alarm_interval )
slouken@630
   134
		{
slouken@630
   135
			if ( ms )
slouken@630
   136
			{
slouken@630
   137
				SDL_alarm_interval = ROUND_RESOLUTION(ms);
slouken@630
   138
			} else
slouken@630
   139
			{
slouken@630
   140
				SDL_alarm_interval = 0;
slouken@630
   141
				SDL_timer_running = 0;
slouken@630
   142
			}
slouken@630
   143
		}
slouken@630
   144
		if (SDL_alarm_interval) timerStart = SDL_GetTicks();
slouken@630
   145
	}
slouken@630
   146
}