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