/***************************************************************************
                          main.cpp  -  description
                             -------------------
    begin                : Sun Nov  7 16:54:22 CET 2004
    copyright            : (C) 2004 by 
    email                : 
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qapplication.h>
#include <qtextcodec.h>
#include <qsplashscreen.h>

#include "globdefines.h"
#include "qcakerun.h"
#include "listviews.h"
#include "audioenv.h"
#include "netclient.h"
#include "astar.h"
#include "glcontrol.h"

#include "qcake.xpm"

#ifdef Q_WS_X11
#include <X11/Xlib.h>
#endif

// global variables würg !!!!
int g_mouse_mode=0;
int g_mouse_x=0;
int g_mouse_y=0;
int g_mouse_button=0;
sgVec3 g_poi; // point of interest
int g_window_w=0;
int g_window_h=0;
int g_key=0;
int g_playmode=0;
int g_playmode_reset=0;
int g_switch = 0;
long g_nextitem=1;
long g_nextid=0;
FolderListItem *g_ListItem[MAX_LIST_ITEMS];
int gl_spl_num=0;
splPrg  *gl_spl_obj[MAX_SPL];
float g_fps=0.0f;
float g_phfps=0.0f;
Glcontrol    *g_gl=NULL;
usercamera *g_usercam=NULL;
QString g_mypath;
QString g_prjpath;
FolderListItem *g_tmpItem=NULL;
FolderListItem *g_switchScene = NULL;
netclient    *g_client;
// js
jsJoystick *g_js[2] ;
float    g_ax[2][8] ;
int g_buttons[2] ;

// AStar
aStar *gastar= NULL;

int gl_sound_num = 0;

#ifdef KCOPENAL
 AudioEnv *Audio = NULL;
#endif

void help(int ret)
{
	printf("\n");

	switch ( ret )
	{
	case 2:
		printf("Input-Output Error!\n\n");
		break;
	}

	printf("Usage: qcakeplayer [Options] filename.xml  \n\n");
        printf("	-f	start game in full screen mode\n");
        printf("	-w	display in a window (default) \n");
        printf("\n");
	exit(ret);
}


int main(int argc, char *argv[])
{
  int a;
  int opt;
  int opt_fullscreen = 0;
    
  #ifdef Q_WS_X11
        XInitThreads();
  #endif

  
  QApplication ap(argc, argv);
  
  while ( (opt = getopt(argc, argv, "+fw")) != -1 ) {
	switch ( opt )
	{
		case 'f': {
                       	opt_fullscreen=1;
			break;
		}
		case 'w': {
                       	opt_fullscreen=0;
			break;
		}
		default:
			help(1);
	}
  }
  if ( argc <= 1 ) help(1);

  // SplahScreen
  QSplashScreen *splash = new QSplashScreen(QPixmap(qcake));
  splash->show();

  //--------------
  // AStar
  gastar = new aStar(40,40,1,8); 

#ifdef KCOPENAL
 Audio = new AudioEnv();
 Audio->Init();
#endif

  for(a=0; a < MAX_LIST_ITEMS; a++)
  {
    g_ListItem[a] = NULL;
  }

  for(a=0; a < MAX_SPL; a++)
  {
    gl_spl_obj[a] = NULL;
  }

  g_client = NULL;

  //js  
  g_js[0] = new jsJoystick ( 0 ) ;
  g_js[1] = new jsJoystick ( 1 ) ;


  if ( g_js[0] -> notWorking () || g_js[0] -> getNumAxes () < 2 )
  {
    fprintf ( stderr,
       "No Joystick 1 found\n" ) ;
//    exit ( 0 ) ;
  }
  if ( g_js[1] -> notWorking () || g_js[1] -> getNumAxes () < 2 )
  {
    fprintf ( stderr,
       "No Joystick 2 found\n" ) ;
//    exit ( 0 ) ;
  }

  ap.setFont(QFont("helvetica", 12));
  QTranslator tor( 0 );
  // set the location where your .qm files are in load() below as the last parameter instead of "."
  // for development, use "/" to use the english original as
  // .qm files are stored in the base project directory.
  tor.load( QString("qcake.") + QTextCodec::locale(), "." );
  ap.installTranslator( &tor );

  QcakeRunApp *qcakerun=new QcakeRunApp(&ap);
  
  g_gl = new Glcontrol( );
  ap.setMainWidget(g_gl);
//  g_gl->resize(640,480);
//  g_gl->showFullScreen ();
 

  if(argc>1)
  {
    if(opt_fullscreen){
    	g_gl->showFullScreen ();
    }else{
  	g_gl->show();    
    }
    qcakerun->openDocumentFile(argv[argc -1]);
    qcakerun->slotToolsPlay();
  }
//  else
//    qcakerun->openDocumentFile("./example/bodys.xml");
//    qcakerun->slotToolsPlay();
  
  splash->finish(g_gl);
  int result = ap.exec();
  
  // free memory
  delete g_gl;
  delete gastar;
  delete splash;
  #ifdef KCOPENAL
  	delete Audio;
  #endif
  delete g_js[0];
  delete g_js[1];
  
  return result;
}
