/*
  Copyright (C) 2010-2011  Charles Pence <charles@charlespence.net>
  Copyright (C) 1996-1997  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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "quakedef.h"

#define MINIMUM_WIN_MEMORY        0x0880000
#define MAXIMUM_WIN_MEMORY        0x1000000


void Sys_Error (char *error, ...)
{
    va_list        argptr;
    char        text[1024];

    va_start (argptr, error);
    vsprintf (text, error, argptr);
    va_end (argptr);

    fprintf (stderr, "%s\n", text);

    Host_Shutdown ();
    exit (1);
}

void Sys_Quit (void)
{
    Host_Shutdown();
    exit (0);
}


/*
================
Sys_FloatTime
================
*/
double Sys_FloatTime (void)
{
    return (float)SDL_GetTicks() / 1000.0f;
}

void Sys_SendKeyEvents (void)
{
    SDL_Event event;

    while (SDL_PollEvent(&event))
    {
        if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
            Key_Event(event.key.keysym.sym, (event.key.state == SDL_PRESSED));
    }
}


/*
==================
main
==================
*/
int main(int argc, char *argv[])
{
    quakeparms_t    parms;
    double            time, oldtime, newtime;

    SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
    atexit(SDL_Quit);

    parms.argv0 = argv[0];
    parms.basedir = ".";
    parms.memsize = MAXIMUM_WIN_MEMORY;
    parms.membase = malloc (parms.memsize);

    if (!parms.membase)
        Sys_Error ("Not enough memory free; check disk space\n");

    Host_Init (&parms);

    oldtime = Sys_FloatTime ();

    /* main window message loop */
    while (1)
    {
        newtime = Sys_FloatTime ();
        time = newtime - oldtime;

        Host_Frame (time);
        oldtime = newtime;
    }

    return 0;
}

