/*
Copyright (C) 1997-2001 Id Software, Inc.

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

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// sys_null.h -- null system driver to aid porting efforts

#ifdef GCCBROKENRELATIVEPATHS
#include "../../qcommon/qcommon.h"
#include "../../riscos/glob.h"
#include "../../game/game.h"
#else
#include "../qcommon/qcommon.h"
#include "../riscos/glob.h"
#include "../game/game.h"
#endif

#include "errno.h"
#include "prof.h"

// RISC OS includes
#include "kernel.h"
#include "swis.h"

#include "q2swis.h"

static int keyboard_started = false;
int        gutils_handle;

int	curtime;

unsigned	sys_frame_time;

int prevBestTime;
int Sys_GetBestTime(void)
{
    unsigned int time;
    _swix(GameUtils_Time, _IN(0) | _OUT(0), gutils_handle, &time);

    return time;
}

int Sys_GetLastTime(void)
{
    int bestTime = Sys_GetBestTime();
    int taken = bestTime - prevBestTime;

    prevBestTime = bestTime;
    return taken;
}

int currentProf = -1;
unsigned int profStart[100];

void beginProf(void)
{
    profStart[++currentProf] = Sys_GetBestTime();
}

void endProf(char * proc)
{
    int i;
    unsigned profEnd = Sys_GetBestTime();
    printf("PROF:");
    for(i = 0; i < currentProf; i++) putc(' ', stdout);
    printf("%s: %d\n", proc, profEnd - profStart[currentProf]);
    currentProf--;
}

void quit(int retval)
{
    LogFloatResults("raFS::Quake2.$.logfloat");
    Qcommon_Shutdown();

#ifdef USE_GAMEUTILS
    if (keyboard_started) {
        _swix(GameUtils_DeRegister, _IN(0), gutils_handle);
        keyboard_started = false;
    }
#endif

    _swix(Wimp_CommandWindow, _IN(0), -1);

    exit(retval);
}

void Sys_Error (char *error, ...)
{
	va_list		argptr;

	printf ("Sys_Error: ");
	va_start (argptr,error);
	vprintf (error,argptr);
	va_end (argptr);
	printf ("\n");

	quit(1);
}

void Sys_Quit (void)
{
    _swix(OS_Byte, _INR(0, 1), 21, 0); /* Flush keyboard buffer */

    quit(0);
}

void	Sys_UnloadGame (void)
{
}

void	*Sys_GetGameAPI (void *parms)
{
	return (void*)GetGameAPI(parms);
}

void Sys_AppActivate (void)
{
    // Shows the window in win32 version
}

void Sys_CopyProtect (void)
{
    // Should search for CD
}

char *Sys_GetClipboardData( void )
{
	return NULL;
}

int Sys_Milliseconds(void)
{
    int time;
    _swix(OS_ReadMonotonicTime, _OUT(0), &time);
//    _swix(GameUtils_Time, _IN(0) | _OUT(0), gutils_handle, &time);

//    return time / 1000;
    return time * 10;
}

void	Sys_Mkdir (char *path)
{
     mkdir(path);
}

//============================================

extern void fpusetup();

void	Sys_Init (void)
{
#ifdef USE_GAMEUTILS
    _kernel_oserror * e;

    e = _swix(GameUtils_Register, _OUT(0), &gutils_handle);
    if (gutils_handle == -1 || e) Sys_Error("GameUtils could not be registered\n");

    keyboard_started = true;
#endif

    // Disable escape
    _swix(OS_Byte, _INR(0, 2), 200, 1, 0);

    // Disable FP exception checking
    fpusetup();
}

// Input

cvar_t	*in_joystick;

//=============================================================================

//const char * __dynamic_da_name = "Quake 2 heap";

int main (int argc, char **argv)
{
    int oldtime, newtime, time;

    Qcommon_Init (argc, argv);

    oldtime = Sys_Milliseconds ();

    while (1)
    {
        do
        {
            newtime = Sys_Milliseconds();
            time = newtime - oldtime;
        } while(time < 1);

BEGIN_PROF();
        Qcommon_Frame(time);
END_PROF("Qcommon_Frame");
    }

    return 0;
}
