// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See License.txt for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
CVS AUTO-GENERATED INFO
$Author: cavac $
$Date: 2006/03/02 13:03:43 $
$Name: rel-0-3-patches $
$Revision: 1.4 $
$State: Exp $

$Log: menu.cpp,v $
Revision 1.4  2006/03/02 13:03:43  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.3  2006/02/27 16:37:21  dauti
Generalized renderFontHandlerText(), so that one can specify the color of the text.

Revision 1.2  2006/02/26 21:30:06  dauti
- Allow exit with the BOSS-Key (esc) if compiled in
- Rewrote display of selected choices

Revision 1.1  2006/02/26 14:06:05  cavac
Added menu support

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

#include "globals.h"
#include "menu.h"
#include "fonthandler.h"
#include "errorhandler.h"
#include "memops.h"
#include "sound.h"
#include "joystick.h"
#include <string.h>

SDL_Surface* menubg = 0;

void initMenu() {
	SDL_Surface* temp = IMG_Load(RESPATH "menubg.jpg");
	if(!temp) {
		DIE(ERROR_IMAGE_READ, "menubg.jpg");
	}
	menubg = SDL_ConvertSurface(temp, gScreen->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp);
	
}

void deInitMenu() {
	SDL_FreeSurface(menubg);
	menubg = 0;
}

bool menuDisplay() {
		char* menutexte[] = {"Play","Configure","Quit", NULL};
	SDL_Color MENUCOLOR_ACTIVE   = { 0xff, 0xff, 0xff, 0 };
	SDL_Color MENUCOLOR_INACTIVE = { 0xa0, 0xa0, 0xa0, 0 };

	Uint32 sel = 1;
	Uint32 max = 3; /* TODO: compute max as the size of the array menutexte */
	bool menuRunning = true;
	bool joystickIdle = false;
	soundStartMusic("menuMusic.ogg");
	while(menuRunning) {
		
		// Lock Surface if needed
		if (SDL_MUSTLOCK(gScreen))
			if (SDL_LockSurface(gScreen) < 0) 
				return false;	
		
		if (SDL_MUSTLOCK(menubg))
			if (SDL_LockSurface(menubg) < 0) 
				return false;	


		Uint32 bigpitch = gScreen->pitch;
		Uint32 bigpitchBG = menubg->pitch;

		long fc_src_long = (long)menubg->pixels;
		long fc_dst_long = (long)gScreen->pixels;

		for(Uint32 j=0; j < SCR_HEIGHT; j++) {
			fc_src = (fastcopy *)fc_src_long;
			fc_dest = (fastcopy *)fc_dst_long;
			*fc_dest = *fc_src;
			fc_dst_long += bigpitch;
			fc_src_long += bigpitchBG;
		}

		Uint32 cnt=0 ;
		while(menutexte[cnt] != NULL) {
			if ((cnt+1)==sel) {
			char s[255] ; 
		sprintf(s,"* %s *",menutexte[cnt]) ;
				renderFontHandlerText(10, 100+cnt*70, s, MENUCOLOR_ACTIVE, true, false); /* selected */
			} else {
				renderFontHandlerText(10, 100+cnt*70, menutexte[cnt], MENUCOLOR_INACTIVE, true, false); /* not selected */
			}
			cnt++ ;
		}

		// Unlock Surface if needed
		if (SDL_MUSTLOCK(menubg)) 
			SDL_UnlockSurface(menubg);
		if (SDL_MUSTLOCK(gScreen)) 
			SDL_UnlockSurface(gScreen);
		SDL_UpdateRect(gScreen, 0, 0, SCR_WIDTH, SCR_HEIGHT);
		
		// Handle Joystick
		Uint32 joymove = getJoystickMoves();
		if(joymove == JOYSTICK_NONE) {
			joystickIdle = true;
		}
		if(joystickIdle && joymove != JOYSTICK_NONE) {
			if((joymove & JOYSTICK_UP)) {
				soundPlayFX(FX_COLLECT_PIXEL);
				sel--;
				if(!sel) {
					sel = max;
				}
			} else if((joymove & JOYSTICK_DOWN)) {
				soundPlayFX(FX_COLLECT_PIXEL);
				sel++;
				if(sel > max) {
					sel = 1;
				}
			} else if((joymove & JOYSTICK_ACTION)) {
				// FIXME: Handle CONFIGURE option
				soundPlayFX(FX_COLLECT_PIXEL);
				menuRunning = false;
			} 
			joystickIdle = false;
		}
		
		
		// Handle Keyboard
		SDL_Event event;
		while (SDL_PollEvent(&event)) 
		{
			switch (event.type) 
			{
				case SDL_KEYUP:
					if (event.key.keysym.sym == SDLK_UP) {
						soundPlayFX(FX_COLLECT_PIXEL);
						sel--;
						if(!sel) {
							sel = max;
						}
					} else if (event.key.keysym.sym == SDLK_DOWN) {
						soundPlayFX(FX_COLLECT_PIXEL);
						sel++;
						if(sel > max) {
							sel = 1;
						}
					} else if (event.key.keysym.sym == SDLK_RETURN) {
						// FIXME: Handle CONFIGURE option
						soundPlayFX(FX_COLLECT_PIXEL);
						menuRunning = false;
#ifdef ALLOW_BOSSKEY
					} else if (event.key.keysym.sym == SDLK_ESCAPE) {
							exit(0);
#endif // ALLOW_BOSSKEY
					}

					break;
			}
		}
		
		SDL_Delay(50);
	}
	
	soundStopMusic();
	if(sel == 1) {
		return true;
	} else {
		return false;
	}
}
