// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See LICENSE for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
					CVS AUTO-GENERATED INFO
$Author: cavac $
$Date: 2006/03/16 10:47:53 $
$Name: rel-0-3-patches $
$Revision: 1.12 $
$State: Exp $

$Log: gameengine.cpp,v $
Revision 1.12  2006/03/16 10:47:53  cavac
Changes some .bmp to .jpg for smaller downloads
Re-compressed sisters_orient.ogg for smaller download
Updated .cpp files to reflect changes

Revision 1.11  2006/03/02 13:03:42  cavac
Re-formatted all .cpp and .h files a bit to get rid of the TAB/SPACE mess
that makes troubles with XCode's "re-format" feature

Revision 1.10  2006/02/27 10:17:54  cavac
Removed GLOBbing of level*.conf for filecount; now using a while-loop
whith fopen to test for files instead

Revision 1.9  2006/02/26 14:05:33  cavac
Fixed a bug with the maximum level number: level0 is also an existing level file

Revision 1.8  2006/02/24 18:48:26  cavac
Finally added real scripting support
Added Testlevel (level0) which can be enabled from globals.h

Revision 1.7  2006/02/21 17:02:28  cavac
Bugfixed to typos

Revision 1.6  2006/02/20 09:04:16  cavac
Small bugfixes

Revision 1.5  2006/02/19 17:33:36  dauti
The number of Levels is now computed (using file-globbing for "level*.conf" in
the RESPATH) - no need to increment a #define in globals.h for each new level...

Revision 1.4  2006/02/17 19:07:59  cavac
Added rudimentary sound effects (FX) support

Revision 1.3  2006/02/10 17:50:26  cavac
Added ActionCapture feature (define ALLOW_ACTIONCAPTURE to allow this
resource-hungry extra)

Revision 1.2  2006/01/09 00:50:34  cavac
Bugfixed CVS autogenerated entries in background.cpp
Added CVS autogenerated entries to remaining .cpp and .h files
Updated copyright from "2005 Rene Schickbauer" to "2005-06 Rene Schickbauer"


--------------------------------------------------------------------*/
#endif //NOP_CODE

#include "globals.h"
#include "gameengine.h"
#include "showloading.h"
#include "engine.h"
#include "showpicture.h"
#ifdef ALLOW_ACTIONCAPTURE
#include "actioncapture.h"
#endif // ALLOW_ACTIONCAPTURE
#include "sound.h"
#include "errorhandler.h"
#include <stdio.h>

GAMEDATA gamedata;

void initGame() {
	gamedata.player = &gamedata.players[0];
#ifdef ENABLE_TESTLEVEL
	gamedata.player->level = 0;
#else // NO ENABLE_TESTLEVEL
	gamedata.player->level = 1;
#endif // ENABLE_TESTLEVEL
	gamedata.player->score = 0;
	gamedata.player->lives = 2;
}

void playGame() {	
#ifdef ALLOW_ACTIONCAPTURE
	initActionCapture();
#endif // ALLOW_ACTIONCAPTURE

	/* Compute MAX_LEVELS by doing a fileglobbing (levels*.conf) */
		char fullfname_conf[MAX_FNAME_LENGTH];

	Uint32 max_levels = 0;
	bool searchnext = true;
	while(searchnext) {
		max_levels++;
		sprintf(fullfname_conf, "%slevel%d.conf", RESPATH, max_levels);
		FILE* fh = fopen(fullfname_conf, "r");
		if(!fh) {
			searchnext = false;
		} else {
			fclose(fh);
		}
	}
	max_levels--;
	printf("MAX LEVELS: %d\n", max_levels);

	while(gamedata.player->level <= max_levels && gamedata.player->lives > 0) {
		if(gamedata.player->level > 1) {
			soundPlayFX(FX_LEVEL_FINISHED);
		}
		showLoading();
		gamedata.player->lives++;
		displayEngine();
		gamedata.player->level++;
	}
#ifdef ALLOW_ACTIONCAPTURE
	deInitActionCapture();
#endif // ALLOW_ACTIONCAPTURE
	
	printf("FINAL SCORE: %d\n", gamedata.player->score);
	
	if(gamedata.player->lives > 0) {
		showPicture("gamewon.jpg", 5000);
	} else {
		showPicture("gameover.jpg", 5000);
	}
}
