
#include "Video.h"
#include "anaglym.h"
#include "Engine.h"
#include "SDL.h"
#include <stdlib.h>                 /* exit(), srand() */
#include <iostream>
#include <string>
using namespace std;

static void usage();

static void usage()
{
    cerr << "Usage: anaglym [options] program.lua[c]" << endl;
    exit(42);
}

int main(int argc, char * argv[])
{
    bool fullscreen = true;
    bool grab_input = true;
    int width = 0;
    int height = 0;
    const char * program = NULL;
    for (int i = 1; i < argc; i++)
    {
        if (argv[i][0] != '-')
        {
            if (program == NULL)
            {
                program = argv[i];
            }
            else
            {
                cerr << "Warning: argument " << argv[i] << " ignored!" << endl;
                usage();
            }
        }
        else
        {
            if      (!strcmp(argv[i], "-f"))
            {
                fullscreen = false;
            }
            else if (!strcmp(argv[i], "-g"))
            {
                grab_input = false;
            }
            else if (!strncmp(argv[i], "-w", 2))
            {
                width = atoi((strlen(argv[i]) == 2) ? argv[++i] : argv[i] + 2);
            }
            else if (!strncmp(argv[i], "-h", 2))
            {
                height = atoi((strlen(argv[i]) == 2) ? argv[++i] : argv[i] + 2);
            }
            else
            {
                cerr << "Warning: Unrecognized option '" << argv[i]+1
                     << "'" << endl;
                usage();
            }
        }
    }

    if (program == NULL)
    {
        usage();
    }

    srand(time(NULL));

    Video video;
    video.start(width, height, fullscreen, grab_input);

    dInitODE();
    g_engine = new Engine(argv[0], video);
    if (g_engine->load(program))
        g_engine->run();
    delete g_engine;
    video.stop();
    dCloseODE();

    return 0;
}
