#include "gens.h"

#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <string.h>
#include "G_main.h"
#include "G_ddraw.h"
#include "G_dsound.h"
#include "G_Input.h"
#include "Debug.h"
#include "rom.h"
#include "save.h"
#include "resource.h"
#include "misc.h"
#include "ggenie.h"
#include "Cpu_68k.h"
#include "Star_68k.h"
#include "Cpu_SH2.h"
#include "Cpu_Z80.h"
#include "z80.h"
#include "mem_M68K.h"
#include "mem_S68K.h"
#include "mem_SH2.h"
#include "mem_Z80.h"
#include "io.h"
#include "psg.h"
#include "ym2612.h"
#include "pwm.h"
#include "scrshot.h"
#include "vdp_io.h"
#include "vdp_rend.h"
#include "vdp_32X.h"
#include "LC89510.h"
#include "gfx_cd.h"
#include "cd_aspi.h"
#include "net.h"
#include "pcm.h"
#include "htmlhelp.h"
#include "wave.h"

unsigned int MovieInfo[10] ={0};

//User;
#include "AWAKE_Std.h"
char W_TextBuffer[50]; 

int StretchModelIndex =0;
int VolumeIndex =0;
int Window_Width =0;
int Window_Height =0;

bool ModeToChange =false;
bool NONE_RAM_CARD =true;
bool One_Internal =false;
bool HalfWork =false;
bool Specal_Fix =false;
bool Stretch_AUTO =false;
bool MoviePlaying =false;
bool MovieRecord =false;
bool Glossiness_Mode =false;
bool TranslationMode =false;

FILE *MovieFile;

bool MediaMode =false;
bool MediaPause =false;
bool Support_7z =false;

//Force feedback for perss key;
bool FFD_A =false;
bool FFD_B =false;
bool FFD_C =false;

bool FFD_X =false;
bool FFD_Y =false;
bool FFD_Z =false;

bool FFD_ForPerssKey =false;

//Turbo Input;
bool Turbo_On =false;
bool Turbo_1P_A =false;
bool Turbo_1P_B =false;
bool Turbo_1P_C =false;

bool Turbo_2P_A =false;
bool Turbo_2P_B =false;
bool Turbo_2P_C =false;

//Hue;
unsigned char Hue =4;
bool Force32bpp =false;

//Game Name System;
unsigned int GameID_ADB[10000] ={0};
unsigned char DataBuffer_ADB[49*10000] ={0};

HBITMAP Logo_bitmap;
HDROP hDrop;
RECT W_RECT;
POINTS point;

extern "C" void Read_To_68K_Space(int adr);

#define WM_KNUX WM_USER + 3
#define GENS_VERSION   2.10
#define GENS_VERSION_H 2 * 65536 + 10

#define MINIMIZE								\
if (Sound_Initialised) Clear_Sound_Buffer();	\
if (Full_Screen)								\
{												\
	Set_Render(hWnd, 0, -1, true);				\
	FS_Minimised = 1;							\
}

#define MENU_L(smenu, pos, flags, id, str, suffixe, def)										\
GetPrivateProfileString(language_name[Language], (str), (def), Str_Tmp, 1024, Language_Path);	\
strcat(Str_Tmp, (suffixe));																			\
InsertMenu((smenu), (pos), (flags), (id), Str_Tmp);

#define WORD_L(id, str, suffixe, def)															\
GetPrivateProfileString(language_name[Language], (str), (def), Str_Tmp, 1024, Language_Path);	\
strcat(Str_Tmp, (suffixe));																			\
SetDlgItemText(hDlg, id, Str_Tmp);

#define MESSAGE_L(str, def, time)																	\
{																									\
	GetPrivateProfileString(language_name[Language], (str), (def), Str_Tmp, 1024, Language_Path);	\
	Put_Info(Str_Tmp, (time));																		\
}

#define MESSAGE_NUM_L(str, def, num, time)															\
{																									\
	char mes_tmp[1024];																				\
	GetPrivateProfileString(language_name[Language], (str), (def), Str_Tmp, 1024, Language_Path);	\
	sprintf(mes_tmp, Str_Tmp, (num));																\
	Put_Info(mes_tmp, (time));																		\
}


HINSTANCE ghInstance;
HACCEL hAccelTable;
WNDCLASS WndClass;
HWND HWnd;
HMENU Gens_Menu;

char Str_Tmp[1024];
char Gens_Path[1024];
char Language_Path[1024];
char CGOffline_Path[1024];
char Manual_Path[1024];

char **language_name = NULL;
struct Rom *Game = NULL;
int Active = 0;
int Paused = 0;
int Net_Play = 0;
int Full_Screen = -1;
int Resolution = 1;
int Fast_Blur = 0;
int Render_W = 0;
int Render_FS = 0;
int Show_FPS = 0;
int Show_Message = 0;
int Show_LED = 0;
int FS_Minimised = 0;
int Auto_Pause = 0;
int Auto_Fix_CS = 0;
int Language = 0;
int Country = -1;
int Country_Order[3];
int Kaillera_Client_Running = 0;
int Intro_Style = 0;
int SegaCD_Accurate = 0;
int Gens_Running = 0;
int WinNT_Flag = 0;
int SS_Actived;
int Stretch_W_Index =0;

POINT Window_Pos;

LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK GGenieProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ColorProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK DirectoriesProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK FilesProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ControllerProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK OptionProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK AboutProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK SetGameNameProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK SetStretchModeProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK InputTranslationProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK SetValidAreaProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK TranslationEdit(HWND, UINT, WPARAM, LPARAM);

int Set_Render(HWND hWnd, int Num, bool Force);
HMENU Build_Main_Menu(void);


int Change_VSync(HWND hWnd)
{
	int *p_vsync;
	
	if (Full_Screen)
	{
		End_DDraw();
		p_vsync = &FS_VSync;
	}
	else p_vsync = &W_VSync;
	
	*p_vsync = 1 - *p_vsync;
	
	if (*p_vsync) MESSAGE_L("Vertical Sync Enabled", "Vertical Sync Enabled", 1000)
	else MESSAGE_L("Vertical Sync Disabled", "Vertical Sync Disabled", 1000)

	Build_Main_Menu();
	if (Full_Screen) return Init_DDraw(HWnd);
	else return 1;
}

void Save_GameNameData()
{
      FILE* GameName =fopen("GameName.adb","rb+");
	  if(GameName ==NULL)
	  return;
      fwrite(&GameID_ADB[0],2,10000,GameName);
      fwrite(&DataBuffer_ADB[0],1,49*10000,GameName);
      fclose(GameName);
	  return;
}

int BackUpTextID(int TextID,char Text[100])
{
	FILE* file =fopen("Text ID.txt","at+");
	fprintf(file,"\n%s%d","ID: ",TextID);
	fprintf(file,"\n%s%s","Text: ",Text);
	fclose(file);
	return 1;
}

int Set_Frame_Skip(HWND hWnd, int Num)
{
	Frame_Skip = Num;

	if (Frame_Skip != -1)
		MESSAGE_NUM_L("Frame skip set to %d", "Frame skip set to %d", Frame_Skip, 1500)
	else
		MESSAGE_L("Frame skip set to Auto", "Frame skip set to Auto", 1500)

	Build_Main_Menu();
	return(1);
}

int ResizeWondow(HWND hWnd)
{
RECT Rect;
RECT r;

GetWindowRect(hWnd,&Rect);

Render_W =1;
Init_DDraw(hWnd);

if(Stretch_W_Index ==Stretch_W_2X)
{
SetRect(&r,0,0,320*2,224*2);
AdjustWindowRectEx(&r, GetWindowLong(hWnd, GWL_STYLE), 1, GetWindowLong(hWnd, GWL_EXSTYLE));
SetWindowPos(hWnd,NULL,Rect.left,Rect.top,r.right - r.left,r.bottom-r.top,SWP_NOZORDER | SWP_NOACTIVATE);
MESSAGE_L("Stretched To 2x", "Stretched To 2x", 1000)
}

if(Stretch_W_Index ==Stretch_W_3X)
{
SetRect(&r,0,0,320*3,224*3);
AdjustWindowRectEx(&r, GetWindowLong(hWnd, GWL_STYLE), 1, GetWindowLong(hWnd, GWL_EXSTYLE));
SetWindowPos(hWnd,NULL,Rect.left,Rect.top,r.right - r.left,r.bottom-r.top,SWP_NOZORDER | SWP_NOACTIVATE);
MESSAGE_L("Stretched To 3x", "Stretched To 3x", 1000)
}

if(Stretch_W_Index ==Stretch_W_4X)
{
SetRect(&r,0,0,320*4,224*4);
AdjustWindowRectEx(&r, GetWindowLong(hWnd, GWL_STYLE), 1, GetWindowLong(hWnd, GWL_EXSTYLE));
SetWindowPos(hWnd,NULL,Rect.left,Rect.top,r.right - r.left,r.bottom-r.top,SWP_NOZORDER | SWP_NOACTIVATE);
MESSAGE_L("Stretched To 4x", "Stretched To 4x", 1000)
}

if(Stretch_W_Index ==Stretch_W_FixedRate)
{
SetRect(&r,0,0,((Rect.right-Rect.left)+(Rect.bottom-Rect.top))/7*4,((Rect.right-Rect.left)+(Rect.bottom-Rect.top))/7*3);
AdjustWindowRectEx(&r, GetWindowLong(hWnd, GWL_STYLE), 1, GetWindowLong(hWnd, GWL_EXSTYLE));
SetWindowPos(hWnd,NULL,Rect.left,Rect.top,r.right - r.left,r.bottom-r.top,SWP_NOZORDER | SWP_NOACTIVATE);
MESSAGE_L("Fixed the Rate Of Stretch", "Fixed the Rate Of Stretch", 1000)
}

if(Stretch_W_Index ==Stretch_W_Custom)
{
SetRect(&r,0,0,Custom_Horizontal,Custom_Vertical);
AdjustWindowRectEx(&r, GetWindowLong(hWnd, GWL_STYLE), 1, GetWindowLong(hWnd, GWL_EXSTYLE));
SetWindowPos(hWnd,NULL,Rect.left,Rect.top,r.right - r.left,r.bottom-r.top,SWP_NOZORDER | SWP_NOACTIVATE);
MESSAGE_L("Setted To the Customzied Stretch", "Setted To the Customzied Stretch", 1000)
}

Build_Main_Menu();
return 1;
}

int SetAutoStretch(HWND hWnd)
{
RECT Rect;
RECT r;

GetWindowRect(hWnd,&Rect);

Render_W =1;
Init_DDraw(hWnd);

if(Stretch_AUTO)
{
Stretch_AUTO =false;
MESSAGE_L("AUTO Stretch Disabled", "AUTO Stretch Disabled", 1000)
}
else
{
Stretch_AUTO =true;
MESSAGE_L("AUTO Stretch Enabled", "AUTO Stretch Enabled", 1000)
}
Build_Main_Menu();
return 1;
}


int Set_Current_State(HWND hWnd, int Num)
{
	FILE *f;
	
	Current_State = Num;

	if (f = Get_State_File())
	{
		fclose(f);
		MESSAGE_NUM_L("SLOT %d [EMPTY]", "SLOT %d [OCCUPIED]", Current_State, 1500)
	}
	else
		MESSAGE_NUM_L("SLOT %d [EMPTY]", "SLOT %d [EMPTY]", Current_State, 1500)

	Build_Main_Menu();
	return 1;
}

/*
int Change_Stretch(void)
{
	if ((Full_Screen) && (Render_FS > 1)) return(0);
	
	Flag_Clr_Scr = 1;

	if (Stretch = (1 - Stretch))
		MESSAGE_L("Stretched mode", "Stretched mode", 1000)
	else
		MESSAGE_L("Correct ratio mode", "Correct ratio mode", 1000)

	Build_Main_Menu();
	return(1);
}
*/

int Change_Blit_Style(void)
{
	if ((!Full_Screen) || (Render_FS > 1)) return(0);

	Flag_Clr_Scr = 1;

	if (Blit_Soft = (1 - Blit_Soft))
		MESSAGE_L("Force software blit for Full-Screen", "Force software blit for Full-Screen", 1000)
	else
		MESSAGE_L("Enable hardware blit for Full-Screen", "Enable hardware blit for Full-Screen", 1000)

	return(1);
}


int Set_Sprite_Over(HWND hWnd, int Num)
{
	if (Sprite_Over = Num)
		MESSAGE_L("Sprite Limit Enabled", "Sprite Limit Enabled", 1000)
	else
		MESSAGE_L("Sprite Limit Disabled", "Sprite Limit Disabled", 1000)

	Build_Main_Menu();
	return(1);
}


int Change_Debug(HWND hWnd, int Debug_Mode)
{
	if (!Game) return 0;
		
	Flag_Clr_Scr = 1;
	Clear_Sound_Buffer();

	if (Debug_Mode == Debug) Debug = 0;
	else Debug = Debug_Mode;
	
	Build_Main_Menu();
	return 1;
}


int Change_Fast_Blur(HWND hWnd)
{
	Flag_Clr_Scr = 1;

	if (Fast_Blur = (1 - Fast_Blur))
		MESSAGE_L("Fast Blur Enabled", "Fast Blur Enabled", 1000)
	else
		MESSAGE_L("Fast Blur Disabled", "Fast Blur Disabled", 1000)

	Build_Main_Menu();
	return(1);
}


int Set_Render(HWND hWnd, int Full, int Num, int Force)
{
	int Old_Rend, *Rend;
//	void (**Blit)(unsigned char*, int, int, int, int);

	if (Full)
	{
		Rend = &Render_FS;
		if(Num !=1)
          StretchModelIndex =-1;  
	}
	else
	{
		Rend = &Render_W;
	}

	Old_Rend = *Rend;
	Flag_Clr_Scr = 1;

	switch(Num)
	{
		case -1:
			switch(Old_Rend)
			{
				case 0:

					break;

				case 1:
		
					break;

				case 2:
		
					break;

				case 3:
					break;

				case 4:
			
					break;

				case 5:
	
					break;

				case 6:

					break;

				case 7:
			
					break;

				case 8:

					break;

				case 9:
				
					break;
                case 10:
				
					break;

				default:
			
					break;
			}
			break;

		case 0:
			*Rend = 0;
			break;

		case 1:
			*Rend = 1;
			break;

		case 2:
			*Rend = 2;
		
			break;

		case 3:
			*Rend = 3;

			break;

		case 4:
				*Rend = 4;
			break;

		case 5:
				*Rend = 5;
		
			break;

		case 6:
			*Rend = 6;
		
			break;

		case 7:
				*Rend = 7;
			
			break;

		case 8:

				*Rend = 8;
			
			break;

		case 9:
			
				*Rend = 9;
		
			break;
        case 10:
				*Rend = 10;
		    break;

		default:
			*Rend = 1;
			break;
	}

	if (!((Full == Full_Screen) && ((Num >=2) && (Old_Rend >= 2)) && (!Force)))
	{
		RECT r;

		End_DDraw();

		if (Sound_Initialised) Clear_Sound_Buffer();

		if (Full_Screen = Full)
		{
			while (ShowCursor(true) < 1);
			while (ShowCursor(false) >= 0);

    		SetWindowPos(hWnd, NULL, 0, 0, 320 * ((*Rend == 0)?1:2), 240 * ((*Rend == 0)?1:2), SWP_NOZORDER | SWP_NOACTIVATE);
            
			SetWindowLong(hWnd, GWL_STYLE, NULL);
		}
		else
		{
	
           if(Force32bpp)
		   {
	        DEVMODE dm;
	
			memset(&dm, 0, sizeof(DEVMODE));
			dm.dmSize = sizeof(DEVMODE);
			dm.dmBitsPerPel = 32;
			dm.dmFields = DM_BITSPERPEL;
	
			ChangeDisplaySettings(&dm, 0);
		   }

			while (ShowCursor(false) >= 0);
			while (ShowCursor(true) < 1);

			SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) | WS_OVERLAPPEDWINDOW);
			SetRect(&r, 0, 0, 320 * ((*Rend == 0)?1:2), 240 * ((*Rend == 0)?1:2));
			AdjustWindowRectEx(&r, GetWindowLong(hWnd, GWL_STYLE), 1, GetWindowLong(hWnd, GWL_EXSTYLE));
			SetWindowPos(hWnd, NULL, Window_Pos.x, Window_Pos.y, r.right - r.left, r.bottom - r.top, SWP_NOZORDER | SWP_NOACTIVATE);
	
		}
	
		Build_Main_Menu();
		return(Init_DDraw(HWnd));
	}

	Build_Main_Menu();
	return(1);
}


int Change_SegaCD_Synchro(void)
{
	if (SegaCD_Accurate)
	{
		SegaCD_Accurate = 0;

		if (SegaCD_Started)
		{
			Update_Frame = Do_SegaCD_Frame;
			Update_Frame_Fast = Do_SegaCD_Frame_No_VDP;
		}

		MESSAGE_L("SegaCD normal mode", "SegaCD normal mode", 1500)
	}
	else
	{
		SegaCD_Accurate = 1;

		if (SegaCD_Started)
		{
			Update_Frame = Do_SegaCD_Frame_Cycle_Accurate;
			Update_Frame_Fast = Do_SegaCD_Frame_No_VDP_Cycle_Accurate;
		}

		MESSAGE_L("SegaCD perfect synchro mode (SLOW)", "SegaCD perfect synchro mode (slower)", 1500)
	}

	Build_Main_Menu();
	return 1;
}


int Change_SegaCD_SRAM_Size(int num)
{
	if (num == -1)
	{
		BRAM_Ex_State &= 1;
        NONE_RAM_CARD =true;
		MESSAGE_L("SegaCD SRAM cart removed", "SegaCD SRAM cart removed", 1500)
	}
	else
	{
		char bsize[256];
	
		BRAM_Ex_State |= 0x100;
		BRAM_Ex_Size = num;
        NONE_RAM_CARD =false;
		sprintf(bsize, "SegaCD SRAM cart plugged (%d Kb)", 8 << num);
		MESSAGE_L(bsize, bsize, 1500)
	}

	Build_Main_Menu();
	return 1;
}


int Change_Z80(HWND hWnd)
{
	if (Z80_State & 1)
	{
		Z80_State &= ~1;
		MESSAGE_L("Z80 Disabled", "Z80 Disabled", 1000)
	}
	else
	{
		Z80_State |= 1;
		MESSAGE_L("Z80 Enabled", "Z80 Enabled", 1000)
	}

	Build_Main_Menu();
	return(1);
}


int Change_DAC(HWND hWnd)
{
	if (DAC_Enable)
	{
		DAC_Enable = 0;
		MESSAGE_L("DAC Disabled", "DAC Disabled", 1000)
	}
	else
	{
		DAC_Enable = 1;
		MESSAGE_L("DAC Enabled", "DAC Enabled", 1000)
	}

	Build_Main_Menu();
	return(1);
}


int Change_DAC_Improv(HWND hWnd)
{
	if (DAC_Improv)
	{
		DAC_Improv = 0;
		MESSAGE_L("Normal DAC sound", "Normal DAC sound", 1000)
	}
	else
	{
		DAC_Improv = 1;
		MESSAGE_L("Improved DAC sound (voices)", "Improved DAC sound (voices)", 1000)
	}
	Build_Main_Menu();
	return 1;
}


int Change_YM2612(HWND hWnd)
{
	if (YM2612_Enable)
	{
		YM2612_Enable = 0;
		MESSAGE_L("YM2612 Disabled", "YM2612 Disabled", 1000)
	}
	else
	{
		YM2612_Enable = 1;
		MESSAGE_L("YM2612 Enabled", "YM2612 Enabled", 1000)
	}

	Build_Main_Menu();
	return(1);
}


int Change_YM2612_Improv(HWND hWnd)
{
	unsigned char Reg_1[0x200];

	if (YM2612_Improv)
	{
		YM2612_Improv = 0;
		MESSAGE_L("Normal YM2612 emulation", "Normal YM2612 emulation", 1000)
	}
	else
	{
		YM2612_Improv = 1;
		MESSAGE_L("High Quality YM2612 emulation", "High Quality YM2612 emulation (slower)", 1000)
	}

	YM2612_Save(Reg_1);

	if (CPU_Mode)
	{
		YM2612_Init(CLOCK_PAL / 7, Sound_Rate, YM2612_Improv);
	}
	else
	{
		YM2612_Init(CLOCK_NTSC / 7, Sound_Rate, YM2612_Improv);
	}

	YM2612_Restore(Reg_1);

	Build_Main_Menu();
	return 1;
}


int Change_PSG(HWND hWnd)
{
	if (PSG_Enable)
	{
		PSG_Enable = 0;
		MESSAGE_L("PSG Disabled", "PSG Disabled", 1000)
	}
	else
	{
		PSG_Enable = 1;
		MESSAGE_L("PSG Enabled", "PSG Enabled", 1000)
	}

	Build_Main_Menu();
	return 1;
}


int Change_PSG_Improv(HWND hWnd)
{
	if (PSG_Improv)
	{
		PSG_Improv = 0;
		MESSAGE_L("Normal PSG sound", "Normal PSG sound", 1000)
	}
	else
	{
		PSG_Improv = 1;
		MESSAGE_L("Improved PSG sound", "Improved PSG sound", 1000)
	}
	Build_Main_Menu();
	return 1;
}


int Change_PCM(HWND hWnd)
{
	if (PCM_Enable)
	{
		PCM_Enable = 0;
		MESSAGE_L("PCM Sound Disabled", "PCM Sound Disabled", 1000)
	}
	else
	{
		PCM_Enable = 1;
		MESSAGE_L("PCM Sound Enabled", "PCM Sound Enabled", 1000)
	}

	Build_Main_Menu();
	return 1;
}


int Change_PWM(HWND hWnd)
{
	if (PWM_Enable)
	{
		PWM_Enable = 0;
		MESSAGE_L("PWM Sound Disabled", "PWM Sound Disabled", 1000)
	}
	else
	{
		PWM_Enable = 1;
		MESSAGE_L("PWM Sound Enabled", "PWM Sound Enabled", 1000)
	}

	Build_Main_Menu();
	return 1;
}


int Change_CDDA(HWND hWnd)
{
	if (CDDA_Enable)
	{
		CDDA_Enable = 0;
		MESSAGE_L("CD Audio Sound Disabled", "CD Audio Sound Disabled", 1000)
	}
	else
	{
		CDDA_Enable = 1;
		MESSAGE_L("CD Audio Enabled", "CD Audio Enabled", 1000)
	}

	Build_Main_Menu();
	return(1);
}


int	Change_Sound(HWND hWnd)
{
	if (Sound_Enable)
	{
		End_Sound();

		Sound_Enable = 0;
		YM2612_Enable = 0;
		PSG_Enable = 0;
		DAC_Enable = 0;
		PCM_Enable = 0;
		PWM_Enable = 0;
		CDDA_Enable = 0;

		MESSAGE_L("Sound Disabled", "Sound Disabled", 1500)
	}
	else
	{
		if (!Init_Sound(hWnd))
		{
			Sound_Enable = 0;
			YM2612_Enable = 0;
			PSG_Enable = 0;
			DAC_Enable = 0;
			PCM_Enable = 0;
			PWM_Enable = 0;
			CDDA_Enable = 0;

			return 0;
		}

		Sound_Enable = 1;
		Play_Sound();

		if (!(Z80_State & 1)) Change_Z80(hWnd);

		YM2612_Enable = 1;
		PSG_Enable = 1;
		DAC_Enable = 1;
		PCM_Enable = 1;
		PWM_Enable = 1;
		CDDA_Enable = 1;

		MESSAGE_L("Sound Enabled", "Sound Enabled", 1500)
	}

	Build_Main_Menu();
	return 1;
}


int Change_Sample_Rate(HWND hWnd, int Rate)
{
	unsigned char Reg_1[0x200];

	switch (Rate)
	{
	case 0:
		Sound_Rate = 11025;
		MESSAGE_L("Sound rate set to 11025", "Sound rate set to 11025", 2500)
		break;

	case 1:
		Sound_Rate = 22050;
		MESSAGE_L("Sound rate set to 22050", "Sound rate set to 22050", 2500)
		break;

	case 2:
		Sound_Rate = 44100;
		MESSAGE_L("Sound rate set to 44100", "Sound rate set to 44100", 2500)
		break;

	case 3:
		Sound_Rate = 48000;
		MESSAGE_L("Sound rate set to 48000", "Sound rate set to 48000", 2500)
		break;
	}

	if (Sound_Enable)
	{
		PSG_Save_State();
		YM2612_Save(Reg_1);

		End_Sound();
		Sound_Enable = 0;

		if (CPU_Mode)
		{
			YM2612_Init(CLOCK_PAL / 7, Sound_Rate, YM2612_Improv);
			PSG_Init(CLOCK_PAL / 15, Sound_Rate);
		}
		else
		{
			YM2612_Init(CLOCK_NTSC / 7, Sound_Rate, YM2612_Improv);
			PSG_Init(CLOCK_NTSC / 15, Sound_Rate);
		}

		if (SegaCD_Started) Set_Rate_PCM(Sound_Rate);
		YM2612_Restore(Reg_1);
		PSG_Restore_State();
		
		if(!Init_Sound(hWnd)) return(0);

		Sound_Enable = 1;
		Play_Sound();
	}
	
	Build_Main_Menu();
	return(1);
}


int Change_Sound_Stereo(HWND hWnd)
{
	unsigned char Reg_1[0x200];

	if (Sound_Stereo)
	{
		Sound_Stereo = 0;
		MESSAGE_L("Mono sound", "Mono sound", 1000)
	}
	else
	{
		Sound_Stereo = 1;
		MESSAGE_L("Stereo sound", "Stereo sound", 1000)
	}

	if (Sound_Enable)
	{
		PSG_Save_State();
		YM2612_Save(Reg_1);

		End_Sound();
		Sound_Enable = 0;

		if (CPU_Mode)
		{
			YM2612_Init(CLOCK_PAL / 7, Sound_Rate, YM2612_Improv);
			PSG_Init(CLOCK_PAL / 15, Sound_Rate);
		}
		else
		{
			YM2612_Init(CLOCK_NTSC / 7, Sound_Rate, YM2612_Improv);
			PSG_Init(CLOCK_NTSC / 15, Sound_Rate);
		}

		if (SegaCD_Started) Set_Rate_PCM(Sound_Rate);
		YM2612_Restore(Reg_1);
		PSG_Restore_State();
		
		if(!Init_Sound(hWnd)) return(0);

		Sound_Enable = 1;
		Play_Sound();
	}

	Build_Main_Menu();
	return(1);
}


int Change_Country(HWND hWnd, int Num)
{
	unsigned char Reg_1[0x200];

	Flag_Clr_Scr = 1;

	switch(Country = Num)
	{
		default:
		case -1:
			if (Genesis_Started || _32X_Started) Detect_Country_Genesis();
			else if (SegaCD_Started) Detect_Country_SegaCD();
			break;

		case 0:
			Game_Mode = 0;
			CPU_Mode = 0;
			break;

		case 1:
			Game_Mode = 1;
			CPU_Mode = 0;
			break;

		case 2:
			Game_Mode = 1;
			CPU_Mode = 1;
			break;

		case 3:
			Game_Mode = 0;
			CPU_Mode = 1;
			break;
	}

	if (CPU_Mode)
	{
		CPL_Z80 = Round_Double((((double) CLOCK_PAL / 15.0) / 50.0) / 312.0);
		CPL_M68K = Round_Double((((double) CLOCK_PAL / 7.0) / 50.0) / 312.0);
		CPL_MSH2 = Round_Double(((((((double) CLOCK_PAL / 7.0) * 3.0) / 50.0) / 312.0) * (double) MSH2_Speed) / 100.0);
		CPL_SSH2 = Round_Double(((((((double) CLOCK_PAL / 7.0) * 3.0) / 50.0) / 312.0) * (double) SSH2_Speed) / 100.0);

		VDP_Num_Lines = 312;
		VDP_Status |= 0x0001;
		_32X_VDP.Mode &= ~0x8000;

		CD_Access_Timer = 2080;
		Timer_Step = 136752;
	}
	else
	{
		CPL_Z80 = Round_Double((((double) CLOCK_NTSC / 15.0) / 60.0) / 262.0);
		CPL_M68K = Round_Double((((double) CLOCK_NTSC / 7.0) / 60.0) / 262.0);
		CPL_MSH2 = Round_Double(((((((double) CLOCK_NTSC / 7.0) * 3.0) / 60.0) / 262.0) * (double) MSH2_Speed) / 100.0);
		CPL_SSH2 = Round_Double(((((((double) CLOCK_NTSC / 7.0) * 3.0) / 60.0) / 262.0) * (double) SSH2_Speed) / 100.0);

		VDP_Num_Lines = 262;
		VDP_Status &= 0xFFFE;
		_32X_VDP.Mode |= 0x8000;

		CD_Access_Timer = 2096;
		Timer_Step = 135708;
	}

	if (Sound_Enable)
	{
		PSG_Save_State();
		YM2612_Save(Reg_1);

		End_Sound();
		Sound_Enable = 0;

		if (CPU_Mode)
		{
			YM2612_Init(CLOCK_PAL / 7, Sound_Rate, YM2612_Improv);
			PSG_Init(CLOCK_PAL / 15, Sound_Rate);
		}
		else
		{
			YM2612_Init(CLOCK_NTSC / 7, Sound_Rate, YM2612_Improv);
			PSG_Init(CLOCK_NTSC / 15, Sound_Rate);
		}

		if (SegaCD_Started) Set_Rate_PCM(Sound_Rate);
		YM2612_Restore(Reg_1);
		PSG_Restore_State();
		
		if(!Init_Sound(hWnd)) return(0);

		Sound_Enable = 1;
		Play_Sound();
	}

	if (Game_Mode)
	{
		if (CPU_Mode) MESSAGE_L("Europe system (50 FPS)", "Europe system (50 FPS)", 1500)
		else MESSAGE_L("USA system (60 FPS)", "USA system (60 FPS)", 1500)
	}
	else
	{
		if (CPU_Mode) MESSAGE_L("Japan system (50 FPS)", "Japan system (50 FPS)", 1500)
		else MESSAGE_L("Japan system (60 FPS)", "Japan system (60 FPS)", 1500)
	}

	if (Genesis_Started)
	{
		if ((CPU_Mode == 1) || (Game_Mode == 0))
			sprintf(Str_Tmp, "Gens - Megadrive : %s", Game->Rom_Name_W);
		else
			sprintf(Str_Tmp, "Gens - Genesis : %s", Game->Rom_Name_W);

		SetWindowText(HWnd, Str_Tmp);
	}
	else if (_32X_Started)
	{
		if (CPU_Mode == 1)
			sprintf(Str_Tmp, "Gens - 32X (PAL) : %s", Game->Rom_Name_W);
		else
			sprintf(Str_Tmp, "Gens - 32X (NTSC) : %s", Game->Rom_Name_W);

		SetWindowText(HWnd, Str_Tmp);
	}
	else if (SegaCD_Started)
	{
		if ((CPU_Mode == 1) || (Game_Mode == 0))
			sprintf(Str_Tmp, "Gens - MegaCD : %s", Rom_Name);
		else
			sprintf(Str_Tmp, "Gens - SegaCD : %s", Rom_Name);

		SetWindowText(HWnd, Str_Tmp);
	}

	Build_Main_Menu();
	return 1;
}


int Change_Country_Order(int Num)
{
	char c_str[4][4] = {"USA", "JAP", "EUR"};
	char str_w[128];
	int sav = Country_Order[Num];
		
	if (Num == 1) Country_Order[1] = Country_Order[0];
	else if (Num == 2)
	{
		Country_Order[2] = Country_Order[1];
		Country_Order[1] = Country_Order[0];
	}
	Country_Order[0] = sav;

	if (Country == -1) Change_Country(HWnd, -1);		// Update Country

	wsprintf(str_w, "Country detec.order : %s %s %s", c_str[Country_Order[0]], c_str[Country_Order[1]], c_str[Country_Order[2]]);
	MESSAGE_L(str_w, str_w, 1500)

	Build_Main_Menu();
	return(1);
}


int Check_If_Kaillera_Running(void)
{
	if (Kaillera_Client_Running)
	{
		if (Sound_Initialised) Clear_Sound_Buffer();
		MessageBox(HWnd, "You can't do it during netplay, you have to close rom and kaillera client before", "info", MB_OK);
		return 1;
	}

	return 0;
}


int WINAPI Play_Net_Game(char *game, int player, int maxplayers)
{
	MSG msg;
	char name[2048];
	HANDLE f;
	WIN32_FIND_DATA fd;

	SetCurrentDirectory(Rom_Dir);

	sprintf(name, "%s.*", game);
	memset(&fd, 0, sizeof(fd));
	fd.dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
	f = FindFirstFile(name, &fd);

	if (f == INVALID_HANDLE_VALUE) return 1;

	if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
	{
		sprintf(name, "%s%s", Rom_Dir, fd.cFileName);
		Pre_Load_Rom(HWnd, name);
	}

	if ((!Genesis_Started) && (!_32X_Started)) return 1;
	
	Net_Play = 1;
	SetFocus(HWnd);

	if (maxplayers > 4) maxplayers = 4;
	if (player > 4) player = 0;

	Controller_1_Type &= 0xF;
	Controller_2_Type &= 0xF;
	if (maxplayers > 2)
	{
		Controller_1_Type |= 0x10;
		Make_IO_Table();
	}

	Kaillera_Keys[0] = Kaillera_Keys[1] = Kaillera_Keys[2] = Kaillera_Keys[3] = 0xFF;
	Kaillera_Keys[4] = Kaillera_Keys[5] = Kaillera_Keys[6] = Kaillera_Keys[7] = 0xFF;
	Kaillera_Keys[8] = Kaillera_Keys[9] = 0xFF;
	Kaillera_Error = 0;

	while (Net_Play)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if (!GetMessage(&msg, NULL, 0, 0)) return msg.wParam;
			if (!TranslateAccelerator (HWnd, hAccelTable, &msg))
			{
				TranslateMessage(&msg); 
				DispatchMessage(&msg);
			}
		}
		else if ((Active) && (!Paused))
		{
			Update_Emulation_Netplay(HWnd, player, maxplayers);
		}
		else
		{
			Flip(HWnd);
			Sleep(100);
		}
	}

	Kaillera_End_Game();

	return 0;
}


int Start_Netplay(void)
{
	kailleraInfos K_Infos;
	char name[2048];
	char *Liste_Games = NULL, *LG = NULL, *Min_Game, *Cur_Game;
	int cursize = 8192, num = 0, dep = 0, len;
	int backup_infos[32];
	HANDLE f;
	WIN32_FIND_DATA fd;

	if (Kaillera_Initialised == 0)
	{
		MessageBox(HWnd, "You need the KAILLERACLIENT.DLL file to enable this feature", "Info", MB_OK);
		return 0;
	}
		
	if (Kaillera_Client_Running) return 0;

	SetCurrentDirectory(Rom_Dir);

	Liste_Games = (char *) malloc(cursize);
	Liste_Games[0] = 0;
	Liste_Games[1] = 0;

	memset(&fd, 0, sizeof(fd));
	fd.dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
	f = FindFirstFile("*.bin", &fd);
	if (f != INVALID_HANDLE_VALUE)
	{
		if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			len = strlen(fd.cFileName) - 4;
			fd.cFileName[len++] = 0;
			if ((dep + len) > cursize)
			{
				cursize += 8192;
				Liste_Games = (char*) realloc(Liste_Games, cursize);
			}
			strcpy(Liste_Games + dep, fd.cFileName);
			dep += len;
			num++;
		}

		while (FindNextFile(f, &fd))
		{
			if(!(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
			{
				len = strlen(fd.cFileName) - 4;
				fd.cFileName[len++] = 0;
				if ((dep + len) > cursize)
				{
					cursize += 8192;
					Liste_Games = (char*) realloc(Liste_Games, cursize);
				}
				strcpy(Liste_Games + dep, fd.cFileName);
				dep += len;
				num++;
			}
		}
	}

	memset(&fd, 0, sizeof(fd));
	fd.dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
	f = FindFirstFile("*.smd", &fd);
	if (f != INVALID_HANDLE_VALUE)
	{
		if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			len = strlen(fd.cFileName) - 4;
			fd.cFileName[len++] = 0;
			if ((dep + len) > cursize)
			{
				cursize += 8192;
				Liste_Games = (char*) realloc(Liste_Games, cursize);
			}
			strcpy(Liste_Games + dep, fd.cFileName);
			dep += len;
			num++;
		}

		while (FindNextFile(f, &fd))
		{
			if(!(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
			{
				len = strlen(fd.cFileName) - 4;
				fd.cFileName[len++] = 0;
				if ((dep + len) > cursize)
				{
					cursize += 8192;
					Liste_Games = (char*) realloc(Liste_Games, cursize);
				}
				strcpy(Liste_Games + dep, fd.cFileName);
				dep += len;
				num++;
			}
		}
	}

	memset(&fd, 0, sizeof(fd));
	fd.dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
	f = FindFirstFile("*.32X", &fd);
	if (f != INVALID_HANDLE_VALUE)
	{
		if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			len = strlen(fd.cFileName) - 4;
			fd.cFileName[len++] = 0;
			if ((dep + len) > cursize)
			{
				cursize += 8192;
				Liste_Games = (char*) realloc(Liste_Games, cursize);
			}
			strcpy(Liste_Games + dep, fd.cFileName);
			dep += len;
			num++;
		}

		while (FindNextFile(f, &fd))
		{
			if(!(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
			{
				len = strlen(fd.cFileName) - 4;
				fd.cFileName[len++] = 0;
				if ((dep + len) > cursize)
				{
					cursize += 8192;
					Liste_Games = (char*) realloc(Liste_Games, cursize);
				}
				strcpy(Liste_Games + dep, fd.cFileName);
				dep += len;
				num++;
			}
		}
	}

	memset(&fd, 0, sizeof(fd));
	fd.dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
	f = FindFirstFile("*.zip", &fd);
	if (f != INVALID_HANDLE_VALUE)
	{
		if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			len = strlen(fd.cFileName) - 4;
			fd.cFileName[len++] = 0;
			if ((dep + len) > cursize)
			{
				cursize += 8192;
				Liste_Games = (char*) realloc(Liste_Games, cursize);
			}
			strcpy(Liste_Games + dep, fd.cFileName);
			dep += len;
			num++;
		}

		while (FindNextFile(f, &fd))
		{
			if(!(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
			{
				len = strlen(fd.cFileName) - 4;
				fd.cFileName[len++] = 0;
				if ((dep + len) > cursize)
				{
					cursize += 8192;
					Liste_Games = (char*) realloc(Liste_Games, cursize);
				}
				strcpy(Liste_Games + dep, fd.cFileName);
				dep += len;
				num++;
			}
		}
	}

	memset(&fd, 0, sizeof(fd));
	fd.dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
	f = FindFirstFile("*.tif", &fd);
	if (f != INVALID_HANDLE_VALUE)
	{
		if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
		{
			len = strlen(fd.cFileName) - 4;
			fd.cFileName[len++] = 0;
			if ((dep + len) > cursize)
			{
				cursize += 8192;
				Liste_Games = (char*) realloc(Liste_Games, cursize);
			}
			strcpy(Liste_Games + dep, fd.cFileName);
			dep += len;
			num++;
		}

		while (FindNextFile(f, &fd))
		{
			if(!(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
			{
				len = strlen(fd.cFileName) - 4;
				fd.cFileName[len++] = 0;
				if ((dep + len) > cursize)
				{
					cursize += 8192;
					Liste_Games = (char*) realloc(Liste_Games, cursize);
				}
				strcpy(Liste_Games + dep, fd.cFileName);
				dep += len;
				num++;
			}
		}
	}

	Liste_Games[dep] = 0;
	LG = (char*) malloc(dep);
	dep = 0;
	
	for(; num > 0; num--)
	{
		Min_Game = Cur_Game = Liste_Games;

		while(*Cur_Game)
		{
			if (stricmp(Cur_Game, Min_Game) < 0) Min_Game = Cur_Game; 
			Cur_Game += strlen(Cur_Game) + 1;
		}

		strlwr(Min_Game);
		strcpy(LG + dep, Min_Game);
		dep += strlen(Min_Game) + 1;
		Min_Game[0] = -1;
	}

	GetWindowText(HWnd, name, 2046);
	backup_infos[0] = Controller_1_Type;
	backup_infos[1] = Controller_2_Type;

	memset(&K_Infos, 0, sizeof(K_Infos));
	
	K_Infos.appName = "Gens32";
	K_Infos.gameList = LG;
	K_Infos.gameCallback = Play_Net_Game;

//	K_Infos.chatReceivedCallback = NULL;
//	K_Infos.clientDroppedCallback = NULL;
//	K_Infos.moreInfosCallback = NULL;

	Kaillera_Set_Infos(&K_Infos);

	Kaillera_Client_Running = 1;
	Kaillera_Select_Server_Dialog(NULL);
	Kaillera_Client_Running = 0;

	Controller_1_Type = backup_infos[0];
	Controller_2_Type = backup_infos[1];
	Make_IO_Table();
	SetWindowText(HWnd, name);

	free(Liste_Games);
	free(LG);

	return 1;
}

 
BOOL Init(HINSTANCE hInst, int nCmdShow)
{
	int i;
	
	Net_Play = 0;
	Full_Screen = -1;
	VDP_Num_Vis_Lines = 224;
	Resolution = 1;
	W_VSync = 0;
	FS_VSync = 0;
	Stretch = 0;
	Sprite_Over = 1;
	Render_W = 0;
	Render_FS = 0;
	Show_Message = 1;

	Sound_Enable = 0;
	Sound_Segs = 8;
	Sound_Stereo = 1;
	Sound_Initialised = 0;
	Sound_Is_Playing = 0;
	WAV_Dumping = 0;
	GYM_Dumping = 0;

	FS_Minimised = 0;
	Game = NULL;
	Genesis_Started = 0;
	SegaCD_Started = 0;
	_32X_Started = 0;
	Debug = 0;
	CPU_Mode = 0;
	Window_Pos.x = 0;
	Window_Pos.y = 0;
    Window_Width =640;
    Window_Height =480;

	WndClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	WndClass.lpfnWndProc = WinProc;
	WndClass.cbClsExtra = 0;
	WndClass.cbWndExtra = 0;
	WndClass.hInstance = hInst;
	WndClass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_SONIC));
	WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	WndClass.hbrBackground = NULL;
	WndClass.lpszMenuName = NULL;
	WndClass.lpszClassName = "Gens";

	RegisterClass(&WndClass);

	ghInstance = hInst;

	HWnd = CreateWindowEx(
		NULL,
		"Gens",
		"Gens32 - Idle",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		320 * 2,
		240 * 2,
		NULL,
		NULL,
		hInst,
		NULL);

	if (!HWnd) return FALSE;

	hAccelTable = LoadAccelerators(hInst, MAKEINTRESOURCE(RAC));
  
	Identify_CPU();

	i = GetVersion();
 
	// Get major and minor version numbers of Windows

	if (((i & 0xFF) > 4) || (i & 0x80000000)) WinNT_Flag = 0;
	else WinNT_Flag = 1;

	GetCurrentDirectory(1024, Gens_Path);
	GetCurrentDirectory(1024, Language_Path);
	GetCurrentDirectory(1024, Str_Tmp);
	strcpy(Manual_Path, "");
	strcpy(CGOffline_Path, "");

	strcat(Gens_Path, "\\");
	strcat(Language_Path, "\\language.dat");
	strcat(Str_Tmp, "\\gens.cfg");

	Load_Config(Str_Tmp, NULL);

	MSH2_Init();
	SSH2_Init();
	M68K_Init();
	S68K_Init();
	Z80_Init();

	YM2612_Init(CLOCK_NTSC / 7, Sound_Rate, YM2612_Improv);
	PSG_Init(CLOCK_NTSC / 15, Sound_Rate);
	PWM_Init();

//  ShowWindow(HWnd, nCmdShow);

//Regester drop files; 
	DragAcceptFiles(HWnd,TRUE);

	if (!Init_Input(hInst, HWnd))
	{
		End_Sound();
		End_DDraw();
		return FALSE;
	}

	Init_CD_Driver();
	Init_Network();
	Init_Tab();
	Build_Main_Menu();

	SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &SS_Actived, 0);
	SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);

//	SetThreadPriority(hInst, THREAD_PRIORITY_HIGHEST);
//	SetWindowPos(HWnd, NULL, Window_Pos.x, Window_Pos.y, Window_Width, Window_Height, SWP_NOZORDER | SWP_NOACTIVATE);
    SetWindowPos(HWnd, NULL, Window_Pos.x, Window_Pos.y, Window_Width, Window_Height, SWP_SHOWWINDOW);

	Gens_Running = 1;

	return TRUE;
}


void End_All(void)
{
	End_DDraw();
	End_Input();
	YM2612_End();
	End_Sound();
	End_CD_Driver();
	End_Network();

	SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, SS_Actived, NULL, 0);
}



int PASCAL WinMain(HINSTANCE hInst,	HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;

	Init(hInst, nCmdShow);
    //Check Supported format;
    //Check 7z;
     char FormatBuffer[1024];
	 strcpy(FormatBuffer,Gens_Path);
     strcat(FormatBuffer,"Temp\\7za.exe");
     FILE *file =fopen(FormatBuffer,"rb");
	 if(file != NULL)
	 {
       Support_7z =true;
	   fclose(file);
//Write "7z.bat";
     strcpy(FormatBuffer,Gens_Path);
     strcat(FormatBuffer,"7z.dat");
//     strcat(FormatBuffer,"Temp\\7z.bat");
	 //Chack 7z.bat;
	 file =fopen(FormatBuffer,"rt");
	 if(file ==NULL)
	 {
     file =fopen(FormatBuffer,"wt+");
	 fprintf(file,"%s","7za.exe e Rom.7z *.bin");
	 fprintf(file,"\n%s","7za.exe e Rom.7z *.smd");
     fprintf(file,"\n%s","7za.exe e Rom.7z *.32X");
	 fprintf(file,"\n%s","7za.exe e Rom.7z *.gen");

     fprintf(file,"\n%s","ren *.bin Rom.dat");
	 fprintf(file,"\n%s","ren *.smd Rom.dat");
     fprintf(file,"\n%s","ren *.32X Rom.dat");
	 fprintf(file,"\n%s","ren *.gen Rom.dat");
    
     fprintf(file,"\n%s","del *.bin");
     fprintf(file,"\n%s","del *.smd");
     fprintf(file,"\n%s","del *.32X");
     fprintf(file,"\n%s","del *.gen");
	 }
	 fclose(file);
//Make 7z.bat;
	 SetCurrentDirectory(Gens_Path);
	 CopyFile(FormatBuffer,"Temp//7z.bat",TRUE);
	 }
	 else
         Support_7z =false;

//Init Game Name System;
FILE *GameName =fopen("GameName.adb","rb");
if(GameName ==NULL)
{
GameName =fopen("GameName.adb","wb+");
//Format GameName.dat;
fwrite(&GameID_ADB[0],2,10000,GameName);
fwrite(&DataBuffer_ADB[0],1,49*10000,GameName);
fclose(GameName);
}
else
{
//Load Game name data;
fread(&GameID_ADB[0],2,10000,GameName);
fread(&DataBuffer_ADB[0],1,49*10000,GameName);
fclose(GameName);
}

//Play Welcom Music;
PlayWelcomeMusic();

	// Have to do it *before* load by command line
	Init_Genesis_Bios();

	if (lpCmdLine[0])
	{
		int src;

#ifdef CC_SUPPORT
//		src = CC_Connect("CCGEN://Stef:gens@emu.consoleclassix.com/sonicthehedgehog2.gen", (char *) Rom_Data, CC_End_Callback);
		src = CC_Connect(lpCmdLine, (char *) Rom_Data, CC_End_Callback);

		if (src == 0)
		{
			Load_Rom_CC(CCRom.RName, CCRom.RSize);
			Build_Main_Menu();
		}
		else if (src == 1)
		{
			MessageBox(NULL, "Error during connection", NULL, MB_OK);
		}
		else if (src == 2)
		{
#endif
		src = 0;
		
		if (lpCmdLine[src] == '"')
		{
			src++;
			
			while ((lpCmdLine[src] != '"') && (lpCmdLine[src] != 0))
			{
				Str_Tmp[src - 1] = lpCmdLine[src];
				src++;
			}

			Str_Tmp[src - 1] = 0;
		}
		else
		{
			while (lpCmdLine[src] != 0)
			{
				Str_Tmp[src] = lpCmdLine[src];
				src++;
			}

			Str_Tmp[src] = 0;
		}

		Pre_Load_Rom(HWnd, Str_Tmp);

#ifdef CC_SUPPORT
		}
#endif
	}
Logo_bitmap =(HBITMAP)::LoadImage(NULL,"Painting.bmp",IMAGE_BITMAP,NULL,NULL,LR_LOADFROMFILE);
	while (Gens_Running)
	{
		while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if (!GetMessage(&msg, NULL, 0, 0)) Gens_Running = 0;
			if (!TranslateAccelerator (HWnd, hAccelTable, &msg))
			{
				TranslateMessage(&msg); 
				DispatchMessage(&msg);
			}
		}

#ifdef GENS_DEBUG
		if (Debug)						// DEBUG
		{
			Update_Debug_Screen();
			Flip(HWnd);
		}
		else
#endif
		if (Genesis_Started || _32X_Started || SegaCD_Started)
		{
			if ((Active) && (!Paused))	// EMULATION
			{
				Update_Emulation(HWnd);
			}
			else						// EMULATION PAUSED
			{
				Flip(HWnd);
				Sleep(100);
			}
		}
		else if (GYM_Playing)			// PLAY GYM
		{
			Play_GYM();
			Update_Gens_Logo(HWnd);
		}
		else if (Intro_Style == 1)		// GENS LOGO EFFECT
		{
			Update_Gens_Logo(HWnd);
			Sleep(5);
		}
		else if (Intro_Style == 2)		// STRANGE EFFECT
		{
//			Update_Crazy_Effect(HWnd);
			Sleep(10);
		}
		else if (Intro_Style == 3)		// GENESIS BIOS
		{
			Do_Genesis_Frame();
			Flip(HWnd);
			Sleep(20);
		}
		else							// BLANK SCREEN (MAX IDLE)
		{
			Clear_Back_Screen(HWnd);
			Flip(HWnd);
			Sleep(200);
		}
     
	}

	strcpy(Str_Tmp, Gens_Path);
	strcat(Str_Tmp, "Gens.cfg");
	Save_Config(Str_Tmp);

	End_All();
    DeleteObject(Logo_bitmap);
	ChangeDisplaySettings(NULL, 0);

	DestroyWindow(HWnd);

	while(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
	{
		if (!GetMessage(&msg, NULL, 0, 0)) return msg.wParam;
	}

	return 0;
}

long PASCAL WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	int i =0;

	switch(message)
	{
		case WM_ACTIVATE:
			if (Gens_Running == 0) break;

			if (LOWORD(wParam) != WA_INACTIVE)
			{
				Active = 1;

				if (FS_Minimised)
				{
					FS_Minimised = 0;
					Set_Render(hWnd, 1, -1, true);
				}
			}
			else
			{
				if ((Full_Screen) && ((BOOL) HIWORD(wParam)) && (Active))
				{
					Set_Render(hWnd, 0, -1, false);
					FS_Minimised = 1;
				}

				if (Auto_Pause && Active)
				{
					Active = 0;

					if (!Paused) Pause_Screen();
					Clear_Sound_Buffer();
				}
			}
			break;

	    case WM_MENUSELECT:
 		case WM_ENTERSIZEMOVE:
			Clear_Sound_Buffer();
			break;

 		case WM_EXITSIZEMOVE:
			if (!Full_Screen)
			{
				GetWindowRect(HWnd, &r);
				Window_Pos.x = r.left;
				Window_Pos.y = r.top;
  //              Window_Width =r.right-r.left;
  //              Window_Height =r.bottom-r.top;
			}
			break;

		case WM_CLOSE:
			if ((Check_If_Kaillera_Running())) return 0;
            Free_Rom(Game);
			Gens_Running = 0;
		    Save_GameNameData();
        	if (!Full_Screen)
			{
				GetWindowRect(HWnd, &r);
				Window_Pos.x = r.left;
				Window_Pos.y = r.top;

                Window_Width =r.right-r.left;
                Window_Height =r.bottom-r.top;
			}
			return 0;
		
		case WM_RBUTTONDOWN:
			if (Full_Screen)
			{
				Clear_Sound_Buffer();
				SetCursorPos(40, 30);
				while (ShowCursor(false) >= 0);
				while (ShowCursor(true) < 0);
				Restore_Primary();
				TrackPopupMenu(Gens_Menu, TPM_LEFTALIGN | TPM_TOPALIGN, 20, 20, NULL, hWnd, NULL);
				while (ShowCursor(true) < 0);
				while (ShowCursor(false) >= 0);
			}
			break;

		case WM_CREATE:
			Active = 1;
			break;
		
		case WM_PAINT:
	        if(!Game)
            Update_Crazy_Effect(HWnd);
			else
			{
			Clear_Primary_Screen(HWnd);
			Flip(hWnd);
			}
			break;
		case WM_DROPFILES:
            if ((Check_If_Kaillera_Running())) return 0;
            hDrop =(HDROP)wParam;
            DragQueryFile(hDrop,0,Str_Tmp,MAX_PATH);

            Pre_Load_Rom(HWnd,Str_Tmp);
	    	return 0;

          case WM_MOUSEMOVE:
               if(wParam ==MK_CONTROL)
			   {
				   point =MAKEPOINTS(lParam);
				   SetVol(point);
			   }
		       return 0;

		case WM_COMMAND:
			if ((LOWORD(wParam) >= ID_HELP_LANG) && (LOWORD(wParam) < ID_HELP_LANG + 50))
			{
				Language = LOWORD(wParam) - ID_HELP_LANG;
				Build_Main_Menu();
				return 0;
			}
			else switch(LOWORD(wParam))
			{
				case ID_FILES_QUIT:
					PostMessage(hWnd, WM_CLOSE, 0, 0);
					return 0;

				case ID_FILES_OPENROM:
					if ((Check_If_Kaillera_Running())) return 0;
					MINIMIZE
					if (GYM_Playing) Stop_Play_GYM();
					return Get_Rom(hWnd);
				case ID_FILES_OPENRECENTROM0:
				case ID_FILES_OPENRECENTROM1:
				case ID_FILES_OPENRECENTROM2:
				case ID_FILES_OPENRECENTROM3:
				case ID_FILES_OPENRECENTROM4:
				case ID_FILES_OPENRECENTROM5:
				case ID_FILES_OPENRECENTROM6:
				case ID_FILES_OPENRECENTROM7:
				case ID_FILES_OPENRECENTROM8:

					if ((Check_If_Kaillera_Running())) return 0;
					if (GYM_Playing) Stop_Play_GYM();
					return Pre_Load_Rom(HWnd, Recent_Rom[LOWORD(wParam) - ID_FILES_OPENRECENTROM0]);

				case ID_FILES_BOOTCD:
					if (Num_CD_Drive == 0) return 1;
					if (Check_If_Kaillera_Running()) return 0;
					if (GYM_Playing) Stop_Play_GYM();
					Free_Rom(Game);			// Don't forget it !
					SegaCD_Started = Init_SegaCD(NULL);
					Build_Main_Menu();
					return SegaCD_Started;

				case ID_FILES_OPENCLOSECD:
					if (SegaCD_Started) Change_CD();
					return 0;

				case ID_FILES_NETPLAY:
					MINIMIZE
					if (GYM_Playing) Stop_Play_GYM();
					Start_Netplay();
					return 0;

				case ID_FILES_CLOSEROM:
					if (Sound_Initialised) Clear_Sound_Buffer();
					Debug = 0;
					if (Net_Play)
					{
						if (Full_Screen) Set_Render(hWnd, 0, -1, true);
					}
					Free_Rom(Game);
					Build_Main_Menu();
					return 0;

				case Translation_LoadDate:
                    if ((Check_If_Kaillera_Running())) return 0;
					MINIMIZE
                    Load_TranslationData(hWnd);
                    return 0;

	            case Translation_SaveDate:
                    if ((Check_If_Kaillera_Running())) return 0;
					MINIMIZE
                    Save_TranslationData(hWnd);
                    return 0;

		        case ID_Meida_REMOVE:
                      MediaMode =false;
                      Media_Stop();
                      Build_Main_Menu();
                     return 0;

				case ID_Meida_STOP:
                      Media_Stop();
                    return 0;

				case ID_Meida_PAUSE:
                     if(MediaPause)
					 {
				       Media_Pause(false);
                       MediaPause =false;
					 }
					 else
					 {
                       Media_Pause(true);
                       MediaPause =true;
					 }
                    Build_Main_Menu();
					return 0;
                   
				case ID_Meida_REPLAY:
                     Media_Replay();
					return 0;

				case ID_FILES_GAMEGENIE:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					DialogBox(ghInstance, MAKEINTRESOURCE(IDD_GAMEGENIE), hWnd, (DLGPROC) GGenieProc);
					Build_Main_Menu();
					return 0;

             	case ID_WRITE_GAMEINFO:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					ShowRomInfo();
					Build_Main_Menu();
					return 0;

 	            case IDD_SetGameNameDisplayed:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
                	DialogBox(ghInstance, MAKEINTRESOURCE(IDD_SETGAMENAME), hWnd, (DLGPROC) SetGameNameProc);
					Build_Main_Menu();
					return 0;

                case Input_Translation:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
                    if(!TranslationMode)return 0;

                	DialogBox(ghInstance, MAKEINTRESOURCE(IDD_TranslateSystem), hWnd, (DLGPROC) InputTranslationProc);
					Build_Main_Menu();
					return 0;

                case TRANSLATION_EDIT:
	                if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
                    if(!TranslationMode)return 0;
                	DialogBox(ghInstance, MAKEINTRESOURCE(IDD_EDIT_TRANSLATION), hWnd, (DLGPROC) TranslationEdit);
					Build_Main_Menu();
					return 0;

                case Import_AVD:
					 MINIMIZE
                     ImportAVD(hWnd);
					return 0;

                case Set_Valid_Area:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
                	DialogBox(ghInstance, MAKEINTRESOURCE(IDD_Set_Valid_Area), hWnd, (DLGPROC) SetValidAreaProc);
					Build_Main_Menu();
					return 0;

                 case Translate_Mode_Set:
                    if(TranslationMode)
					{
                      TranslationMode =false;
                      SetRect(&Check_Area,0,0,320,240);
	                  MESSAGE_L("Translation Mode Disabled", "Translation Mode Disabled", 1500)
                    }  
				    else
					{
                     TranslationMode =true;
                     MESSAGE_L("Translation Mode Enabled", "Translation Mode Enabled", 1500)
					}
                    Init_DDraw(hWnd);
                    Build_Main_Menu();
                     return 0;

                case ID_DisplayArea:
                    if(DisplayValidArea)
					{
                      DisplayValidArea =false;
	                  MESSAGE_L("Display Valid Area Disabled", "Display Valid Area Disabled", 1500)
                    }  
				    else
					{
                     DisplayValidArea =true;
                     MESSAGE_L("Display Valid Area Enabled", "Display Valid Area Enabled", 1500)
					}
                    Build_Main_Menu();
					return 0;

                case ID_STOPMOVIEPLAYING:
                     if(MoviePlaying)
					 {
                     MoviePlaying =false;
					 fclose(MovieFile);
                     //Get Back config;
                     Load_Config("Conf.bak",Game);
					 MESSAGE_L("GIM Stoped", "GIM Stoped", 1500)
					 }
                     Build_Main_Menu();
                     return 0;

                case ID_INPUTMOVIE:
					if(MovieRecord)
					{
                      MovieRecord =false;
					  fclose(MovieFile);
                      MESSAGE_L("Stop GIM Record", "Stop GIM Record", 1500)
					}
					else
					{
                    if(!Game)return 0;
//				    if (Check_If_Kaillera_Running()) return 0;
					Str_Tmp[0] = 0;
					Get_State_File_Name(Str_Tmp);
                    //Set Movie File Name;
                    Str_Tmp[strlen(Str_Tmp)-3] ='g';
                    Str_Tmp[strlen(Str_Tmp)-2] ='i';
                    Str_Tmp[strlen(Str_Tmp)-1] ='m';

                    MovieRecord =true;
					Save_State(Str_Tmp);
					MovieFile =fopen(Str_Tmp,"ab");
					//Write currens info;
                    MovieInfo[0] =GameID;
                    MovieInfo[1] =Controller_1_Type;
                    MovieInfo[2] =Controller_2_Type;
					fwrite(&MovieInfo[0],2,10,MovieFile);
                    MESSAGE_L("Start GIM Record", "Start GIM Record", 1500)
					}
                	Build_Main_Menu();
                    return 0;

                case Export_RAM_Carts:
					 MINIMIZE
                     Export_BRAM(HWnd);
                     Build_Main_Menu();
					 return 0;

                case Inport_RAM_Carts:
	                 MINIMIZE
                     Inport_BRAM(HWnd);
                     Build_Main_Menu();
					return 0;

				case ID_FILES_LOADSTATE:
					if (Check_If_Kaillera_Running()) return 0;
					Str_Tmp[0] = 0;
					Get_State_File_Name(Str_Tmp);
					Load_State(Str_Tmp);
					return 0;

				case ID_FILES_LOADSTATEAS:
					if (Check_If_Kaillera_Running()) return 0;
					Str_Tmp[0] = 0;
					Change_File_L(Str_Tmp, State_Dir, "Load state", "State Files\0*.gs*\0All Files\0*.*\0\0", "");
					Load_State(Str_Tmp);
					return 0;

				case ID_FILES_SAVESTATE:
					if (Check_If_Kaillera_Running()) return 0;
					Str_Tmp[0] = 0;
					Get_State_File_Name(Str_Tmp);
					Save_State(Str_Tmp);
					return 0;

				case ID_FILES_SAVESTATEAS:
					if (Check_If_Kaillera_Running()) return 0;
					Change_File_S(Str_Tmp, State_Dir, "Load state", "State Files\0*.gs*\0All Files\0*.*\0\0", "");
					Save_State(Str_Tmp);
					return 0;

				case ID_FILES_PREVIOUSSTATE:
					Set_Current_State(hWnd, (Current_State + 9) % 10);
					return 0;

				case ID_FILES_NEXTSTATE:
					Set_Current_State(hWnd, (Current_State + 1) % 10);
					return 0;

				case ID_GRAPHICS_VSYNC:
					Change_VSync(hWnd);
					return 0;

				case ID_GRAPHICS_SWITCH_MODE:
					if (Full_Screen) Set_Render(hWnd, 0, -1, true);
					else Set_Render(hWnd, 1, Render_FS, true);
					return 0;

				case ID__STRETCH_CUSTOM_SET:
	               if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					DialogBox(ghInstance, MAKEINTRESOURCE(IDD_COSTOM_STRETCH), hWnd, (DLGPROC)SetStretchModeProc);
					Build_Main_Menu();
					 return 0;

				case ID__STRETCH_640_480:
                    StretchModelIndex =0;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

                 case ID__STRETCH_800_600:
                    StretchModelIndex =1;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

                 case ID__STRETCH_1024_768:
                    StretchModelIndex =2;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

			case ID__STRETCH_720_480:
                    StretchModelIndex =3;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

                 case ID__STRETCH_720_576:
                    StretchModelIndex =4;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

                 case ID__STRETCH_848_480:
                    StretchModelIndex =5;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

	            case ID__STRETCH_1152_864:
                    StretchModelIndex =6;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

                 case ID__STRETCH_1280_720:
                    StretchModelIndex =7;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

                 case ID__STRETCH_1280_768:
                    StretchModelIndex =8;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;


                 case ID__STRETCH_1280_960:
                    StretchModelIndex =9;      
                     Init_DDraw(HWnd);
                    Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

                 case ID__STRETCH_1280_1024:
                    StretchModelIndex =10;      
                     Init_DDraw(HWnd);
                    Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

                 case ID__STRETCH_CUSTOM:
                     StretchModelIndex =11;      
                     Init_DDraw(HWnd);
                     Set_Render(hWnd, Full_Screen, 1, false);
                     return 0;

				case ID_GRAPHICS_COLOR_ADJUST:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					DialogBox(ghInstance, MAKEINTRESOURCE(IDD_COLOR), hWnd, (DLGPROC) ColorProc);
					return 0;

	                case Gens32_Glossiness:
                    if(Glossiness_Mode)
					{
                      Glossiness_Mode =false;
	                  MESSAGE_L("Glossiness Mode Disabled", "Glossiness Mode Disabled", 1500)
                    }  
				    else
					{
                     Glossiness_Mode =true;
                     MESSAGE_L("Glossiness Mode Enabled", "Glossiness Mode Enabled", 1500)
					}
                    InitColorMap();
                    Build_Main_Menu();
                     return 0;

                case ID_HUE_NORMAL:
					Hue =4;
                    InitColorMap();
                    Build_Main_Menu();
					return 0;

                case ID_HUE_WARM:
					Hue =3;
                    InitColorMap();
                    Build_Main_Menu();
					return 0;

                case ID_HUE_WARMER:
					Hue =2;
                    InitColorMap();
                    Build_Main_Menu();
					return 0;

                case ID_HUE_WARMEST:
					Hue =1;
                    InitColorMap();
                    Build_Main_Menu();
					return 0;

                case ID_HUE_COLD:
					Hue =5;
                    InitColorMap();
                    Build_Main_Menu();
					return 0;
                case ID_HUE_COLDER:
					Hue =6;
                    InitColorMap();
                    Build_Main_Menu();
					return 0;

               case ID_HUE_COLDEST:
					Hue =7;
                    InitColorMap();
                    Build_Main_Menu();
					return 0;

			   case Stretch_W_2X:
                    Stretch_W_Index =Stretch_W_2X;
                    ResizeWondow(hWnd);
                    return 0;

			   case Stretch_W_3X:
                    Stretch_W_Index =Stretch_W_3X;
                    ResizeWondow(hWnd);
                    return 0;

			   case Stretch_W_4X:
                    Stretch_W_Index =Stretch_W_4X;
                    ResizeWondow(hWnd);
                    return 0;

			   case Stretch_W_Custom:
                    Stretch_W_Index =Stretch_W_Custom;
                    ResizeWondow(hWnd);
                    return 0;

                case Stretch_W_FixedRate:
                    Stretch_W_Index =Stretch_W_FixedRate;
                    ResizeWondow(hWnd);
                    return 0;

                case Stretch_W_AUTO:
                    SetAutoStretch(hWnd);
					return 0;

				case ID_GRAPHICS_RENDER_NORMAL:
					Set_Render(hWnd, Full_Screen, 0, false);
					return 0;

				case ID_GRAPHICS_RENDER_DOUBLE:
					Set_Render(hWnd, Full_Screen, 1, false);
					return 0;

				case ID_GRAPHICS_RENDER_DOUBLE_INT:
					Set_Render(hWnd, Full_Screen, 2, false);
					return 0;

				case ID_GRAPHICS_RENDER_FULLSCANLINE:
					Set_Render(hWnd, Full_Screen, 3, false);
					return 0;

				case ID_GRAPHICS_RENDER_50SCANLINE:
					Set_Render(hWnd, Full_Screen, 4, false);
					return 0;

				case ID_GRAPHICS_RENDER_25SCANLINE:
					Set_Render(hWnd, Full_Screen, 5, false);
					return 0;

				case ID_GRAPHICS_RENDER_INTESCANLINE:
					Set_Render(hWnd, Full_Screen, 6, false);
					return 0;

				case ID_GRAPHICS_RENDER_INT50SCANLIN:
					Set_Render(hWnd, Full_Screen, 7, false);
					return 0;

				case ID_GRAPHICS_RENDER_INT25SCANLIN:
					Set_Render(hWnd, Full_Screen, 8, false);
					return 0;

				case ID_GRAPHICS_RENDER_2XSAI:
					Set_Render(hWnd, Full_Screen, 9, false);
					return 0;

                case ID_GRAPHICS_RENDER_FOURSCREEN:
                    Set_Render(hWnd, Full_Screen, 10, false);
					return 0;

				case ID_GRAPHICS_PREVIOUS_RENDER:
					if ((Full_Screen) && (Render_FS > 0)) Set_Render(hWnd, 1, Render_FS - 1, false);
					else if ((!Full_Screen) && (Render_W > 0)) Set_Render(hWnd, 0, Render_W - 1, false);
					return 0;

				case ID_GRAPHICS_NEXT_RENDER:
					if ((Full_Screen) && (Render_FS < 9)) Set_Render(hWnd, 1, Render_FS + 1, false);
					else if ((!Full_Screen) && (Render_W < 9)) Set_Render(hWnd, 0, Render_W + 1, false);
					return 0;
/*
				case ID_GRAPHICS_STRETCH:
					Change_Stretch();
					return 0;
*/
				case ID_FORCE32BPP:
                    if(Force32bpp)
					{
                      Force32bpp =false;
	                  MESSAGE_L("Force 32bpp Disabled", "Force 32bpp Disabled", 1500)
                    }  
				    else
					{
                     Force32bpp =true;

	                 DEVMODE dm;
			         memset(&dm, 0, sizeof(DEVMODE));
			         dm.dmSize = sizeof(DEVMODE);
			         dm.dmBitsPerPel = 32;
			         dm.dmFields = DM_BITSPERPEL;
			         ChangeDisplaySettings(&dm, 0);
                     Init_DDraw(HWnd);
                     MESSAGE_L("Force 32bpp Enabled", "Force 32bpp Enabled", 1500)
					}
                    Build_Main_Menu();
                     return 0;

				case ID_GRAPHICS_FORCESOFT:
					Change_Blit_Style();
					return 0;

				case ID_GRAPHICS_FRAMESKIP_AUTO:
					Set_Frame_Skip(hWnd, -1);
					return 0;

				case ID_GRAPHICS_FRAMESKIP_0:
				case ID_GRAPHICS_FRAMESKIP_1:
				case ID_GRAPHICS_FRAMESKIP_2:
				case ID_GRAPHICS_FRAMESKIP_3:
				case ID_GRAPHICS_FRAMESKIP_4:
				case ID_GRAPHICS_FRAMESKIP_5:
				case ID_GRAPHICS_FRAMESKIP_6:
				case ID_GRAPHICS_FRAMESKIP_7:
				case ID_GRAPHICS_FRAMESKIP_8:
					Set_Frame_Skip(hWnd, LOWORD(wParam) - ID_GRAPHICS_FRAMESKIP_0);
					return 0;

				case ID_GRAPHICS_FRAMESKIP_MOINS:
					if (Frame_Skip == -1)
					{
						Set_Frame_Skip(hWnd, 0);
					}
					else
					{
						if (Frame_Skip > 0) Set_Frame_Skip(hWnd, Frame_Skip - 1);
					}
					return 0;

				case ID_GRAPHICS_FRAMESKIP_PLUS:
					if (Frame_Skip == -1)
					{
						Set_Frame_Skip(hWnd, 1);
					}
					else
					{
						if (Frame_Skip < 8) Set_Frame_Skip(hWnd, Frame_Skip + 1);
					}
					return 0;

				case ID_GRAPHICS_SPRITEOVER:
					Set_Sprite_Over(hWnd, Sprite_Over ^ 1);
					return 0;

				case ID_GRAPHICS_SHOT:
					Clear_Sound_Buffer();
					Take_Shot();
					Build_Main_Menu();
					return 0;

				case ID_FILES_CHANGESTATE_0:
				case ID_FILES_CHANGESTATE_1:
				case ID_FILES_CHANGESTATE_2:
				case ID_FILES_CHANGESTATE_3:
				case ID_FILES_CHANGESTATE_4:
				case ID_FILES_CHANGESTATE_5:
				case ID_FILES_CHANGESTATE_6:
				case ID_FILES_CHANGESTATE_7:
				case ID_FILES_CHANGESTATE_8:
				case ID_FILES_CHANGESTATE_9:
					Set_Current_State(hWnd, LOWORD(wParam) - ID_FILES_CHANGESTATE_0);
					return 0;

                case Turbo_Games:
                     Turboing =true;
					return 0;

#ifdef GENS_DEBUG
				case ID_CPU_DEBUG_GENESIS_68000:
					Change_Debug(hWnd, 1);
					return 0;

				case ID_CPU_DEBUG_GENESIS_Z80:
					Change_Debug(hWnd, 2);
					return 0;

				case ID_CPU_DEBUG_GENESIS_VDP:
					Change_Debug(hWnd, 3);
					return 0;

				case ID_CPU_DEBUG_SEGACD_68000:
					Change_Debug(hWnd, 4);
					return 0;

				case ID_CPU_DEBUG_SEGACD_CDC:
					Change_Debug(hWnd, 5);
					return 0;

				case ID_CPU_DEBUG_SEGACD_GFX:
					Change_Debug(hWnd, 6);
					return 0;

				case ID_CPU_DEBUG_32X_MAINSH2:
					Change_Debug(hWnd, 7);
					return 0;

				case ID_CPU_DEBUG_32X_SUBSH2:
					Change_Debug(hWnd, 8);
					return 0;

				case ID_CPU_DEBUG_32X_VDP:
					Change_Debug(hWnd, 9);
					return 0;
#endif

				case ID_CPU_RESET:
					if (Check_If_Kaillera_Running()) return 0;

					if (Genesis_Started)
					{
						Reset_Genesis();
						MESSAGE_L("Genesis reseted", "Genesis reset", 1500)
					}
					else if (_32X_Started)
					{
						Reset_32X();
						MESSAGE_L("32X reseted", "32X reset", 1500)
					}
					else if (SegaCD_Started)
					{
						Reset_SegaCD();
						MESSAGE_L("SegaCD reseted", "SegaCD reset", 1500)
					}
					return 0;

				case ID_CPU_RESET68K:
					if (Check_If_Kaillera_Running()) return 0;
					if (Game)
					{
						Paused = 0;
						main68k_reset();
						if (Genesis_Started) MESSAGE_L("68000 CPU reseted", "68000 CPU reseted", 1000)
						else if (SegaCD_Started) MESSAGE_L("Main 68000 CPU reseted", "Main 68000 CPU reseted", 1000)
					}
					return 0;

				case ID_CPU_RESET_MSH2:
					if (Check_If_Kaillera_Running()) return 0;
					if ((Game) && (_32X_Started))
					{
						Paused = 0;
						SH2_Reset(&M_SH2, 1);
						MESSAGE_L("Master SH2 reseted", "Master SH2 reseted", 1000)
					}
					return 0;

				case ID_CPU_RESET_SSH2:
					if (Check_If_Kaillera_Running()) return 0;
					if ((Game) && (_32X_Started))
					{
						Paused = 0;
						SH2_Reset(&S_SH2, 1);
						MESSAGE_L("Slave SH2 reseted", "Slave SH2 reseted", 1000)
					}
					return 0;

				case ID_CPU_RESET_SUB68K:
					if (Check_If_Kaillera_Running()) return 0;
					if ((Game) && (SegaCD_Started))
					{
						Paused = 0;
						sub68k_reset();
						MESSAGE_L("Sub 68000 CPU reseted", "Sub 68000 CPU reseted", 1000)
					}
					return 0;

				case ID_CPU_RESETZ80:
					if (Check_If_Kaillera_Running()) return 0;
					if (Game)
					{
						z80_Reset(&M_Z80);
						MESSAGE_L("CPU Z80 reseted", "CPU Z80 reseted", 1000)
					}
					return 0;

				case ID_CPU_ACCURATE_SYNCHRO:
					Change_SegaCD_Synchro();
					return 0;

				case ID_CPU_COUNTRY_AUTO:
					Change_Country(hWnd, -1);
					return 0;

				case ID_CPU_COUNTRY_JAPAN:
					Change_Country(hWnd, 0);
					return 0;

				case ID_CPU_COUNTRY_USA:
					Change_Country(hWnd, 1);
					return 0;

				case ID_CPU_COUNTRY_EUROPE:
					Change_Country(hWnd, 2);
					return 0;

				case ID_CPU_COUNTRY_MISC:
					Change_Country(hWnd, 3);
					return 0;

				case ID_CPU_COUNTRY_ORDER + 0:
				case ID_CPU_COUNTRY_ORDER + 1:
				case ID_CPU_COUNTRY_ORDER + 2:
					Change_Country_Order(LOWORD(wParam) - ID_CPU_COUNTRY_ORDER);
					return 0;

				case ID_SOUND_Z80ENABLE:
					Change_Z80(hWnd);
					return 0;

				case ID_SOUND_YM2612ENABLE:
					Change_YM2612(hWnd);
					return 0;

				case ID_SOUND_PSGENABLE:
					Change_PSG(hWnd);
					return 0;

				case ID_SOUND_DACENABLE:
					Change_DAC(hWnd);
					return 0;

				case ID_SOUND_PCMENABLE:
					Change_PCM(hWnd);
					return 0;

				case ID_SOUND_PWMENABLE:
					Change_PWM(hWnd);
					return 0;

				case ID_SOUND_CDDAENABLE:
					Change_CDDA(hWnd);
					return 0;

				case ID_SOUND_DACIMPROV:
					Change_DAC_Improv(hWnd);
					return 0;

				case ID_SOUND_PSGIMPROV:
					Change_PSG_Improv(hWnd);
					return 0;

				case ID_SOUND_YMIMPROV:
					Change_YM2612_Improv(hWnd);
					return 0;

				case ID_SOUND_ENABLE:
					Change_Sound(hWnd);
					return 0;

                case VOLUME_NORMAL:
                     VolumeIndex =VOLUME_NORMAL;
                     MESSAGE_L("Set Volume to normal", "Set Volume to normal", 1500)
                     Build_Main_Menu();
                     return 0;

                case VOLUME_2PX:
                     VolumeIndex =VOLUME_2PX;
                     MESSAGE_L("Set Volume to 1/2X", "Set Volume to 1/2X", 1500)
                     Build_Main_Menu();
                     return 0;
             
                case VOLUME_4PX:
                     VolumeIndex =VOLUME_4PX;
                     MESSAGE_L("Set Volume to 1/4X", "Set Volume to 1/4X", 1500)
                     Build_Main_Menu();
                     return 0;

                   case VOLUME_8PX:
                     VolumeIndex =VOLUME_8PX;
                     MESSAGE_L("Set Volume to 1/8X", "Set Volume to 1/8X", 1500)
                     Build_Main_Menu();
                     return 0;

                case VOLUME_2X:
                     VolumeIndex =VOLUME_2X;
                     MESSAGE_L("Set Volume to 2X", "Set Volume to 2X", 1500)
                     Build_Main_Menu();
                     return 0;

                case VOLUME_4X:
                     VolumeIndex =VOLUME_4X;
                     MESSAGE_L("Set Volume to 4X", "Set Volume to 4X", 1500)
                     Build_Main_Menu();
                     return 0;

				case ID_SOUND_RATE_11000:
					Change_Sample_Rate(hWnd, 0);
					return 0;

				case ID_SOUND_RATE_22000:
					Change_Sample_Rate(hWnd, 1);
					return 0;

				case ID_SOUND_RATE_44000:
					Change_Sample_Rate(hWnd, 2);
					return 0;

                case ID_SOUND_RATE_48000:
                     	Change_Sample_Rate(hWnd, 3);
                    return 0;

				case ID_SOUND_STEREO:
					Change_Sound_Stereo(hWnd);
					return 0;

				case ID_SOUND_STARTWAVDUMP:
					if (WAV_Dumping) Stop_WAV_Dump();
					else Start_WAV_Dump();
					Build_Main_Menu();
					return 0;

				case ID_SOUND_STARTGYMDUMP:
					if (GYM_Dumping) Stop_GYM_Dump();
					else Start_GYM_Dump();
					Build_Main_Menu();
					return 0;

				case ID_SOUND_PLAYGYM:
					MINIMIZE
					if (!Genesis_Started && !SegaCD_Started && !_32X_Started)
					{
						if (GYM_Playing) Stop_Play_GYM();
						else Start_Play_GYM();
					}
					Build_Main_Menu();
					return 0;

				case ID_OPTIONS_FASTBLUR:
					Change_Fast_Blur(hWnd);
					return 0;

				case ID_OPTIONS_SHOWFPS:
					if (Show_FPS) Show_FPS = 0;
					else Show_FPS = 1;
					return 0;

				case ID_OPTIONS_GENERAL:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					DialogBox(ghInstance, MAKEINTRESOURCE(IDD_OPTION), hWnd, (DLGPROC) OptionProc);
					Build_Main_Menu();
					return 0;

                case ID_SPECALGAMEFIX:
                     if(Specal_Fix)
					 {
                      Specal_Fix =false;
                      MESSAGE_L("Specal Game Fix Disabled", "Specal Game Fix Disabled", 1500)
					 }
					 else
					 {
                     Specal_Fix =true;
                     MESSAGE_L("Specal Game Fix Ensabled", "Specal Game Fix Ensabled", 1500)
					 }
                    Build_Main_Menu();
					return 0;
                
                 case ID_MODETOCHANGE :
                     if(ModeToChange)
					 {
                      ModeToChange =false;
                      MESSAGE_L("MODE To Change Disabled", "MODE To Change Disabled", 1500)
					 }
					 else
					 {
                     ModeToChange =true;
                     MESSAGE_L("MODE To Change Enabled", "MODE To Change Enabled", 1500)
					 }
                    Build_Main_Menu();
					return 0;

				 case Controllers_Save:
                     if (Check_If_Kaillera_Running()) return 0;
					 MINIMIZE
                     Save_Controllers(hWnd);
					 Build_Main_Menu();
					 return 0;

                 case Controllers_Load:
					 if (Check_If_Kaillera_Running()) return 0;
					 MINIMIZE
                     Load_Controllers(hWnd);
					 Build_Main_Menu();
					 return 0;

				case ID_OPTIONS_JOYPADSETTING:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					End_Input();
					DialogBox(ghInstance, MAKEINTRESOURCE(IDD_CONTROLLER), hWnd, (DLGPROC) ControllerProc);
					if (!Init_Input(ghInstance, HWnd)) return false;
					Build_Main_Menu();
					return 0;

                case ID__TURBO_1P_A:
                      if(Turbo_1P_A)
					  {
                       Turbo_1P_A =false;
                       MESSAGE_L("1P A Turbo Disabled", "1P A Turbo Disabled", 1500)
					  }
					  else
					  {
                       Turbo_1P_A =true;
                       MESSAGE_L("1P A Turbo Enabled", "1P A Turbo Enabled", 1500)
					  }
                    Turbo_On =true;
                    Build_Main_Menu();
                    return 0;

                case ID__TURBO_1P_B:
                      if(Turbo_1P_B)
					  {
                       Turbo_1P_B =false;
                       MESSAGE_L("1P B Turbo Disabled", "1P B Turbo Disabled", 1500)
					  }
					  else
					  {
                       Turbo_1P_B =true;
                       MESSAGE_L("1P B Turbo Enabled", "1P B Turbo Enabled", 1500)
					  }
                    Turbo_On =true;
                    Build_Main_Menu();
                    return 0;

                   case ID__TURBO_1P_C:
                      if(Turbo_1P_C)
					  {
                       Turbo_1P_C =false;
                       MESSAGE_L("1P C Turbo Disabled", "1P C Turbo Disabled", 1500)
					  }
					  else
					  {
                       Turbo_1P_C =true;
                       MESSAGE_L("1P C Turbo Enabled", "1P C Turbo Enabled", 1500)
					  }
                    Turbo_On =true;
                    Build_Main_Menu();
                    return 0;

                    case ID__TURBO_2P_A:
                      if(Turbo_2P_A)
					  {
                       Turbo_2P_A =false;
                       MESSAGE_L("2P A Turbo Disabled", "2P A Turbo Disabled", 1500)
					  }
					  else
					  {
                       Turbo_2P_A =true;
                       MESSAGE_L("2P A Turbo Enabled", "2P A Turbo Enabled", 1500)
					  }
                    Turbo_On =true;
                    Build_Main_Menu();
                    return 0;

                   case ID__TURBO_2P_B:
                      if(Turbo_2P_B)
					  {
                       Turbo_2P_B =false;
                       MESSAGE_L("2P B Turbo Disabled", "2P B Turbo Disabled", 1500)
					  }
					  else
					  {
                       Turbo_2P_B =true;
                       MESSAGE_L("2P B Turbo Enabled", "2P B Turbo Enabled", 1500)
					  }
                    Turbo_On =true;
                    Build_Main_Menu();
                    return 0;

                case ID__TURBO_2P_C:
                      if(Turbo_2P_C)
					  {
                       Turbo_2P_C =false;
                       MESSAGE_L("2P C Turbo Disabled", "2P C Turbo Disabled", 1500)
					  }
					  else
					  {
                       Turbo_2P_C =true;
                       MESSAGE_L("2P C Turbo Enabled", "2P C Turbo Enabled", 1500)
					  }
                    Turbo_On =true;
                    Build_Main_Menu();
                    return 0;

                  case ID_FFD_A:
                      if(FFD_A)
					  {
                       FFD_A =false;
                       MESSAGE_L("Force feedback of A Disabled", "Force feedback of A Disabled", 1500)
					  }
					  else
					  {
                       FFD_A =true;
                       MESSAGE_L("Force feedback of A Enabled", "Force feedback of A Enabled", 1500)
					  }
					FFD_ForPerssKey =true;
                    Build_Main_Menu();
                    return 0;
              
                    case ID_FFD_B:
                      if(FFD_B)
					  {
                       FFD_B =false;
                       MESSAGE_L("Force feedback of B Disabled", "Force feedback of B Disabled", 1500)
					  }
					  else
					  {
                       FFD_B =true;
                       MESSAGE_L("Force feedback of B Enabled", "Force feedback of B Enabled", 1500)
					  }
                    FFD_ForPerssKey =true;
                    Build_Main_Menu();
                    return 0;

                    case ID_FFD_C:
                      if(FFD_C)
					  {
                       FFD_C =false;
                       MESSAGE_L("Force feedback of C Disabled", "Force feedback of C Disabled", 1500)
					  }
					  else
					  {
                       FFD_C =true;
                       MESSAGE_L("Force feedback of C Enabled", "Force feedback of C Enabled", 1500)
					  }
                    FFD_ForPerssKey =true;
                    Build_Main_Menu();
                    return 0;

                  case ID_FFD_X:
                      if(FFD_X)
					  {
                       FFD_X =false;
                       MESSAGE_L("Force feedback of X Disabled", "Force feedback of X Disabled", 1500)
					  }
					  else
					  {
                       FFD_X =true;
                       MESSAGE_L("Force feedback of X Enabled", "Force feedback of X Enabled", 1500)
					  }
                    FFD_ForPerssKey =true;
                    Build_Main_Menu();
                    return 0;

                  case ID_FFD_Y:
                      if(FFD_Y)
					  {
                       FFD_Y =false;
                       MESSAGE_L("Force feedback of Y Disabled", "Force feedback of Y Disabled", 1500)
					  }
					  else
					  {
                       FFD_Y =true;
                       MESSAGE_L("Force feedback of Y Enabled", "Force feedback of Y Enabled", 1500)
					  }
                    FFD_ForPerssKey =true;
                    Build_Main_Menu();
                    return 0;

                  case ID_FFD_Z:
                      if(FFD_Z)
					  {
                       FFD_Z =false;
                       MESSAGE_L("Force feedback of Z Disabled", "Force feedback of Z Disabled", 1500)
					  }
					  else
					  {
                       FFD_Z =true;
                       MESSAGE_L("Force feedback of Z Enabled", "Force feedback of Z Enabled", 1500)
					  }
                    FFD_ForPerssKey =true;
                    Build_Main_Menu();
                    return 0;

				case ID_OPTIONS_CHANGEDIR:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					DialogBox(ghInstance, MAKEINTRESOURCE(IDD_DIRECTORIES), hWnd, (DLGPROC) DirectoriesProc);
					Build_Main_Menu();
					return 0;

				case ID_OPTIONS_CHANGEFILES:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					DialogBox(ghInstance, MAKEINTRESOURCE(IDD_FILES), hWnd, (DLGPROC) FilesProc);
					Build_Main_Menu();
					return 0;

				case ID_OPTION_CDDRIVE_0:
				case ID_OPTION_CDDRIVE_1:
				case ID_OPTION_CDDRIVE_2:
				case ID_OPTION_CDDRIVE_3:
				case ID_OPTION_CDDRIVE_4:
				case ID_OPTION_CDDRIVE_5:
				case ID_OPTION_CDDRIVE_6:
				case ID_OPTION_CDDRIVE_7:
					if (Num_CD_Drive > (LOWORD(wParam) - ID_OPTION_CDDRIVE_0))
					{
						CUR_DEV = LOWORD(wParam) - ID_OPTION_CDDRIVE_0;
					}
					Build_Main_Menu();
					return 0;

                    case Use_One_Internal:
                      if(One_Internal)
					  {
                       One_Internal =false;
                       MESSAGE_L("Shared Internal Ram Disabled", "Shared Internal Ram Disabled", 1500)
					  }
					  else
					  {
                       One_Internal =true;
                       MESSAGE_L("Shared Internal Ram Enabled", "Shared Internal Ram Enabled", 1500)
					  }
                    Build_Main_Menu();
					return 0;

				case ID_OPTION_SRAMSIZE_0:
					Change_SegaCD_SRAM_Size(-1);
					return 0;

				case ID_OPTION_SRAMSIZE_8:
					Change_SegaCD_SRAM_Size(0);
					return 0;

				case ID_OPTION_SRAMSIZE_16:
					Change_SegaCD_SRAM_Size(1);
					return 0;

				case ID_OPTION_SRAMSIZE_32:
					Change_SegaCD_SRAM_Size(2);
					return 0;

				case ID_OPTION_SRAMSIZE_64:
					Change_SegaCD_SRAM_Size(3);
					return 0;

				case ID_OPTIONS_SAVECONFIG:
					strcpy(Str_Tmp, Gens_Path);
					strcat(Str_Tmp, "Gens.cfg");
					Save_Config(Str_Tmp);
					return 0;

                case Edit_Config:
	                strcpy(Str_Tmp, Gens_Path);
					strcat(Str_Tmp, "Gens.cfg");
					system("notepad Gens.cfg");
					Load_Config(Str_Tmp,Game);
					return 0;

				case ID_OPTIONS_LOADCONFIG:
					if (Check_If_Kaillera_Running()) return 0;
					MINIMIZE
					Load_As_Config(hWnd, Game);
					return 0;

				case ID_OPTIONS_SAVEASCONFIG:
					MINIMIZE
					Save_As_Config(hWnd);
					return 0;

				case ID_HELP_ABOUT:
					Clear_Sound_Buffer();
					DialogBox(ghInstance, MAKEINTRESOURCE(ABOUTDIAL), hWnd, (DLGPROC) AboutProc);
					return 0;

				case ID_HELP_HELP:
					if (Game)
					{
						if (Genesis_Started)
						{
							HtmlHelp(GetDesktopWindow(), CGOffline_Path, HH_HELP_CONTEXT, Calculate_CRC32());
						}
					}
					else
					{
						if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
						{
							strcpy(Str_Tmp, Manual_Path);
							strcat(Str_Tmp, " index.html");
							system(Str_Tmp);
						}
					}

					/* Gens manual file :
					**
					**	File menu						manual.exe helpfilemenu.html
					**	Graphics menu					manual.exe helpgraphicsmenu.html
					**	CPU menu						manual.exe helpcpumenu.html
					**	Sound menu						manual.exe helpsoundmenu.html
					**	Options menu					manual.exe helpoptionmenu.html
					**	Netplay							manual.exe helpnetplay.html
					**
					**	Game Genie						manual.exe helpgamegenie.html
					**	Misc/General options			manual.exe helpmisc.html
					**	Joypad/Controllers settings		manual.exe helpjoypads.html
					**	Directories/file configuration	manual.exe helpdir.html
					**
					**	Help menu						manual.exe helphelpmenu.html
					**	Mega-CD                         manual.exe helpmegacd.html
					**	FAQ                             manual.exe helpfaq.html
					**	Default Keys/keyboard shortcuts manual.exe helpkeys.html
					**	Multitap                        manual.exe helpmultitap.html
					*/
					return 0;

				case ID_HELP_MENU_FILE:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpfilemenu.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_HELP_MENU_GRAPHICS:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpgraphicsmenu.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_HELP_MENU_CPU:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpcpumenu.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_HELP_MENU_SOUND:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpsoundmenu.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_HELP_MENU_OPTIONS:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpoptionmenu.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_HELP_NETPLAY:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpnetplay.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_HELP_MEGACD:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpmegacd.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_HELP_FAQ:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpfaq.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_HELP_KEYS:
					if (Detect_Format(Manual_Path) != -1)		// Can be used to test if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpkeys.html");
						system(Str_Tmp);
					}
					return 0;

				case ID_EMULATION_PAUSED:
					if (Debug)
					{
						Change_Debug(HWnd, 0);
						Paused = 0;
						Build_Main_Menu();
					}
					else if (Paused)
					{
						Paused = 0;
					}
					else
					{
						Paused = 1;
						Pause_Screen();
						Clear_Sound_Buffer();
					}
					return 0;
			}
			break;

/*
			// A device has be modified (new CD inserted for instance)
			case WM_DEVICECHANGE:
				ASPI_Mechanism_State(0, NULL);
				break;
*/

#ifdef GENS_DEBUG
		case WM_KEYDOWN:
			if (Debug) Debug_Event((lParam >> 16) & 0x7F);
			break;
#endif

		case WM_KNUX:
			MESSAGE_L("Communicating", "Communicating ...", 1000)

			switch(wParam)
			{
				case 0:
					switch(lParam)
					{
						case 0:
							return 4;

						case 1:
							GetWindowText(HWnd, Str_Tmp, 1024);
							return (long) (char *) Str_Tmp;

						case 2:
							return 5;

						case 3:
							return GENS_VERSION_H;

						default:
							return -1;
					}

				case 1:
					switch(lParam)
					{
						case 0:
							return((long) (unsigned short *)&Ram_68k[0]);
						case 1:
							return(64 * 1024);
						case 2:
							return(1);
						default:
							return(-1);
					}

				case 2:
					switch(lParam)
					{
						case 0:
							return((long) (unsigned char *)&Ram_Z80[0]);
						case 1:
							return(8 * 1024);
						case 2:
							return(0);
						default:
							return(-1);
					}

				case 3:
					switch(lParam)
					{
						case 0:
							return((long) (char *)&Rom_Data[0]);
						case 1:
							return(0);
						case 2:
							return(Rom_Size);
						default:
							return(-1);
					}

				case 4:
					switch(lParam)
					{
						case 0:
							return(0);
						case 1:
							return((Game != NULL)?1:0);
						case 2:
							return(0);
						default:
							return(-1);
					}

				default:
					return(-1);
			}
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}


int Build_Language_String(void)
{
	unsigned long nb_lue = 1;
	int sec_alloue = 1, poscar = 0;
	enum etat_sec {DEB_LIGNE, SECTION, NORMAL} etat = DEB_LIGNE;
	HANDLE LFile;
	char c;

	if (language_name)
	{
		free(language_name);
		language_name = NULL;
	}

	language_name = (char**)malloc(sec_alloue * sizeof(char*));
	language_name[0] = NULL;

	LFile = CreateFile(Language_Path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 	
	while(nb_lue)
	{
		ReadFile(LFile, &c, 1, &nb_lue, NULL);
		
		switch(etat)
		{
			case DEB_LIGNE:
				switch(c)
				{
					case '[':
						etat = SECTION;
						sec_alloue++;
						language_name = (char**)realloc(language_name, sec_alloue * sizeof(char*));
						language_name[sec_alloue - 2] = (char*)malloc(32 * sizeof(char));
						language_name[sec_alloue - 1] = NULL;
						poscar = 0;
						break;

					case '\n':
						break;

					default: etat = NORMAL;
						break;
				}
				break;

			case NORMAL:
				switch(c)
				{
					case '\n':
						etat = DEB_LIGNE;
						break;

					default:
						break;
				}
				break;

			case SECTION:
				switch(c)
				{
					case ']':
						language_name[sec_alloue - 2][poscar] = 0;
						etat = DEB_LIGNE;
						break;

					default:
						if(poscar < 32)
							language_name[sec_alloue - 2][poscar++] = c;
						break;
				}
				break;
		}
	}

	CloseHandle(LFile);

	if (sec_alloue == 1)
	{
		language_name = (char**)realloc(language_name, 2 * sizeof(char*));
		language_name[0] = (char*)malloc(32 * sizeof(char));
		strcpy(language_name[0], "English");
		language_name[1] = NULL;
		WritePrivateProfileString("English", "Menu Language", "&English menu", Language_Path);
	}

	return(0);	
}


HMENU Build_Main_Menu(void)
{
	unsigned int Flags;
	int i, Rend;

	HMENU MainMenu;
	HMENU Files;
	HMENU Graphics;
	HMENU CPU;
	HMENU Sound;
	HMENU Options;
	HMENU Help;

	HMENU FilesChangeState;
	HMENU FilesHistory;
    HMENU FilesMedia;

	HMENU GraphicsRender;
    HMENU GraphicsHueSelection;
	HMENU GraphicsFrameSkip;

    HMENU SEGA_CD_Manager;
    HMENU ColorManager;
    HMENU RenderMode;
    HMENU StretchMode;
    HMENU ChipSetSwitchs;
    HMENU SoundQuality;
    HMENU FileSaveStates;
    HMENU TranslationManager;
    HMENU Translation_Advanced;

#ifdef GENS_DEBUG
	HMENU CPUDebug;
#endif
	HMENU CPUCountry;
	HMENU CPUCountryOrder;
	HMENU SoundRate;
    HMENU SoundVolume;
	HMENU OptionsCDDrive;
	HMENU OptionsSRAMSize;
    HMENU OptionsInputAssistant;

    HMENU Assistants_1P_Turbo;
    HMENU Assistants_2P_Turbo;
    HMENU ForceFeedback;

	DestroyMenu(Gens_Menu);

	Build_Language_String();

	if (Full_Screen)
	{
		MainMenu = CreatePopupMenu();
		Rend = Render_FS;
	}
	else
	{
		MainMenu = CreateMenu();
		Rend = Render_W;
	}

	Files = CreatePopupMenu();
	Graphics = CreatePopupMenu();
	CPU = CreatePopupMenu();
	Sound = CreatePopupMenu();
	Options = CreatePopupMenu();
	Help = CreatePopupMenu();
	FilesChangeState = CreatePopupMenu();
	FilesHistory = CreatePopupMenu();
    FilesMedia =CreatePopupMenu();
	GraphicsRender = CreatePopupMenu();
    GraphicsHueSelection =CreatePopupMenu();
	GraphicsFrameSkip = CreatePopupMenu();
    ForceFeedback =CreatePopupMenu();
    SEGA_CD_Manager =CreatePopupMenu();
    ColorManager =CreatePopupMenu();
    RenderMode =CreatePopupMenu();
    StretchMode =CreatePopupMenu();
    ChipSetSwitchs =CreatePopupMenu();
    SoundQuality =CreatePopupMenu();
    FileSaveStates =CreatePopupMenu();
    TranslationManager =CreatePopupMenu();
    Translation_Advanced =CreatePopupMenu();

#ifdef GENS_DEBUG
	CPUDebug = CreatePopupMenu();
#endif
	CPUCountry = CreatePopupMenu();
	CPUCountryOrder = CreatePopupMenu();
	SoundRate = CreatePopupMenu();
    SoundVolume = CreatePopupMenu();
	OptionsCDDrive = CreatePopupMenu();
	OptionsSRAMSize = CreatePopupMenu();
    OptionsInputAssistant = CreatePopupMenu();
    Assistants_1P_Turbo = CreatePopupMenu();
    Assistants_2P_Turbo = CreatePopupMenu();

	// Création des sous-menu pricipaux

	Flags = MF_BYPOSITION | MF_POPUP | MF_STRING;

	MENU_L(MainMenu, 0, Flags, (UINT)Files, "File", "", "&File");
	MENU_L(MainMenu, 1, Flags, (UINT)Graphics, "Graphic", "", "&Graphic");
	MENU_L(MainMenu, 2, Flags, (UINT)CPU, "CPU", "", "&CPU");
	MENU_L(MainMenu, 3, Flags, (UINT)Sound, "Sound", "", "&Sound");
	MENU_L(MainMenu, 4, Flags, (UINT)Options, "Option", "", "&Option");
	MENU_L(MainMenu, 5, Flags, (UINT)Help, "Help", "", "&Help");

	// Menu Files 
	
	Flags = MF_BYPOSITION | MF_STRING;
	
	MENU_L(Files, 0, Flags, ID_FILES_OPENROM, "Open Rom", "\tCtrl+O", "&Open ROM");
	MENU_L(Files, 1, Flags, ID_FILES_CLOSEROM, "Free Rom", "\tCtrl+C", "&Close ROM");

	i = 2;

	MENU_L(Files, i++, Flags, ID_FILES_BOOTCD, "Boot CD", "\tCtrl+B", "&Boot CD");

	if (Kaillera_Initialised)
	{
		MENU_L(Files, i++, Flags, ID_FILES_NETPLAY, "Netplay", "", "&Netplay");
	}
	
	InsertMenu(Files, i++, MF_SEPARATOR, NULL, NULL);

  //MediaMode;
  if(MediaMode)
  {
    MENU_L(Files, i++, Flags | MF_POPUP, (UINT)FilesMedia, "Media", "", MediaName);

    MENU_L(FilesMedia, 0, MF_BYPOSITION | MF_STRING , ID_Meida_REMOVE , "Remove", "", "&Remove");

	if (MediaPause)
	{
		MENU_L(FilesMedia, 1, MF_BYPOSITION | MF_STRING , ID_Meida_PAUSE, "Continue", "", "&Continue");
	}
	else
	{
		MENU_L(FilesMedia, 1, MF_BYPOSITION | MF_STRING , ID_Meida_PAUSE, "Pause", "", "&Pause");
	}

		MENU_L(FilesMedia, 2, MF_BYPOSITION | MF_STRING , ID_Meida_STOP , "Stop", "", "&Stop");
        MENU_L(FilesMedia, 3, MF_BYPOSITION | MF_STRING , ID_Meida_REPLAY , "Replay", "", "&Replay");

  }

	MENU_L(Files, i++, Flags, ID_FILES_GAMEGENIE, "Game Genie", "", "&Game Genie");

	if(Genesis_Started || _32X_Started)
	{
    MENU_L(Files, i++, Flags, ID_WRITE_GAMEINFO, "Rom Info", "", "&Rom Info");
    MENU_L(Files, i++, Flags, IDD_SetGameNameDisplayed, "Config Displayed Game Name", "", "&Config Displayed Game Name");
	}

    if(TranslationMode)
	{
	MENU_L(Files, i++, Flags | MF_POPUP, (UINT)TranslationManager, "Tickler Manager", "", "&Tickler Manager");
	}

	if (TranslationMode)
	{
		MENU_L(TranslationManager, 0, Flags | MF_CHECKED, Translate_Mode_Set, "Tickler Mode", "", "&Tickler Mode");
	}
	else
	{
		MENU_L(Files, i++, Flags | MF_UNCHECKED,Translate_Mode_Set, "Tickler Mode", "", "&Tickler Mode");
	}

	if(TranslationMode)
	{
	if (DisplayValidArea)
	{
		MENU_L(Translation_Advanced, 0, Flags | MF_CHECKED, ID_DisplayArea, "Display Valid Area", "", "&Display Valid Area");
	}
	else
	{
		MENU_L(Translation_Advanced, 0, Flags | MF_UNCHECKED,ID_DisplayArea, "Display Valid Area", "", "&Display Valid Area");
	}
	MENU_L(TranslationManager, 2, Flags, Translation_LoadDate, "Load Avd", "", "&Load Avd");
	
    MENU_L(TranslationManager, 3, Flags | MF_POPUP, (UINT)Translation_Advanced, "Advanced", "", "&Advanced");
    
	MENU_L(Translation_Advanced, 1, Flags, Translation_SaveDate, "Save Tickler As", "", "&Save Tickler As");
    MENU_L(Translation_Advanced, 2, Flags, Import_AVD, "Import Tickler", "", "&Import Tickler");
	MENU_L(Translation_Advanced, 3, Flags, TRANSLATION_EDIT, "Edit Tickler", "", "&Edit Tickler");
    MENU_L(Translation_Advanced, 4, Flags, Input_Translation, "Add Notes", "", "&Add Notes");
    MENU_L(Translation_Advanced, 5, Flags, Set_Valid_Area, "Adjust Valid Area", "", "&Adjust Valid Area");

	}



   if(MoviePlaying)
   {
       MENU_L(Files, 1, Flags , ID_STOPMOVIEPLAYING , "Stop GIM", "", "&Stop GIM");
   }

if(!MoviePlaying && Game)
{
   if (MovieRecord)
	{
		MENU_L(Files, i++, Flags , ID_INPUTMOVIE , "Stop GIM Record", "", "&Stop GIM Record");
	}
	else
	{
		MENU_L(Files, i++, Flags , ID_INPUTMOVIE , "Start GIM Record", "", "&Start GIM Record");
	}
}
	InsertMenu(Files, i++, MF_SEPARATOR, NULL, NULL);

	MENU_L(Files, i++, Flags | MF_POPUP, (UINT)FileSaveStates, "State Manager", "", "&State Manager");

	MENU_L(FileSaveStates, i++, Flags, ID_FILES_LOADSTATEAS, "Load State as", "\tShift+F8", "&Load State ...");
	MENU_L(FileSaveStates, i++, Flags, ID_FILES_SAVESTATEAS, "Save State as", "\tShift+F5", "&Save State as...");
	MENU_L(FileSaveStates, i++, Flags, ID_FILES_LOADSTATE, "Load State", "\tF8", "Quick &Load");
	MENU_L(FileSaveStates, i++, Flags, ID_FILES_SAVESTATE, "Save State", "\tF5", "Quick &Save");
	MENU_L(FileSaveStates, i++, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT)FilesChangeState, "Change State", "\tF6-F7", "C&hange State");

//	InsertMenu(Files, i++, MF_SEPARATOR, NULL, NULL);

	if (strcmp(Recent_Rom[0], ""))
	{
		MENU_L(Files, i++, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT)FilesHistory, "Rom History", "", "&ROM History");
		InsertMenu(Files, i++, MF_SEPARATOR, NULL, NULL);
	}

	MENU_L(Files, i++, Flags, ID_FILES_QUIT, "Quit", "", "&Quit");

	// Menu FilesChangeState
	
	for(i = 0; i < 10; i++)
	{
		wsprintf(Str_Tmp ,"&%d", i);

		if(Current_State == i)
			InsertMenu(FilesChangeState, i, Flags | MF_CHECKED, ID_FILES_CHANGESTATE_0 + i, Str_Tmp);
		else
			InsertMenu(FilesChangeState, i, Flags | MF_UNCHECKED, ID_FILES_CHANGESTATE_0 + i, Str_Tmp);
	}

	// Menu FilesHistory
	
	for(i = 0; i < 9; i++)
	{
		if (strcmp(Recent_Rom[i], ""))
		{
			char tmp[1024];

			switch (Detect_Format(Recent_Rom[i]) >> 1)			// do not exist anymore
			{
				default:
					strcpy(tmp, "[---]\t- ");
					break;

				case 1:
					strcpy(tmp, "[MD]\t- ");
					break;

				case 2:
					strcpy(tmp, "[32X]\t- ");
					break;

				case 3:
					strcpy(tmp, "[SCD]\t- ");
					break;

				case 4:
					strcpy(tmp, "[SCDX]\t- ");
					break;
			}

			Get_Name_From_Path(Recent_Rom[i], Str_Tmp);
			strcat(tmp, Str_Tmp);
			InsertMenu(FilesHistory, i, Flags, ID_FILES_OPENRECENTROM0 + i, tmp);

		}
		else break;
	}

	// Menu Graphics

	Flags = MF_BYPOSITION | MF_STRING;
	
	if (Full_Screen)
	{
		MENU_L(Graphics, 0, Flags, ID_GRAPHICS_SWITCH_MODE, "Windowed", "\tAlt+Enter", "&Windowed");
	}
	else
	{
		MENU_L(Graphics, 0, Flags, ID_GRAPHICS_SWITCH_MODE, "Full Screen", "\tAlt+Enter", "&Full Screen");
	}

	if (Sprite_Over)
	{
		MENU_L(Graphics, 1, Flags | MF_CHECKED, ID_GRAPHICS_SPRITEOVER, "Sprite Limit", "", "&Sprite Limit");
	}
	else
	{
		MENU_L(Graphics, 1, Flags | MF_UNCHECKED, ID_GRAPHICS_SPRITEOVER, "Sprite Limit", "", "&Sprite Limit");
	}

	if (Full_Screen)
	{
		if(FS_VSync)
		{
		MENU_L(Graphics, 3, Flags | MF_CHECKED, ID_GRAPHICS_VSYNC, "VSync", "\tShift+F3", "&VSync"); //VSyunc;
		}
	    else
		{
		MENU_L(Graphics, 3, Flags | MF_UNCHECKED, ID_GRAPHICS_VSYNC, "VSync", "\tShift+F3", "&VSync");
		}
	}
	InsertMenu(Graphics, 4, MF_SEPARATOR, NULL, NULL);
/*
	if ((Full_Screen) && (Render_FS > 1))
	{
		MENU_L(Graphics, 2, Flags | MF_UNCHECKED | MF_GRAYED, ID_GRAPHICS_STRETCH, "Stretch", "\tShift+F2", "&Stretch");//tretch;
	}
	else
	{
		if (Stretch)
		{
			MENU_L(Graphics, 2, Flags | MF_CHECKED, ID_GRAPHICS_STRETCH, "Stretch", "\tShift+F2", "&Stretch");
		}
		else
		{
			MENU_L(Graphics, 2, Flags | MF_UNCHECKED, ID_GRAPHICS_STRETCH, "Stretch", "\tShift+F2", "&Stretch");
		}
	}
*/

	MENU_L(Graphics, 5, Flags | MF_POPUP, (UINT)ColorManager, "Color Manager", "", "&Color Manager");
	MENU_L(Graphics, 6, Flags | MF_POPUP, (UINT)GraphicsRender, "Video Out", "", "&Video Out");
    MENU_L(GraphicsRender, 0, Flags | MF_POPUP, (UINT)RenderMode, "Render", "", "&Render");
	MENU_L(GraphicsRender, 1, Flags | MF_POPUP, (UINT)StretchMode, "Stretch", "", "&Stretch");
	InsertMenu(Graphics, 7, MF_SEPARATOR, NULL, NULL);
//Custom Stretch;
    MENU_L(StretchMode, 0, Flags, ID__STRETCH_CUSTOM_SET, "Stretch Adjust", "", "&Stretch Adjust");
 
//Color Manager;
   if (Force32bpp)
	{
		MENU_L(ColorManager, 1, Flags | MF_CHECKED, ID_FORCE32BPP, "Force 32bpp", "", "&Force 32bpp");
	}
	else
	{
		MENU_L(ColorManager, 1, Flags | MF_UNCHECKED, ID_FORCE32BPP, "Force 32bpp", "", "&Force 32bpp");
	}

    if(Cdp ==32)
	{
    if (Glossiness_Mode)
	{
		MENU_L(ColorManager, 2, Flags | MF_CHECKED, Gens32_Glossiness, "Glossiness Mode", "", "&Glossiness Mode");
	}
	else
	{
		MENU_L(ColorManager, 2, Flags | MF_UNCHECKED, Gens32_Glossiness, "Glossiness Mode", "", "&Glossiness Mode");
	}
	}

    MENU_L(ColorManager, 3, Flags, ID_GRAPHICS_COLOR_ADJUST, "Basal Adjust", "", "&Basal Adjust");

	MENU_L(Graphics, 10, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT)GraphicsFrameSkip, "Frame Skip", "", "&Frame Skip");
	InsertMenu(Graphics, 11, MF_SEPARATOR, NULL, NULL);
	MENU_L(Graphics, 12, Flags | MF_UNCHECKED, ID_GRAPHICS_SHOT, "Screen Shot", "\tShift+Backspc", "&Screen Shot");

if(!Full_Screen)
	{
//Windowed mode Stretch;
   MENU_L(StretchMode, 1, Flags, Stretch_W_FixedRate, "Fixed Rate", "", "&Fixed Rate");

  if(Stretch_AUTO)
	{
		MENU_L(StretchMode, 2, MF_BYPOSITION | MF_STRING | MF_CHECKED, Stretch_W_AUTO, "Auto Stretch", "", "&Auto Stretch");
	}
	else
	{
		MENU_L(StretchMode, 2, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, Stretch_W_AUTO, "Auto Stretch", "", "&Auto Stretch");
	}

    if(Stretch_W_Index == Stretch_W_2X)
	{
		MENU_L(StretchMode, 3, MF_BYPOSITION | MF_STRING | MF_CHECKED, Stretch_W_2X, "Stretch to 2X", "", "&Stretch to 2X");
	}
	else
	{
		MENU_L(StretchMode, 3, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, Stretch_W_2X, "Stretch to 2X", "", "&Stretch to 2X");
	}

    if (Stretch_W_Index == Stretch_W_3X)
	{
		MENU_L(StretchMode, 4, MF_BYPOSITION | MF_STRING | MF_CHECKED, Stretch_W_3X, "Stretch to 3X", "", "&Stretch to 3X");
	}
	else
	{
		MENU_L(StretchMode, 4, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, Stretch_W_3X, "Stretch to 3X", "", "&Stretch to 3X");
	}

    if (Stretch_W_Index == Stretch_W_4X)
	{
		MENU_L(StretchMode, 5, MF_BYPOSITION | MF_STRING | MF_CHECKED, Stretch_W_4X, "Stretch to 4X", "", "&Stretch to 4X");
	}
	else
	{
		MENU_L(StretchMode, 5, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, Stretch_W_4X, "Stretch to 4X", "", "&Stretch to 4X");
	}

    if(Stretch_W_Index == Stretch_W_Custom)
	{
		MENU_L(StretchMode, 6, MF_BYPOSITION | MF_STRING | MF_CHECKED, Stretch_W_Custom, "Customized", "", "&Customize");
	}
	else
	{
		MENU_L(StretchMode, 6, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, Stretch_W_Custom, "Customized", "", "&Customized");
	}

}
	// Menu GraphicsRender

	if (Rend == 0)
	{
		MENU_L(RenderMode, 0, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_GRAPHICS_RENDER_NORMAL, "Normal", "", "&Normal");
	}
	else
	{
		MENU_L(RenderMode, 0, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_GRAPHICS_RENDER_NORMAL, "Normal", "", "&Normal");
	}
    if(!Full_Screen)
	{
	if (Rend == 1)
	{
		MENU_L(StretchMode, 1, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_GRAPHICS_RENDER_DOUBLE, "Fixed Window", "", "&Fixed Window");
	}
	else
	{
		MENU_L(StretchMode, 1, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_GRAPHICS_RENDER_DOUBLE, "Fixed Window", "", "&Fixed Window");
	}
	}
if(Cdp ==32 || Full_Screen)
{

    MENU_L(ColorManager, 4, Flags | MF_POPUP, (UINT)GraphicsHueSelection, "Hue Selection", "", "&Hue Selection");

// Menu Hue Selection;
if (Hue == 4)
	{
		MENU_L(GraphicsHueSelection, 0, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_HUE_NORMAL , "Normal", "", "&Normal");
	}
	else
	{
		MENU_L(GraphicsHueSelection, 0, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_HUE_NORMAL, "Normal", "", "&Normal");
	}

if (Hue == 3)
	{
		MENU_L(GraphicsHueSelection, 1, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_HUE_WARM , "Warm", "", "&Warm");
	}
	else
	{
		MENU_L(GraphicsHueSelection, 1, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_HUE_WARM, "Warm", "", "&Warm");
	}

if (Hue == 2)
	{
		MENU_L(GraphicsHueSelection, 2, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_HUE_WARMER , "Warmer", "", "&Warmer");
	}
	else
	{
		MENU_L(GraphicsHueSelection, 2, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_HUE_WARMER, "Warmer", "", "&Warmer");
	}

if (Hue == 1)
	{
		MENU_L(GraphicsHueSelection, 3, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_HUE_WARMEST , "Warmest", "", "&Warmest");
	}
	else
	{
		MENU_L(GraphicsHueSelection, 3, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_HUE_WARMEST, "Warmest", "", "&Warmest");
	}

if (Hue == 5)
	{
		MENU_L(GraphicsHueSelection, 4, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_HUE_COLD , "Cold", "", "&Cold");
	}
	else
	{
		MENU_L(GraphicsHueSelection, 4, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_HUE_COLD, "Cold", "", "&Cold");
	}

if (Hue == 6)
	{
		MENU_L(GraphicsHueSelection, 5, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_HUE_COLDER, "Colder", "", "&Colder");
	}
	else
	{
		MENU_L(GraphicsHueSelection, 5, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_HUE_COLDER, "Colder", "", "&Colder");
	}

if (Hue == 7)
	{
		MENU_L(GraphicsHueSelection, 6, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_HUE_COLDEST, "Coldest", "", "&Coldest");
	}
	else
	{
		MENU_L(GraphicsHueSelection, 6, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_HUE_COLDEST, "Coldest", "", "&Coldest");
	}


	if(Full_Screen)
	{

//Stretch Model;
 //    MENU_L(StretchMode, 1, Flags | MF_POPUP, (UINT)GraphicsStretch, "Stretch Model", "", "&Stretch Model");

  if (StretchModelIndex ==0)
	{
		MENU_L(StretchMode, 1, Flags | MF_CHECKED, ID__STRETCH_640_480, "640 * 480", "", "&640 * 480");
	}
	else
	{
		MENU_L(StretchMode, 1, Flags | MF_UNCHECKED, ID__STRETCH_640_480, "640 * 480", "", "&640 * 480");
	}

  if (StretchModelIndex ==1)
	{
		MENU_L(StretchMode, 2, Flags | MF_CHECKED,ID__STRETCH_800_600, "800 * 600", "", "&800 * 600");
	}
	else
	{
		MENU_L(StretchMode, 2, Flags | MF_UNCHECKED, ID__STRETCH_800_600, "800 * 600", "", "&800 * 600");
	}

 if (StretchModelIndex ==2)
	{
		MENU_L(StretchMode, 3, Flags | MF_CHECKED, ID__STRETCH_1024_768, "1024 * 768", "", "&1024 * 768");
	}
	else
	{
		MENU_L(StretchMode, 3, Flags | MF_UNCHECKED,ID__STRETCH_1024_768, "1024 * 768", "", "&1024 * 768");
	}

 if (StretchModelIndex ==3)
	{
		MENU_L(StretchMode, 4, Flags | MF_CHECKED, ID__STRETCH_720_480, "720 * 480", "", "&720 * 480");
	}
	else
	{
		MENU_L(StretchMode, 4, Flags | MF_UNCHECKED, ID__STRETCH_720_480, "720 * 480", "", "&720 * 480");
	}

  if (StretchModelIndex ==4)
	{
		MENU_L(StretchMode, 5, Flags | MF_CHECKED,ID__STRETCH_720_576, "720 * 576", "", "&720 * 576");
	}
	else
	{
		MENU_L(StretchMode, 5, Flags | MF_UNCHECKED, ID__STRETCH_720_576, "720 * 576", "", "&720 * 576");
	}

 if (StretchModelIndex ==5)
	{
		MENU_L(StretchMode, 6, Flags | MF_CHECKED, ID__STRETCH_848_480, "848 * 480", "", "&848 * 480");
	}
	else
	{
		MENU_L(StretchMode, 6, Flags | MF_UNCHECKED,ID__STRETCH_848_480, "848 * 480", "", "&848 * 480");
	}


 if (StretchModelIndex ==6)
	{
		MENU_L(StretchMode, 7, Flags | MF_CHECKED, ID__STRETCH_1152_864, "1152 * 864", "", "&1152 * 864");
	}
	else
	{
		MENU_L(StretchMode, 7, Flags | MF_UNCHECKED, ID__STRETCH_1152_864, "1152 * 864", "", "&1152 * 864");
	}

  if (StretchModelIndex ==7)
	{
		MENU_L(StretchMode, 8, Flags | MF_CHECKED,ID__STRETCH_1280_720, "1280 * 720", "", "&1280 * 720");
	}
	else
	{
		MENU_L(StretchMode, 8, Flags | MF_UNCHECKED, ID__STRETCH_1280_720, "1280 * 720", "", "&1280 * 720");
	}

 if (StretchModelIndex ==8)
	{
		MENU_L(StretchMode, 9, Flags | MF_CHECKED, ID__STRETCH_1280_768, "1280 * 768", "", "&1280 * 768");
	}
	else
	{
		MENU_L(StretchMode, 9, Flags | MF_UNCHECKED,ID__STRETCH_1280_768, "1280 * 768", "", "&1280 * 768");
	}


 if (StretchModelIndex ==9)
	{
		MENU_L(StretchMode, 10, Flags | MF_CHECKED, ID__STRETCH_1280_960, "1280 * 960", "", "&1280 * 960");
	}
	else
	{
		MENU_L(StretchMode, 10, Flags | MF_UNCHECKED, ID__STRETCH_1280_960, "1280 * 960", "", "&1280 * 960");
	}

  if (StretchModelIndex ==10)
	{
		MENU_L(StretchMode, 11, Flags | MF_CHECKED,ID__STRETCH_1280_1024, "1280 * 1024", "", "&1280 * 1024");
	}
	else
	{
		MENU_L(StretchMode, 11, Flags | MF_UNCHECKED, ID__STRETCH_1280_1024, "1280 * 1024", "", "&1280 * 1024");
	}

  if (StretchModelIndex ==11)
	{
		MENU_L(StretchMode, 12, Flags | MF_CHECKED,ID__STRETCH_CUSTOM, "Customized", "", "&Customized");
	}
	else
	{
		MENU_L(StretchMode, 12, Flags | MF_UNCHECKED, ID__STRETCH_CUSTOM, "Customized", "", "&Customized");
	}

	}

	if (Rend == 2)
	{
		MENU_L(RenderMode, 2, MF_BYPOSITION | MF_CHECKED, ID_GRAPHICS_RENDER_DOUBLE_INT, "Monitor", "", "&Monitor");
	}
	else
	{
		MENU_L(RenderMode, 2, MF_BYPOSITION | MF_UNCHECKED, ID_GRAPHICS_RENDER_DOUBLE_INT, "Monitor", "", "&Monitor");
	}

	if (Rend == 3)
	{
		MENU_L(RenderMode, 3, MF_BYPOSITION | MF_CHECKED, ID_GRAPHICS_RENDER_FULLSCANLINE, "LCD", "", "&LCD");
	}
	else
	{
		MENU_L(RenderMode, 3, MF_BYPOSITION | MF_UNCHECKED, ID_GRAPHICS_RENDER_FULLSCANLINE, "LCD", "", "&LCD");
	}

	if (Rend == 4)
	{
			MENU_L(RenderMode, 4, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_GRAPHICS_RENDER_50SCANLINE, "Scan Lines", "", "&1:1 Scan Lines");
	}
	else
	{
	        MENU_L(RenderMode, 4, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_GRAPHICS_RENDER_50SCANLINE, "Scan Lines", "", "&1:1 Scan Lines");
	}

		if (Rend == 5)
		{
			MENU_L(RenderMode, 5, MF_BYPOSITION | MF_CHECKED, ID_GRAPHICS_RENDER_25SCANLINE, "TV", "", "&TV");
		}
		else
		{
			MENU_L(RenderMode, 5, MF_BYPOSITION | MF_UNCHECKED, ID_GRAPHICS_RENDER_25SCANLINE, "TV", "", "&TV");
		}


	if (Rend == 6)
	{
		MENU_L(RenderMode, 6, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_GRAPHICS_RENDER_INTESCANLINE, "Paper", "", "&Paper");
	}
	else
	{
		MENU_L(RenderMode, 6, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_GRAPHICS_RENDER_INTESCANLINE, "Paper", "", "&Paper");
	}

	if (Rend == 7)
	{
		MENU_L(RenderMode, 7, MF_BYPOSITION | MF_STRING | MF_CHECKED, ID_GRAPHICS_RENDER_INT50SCANLIN, "Gauze", "", "&Gauze");
	}
	else
	{
	MENU_L(RenderMode, 7, MF_BYPOSITION | MF_STRING | MF_UNCHECKED, ID_GRAPHICS_RENDER_INT50SCANLIN, "Gauze", "", "&Gauze");
	}
    if(Have_MMX)
	{
	if (Rend == 8)
	{
			MENU_L(RenderMode, 8, MF_BYPOSITION | MF_CHECKED, ID_GRAPHICS_RENDER_INT25SCANLIN, "Hq2x", "", "&Hq2x");
	}
	else
	{
			MENU_L(RenderMode, 8, MF_BYPOSITION | MF_UNCHECKED, ID_GRAPHICS_RENDER_INT25SCANLIN, "Hq2x", "", "&Hq2x");
	}
	}
/*
	if (Rend == 10)
	{
			MENU_L(RenderMode, 9, MF_BYPOSITION | MF_CHECKED, ID_GRAPHICS_RENDER_FOURSCREEN, "Four Screen", "", "&Four Screen");
	}
	else
	{
			MENU_L(RenderMode, 9, MF_BYPOSITION | MF_UNCHECKED, ID_GRAPHICS_RENDER_FOURSCREEN, "Four Screen", "", "&Four Screen");
	}
*/
		if (Rend == 9)
		{
			MENU_L(RenderMode, 10, MF_BYPOSITION | MF_CHECKED, ID_GRAPHICS_RENDER_2XSAI, "Use Plug", "", "&Use Plug");
		}
		else
		{
			MENU_L(RenderMode, 10, MF_BYPOSITION | MF_UNCHECKED, ID_GRAPHICS_RENDER_2XSAI, "Use Plug", "", "&Use Plug");
		}
	 }

	// Menu GraphicsFrameSkip

	Flags = MF_BYPOSITION | MF_STRING;

	if (Frame_Skip == -1)
	{
		MENU_L(GraphicsFrameSkip, 0, Flags | MF_CHECKED, ID_GRAPHICS_FRAMESKIP_AUTO, "Auto", "", "&Auto");
	}
	else
	{
		MENU_L(GraphicsFrameSkip, 0, Flags | MF_UNCHECKED, ID_GRAPHICS_FRAMESKIP_AUTO, "Auto", "", "&Auto");
	}

	for(i = 0; i < 9; i++)
	{
		wsprintf(Str_Tmp ,"&%d", i);

		if (Frame_Skip == i)
			InsertMenu(GraphicsFrameSkip, i + 1, Flags | MF_CHECKED, ID_GRAPHICS_FRAMESKIP_0 + i, Str_Tmp);
		else
			InsertMenu(GraphicsFrameSkip, i + 1, Flags | MF_UNCHECKED, ID_GRAPHICS_FRAMESKIP_0 + i, Str_Tmp);
	}
	
	// Menu CPU

	i = 0;

#ifdef GENS_DEBUG
	MENU_L(CPU, i++, Flags | MF_POPUP, (UINT)CPUDebug, "Debug", "", "&Debug");
	InsertMenu(CPU, i++, MF_SEPARATOR, NULL, NULL);
#endif

	MENU_L(CPU, i++, Flags | MF_POPUP, (UINT)CPUCountry, "Country", "", "&Country");
	InsertMenu(CPU, i++, MF_SEPARATOR, NULL, NULL);

       MENU_L(CPU, i++, Flags, ID_CPU_RESET, "Hard Reset", "\tTAB", "&Hard Reset");

	if (SegaCD_Started)
	{
		MENU_L(CPU, i++, Flags, ID_CPU_RESET68K, "Reset main 68000", "", "Reset &main 68000");
		MENU_L(CPU, i++, Flags, ID_CPU_RESET_SUB68K, "Reset sub 68000", "", "Reset &sub 68000");
	}
	else if (_32X_Started)
	{
		MENU_L(CPU, i++, Flags, ID_CPU_RESET68K, "Reset 68K", "", "Reset &68000");
		MENU_L(CPU, i++, Flags, ID_CPU_RESET_MSH2, "Reset master SH2", "", "Reset master SH2");
		MENU_L(CPU, i++, Flags, ID_CPU_RESET_SSH2, "Reset slave SH2", "", "Reset slave SH2");
	}
	else
	{
		MENU_L(CPU, i++, Flags, ID_CPU_RESET68K, "Reset 68K", "", "Reset &68000");
	}

	MENU_L(CPU, i++, Flags, ID_CPU_RESETZ80, "Reset Z80", "", "Reset &Z80");

	if (!Genesis_Started && !_32X_Started)
	{
		InsertMenu(CPU, i++, MF_SEPARATOR, NULL, NULL);

		if (SegaCD_Accurate)
		{
			MENU_L(CPU, i++, Flags | MF_CHECKED, ID_CPU_ACCURATE_SYNCHRO, "Perfect Synchro", "", "&Perfect Synchro (SLOW)");
		}
		else
		{
			MENU_L(CPU, i++, Flags | MF_UNCHECKED, ID_CPU_ACCURATE_SYNCHRO, "Perfect Synchro", "", "&Perfect Synchro (SLOW)");
		}
	}

#ifdef GENS_DEBUG
	// Menu CPU Debug

	if (Debug == 1) Flags |= MF_CHECKED;
	else Flags &= ~MF_CHECKED;

	MENU_L(CPUDebug, 0, Flags, ID_CPU_DEBUG_GENESIS_68000, "Genesis - 68000", "", "&Genesis - 68000");

	if (Debug == 2) Flags |= MF_CHECKED;
	else Flags &= ~MF_CHECKED;

	MENU_L(CPUDebug, 1, Flags, ID_CPU_DEBUG_GENESIS_Z80, "Genesis - Z80", "", "Genesis - &Z80");

	if (Debug == 3) Flags |= MF_CHECKED;
	else Flags &= ~MF_CHECKED;

	MENU_L(CPUDebug, 2, Flags, ID_CPU_DEBUG_GENESIS_VDP, "Genesis - VDP", "", "Genesis - &VDP");

	i = 3;

	if (SegaCD_Started)
	{
		if (Debug == (i + 1)) Flags |= MF_CHECKED;
		else Flags &= ~MF_CHECKED;

		MENU_L(CPUDebug, i++, Flags, ID_CPU_DEBUG_SEGACD_68000, "SegaCD - 68000", "", "&SegaCD - 68000");

		if (Debug == (i + 1)) Flags |= MF_CHECKED;
		else Flags &= ~MF_CHECKED;

		MENU_L(CPUDebug, i++, Flags, ID_CPU_DEBUG_SEGACD_CDC, "SegaCD - CDC", "", "SegaCD - &CDC");

		if (Debug == (i + 1)) Flags |= MF_CHECKED;
		else Flags &= ~MF_CHECKED;

		MENU_L(CPUDebug, i++, Flags, ID_CPU_DEBUG_SEGACD_GFX, "SegaCD - GFX", "", "SegaCD - GF&X");
	}

	if (_32X_Started)
	{
		if (Debug == (i + 1)) Flags |= MF_CHECKED;
		else Flags &= ~MF_CHECKED;

		MENU_L(CPUDebug, i++, Flags, ID_CPU_DEBUG_32X_MAINSH2, "32X - main SH2", "", "32X - main SH2");

		if (Debug == (i + 1)) Flags |= MF_CHECKED;
		else Flags &= ~MF_CHECKED;

		MENU_L(CPUDebug, i++, Flags, ID_CPU_DEBUG_32X_SUBSH2, "32X - sub SH2", "", "32X - sub SH2");

		if (Debug == (i + 1)) Flags |= MF_CHECKED;
		else Flags &= ~MF_CHECKED;

		MENU_L(CPUDebug, i++, Flags, ID_CPU_DEBUG_32X_VDP, "32X - VDP", "", "32X - VDP");
	}
#endif


	// Menu CPU Country

	Flags = MF_BYPOSITION | MF_STRING;

	if (Country == -1)
	{
		MENU_L(CPUCountry, 0, Flags | MF_CHECKED, ID_CPU_COUNTRY_AUTO, "Auto detect", "", "&Auto detect");
	}
	else
	{
		MENU_L(CPUCountry, 0, Flags | MF_UNCHECKED, ID_CPU_COUNTRY_AUTO, "Auto detect", "", "&Auto detect");
	}
	if (Country == 0)
	{
		MENU_L(CPUCountry, 1, Flags | MF_CHECKED, ID_CPU_COUNTRY_JAPAN, "Japan (NTSC)", "", "&Japan (NTSC)");
	}
	else
	{
		MENU_L(CPUCountry, 1, Flags | MF_UNCHECKED, ID_CPU_COUNTRY_JAPAN, "Japan (NTSC)", "", "&Japan (NTSC)");
	}
	if (Country == 1)
	{
		MENU_L(CPUCountry, 2, Flags | MF_CHECKED, ID_CPU_COUNTRY_USA, "USA (NTSC)", "", "&USA (NTSC)");
	}
	else
	{
		MENU_L(CPUCountry, 2, Flags | MF_UNCHECKED, ID_CPU_COUNTRY_USA, "USA (NTSC)", "", "&USA (NTSC)");
	}
	if (Country == 2)
	{
		MENU_L(CPUCountry, 3, Flags | MF_CHECKED, ID_CPU_COUNTRY_EUROPE, "Europe (PAL)", "", "&Europe (PAL)");
	}
	else
	{
		MENU_L(CPUCountry, 3, Flags | MF_UNCHECKED, ID_CPU_COUNTRY_EUROPE, "Europe (PAL)", "", "&Europe (PAL)");
	}
	if (Country == 3)
	{
		MENU_L(CPUCountry, 4, Flags | MF_CHECKED, ID_CPU_COUNTRY_MISC, "Japan (PAL)", "", "Japan (PAL)");
	}
	else
	{
		MENU_L(CPUCountry, 4, Flags | MF_UNCHECKED, ID_CPU_COUNTRY_MISC, "Japan (PAL)", "", "Japan (PAL)");
	}

	InsertMenu(CPUCountry, 5, MF_SEPARATOR, NULL, NULL);

	MENU_L(CPUCountry, 6, Flags | MF_POPUP, (UINT)CPUCountryOrder, "Auto detection order", "", "&Auto detection order");

	// Menu CPU Prefered Country 

	for(i = 0; i < 3; i++)
	{
		if (Country_Order[i] == 0)
		{
			MENU_L(CPUCountryOrder, i, Flags, ID_CPU_COUNTRY_ORDER + i, "USA (NTSC)", "", "&USA (NTSC)");
		}
		else if (Country_Order[i] == 1)
		{
			MENU_L(CPUCountryOrder, i, Flags, ID_CPU_COUNTRY_ORDER + i, "Japan (NTSC)", "", "&Japan (NTSC)");
		}
		else
		{
			MENU_L(CPUCountryOrder, i, Flags, ID_CPU_COUNTRY_ORDER + i, "Europe (PAL)", "", "&Europe (PAL)");
		}
	}

	// Menu Sound

	if (Sound_Enable)
	{
		MENU_L(Sound, 0, Flags | MF_CHECKED, ID_SOUND_ENABLE, "Enable", "", "&Enable");
	}
	else
	{
		MENU_L(Sound, 0, Flags | MF_UNCHECKED, ID_SOUND_ENABLE, "Enable", "", "&Enable");
	}

	if (Sound_Stereo)
	{
		MENU_L(Sound, 1, Flags | MF_CHECKED, ID_SOUND_STEREO, "Stereo", "", "&Stereo");
	}
	else
	{
		MENU_L(Sound, 1, Flags | MF_UNCHECKED, ID_SOUND_STEREO, "Stereo", "", "&Stereo");
	}

	InsertMenu(Sound, 2, MF_SEPARATOR, NULL, NULL);

	MENU_L(Sound, 3, Flags | MF_POPUP, (UINT)SoundRate, "Rate", "", "&Rate");
	MENU_L(Sound, 4, Flags | MF_POPUP, (UINT)SoundVolume, "Volume", "", "&Volume");

	InsertMenu(Sound, 5, MF_SEPARATOR, NULL, NULL);

	MENU_L(Sound, 6, Flags | MF_POPUP, (UINT)ChipSetSwitchs, "Chipset", "", "&Chipset");

	if (Z80_State & 1)
		InsertMenu(ChipSetSwitchs, 0, Flags | MF_CHECKED, ID_SOUND_Z80ENABLE, "&Z80");
	else
		InsertMenu(ChipSetSwitchs, 0, Flags | MF_UNCHECKED, ID_SOUND_Z80ENABLE, "&Z80");

	if (YM2612_Enable)
		InsertMenu(ChipSetSwitchs, 1, Flags | MF_CHECKED, ID_SOUND_YM2612ENABLE, "&YM2612");
	else
		InsertMenu(ChipSetSwitchs, 1, Flags | MF_UNCHECKED, ID_SOUND_YM2612ENABLE, "&YM2612");

	if (PSG_Enable)
		InsertMenu(ChipSetSwitchs, 2, Flags | MF_CHECKED, ID_SOUND_PSGENABLE, "&PSG");
	else
		InsertMenu(ChipSetSwitchs, 2, Flags | MF_UNCHECKED, ID_SOUND_PSGENABLE, "&PSG");

	if (DAC_Enable)
		InsertMenu(ChipSetSwitchs, 3, Flags | MF_CHECKED, ID_SOUND_DACENABLE, "&DAC");
	else
		InsertMenu(ChipSetSwitchs, 3, Flags | MF_UNCHECKED, ID_SOUND_DACENABLE, "&DAC");

	i = 9;
	
	if (!Genesis_Started && !_32X_Started)
	{
		if (PCM_Enable)
			InsertMenu(ChipSetSwitchs, i++, Flags | MF_CHECKED, ID_SOUND_PCMENABLE, "P&CM");
		else
			InsertMenu(ChipSetSwitchs, i++, Flags | MF_UNCHECKED, ID_SOUND_PCMENABLE, "P&CM");
	}

	if (!Genesis_Started && !SegaCD_Started)
	{
		if (PWM_Enable)
			InsertMenu(ChipSetSwitchs, i++, Flags | MF_CHECKED, ID_SOUND_PWMENABLE, "P&WM");
		else
			InsertMenu(ChipSetSwitchs, i++, Flags | MF_UNCHECKED, ID_SOUND_PWMENABLE, "P&WM");
	}

	if (!Genesis_Started && !_32X_Started)
	{
		if (CDDA_Enable)
			InsertMenu(ChipSetSwitchs, i++, Flags | MF_CHECKED, ID_SOUND_CDDAENABLE, "CDD&A");
		else
			InsertMenu(ChipSetSwitchs, i++, Flags | MF_UNCHECKED, ID_SOUND_CDDAENABLE, "CDD&A");
	}

//	InsertMenu(Sound, i++, MF_SEPARATOR, NULL, NULL);

	MENU_L(Sound, 7, Flags | MF_POPUP, (UINT)SoundQuality, "Quality", "", "&Quality");

	if (YM2612_Improv)
	{
		MENU_L(SoundQuality, i++, Flags | MF_CHECKED, ID_SOUND_YMIMPROV, "YM2612 High Quality", "", "YM2612 High &Quality");
	}
	else
	{
		MENU_L(SoundQuality, i++, Flags | MF_UNCHECKED, ID_SOUND_YMIMPROV, "YM2612 High Quality", "", "YM2612 High &Quality");
	}

    if (DAC_Improv)
	{
		MENU_L(SoundQuality, i++, Flags | MF_CHECKED,ID_SOUND_DACIMPROV, "Improved DAC", "", "Improved DAC");
	}
	else
	{
		MENU_L(SoundQuality, i++, Flags | MF_UNCHECKED,ID_SOUND_DACIMPROV, "Improved DAC", "", "Improved DAC");
	}

    if (PSG_Improv)
	{
		MENU_L(SoundQuality, i++, Flags | MF_CHECKED, ID_SOUND_PSGIMPROV, "Improved PSG", "", "Improved PSG");
	}
	else
	{
		MENU_L(SoundQuality, i++, Flags | MF_UNCHECKED, ID_SOUND_PSGIMPROV, "Improved PSG", "", "Improved PSG");
	}

	InsertMenu(Sound, i++, MF_SEPARATOR, NULL, NULL);

	if (WAV_Dumping)
	{
		MENU_L(Sound, i++, Flags, ID_SOUND_STARTWAVDUMP, "Stop Dump", "", "Stop WAV Dump");
	}
	else
	{
		MENU_L(Sound, i++, Flags, ID_SOUND_STARTWAVDUMP, "Start Dump", "", "Start WAV Dump");
	}

	if (GYM_Dumping)
	{
		MENU_L(Sound, i++, Flags, ID_SOUND_STARTGYMDUMP, "Stop GYM Dump", "", "Stop GYM Dump");
	}
	else
	{
		MENU_L(Sound, i++, Flags, ID_SOUND_STARTGYMDUMP, "Start GYM Dump", "", "Start GYM Dump");
	}

//Sound Vloume;

	if (VolumeIndex ==VOLUME_2PX)
		InsertMenu(SoundVolume, 0, Flags | MF_CHECKED, VOLUME_2PX, "&1/2X");
	else
		InsertMenu(SoundVolume, 0, Flags | MF_UNCHECKED, VOLUME_2PX, "1/2X");

	if (VolumeIndex == VOLUME_4PX)
		InsertMenu(SoundVolume, 1, Flags | MF_CHECKED, VOLUME_4PX, "&1/4X");
	else
		InsertMenu(SoundVolume, 1, Flags | MF_UNCHECKED, VOLUME_4PX, "&1/4X");

	if (VolumeIndex == VOLUME_8PX)
		InsertMenu(SoundVolume, 2, Flags | MF_CHECKED, VOLUME_8PX, "&1/8X");
	else
		InsertMenu(SoundVolume, 2, Flags | MF_UNCHECKED, VOLUME_8PX, "&1/8X");

	if (VolumeIndex ==VOLUME_NORMAL)
	{
        MENU_L(SoundVolume, 3, Flags | MF_CHECKED, VOLUME_NORMAL, "Normal", "", "&Normal");
	}
	else
	{
         MENU_L(SoundVolume, 3, Flags | MF_UNCHECKED, VOLUME_NORMAL, "Normal", "", "&Normal");
	}

	if (VolumeIndex ==VOLUME_2X)
		InsertMenu(SoundVolume, 4, Flags | MF_CHECKED, VOLUME_2X, "&2X");
	else
		InsertMenu(SoundVolume, 4, Flags | MF_UNCHECKED, VOLUME_2X, "2X");

	if (VolumeIndex == VOLUME_4X)
		InsertMenu(SoundVolume, 5, Flags | MF_CHECKED, VOLUME_4X, "&4X");
	else
		InsertMenu(SoundVolume, 5, Flags | MF_UNCHECKED, VOLUME_4X, "&4X");

	// Sous-Menu SoundRate

	if (Sound_Rate == 11025)
		InsertMenu(SoundRate, 0, Flags | MF_CHECKED, ID_SOUND_RATE_11000, "&11025");
	else
		InsertMenu(SoundRate, 0, Flags | MF_UNCHECKED, ID_SOUND_RATE_11000, "&11025");

	if (Sound_Rate == 22050)
		InsertMenu(SoundRate, 1, Flags | MF_CHECKED, ID_SOUND_RATE_22000, "&22050");
	else
		InsertMenu(SoundRate, 1, Flags | MF_UNCHECKED, ID_SOUND_RATE_22000, "&22050");

	if (Sound_Rate == 44100)
		InsertMenu(SoundRate, 2, Flags | MF_CHECKED, ID_SOUND_RATE_44000, "&44100");
	else
		InsertMenu(SoundRate, 2, Flags | MF_UNCHECKED, ID_SOUND_RATE_44000, "&44100");

	if (Sound_Rate == 48000)
		InsertMenu(SoundRate, 3, Flags | MF_CHECKED, ID_SOUND_RATE_48000, "&48000");
	else
		InsertMenu(SoundRate, 3, Flags | MF_UNCHECKED, ID_SOUND_RATE_48000, "&48000");

	// Menu Options

	MENU_L(Options, 0, MF_BYPOSITION | MF_STRING, ID_OPTIONS_GENERAL, "Misc", "", "&Misc...");
	MENU_L(Options, 2, Flags, ID_OPTIONS_CHANGEDIR, "Directories", "", "&Directories...");
	MENU_L(Options, 3, Flags, ID_OPTIONS_CHANGEFILES, "Bios/Misc Files", "", "Bios/Misc &Files...");

//Specal Game Fix;
	if (Specal_Fix)
	{
		MENU_L(Options, 4, Flags | MF_CHECKED, ID_SPECALGAMEFIX , "Special Game Fixes", "", "&Special Game Fixes");
	}
	else
	{
		MENU_L(Options, 4, Flags | MF_UNCHECKED, ID_SPECALGAMEFIX , "Special Game Fixes", "", "&Special Game Fixes");
	}

	MENU_L(Options, 5, Flags | MF_POPUP, (UINT)OptionsInputAssistant, "Controllers Assistants", "", "Controllers Assistants");

//Mode To Change;
	if (ModeToChange)
	{
		MENU_L(OptionsInputAssistant, 0, Flags | MF_CHECKED, ID_MODETOCHANGE , "MODE To Change", "", "&MODE To Change");
	}
	else
	{
		MENU_L(OptionsInputAssistant, 0, Flags | MF_UNCHECKED, ID_MODETOCHANGE , "MODE To Change", "", "&MODE To Change");
	}
	MENU_L(OptionsInputAssistant, 1, Flags, ID_OPTIONS_JOYPADSETTING, "Define Controllers", "", "&Define Controllers");
	MENU_L(OptionsInputAssistant, 2, Flags, Controllers_Save, "Controllers Config Export", "", "&Controllers Config Export");
	MENU_L(OptionsInputAssistant, 3, Flags, Controllers_Load, "Controllers Config Import", "", "&Controllers Config Import");

    MENU_L(OptionsInputAssistant, 4, Flags | MF_POPUP, (UINT)ForceFeedback, "Force feedback", "", "Force feedback");
    MENU_L(OptionsInputAssistant, 5, Flags | MF_POPUP, (UINT)Assistants_1P_Turbo, "1P Turbo", "", "1P Turbo");
    MENU_L(OptionsInputAssistant, 6, Flags | MF_POPUP, (UINT)Assistants_2P_Turbo, "2P Turbo", "", "2P Turbo");
   
//Turbo 1P;
   if (Turbo_1P_A)
	{
		MENU_L(Assistants_1P_Turbo, 0, Flags | MF_CHECKED, ID__TURBO_1P_A, "A", "", "&A");
	}
	else
	{
		MENU_L(Assistants_1P_Turbo, 0, Flags | MF_UNCHECKED, ID__TURBO_1P_A, "A", "", "&A");
	}

  if (Turbo_1P_B)
	{
		MENU_L(Assistants_1P_Turbo, 1, Flags | MF_CHECKED, ID__TURBO_1P_B, "B", "", "&B");
	}
	else
	{
		MENU_L(Assistants_1P_Turbo, 1, Flags | MF_UNCHECKED, ID__TURBO_1P_B, "B", "", "&B");
	}

  if (Turbo_1P_C)
	{
		MENU_L(Assistants_1P_Turbo, 2, Flags | MF_CHECKED, ID__TURBO_1P_C, "C", "", "&C");
	}
	else
	{
		MENU_L(Assistants_1P_Turbo, 2, Flags | MF_UNCHECKED, ID__TURBO_1P_C, "C", "", "&C");
	}

//Turbo 2P;
    if (Turbo_2P_A)
	{
		MENU_L(Assistants_2P_Turbo, 0, Flags | MF_CHECKED, ID__TURBO_2P_A, "A", "", "&A");
	}
	else
	{
		MENU_L(Assistants_2P_Turbo, 0, Flags | MF_UNCHECKED, ID__TURBO_2P_A, "A", "", "&A");
	}

    if (Turbo_2P_B)
	{
		MENU_L(Assistants_2P_Turbo, 1, Flags | MF_CHECKED, ID__TURBO_2P_B, "B", "", "&B");
	}
	else
	{
		MENU_L(Assistants_2P_Turbo, 1, Flags | MF_UNCHECKED, ID__TURBO_2P_B, "B", "", "&B");
	}

    if (Turbo_2P_C)
	{
		MENU_L(Assistants_2P_Turbo, 2, Flags | MF_CHECKED, ID__TURBO_2P_C, "C", "", "&C");
	}
	else
	{
		MENU_L(Assistants_2P_Turbo, 2, Flags | MF_UNCHECKED, ID__TURBO_2P_C, "C", "", "&C");
	}

//Force FeedBack;
	if(!Check_If_Kaillera_Running())
	{
    if (FFD_A)
	{
		MENU_L(ForceFeedback, 0, Flags | MF_CHECKED, ID_FFD_A, "A", "", "&A");
	}
	else
	{
		MENU_L(ForceFeedback, 0, Flags | MF_UNCHECKED, ID_FFD_A, "A", "", "&A");
	}

    if (FFD_B)
	{
		MENU_L(ForceFeedback, 1, Flags | MF_CHECKED, ID_FFD_B, "B", "", "&B");
	}
	else
	{
		MENU_L(ForceFeedback, 1, Flags | MF_UNCHECKED, ID_FFD_B, "B", "", "&B");
	}

    if (FFD_C)
	{
		MENU_L(ForceFeedback, 2, Flags | MF_CHECKED, ID_FFD_C, "C", "", "&C");
	}
	else
	{
		MENU_L(ForceFeedback, 2, Flags | MF_UNCHECKED, ID_FFD_C, "C", "", "&C");
	}


 if (FFD_X)
	{
		MENU_L(ForceFeedback, 3, Flags | MF_CHECKED, ID_FFD_X, "X", "", "&X");
	}
	else
	{
		MENU_L(ForceFeedback, 3, Flags | MF_UNCHECKED, ID_FFD_X, "X", "", "&X");
	}

    if (FFD_Y)
	{
		MENU_L(ForceFeedback, 4, Flags | MF_CHECKED, ID_FFD_Y, "Y", "", "&Y");
	}
	else
	{
		MENU_L(ForceFeedback, 4, Flags | MF_UNCHECKED, ID_FFD_Y, "Y", "", "&Y");
	}

    if (FFD_Z)
	{
		MENU_L(ForceFeedback, 5, Flags | MF_CHECKED, ID_FFD_Z, "Z", "", "&Z");
	}
	else
	{
		MENU_L(ForceFeedback, 5, Flags | MF_UNCHECKED, ID_FFD_Z, "Z", "", "&Z");
	}

	}

	InsertMenu(Options, 6, MF_SEPARATOR, NULL, NULL);
    MENU_L(Options, 7, Flags | MF_POPUP, (UINT)SEGA_CD_Manager, "SEGA CD Manager", "", "SEGA CD Manager");
   
	if(One_Internal)
	{
		MENU_L(SEGA_CD_Manager, 0, Flags | MF_CHECKED, Use_One_Internal, "Shared Internal Ram", "", "&Shared Internal Ram");
	}
	else
	{
		MENU_L(SEGA_CD_Manager, 0, Flags | MF_UNCHECKED, Use_One_Internal, "Shared Internal Ram", "", "&Shared Internal Ram");
	}

	MENU_L(SEGA_CD_Manager, 1, Flags | MF_POPUP, (UINT)OptionsCDDrive, "Current CD Drive", "", "Current CD Drive");
	MENU_L(SEGA_CD_Manager, 2, Flags | MF_POPUP, (UINT)OptionsSRAMSize, "RAM Card Size", "", "RAM Card Size");

	//Export/Inport Brm;
	if (SegaCD_Started)
	{
	MENU_L(SEGA_CD_Manager, 3, Flags,Export_RAM_Carts, "Export RAM Card", "", "&Export RAM Card");

	MENU_L(SEGA_CD_Manager, 4, Flags,Inport_RAM_Carts, "Import RAM Card", "", "&Import RAM Card");
	}

	InsertMenu(Options, 9, MF_SEPARATOR, NULL, NULL);

	MENU_L(Options, 10, Flags, Edit_Config, "Edit config", "", "&Edit config");
	MENU_L(Options, 11, Flags, ID_OPTIONS_LOADCONFIG, "Load config", "", "&Load config");
	MENU_L(Options, 12, Flags, ID_OPTIONS_SAVEASCONFIG, "Save config as", "", "&Save config as");


	// Sous-Menu CDDrive

	if (Num_CD_Drive)
	{
		char drive_name[100];

		for(i = 0; i < Num_CD_Drive; i++)
		{
			ASPI_Get_Drive_Info(i, (unsigned char *) drive_name);

			if (CUR_DEV == i)
			{
				InsertMenu(OptionsCDDrive, i, Flags | MF_CHECKED, ID_OPTION_CDDRIVE_0 + i, &drive_name[8]);
			}
			else
			{
				InsertMenu(OptionsCDDrive, i, Flags | MF_UNCHECKED, ID_OPTION_CDDRIVE_0 + i, &drive_name[8]);
			}
		}
	}
	else
	{
		MENU_L(OptionsCDDrive, 0, Flags | MF_GRAYED, NULL, "No drive detected", "", "No Drive Detected");
	}


	// Sous-Menu SRAMSize

	if (BRAM_Ex_State & 0x100)
	{
		MENU_L(OptionsSRAMSize, 0, Flags | MF_UNCHECKED, ID_OPTION_SRAMSIZE_0, "None", "", "&None");

		for (i = 0; i < 4; i++)
		{
			char bsize[16];

			sprintf(bsize, "&%d Kb", 8 << i);

			if (BRAM_Ex_Size == i)
			{
				InsertMenu(OptionsSRAMSize, i + 1, Flags | MF_CHECKED, ID_OPTION_SRAMSIZE_8 + i, bsize);
			}
			else
			{
				InsertMenu(OptionsSRAMSize, i + 1, Flags | MF_UNCHECKED, ID_OPTION_SRAMSIZE_8 + i, bsize);
			}
		}
	}
	else
	{
		MENU_L(OptionsSRAMSize, 0, Flags | MF_CHECKED, ID_OPTION_SRAMSIZE_0, "None", "", "&None");

		for (i = 0; i < 4; i++)
		{
			char bsize[16];

			sprintf(bsize, "&%d Kb", 8 << i);

			InsertMenu(OptionsSRAMSize, i + 1, Flags | MF_UNCHECKED, ID_OPTION_SRAMSIZE_8 + i, bsize);
		}
	}


	// Menu Help

	i = 0;
	
	while (language_name[i])
	{
		GetPrivateProfileString(language_name[i], "Menu Language", "Undefined language", Str_Tmp, 1024, Language_Path);
		if (Language == i)
			InsertMenu(Help, i, Flags | MF_CHECKED, ID_HELP_LANG + i, Str_Tmp);
		else 
			InsertMenu(Help, i, Flags | MF_UNCHECKED, ID_HELP_LANG + i, Str_Tmp);

		i++;
	}

	InsertMenu(Help, i++, MF_SEPARATOR, NULL, NULL);

	if (Detect_Format(Manual_Path) != -1)		// can be used to detect if file exist
	{
		MENU_L(Help, i++, Flags, ID_HELP_MENU_FILE, "File menu" ,"", "&File menu");
		MENU_L(Help, i++, Flags, ID_HELP_MENU_GRAPHICS, "Graphics menu" ,"", "&Graphics menu");
		MENU_L(Help, i++, Flags, ID_HELP_MENU_CPU, "CPU menu" ,"", "&CPU menu");
		MENU_L(Help, i++, Flags, ID_HELP_MENU_SOUND, "Sound menu" ,"", "&Sound menu");
		MENU_L(Help, i++, Flags, ID_HELP_MENU_OPTIONS, "Options menu" ,"", "&Options menu");

		InsertMenu(Help, i++, MF_SEPARATOR, NULL, NULL);

		MENU_L(Help, i++, Flags, ID_HELP_NETPLAY, "Netplay" ,"", "&Netplay");
		MENU_L(Help, i++, Flags, ID_HELP_MEGACD, "Mega/Sega CD" ,"", "&Mega/Sega CD");
		MENU_L(Help, i++, Flags, ID_HELP_FAQ, "FAQ" ,"", "&FAQ");
		MENU_L(Help, i++, Flags, ID_HELP_KEYS, "Shortcuts" ,"", "&Defaults keys && Shortcuts");

		InsertMenu(Help, i++, MF_SEPARATOR, NULL, NULL);
	}

	MENU_L(Help, i, Flags, ID_HELP_ABOUT, "About" ,"", "&About");

	Gens_Menu = MainMenu;

	if (Full_Screen) SetMenu(HWnd, NULL);
	else SetMenu(HWnd, Gens_Menu);

	return(Gens_Menu);
}


LRESULT CALLBACK GGenieProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2, i, value;
	char tmp[1024];

	switch(uMsg)
	{
		case WM_INITDIALOG:
			Build_Language_String();

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);

            GetPrivateProfileString(language_name[Language], "Game Genie / Patch", "Game Genie / Patch",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			WORD_L(IDC_INFO_GG, "Informations GG", "", "Informations about GG/Patch codes");
			WORD_L(IDC_GGINFO1, "Game Genie info 1", "", "Both Game Genie code and Patch code are supported.");
			WORD_L(IDC_GGINFO2, "Game Genie info 2", "", "Highlight a code to activate it.");
			WORD_L(IDC_GGINFO3, "Game Genie info 3", "", "yntax for Game Genie code :  XXXX-XXXX");
			WORD_L(IDC_GGINFO4, "Game Genie info 4", "", "Syntax for Patch code :  XXXXXX:YYYY    (address:data)");

			WORD_L(ID_GGADD, "Add code", "", "Add &code");
			WORD_L(ID_GGREMOVE, "Remove selected codes", "", "&Remove selected codes");
			WORD_L(ID_GGDESACTIVE, "Desactive all codes", "", "&Desactive all codes");

            WORD_L(ID_HELP_HELP, "Help", "", "&Help");
			WORD_L(ID_OK, "OK", "", "&OK");
			WORD_L(ID_CANCEL, "Cancel", "", "&Cancel");

			for(i = 0; i < 256; i++)
			{
				if (Liste_GG[i].code[0] != 0)
				{
					strcpy(Str_Tmp, Liste_GG[i].code);
					while (strlen(Str_Tmp) < 20) strcat(Str_Tmp, " ");
					strcat(Str_Tmp, Liste_GG[i].name);

					SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, (WPARAM) 0, (LONG) (LPTSTR) Str_Tmp);

					if (Liste_GG[i].active)
						SendDlgItemMessage(hDlg, IDC_LIST1, LB_SETSEL, (WPARAM) 1, (LONG) i);
					else
						SendDlgItemMessage(hDlg, IDC_LIST1, LB_SETSEL, (WPARAM) 0, (LONG) i);

					if ((Liste_GG[i].restore != 0xFFFFFFFF) && (Liste_GG[i].addr < Rom_Size) && (Genesis_Started))
					{
						Rom_Data[Liste_GG[i].addr] = (unsigned char)(Liste_GG[i].restore & 0xFF);
						Rom_Data[Liste_GG[i].addr + 1] = (unsigned char)((Liste_GG[i].restore & 0xFF00) >> 8);
					}
				}
			}
			return true;
			break;

		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_GGADD:
					if (GetDlgItemText(hDlg, IDC_EDIT1, Str_Tmp, 14))
					{
						if ((strlen(Str_Tmp) == 9) || (strlen(Str_Tmp) == 11))
						{						
							strupr(Str_Tmp);
							while (strlen(Str_Tmp) < 20) strcat(Str_Tmp, " ");

							GetDlgItemText(hDlg, IDC_EDIT2, (char *) (Str_Tmp + strlen(Str_Tmp)), 240);

							SendDlgItemMessage(hDlg, IDC_LIST1, LB_ADDSTRING, (WPARAM) 0, (LONG) (LPTSTR) Str_Tmp);

							SetDlgItemText(hDlg, IDC_EDIT1, "");
							SetDlgItemText(hDlg, IDC_EDIT2, "");
						}
					}
					return true;
					break;

				case ID_GGREMOVE:
					value = SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCOUNT, (WPARAM) 0, (LPARAM) 0);
					if (value == LB_ERR) value = 0;

					for(i = value - 1; i >= 0; i--)
					{
						if (SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETSEL, (WPARAM) i, NULL) > 0)
							SendDlgItemMessage(hDlg, IDC_LIST1, LB_DELETESTRING , (WPARAM) i, (LPARAM) 0);
					}
					return true;
					break;

				case ID_GGDESACTIVE:
					value = SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCOUNT, (WPARAM) 0, (LPARAM) 0);
					if (value == LB_ERR) value = 0;

					for(i = value - 1; i >= 0; i--)
					{
						SendDlgItemMessage(hDlg, IDC_LIST1, LB_SETSEL , (WPARAM) 0, (LPARAM) i);
					}
					return true;
					break;

				case ID_OK:
					value = SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETCOUNT, (WPARAM) 0, (LPARAM) 0);
					if (value == LB_ERR) value = 0;

					for(i = 0; i < 256; i++)
					{
						Liste_GG[i].code[0] = 0;
						Liste_GG[i].name[0] = 0;
						Liste_GG[i].active = 0;
						Liste_GG[i].addr = 0xFFFFFFFF;
						Liste_GG[i].data = 0;
						Liste_GG[i].restore = 0xFFFFFFFF;
					}

					for(i = 0; i < value; i++)
					{
						if (SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETTEXT, (WPARAM) i, (LONG) (LPTSTR) tmp) != LB_ERR)
						{
							dx1 = 0;

							while ((tmp[dx1] != ' ') && (tmp[dx1] != 0)) dx1++;

							memcpy(Liste_GG[i].code, tmp, dx1);
							Liste_GG[i].code[dx1] = 0;

							while ((tmp[dx1] == ' ') && (tmp[dx1] != 0)) dx1++;

							strcpy(Liste_GG[i].name, (char *) (tmp + dx1));

							if (SendDlgItemMessage(hDlg, IDC_LIST1, LB_GETSEL, (WPARAM) i, NULL) > 0)
								Liste_GG[i].active = 1;
							else Liste_GG[i].active = 0;
						}
					}

					for(i = 0; i < value; i++)
					{
						if ((Liste_GG[i].code[0] != 0) && (Liste_GG[i].addr == 0xFFFFFFFF) && (Liste_GG[i].data == 0))
						{
							decode(Liste_GG[i].code, (patch *) (&(Liste_GG[i].addr)));

							if ((Liste_GG[i].restore == 0xFFFFFFFF) && (Liste_GG[i].addr < Rom_Size) && (Genesis_Started))
							{
								Liste_GG[i].restore = (unsigned int) (Rom_Data[Liste_GG[i].addr] & 0xFF);
								Liste_GG[i].restore += (unsigned int) ((Rom_Data[Liste_GG[i].addr + 1] & 0xFF) << 8);
							}
						}
					}

				case ID_CANCEL:
					EndDialog(hDlg, true);
					return true;
					break;

				case ID_HELP_HELP:
					if (Detect_Format(Manual_Path) != -1)		// can be used to detect if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpgamegenie.html");
						system(Str_Tmp);
					}
					else
					{
						MessageBox(NULL, "You need to configure the Manual Path to have help", "info", MB_OK);
					}
					return true;
					break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}


LRESULT CALLBACK DirectoriesProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	char Str_Tmp2[1024];
	
	switch(uMsg)
	{
		case WM_INITDIALOG:
			RECT r;
			RECT r2;
			int dx1, dy1, dx2, dy2;

			Build_Language_String();

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Directories Configuration", "Directories Configuration",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			WORD_L(IDD_DIRECTORIES, "Directories configuration", "", "Directories configuration");
			WORD_L(IDC_DIRECTORIES, "Setting directories", "", "Configure directories");
			
			WORD_L(ID_HELP_HELP, "Help", "", "&Help");
			WORD_L(ID_CANCEL, "Cancel", "", "&Cancel");
			WORD_L(ID_OK, "OK", "", "&OK");

			WORD_L(ID_CHANGE_SAVE, "Change", "", "Change");
			WORD_L(ID_CHANGE_SRAM, "Change", "", "Change");
			WORD_L(ID_CHANGE_BRAM, "Change", "", "Change");
			WORD_L(ID_CHANGE_WAV, "Change", "", "Change");
			WORD_L(ID_CHANGE_GYM, "Change", "", "Change");
			WORD_L(ID_CHANGE_SHOT, "Change", "", "Change");
			WORD_L(ID_CHANGE_PATCH, "Change", "", "Change");
			WORD_L(ID_CHANGE_IPS, "Change", "", "Change");

			WORD_L(IDC_STATIC_SAVE, "Save static", "", "SAVE STATE");
			WORD_L(IDC_STATIC_SRAM, "Sram static", "", "SRAM BACKUP");
			WORD_L(IDC_STATIC_BRAM, "Bram static", "", "BRAM BACKUP");
			WORD_L(IDC_STATIC_WAV, "Wav static", "", "WAV DUMP");
			WORD_L(IDC_STATIC_GYM, "Gym static", "", "GYM DUMP");
			WORD_L(IDC_STATIC_SHOT, "Shot static", "", "SCREEN SHOT");
			WORD_L(IDC_STATIC_PATCH, "Patch static", "", "PAT PATCH");
			WORD_L(IDC_STATIC_IPS, "IPS static", "", "IPS PATCH");

			SetDlgItemText(hDlg, IDC_EDIT_SAVE, State_Dir);
			SetDlgItemText(hDlg, IDC_EDIT_SRAM, SRAM_Dir);
			SetDlgItemText(hDlg, IDC_EDIT_BRAM, BRAM_Dir);
			SetDlgItemText(hDlg, IDC_EDIT_WAV, Dump_Dir);
			SetDlgItemText(hDlg, IDC_EDIT_GYM, Dump_GYM_Dir);
			SetDlgItemText(hDlg, IDC_EDIT_SHOT, ScrShot_Dir);
			SetDlgItemText(hDlg, IDC_EDIT_PATCH, Patch_Dir);
			SetDlgItemText(hDlg, IDC_EDIT_IPS, IPS_Dir);

			return true;
			break;

		case WM_COMMAND:
			switch(wParam)
			{
				case ID_CHANGE_SAVE:
					GetDlgItemText(hDlg, IDC_EDIT_SAVE, Str_Tmp2, 1024);
					if (Change_Dir(Str_Tmp, Str_Tmp2, "Save state directory", "Save state files\0*.gs*\0\0", "gs0"))
						SetDlgItemText(hDlg, IDC_EDIT_SAVE, Str_Tmp);
					break;

				case ID_CHANGE_SRAM:
					GetDlgItemText(hDlg, IDC_EDIT_SRAM, Str_Tmp2, 1024);
					if (Change_Dir(Str_Tmp, Str_Tmp2, "SRAM backup directory", "SRAM backup files\0*.srm\0\0", "srm"))
						SetDlgItemText(hDlg, IDC_EDIT_SRAM, Str_Tmp);
					break;

				case ID_CHANGE_BRAM:
					GetDlgItemText(hDlg, IDC_EDIT_BRAM, Str_Tmp2, 1024);
					if (Change_Dir(Str_Tmp, Str_Tmp2, "BRAM backup directory", "BRAM backup files\0*.brm\0\0", "brm"))
						SetDlgItemText(hDlg, IDC_EDIT_BRAM, Str_Tmp);
					break;

				case ID_CHANGE_WAV:
					GetDlgItemText(hDlg, IDC_EDIT_WAV, Str_Tmp2, 1024);
					if (Change_Dir(Str_Tmp, Str_Tmp2, "Sound WAV dump directory", "Sound WAV dump files\0*.wav\0\0", "wav"))
						SetDlgItemText(hDlg, IDC_EDIT_WAV, Str_Tmp);
					break;

				case ID_CHANGE_GYM:
					GetDlgItemText(hDlg, IDC_EDIT_GYM, Str_Tmp2, 1024);
					if (Change_Dir(Str_Tmp, Str_Tmp2, "GYM dump directory", "GYM dump files\0*.gym\0\0", "gym"))
						SetDlgItemText(hDlg, IDC_EDIT_GYM, Str_Tmp);
					break;

				case ID_CHANGE_SHOT:
					GetDlgItemText(hDlg, IDC_EDIT_SHOT, Str_Tmp2, 1024);
					if (Change_Dir(Str_Tmp, Str_Tmp2, "Screen-shot directory", "Screen-shot files\0*.bmp\0\0", "bmp"))
						SetDlgItemText(hDlg, IDC_EDIT_SHOT, Str_Tmp);
					break;

				case ID_CHANGE_PATCH:
					GetDlgItemText(hDlg, IDC_EDIT_PATCH, Str_Tmp2, 1024);
					if (Change_Dir(Str_Tmp, Str_Tmp2, "PAT Patch directory", "PAT Patch files\0*.pat\0\0", "pat"))
						SetDlgItemText(hDlg, IDC_EDIT_PATCH, Str_Tmp);
					break;

				case ID_CHANGE_IPS:
					GetDlgItemText(hDlg, IDC_EDIT_IPS, Str_Tmp2, 1024);
					if (Change_Dir(Str_Tmp, Str_Tmp2, "IPS Patch directory", "IPS Patch files\0*.ips\0\0", "ips"))
						SetDlgItemText(hDlg, IDC_EDIT_IPS, Str_Tmp);
					break;

				case ID_OK:
					GetDlgItemText(hDlg, IDC_EDIT_SAVE, State_Dir, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_SRAM, SRAM_Dir, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_BRAM, BRAM_Dir, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_WAV, Dump_Dir, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_GYM, Dump_GYM_Dir, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_SHOT, ScrShot_Dir, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_PATCH, Patch_Dir, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_IPS, IPS_Dir, 1024);

				case ID_CANCEL:
					EndDialog(hDlg, true);
					return true;
					break;

				case ID_HELP_HELP:
					if (Detect_Format(Manual_Path) != -1)		// can be used to detect if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpdir.html");
						system(Str_Tmp);
					}
					else
					{
						MessageBox(NULL, "You need to configure the Manual Path to have help", "info", MB_OK);
					}
					return true;
					break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}


LRESULT CALLBACK FilesProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	char Str_Tmp2[1024];
	
	switch(uMsg)
	{
		case WM_INITDIALOG:
			RECT r;
			RECT r2;
			int dx1, dy1, dx2, dy2;

			Build_Language_String();

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Files Configuration", "Files Configuration",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			WORD_L(IDD_FILES, "Files configuration", "", "Files configuration");
			WORD_L(IDC_GENESISBIOS_FILE, "Setting Genesis bios file", "", "Configure Genesis bios file");
			WORD_L(IDC_32XBIOS_FILES, "Setting 32X bios files", "", "Configure 32X bios files");
			WORD_L(IDC_CDBIOS_FILES, "Setting SEGA CD bios files", "", "Configure SEGA CD bios files");
			WORD_L(IDC_MISC_FILES, "Setting misc files", "", "Configure misc file");
						
	        WORD_L(ID_HELP_HELP, "Help", "", "&Help");
			WORD_L(ID_CANCEL, "Cancel", "", "&Cancel");
			WORD_L(ID_OK, "OK", "", "&OK");

			WORD_L(ID_CHANGE_GENESISBIOS, "Change", "", "Change");
			WORD_L(ID_CHANGE_32XGBIOS, "Change", "", "Change");
			WORD_L(ID_CHANGE_32XMBIOS, "Change", "", "Change");
			WORD_L(ID_CHANGE_32XSBIOS, "Change", "", "Change");
			WORD_L(ID_CHANGE_USBIOS, "Change", "", "Change");
			WORD_L(ID_CHANGE_EUBIOS, "Change", "", "Change");
			WORD_L(ID_CHANGE_JABIOS, "Change", "", "Change");
			WORD_L(ID_CHANGE_CGOFFLINE, "Change", "", "Change");
			WORD_L(ID_CHANGE_MANUAL, "Change", "", "Change");

			WORD_L(IDC_STATIC_GENESISBIOS, "Genesis bios static", "", "Genesis");
			WORD_L(IDC_STATIC_32XGBIOS, "M68000 bios static", "", "M68000");
			WORD_L(IDC_STATIC_32XMBIOS, "M SH2 bios static", "", "Master SH2");
			WORD_L(IDC_STATIC_32XSBIOS, "S SH2 bios static", "", "Slave SH2");
			WORD_L(IDC_STATIC_USBIOS, "US bios static", "", "USA");
			WORD_L(IDC_STATIC_EUBIOS, "EU bios static", "", "Europe");
			WORD_L(IDC_STATIC_JABIOS, "JA bios static", "", "Japan");
			WORD_L(IDC_STATIC_CGOFFLINE, "CGOffline static", "", "CGOffline");
			WORD_L(IDC_STATIC_MANUAL, "Manual static", "", "Manual");

			SetDlgItemText(hDlg, IDC_EDIT_GENESISBIOS, Genesis_Bios);
			SetDlgItemText(hDlg, IDC_EDIT_32XGBIOS, _32X_Genesis_Bios);
			SetDlgItemText(hDlg, IDC_EDIT_32XMBIOS, _32X_Master_Bios);
			SetDlgItemText(hDlg, IDC_EDIT_32XSBIOS, _32X_Slave_Bios);
			SetDlgItemText(hDlg, IDC_EDIT_USBIOS, US_CD_Bios);
			SetDlgItemText(hDlg, IDC_EDIT_EUBIOS, EU_CD_Bios);
			SetDlgItemText(hDlg, IDC_EDIT_JABIOS, JA_CD_Bios);
			SetDlgItemText(hDlg, IDC_EDIT_CGOFFLINE, CGOffline_Path);
			SetDlgItemText(hDlg, IDC_EDIT_MANUAL, Manual_Path);

			return true;
			break;

		case WM_COMMAND:
			switch(wParam)
			{
				case ID_CHANGE_GENESISBIOS:
					GetDlgItemText(hDlg, IDC_EDIT_GENESISBIOS, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "genesis.bin"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "Genesis bios file", "bios files\0*.bin\0\0", "bin"))
						SetDlgItemText(hDlg, IDC_EDIT_GENESISBIOS, Str_Tmp);
					break;

				case ID_CHANGE_32XGBIOS:
					GetDlgItemText(hDlg, IDC_EDIT_32XGBIOS, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "32X_G_bios.bin"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "32X M68000 bios file", "bios files\0*.bin\0\0", "bin"))
						SetDlgItemText(hDlg, IDC_EDIT_32XGBIOS, Str_Tmp);
					break;

				case ID_CHANGE_32XMBIOS:
					GetDlgItemText(hDlg, IDC_EDIT_32XMBIOS, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "32X_M_bios.bin"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "32X Master SH2 bios file", "bios files\0*.bin\0\0", "bin"))
						SetDlgItemText(hDlg, IDC_EDIT_32XMBIOS, Str_Tmp);
					break;

				case ID_CHANGE_32XSBIOS:
					GetDlgItemText(hDlg, IDC_EDIT_32XSBIOS, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "32X_S_bios.bin"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "32X Slave SH2 bios file", "bios files\0*.bin\0\0", "bin"))
						SetDlgItemText(hDlg, IDC_EDIT_32XSBIOS, Str_Tmp);
					break;

				case ID_CHANGE_USBIOS:
					GetDlgItemText(hDlg, IDC_EDIT_USBIOS, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "us_scd1_9210.bin"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "USA CD bios file", "bios files\0*.bin\0\0", "bin"))
						SetDlgItemText(hDlg, IDC_EDIT_USBIOS, Str_Tmp);
					break;

				case ID_CHANGE_EUBIOS:
					GetDlgItemText(hDlg, IDC_EDIT_EUBIOS, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "eu_mcd1_9210.bin"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "EUROPEAN CD bios file", "bios files\0*.bin\0\0", "bin"))
						SetDlgItemText(hDlg, IDC_EDIT_EUBIOS, Str_Tmp);
					break;

				case ID_CHANGE_JABIOS:
					GetDlgItemText(hDlg, IDC_EDIT_JABIOS, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "jp_mcd1_9112.bin"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "JAPAN CD bios file", "bios files\0*.bin\0\0", "bin"))
						SetDlgItemText(hDlg, IDC_EDIT_JABIOS, Str_Tmp);
					break;

				case ID_CHANGE_CGOFFLINE:
					GetDlgItemText(hDlg, IDC_EDIT_CGOFFLINE, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "GCOffline.chm"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "Genesis Collective - CGOffline file", "html help files\0*.chm\0\0", "chm"))
						SetDlgItemText(hDlg, IDC_EDIT_CGOFFLINE, Str_Tmp);
					break;

				case ID_CHANGE_MANUAL:
					GetDlgItemText(hDlg, IDC_EDIT_MANUAL, Str_Tmp2, 1024);
					strcpy(Str_Tmp, "Manual.exe"); 
					if (Change_File_S(Str_Tmp, Str_Tmp2, "Gens Manual", "executable files\0*.exe\0\0", "exe"))
						SetDlgItemText(hDlg, IDC_EDIT_MANUAL, Str_Tmp);
					break;

				case ID_OK:
					GetDlgItemText(hDlg, IDC_EDIT_GENESISBIOS, Genesis_Bios, 1024);

					GetDlgItemText(hDlg, IDC_EDIT_32XGBIOS, _32X_Genesis_Bios, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_32XMBIOS, _32X_Master_Bios, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_32XSBIOS, _32X_Slave_Bios, 1024);

					GetDlgItemText(hDlg, IDC_EDIT_USBIOS, US_CD_Bios, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_EUBIOS, EU_CD_Bios, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_JABIOS, JA_CD_Bios, 1024);

					GetDlgItemText(hDlg, IDC_EDIT_CGOFFLINE, CGOffline_Path, 1024);
					GetDlgItemText(hDlg, IDC_EDIT_MANUAL, Manual_Path, 1024);

				case ID_CANCEL:
					EndDialog(hDlg, true);
					return true;
					break;

				case ID_HELP_HELP:
					if (Detect_Format(Manual_Path) != -1)		// can be used to detect if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpdir.html");
						system(Str_Tmp);
					}
					else
					{
						MessageBox(NULL, "You need to configure the Manual Path to have help", "info", MB_OK);
					}
					return true;
					break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}


LRESULT CALLBACK AboutProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2;

	switch(uMsg)
	{
		case WM_INITDIALOG:
			if (Full_Screen)
			{
				while (ShowCursor(false) >= 0);
				while (ShowCursor(true) < 0);
			}

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
			GetPrivateProfileString(language_name[Language], "About Gens32", "About Gens32",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);
			return true;
			break;

		case WM_COMMAND:
			switch(wParam)
			{
				case IDOK:
					if (Full_Screen)
					{
						while (ShowCursor(true) < 0);
						while (ShowCursor(false) >= 0);
					}

					EndDialog(hDlg, true);
					return true;
			}
			break;

		case WM_CLOSE:
			if (Full_Screen)
			{
				while (ShowCursor(true) < 0);
				while (ShowCursor(false) >= 0);
			}

			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}


LRESULT CALLBACK ColorProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2;

	switch(uMsg)
	{
		case WM_INITDIALOG:
			Build_Language_String();

			if (Full_Screen)
			{
				while (ShowCursor(false) >= 0);
				while (ShowCursor(true) < 0);
			}

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Color Adjustement", "Color Adjustement",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			WORD_L(IDC_STATIC_CONT, "Contrast", "", "Contrast");
			WORD_L(IDC_STATIC_BRIGHT, "Brightness", "", "Brightness");
			WORD_L(IDC_CHECK_GREYSCALE, "Greyscale", "", "Greyscale");
			WORD_L(IDC_CHECK_INVERT, "Invert", "", "Invert");

			WORD_L(ID_APPLY, "Apply", "", "&Apply");
			WORD_L(ID_CLOSE, "Close", "", "&Close");
			WORD_L(ID_DEFAULT, "Default", "", "&Default");

			SendDlgItemMessage(hDlg, IDC_SLIDER_CONTRASTE, TBM_SETRANGE, (WPARAM) (BOOL) TRUE, (LPARAM) MAKELONG(0, 200));
			SendDlgItemMessage(hDlg, IDC_SLIDER_CONTRASTE, TBM_SETPOS, (WPARAM) (BOOL) TRUE, (LPARAM) (LONG) (Contrast_Level));
			SendDlgItemMessage(hDlg, IDC_SLIDER_LUMINOSITE, TBM_SETRANGE, (WPARAM) (BOOL) TRUE, (LPARAM) MAKELONG(0, 200));
			SendDlgItemMessage(hDlg, IDC_SLIDER_LUMINOSITE, TBM_SETPOS, (WPARAM) (BOOL) TRUE, (LPARAM) (LONG) (Brightness_Level));

			SendDlgItemMessage(hDlg, IDC_CHECK_GREYSCALE, BM_SETCHECK, (WPARAM) (Greyscale)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_CHECK_INVERT, BM_SETCHECK, (WPARAM) (Invert_Color)?BST_CHECKED:BST_UNCHECKED, 0);

			return true;
			break;

		case WM_COMMAND:
			switch(wParam)
			{
				case ID_CLOSE:
					if (Full_Screen)
					{
						while (ShowCursor(true) < 0);
						while (ShowCursor(false) >= 0);
					}

					Build_Main_Menu();
					EndDialog(hDlg, true);
					return true;

				case ID_APPLY:
					Contrast_Level = SendDlgItemMessage(hDlg, IDC_SLIDER_CONTRASTE, TBM_GETPOS, 0, 0);
					Brightness_Level = SendDlgItemMessage(hDlg, IDC_SLIDER_LUMINOSITE, TBM_GETPOS, 0, 0);
					Greyscale = (SendDlgItemMessage(hDlg, IDC_CHECK_GREYSCALE, BM_GETCHECK, 0, 0) == BST_CHECKED)?1:0;
					Invert_Color = (SendDlgItemMessage(hDlg, IDC_CHECK_INVERT, BM_GETCHECK, 0, 0) == BST_CHECKED)?1:0;

					Recalculate_Palettes();
					if (Genesis_Started || _32X_Started || SegaCD_Started)
					{
						CRam_Flag = 1;
						if (!Paused) Update_Emulation_One(HWnd);
					}
					return true;

				case ID_DEFAULT:
					SendDlgItemMessage(hDlg, IDC_SLIDER_CONTRASTE, TBM_SETPOS, (WPARAM) (BOOL) TRUE, (LPARAM) (LONG) (100));
					SendDlgItemMessage(hDlg, IDC_SLIDER_LUMINOSITE, TBM_SETPOS, (WPARAM) (BOOL) TRUE, (LPARAM) (LONG) (100));
					SendDlgItemMessage(hDlg, IDC_CHECK_GREYSCALE, BM_SETCHECK, BST_UNCHECKED, 0);
					SendDlgItemMessage(hDlg, IDC_CHECK_INVERT, BM_SETCHECK, BST_UNCHECKED, 0);
					return true;

				case ID_HELP_HELP:
/*					if (Detect_Format(Manual_Path) != -1)		// can be used to detect if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpmisc.html");
						system(Str_Tmp);
					}
					else
					{
						MessageBox(NULL, "You need to configure the Manual Path to have help", "info", MB_OK);
					}
*/					return true;
			}
			break;

		case WM_CLOSE:
			if (Full_Screen)
			{
				while (ShowCursor(true) < 0);
				while (ShowCursor(false) >= 0);
			}

			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}


LRESULT CALLBACK OptionProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2;

	switch(uMsg)
	{
		case WM_INITDIALOG:
			Build_Language_String();

			if (Full_Screen)
			{
				while (ShowCursor(false) >= 0);
				while (ShowCursor(true) < 0);
			}

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "General Option", "General Option",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			WORD_L(IDC_AUTOFIXCHECKSUM, "Auto Fix Checksum", "", "Auto Fix Checksum");
			WORD_L(IDC_AUTOPAUSE, "Auto Pause", "", "Auto Pause");
			WORD_L(IDC_FASTBLUR, "Fast Blur", "", "Fast Blur");
			WORD_L(IDC_SHOWLED, "Show Sega-CD LED", "", "Show Sega-CD LED");
			WORD_L(IDC_ENABLE_FPS, "Enable", "", "Enable");
			WORD_L(IDC_ENABLE_MESSAGE, "Enable", "", "Enable");
			WORD_L(IDC_X2_FPS, "Double Sized", "", "Double Sized");
			WORD_L(IDC_X2_MESSAGE, "Double Sized", "", "Double Sized");
			WORD_L(IDC_TRANS_FPS, "Transparency", "", "Transparency");
			WORD_L(IDC_TRANS_MESSAGE, "Transparency", "", "Transparency");
			WORD_L(IDC_EFFECT_COLOR, "Effect color", "", "Effect color");
            WORD_L(IDC_OPTION_MISC, "Intro", "", "Intro");
			WORD_L(IDC_OPTION_SYSTEM, "System", "", "System");
			WORD_L(IDC_OPTION_FPS, "FPS", "", "FPS");
			WORD_L(IDC_OPTION_MESSAGE, "Message", "", "Message");
            WORD_L(ID_HELP_HELP, "Help", "", "&Help");
			WORD_L(IDC_OPTION_CANCEL, "Cancel", "", "&Cancel");
			WORD_L(IDC_OPTION_OK, "OK", "", "&OK");

			SendDlgItemMessage(hDlg, IDC_COLOR_FPS, TBM_SETRANGE, (WPARAM) (BOOL) TRUE, (LPARAM) MAKELONG(0, 3));
			SendDlgItemMessage(hDlg, IDC_COLOR_FPS, TBM_SETPOS, (WPARAM) (BOOL) TRUE, (LPARAM) (LONG) ((FPS_Style & 0x6) >> 1));
			SendDlgItemMessage(hDlg, IDC_COLOR_MESSAGE, TBM_SETRANGE, (WPARAM) (BOOL) TRUE, (LPARAM) MAKELONG(0, 3));
			SendDlgItemMessage(hDlg, IDC_COLOR_MESSAGE, TBM_SETPOS, (WPARAM) (BOOL) TRUE, (LPARAM) (LONG) ((Message_Style & 0x6) >> 1));
			SendDlgItemMessage(hDlg, IDC_COLOR_EFFECT, TBM_SETRANGE, (WPARAM) (BOOL) TRUE, (LPARAM) MAKELONG(0, 7));
			SendDlgItemMessage(hDlg, IDC_COLOR_EFFECT, TBM_SETPOS, (WPARAM) (BOOL) TRUE, (LPARAM) (LONG) Effect_Color);

			SendDlgItemMessage(hDlg, IDC_AUTOFIXCHECKSUM, BM_SETCHECK, (WPARAM) (Auto_Fix_CS)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_AUTOPAUSE, BM_SETCHECK, (WPARAM) (Auto_Pause)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_FASTBLUR, BM_SETCHECK, (WPARAM) (Fast_Blur)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_SHOWLED, BM_SETCHECK, (WPARAM) (Show_LED)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_ENABLE_FPS, BM_SETCHECK, (WPARAM) (Show_FPS)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_X2_FPS, BM_SETCHECK, (WPARAM) (FPS_Style & 0x10)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_TRANS_FPS, BM_SETCHECK, (WPARAM) (FPS_Style & 0x8)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_ENABLE_MESSAGE, BM_SETCHECK, (WPARAM) (Show_Message)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_X2_MESSAGE, BM_SETCHECK, (WPARAM) (Message_Style & 0x10)?BST_CHECKED:BST_UNCHECKED, 0);
			SendDlgItemMessage(hDlg, IDC_TRANS_MESSAGE, BM_SETCHECK, (WPARAM) (Message_Style & 0x8)?BST_CHECKED:BST_UNCHECKED, 0);

			return true;
			break;

		case WM_COMMAND:
			switch(wParam)
			{
				case IDC_OPTION_OK:
					unsigned int res;

					if (Full_Screen)
					{
						while (ShowCursor(true) < 0);
						while (ShowCursor(false) >= 0);
					}

					res = SendDlgItemMessage(hDlg, IDC_COLOR_FPS, TBM_GETPOS, 0, 0);
					FPS_Style = (FPS_Style & ~0x6) | ((res << 1) & 0x6);
					res = SendDlgItemMessage(hDlg, IDC_COLOR_MESSAGE, TBM_GETPOS, 0, 0);
					Message_Style = (Message_Style & 0xF9) | ((res << 1) & 0x6);
					Effect_Color = SendDlgItemMessage(hDlg, IDC_COLOR_EFFECT, TBM_GETPOS, 0, 0);

					Auto_Fix_CS = (SendDlgItemMessage(hDlg, IDC_AUTOFIXCHECKSUM, BM_GETCHECK, 0, 0) == BST_CHECKED)?1:0;
					Auto_Pause = (SendDlgItemMessage(hDlg, IDC_AUTOPAUSE, BM_GETCHECK, 0, 0) == BST_CHECKED)?1:0;
					Fast_Blur = (SendDlgItemMessage(hDlg, IDC_FASTBLUR, BM_GETCHECK, 0, 0) == BST_CHECKED)?1:0;
					Show_LED = (SendDlgItemMessage(hDlg, IDC_SHOWLED, BM_GETCHECK, 0, 0) == BST_CHECKED)?1:0;
					Show_FPS = (SendDlgItemMessage(hDlg, IDC_ENABLE_FPS, BM_GETCHECK, 0, 0) == BST_CHECKED)?1:0;
					res = SendDlgItemMessage(hDlg, IDC_X2_FPS, BM_GETCHECK, 0, 0);
					FPS_Style = (FPS_Style & ~0x10) | ((res == BST_CHECKED)?0x10:0);
					res = SendDlgItemMessage(hDlg, IDC_TRANS_FPS, BM_GETCHECK, 0, 0);
					FPS_Style = (FPS_Style & ~0x8) | ((res == BST_CHECKED)?0x8:0);
					Show_Message = (SendDlgItemMessage(hDlg, IDC_ENABLE_MESSAGE, BM_GETCHECK, 0, 0) == BST_CHECKED)?1:0;
					res = SendDlgItemMessage(hDlg, IDC_X2_MESSAGE, BM_GETCHECK, 0, 0);
					Message_Style = (Message_Style & ~0x10) | ((res == BST_CHECKED)?0x10:0);
					res = SendDlgItemMessage(hDlg, IDC_TRANS_MESSAGE, BM_GETCHECK, 0, 0);
					Message_Style = (Message_Style & ~0x8) | ((res == BST_CHECKED)?0x8:0);

					Build_Main_Menu();
					
					EndDialog(hDlg, true);
					return true;
					break;

				case IDC_OPTION_CANCEL:
					if (Full_Screen)
					{
						while (ShowCursor(true) < 0);
						while (ShowCursor(false) >= 0);
					}

					EndDialog(hDlg, true);
					return true;
					break;

				case ID_HELP_HELP:
					if (Detect_Format(Manual_Path) != -1)		// can be used to detect if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpmisc.html");
						system(Str_Tmp);
					}
					else
					{
						MessageBox(NULL, "You need to configure the Manual Path to have help", "info", MB_OK);
					}
					return true;
					break;
			}
			break;

		case WM_CLOSE:
			if (Full_Screen)
			{
				while (ShowCursor(true) < 0);
				while (ShowCursor(false) >= 0);
			}

			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}


LRESULT CALLBACK ControllerProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2;
    int i; 
	static HWND Tex0 = NULL;
    
    char Buttons_3[50];
    char Buttons_6[50]; 

    char T_PAD[50];
    char T_TeamPlayer[50];

    char KEY_Setting_Buffer[50];

    GetPrivateProfileString(language_name[Language], "3 buttons", "3 buttons",Buttons_3, 50, Language_Path);
    GetPrivateProfileString(language_name[Language], "6 buttons", "6 buttons",Buttons_6, 50, Language_Path);
    
    GetPrivateProfileString(language_name[Language], "pad", "pad",T_PAD, 50, Language_Path); 
    GetPrivateProfileString(language_name[Language], "teamplayer", "teamplayer",T_TeamPlayer, 50, Language_Path);

	switch(uMsg)
	{
		case WM_INITDIALOG:
			if (Full_Screen)
			{
				while (ShowCursor(false) >= 0);
				while (ShowCursor(true) < 0);
			}

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Controllers Settings", "Controllers Settings",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			Tex0 = GetDlgItem(hDlg, IDC_STATIC_TEXT0);

			if (!Init_Input(ghInstance, hDlg)) return false;


			WORD_L(IDC_TEXT_1, "Controller connected :", "", "Controller connected :");
            WORD_L(IDC_TEXT_2, "Controller connected :", "", "Controller connected :");
            
            WORD_L(IDC_TEXT_3, "Player 1:", "", "Player 1:");
            WORD_L(IDC_TEXT_4, "Player 2:", "", "Player 2:");

            WORD_L(IDC_TEXT_5, "Player 1B:", "", "Player 1B:");
            WORD_L(IDC_TEXT_6, "Player 2B:", "", "Player 2B:");

            WORD_L(IDC_TEXT_7, "Player 1C:", "", "Player 1C:");
            WORD_L(IDC_TEXT_8, "Player 2C:", "", "Player 2C:");

            WORD_L(IDC_TEXT_9, "Player 1D:", "", "Player 1D:");
            WORD_L(IDC_TEXT_10, "Player 2D:", "", "Player 2D:");

            WORD_L(IDC_TEXT_11, "PORT 1", "", "PORT 1");
            WORD_L(IDC_TEXT_12, "PORT 2", "", "PORT 2");

            WORD_L(IDC_BUTTON_SETKEYSP1, "Redefine Keys", "", "Redefine Keys");
            WORD_L(IDC_BUTTON_SETKEYSP1B, "Redefine Keys", "", "Redefine Keys");
            WORD_L(IDC_BUTTON_SETKEYSP1C, "Redefine Keys", "", "Redefine Keys");
            WORD_L(IDC_BUTTON_SETKEYSP1D, "Redefine Keys", "", "Redefine Keys");
            WORD_L(IDC_BUTTON_SETKEYSP2, "Redefine Keys", "", "Redefine Keys");
            WORD_L(IDC_BUTTON_SETKEYSP2B, "Redefine Keys", "", "Redefine Keys");
            WORD_L(IDC_BUTTON_SETKEYSP2C, "Redefine Keys", "", "Redefine Keys");
            WORD_L(IDC_BUTTON_SETKEYSP2D, "Redefine Keys", "", "Redefine Keys");

            WORD_L(IDC_TEXT_13, "Note", "", "Note");
            WORD_L(IDC_STATIC_TEXT0, "SETTING KEYS", "", "SETTING KEYS");

            WORD_L(IDOK, "OK", "", "OK");
            WORD_L(ID_HELP_HELP, "Help", "", "&Help");

			WORD_L(IDC_JOYINFO1, "Controller info 1", "", "Player 1-B 1-C and 1-D are enabled only if a teamplayer is connected to port 1");
			WORD_L(IDC_JOYINFO2, "Controller info 2", "", "Player 2-B 2-C and 2-D are enabled only if a teamplayer is connected to port 2");
			WORD_L(IDC_JOYINFO3, "Controller info 3", "", "Only a few games support teamplayer (games which have 4 players support), so don't forget to use the \"load config\" and \"save config\" possibility :)");

			for(i = 0; i < 2; i++)
			{
				SendDlgItemMessage(hDlg, IDC_COMBO_PORT1 + i, CB_INSERTSTRING, (WPARAM) 0, (LONG) T_TeamPlayer);
				SendDlgItemMessage(hDlg, IDC_COMBO_PORT1 + i, CB_INSERTSTRING, (WPARAM) 0, (LONG) T_PAD);
			}

			for(i = 0; i < 8; i++)
			{
				SendDlgItemMessage(hDlg, IDC_COMBO_PADP1 + i, CB_INSERTSTRING, (WPARAM) 0, (LONG)Buttons_6);
				SendDlgItemMessage(hDlg, IDC_COMBO_PADP1 + i, CB_INSERTSTRING, (WPARAM) 0, (LONG)Buttons_3);
			}

			SendDlgItemMessage(hDlg, IDC_COMBO_PORT1, CB_SETCURSEL, (WPARAM) ((Controller_1_Type >> 4) & 1), (LPARAM) 0);
			SendDlgItemMessage(hDlg, IDC_COMBO_PORT2, CB_SETCURSEL, (WPARAM) ((Controller_2_Type >> 4) & 1), (LPARAM) 0);

			SendDlgItemMessage(hDlg, IDC_COMBO_PADP1, CB_SETCURSEL, (WPARAM) (Controller_1_Type & 1), (LPARAM) 0);
			SendDlgItemMessage(hDlg, IDC_COMBO_PADP1B, CB_SETCURSEL, (WPARAM) (Controller_1B_Type & 1), (LPARAM) 0);
			SendDlgItemMessage(hDlg, IDC_COMBO_PADP1C, CB_SETCURSEL, (WPARAM) (Controller_1C_Type & 1), (LPARAM) 0);
			SendDlgItemMessage(hDlg, IDC_COMBO_PADP1D, CB_SETCURSEL, (WPARAM) (Controller_1D_Type & 1), (LPARAM) 0);
			SendDlgItemMessage(hDlg, IDC_COMBO_PADP2, CB_SETCURSEL, (WPARAM) (Controller_2_Type & 1), (LPARAM) 0);
			SendDlgItemMessage(hDlg, IDC_COMBO_PADP2B, CB_SETCURSEL, (WPARAM) (Controller_2B_Type & 1), (LPARAM) 0);
			SendDlgItemMessage(hDlg, IDC_COMBO_PADP2C, CB_SETCURSEL, (WPARAM) (Controller_2C_Type & 1), (LPARAM) 0);
			SendDlgItemMessage(hDlg, IDC_COMBO_PADP2D, CB_SETCURSEL, (WPARAM) (Controller_2D_Type & 1), (LPARAM) 0);

			return true;
			break;

		case WM_COMMAND:
			switch(wParam)
			{
				case IDOK:
					Controller_1_Type = (SendDlgItemMessage(hDlg, IDC_COMBO_PORT1, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);
					Controller_1_Type <<= 4;

					Controller_1_Type |= (SendDlgItemMessage(hDlg, IDC_COMBO_PADP1, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);
					Controller_1B_Type = (SendDlgItemMessage(hDlg, IDC_COMBO_PADP1B, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);
					Controller_1C_Type = (SendDlgItemMessage(hDlg, IDC_COMBO_PADP1C, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);
					Controller_1D_Type = (SendDlgItemMessage(hDlg, IDC_COMBO_PADP1D, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);

					
					Controller_2_Type = (SendDlgItemMessage(hDlg, IDC_COMBO_PORT2, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);
					Controller_2_Type <<= 4;

					Controller_2_Type |= (SendDlgItemMessage(hDlg, IDC_COMBO_PADP2, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);
					Controller_2B_Type = (SendDlgItemMessage(hDlg, IDC_COMBO_PADP2B, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);
					Controller_2C_Type = (SendDlgItemMessage(hDlg, IDC_COMBO_PADP2C, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);
					Controller_2D_Type = (SendDlgItemMessage(hDlg, IDC_COMBO_PADP2D, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1);

					Make_IO_Table();
					End_Input();
					EndDialog(hDlg, true);
					return true;
					break;

				case IDC_BUTTON_SETKEYSP1:
                    GetPrivateProfileString(language_name[Language], "SETTING KEYS P1", "SETTING KEYS P1",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					Setting_Keys(hDlg, 0, (SendDlgItemMessage(hDlg, IDC_COMBO_PADP1, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1));
                    GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					return true;
					break;

				case IDC_BUTTON_SETKEYSP1B:
                    GetPrivateProfileString(language_name[Language], "SETTING KEYS P1B", "SETTING KEYS P1B",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					Setting_Keys(hDlg, 2, (SendDlgItemMessage(hDlg, IDC_COMBO_PADP1B, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1));
					GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					return true;
					break;

				case IDC_BUTTON_SETKEYSP1C:
					GetPrivateProfileString(language_name[Language], "SETTING KEYS P1C", "SETTING KEYS P1C",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					Setting_Keys(hDlg, 3, (SendDlgItemMessage(hDlg, IDC_COMBO_PADP1C, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1));
					GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					return true;
					break;

				case IDC_BUTTON_SETKEYSP1D:
					GetPrivateProfileString(language_name[Language], "SETTING KEYS P1D", "SETTING KEYS P1D",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					Setting_Keys(hDlg, 4, (SendDlgItemMessage(hDlg, IDC_COMBO_PADP1D, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1));
				    GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					return true;
					break;

				case IDC_BUTTON_SETKEYSP2:
					GetPrivateProfileString(language_name[Language], "SETTING KEYS P2", "SETTING KEYS P2",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					Setting_Keys(hDlg, 1, (SendDlgItemMessage(hDlg, IDC_COMBO_PADP2, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1));
					GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					return true;
					break;

				case IDC_BUTTON_SETKEYSP2B:
					GetPrivateProfileString(language_name[Language], "SETTING KEYS P2B", "SETTING KEYS P2B",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					Setting_Keys(hDlg, 5, (SendDlgItemMessage(hDlg, IDC_COMBO_PADP2B, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1));
					GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					return true;
					break;

				case IDC_BUTTON_SETKEYSP2C:
					GetPrivateProfileString(language_name[Language], "SETTING KEYS P2C", "SETTING KEYS P2C",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					Setting_Keys(hDlg, 6, (SendDlgItemMessage(hDlg, IDC_COMBO_PADP2C, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1));
					GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					return true;
					break;

				case IDC_BUTTON_SETKEYSP2D:
					GetPrivateProfileString(language_name[Language], "SETTING KEYS P2D", "SETTING KEYS P2D",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					Setting_Keys(hDlg, 7, (SendDlgItemMessage(hDlg, IDC_COMBO_PADP2D, CB_GETCURSEL, (WPARAM) 0, (LPARAM) 0) & 1));
					GetPrivateProfileString(language_name[Language], "SETTING KEYS", "SETTING KEYS",KEY_Setting_Buffer, 50, Language_Path);
					SetWindowText(Tex0, KEY_Setting_Buffer);
					return true;
					break;

				case ID_HELP_HELP:
					if (Detect_Format(Manual_Path) != -1)		// can be used to detect if file exist
					{
						strcpy(Str_Tmp, Manual_Path);
						strcat(Str_Tmp, " helpjoypads.html");
						system(Str_Tmp);
					}
					else
					{
						MessageBox(NULL, "You need to configure the Manual Path to have help", "info", MB_OK);
					}
					return true;
					break;
			}
			break;

		case WM_CLOSE:
			End_Input();
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}

LRESULT CALLBACK SetGameNameProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2, i, I,value;
	char Temp_Buffer[50];
	char GameName_Buffer[50];
    char WindowTextBuffer[100];
    bool Jumper =false;

	switch(uMsg)
	{
		case WM_INITDIALOG:
			Build_Language_String();

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Config displayed game name", "Config displayed game name",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			itoa(GameID,GameName_Buffer,10);
			SetDlgItemText(hDlg,IDC_EDIT1,GameName_Buffer);

            WORD_L(IDC_TEXT_1, "Game ID:", "", "Game ID");
            WORD_L(IDC_TEXT_2, "Please input the name will be dispalyed here:", "", "Please input the name will be displayed here:");
            WORD_L(IDOK, "OK", "", "OK");
            WORD_L(IDCANCEL, "CANCEL", "", "CANCEL");

		case WM_COMMAND:
			switch(LOWORD(wParam))
			{

				case IDOK:
					
                     //Check GameID;
                     //Replace the exist
                     for(i=0;i<10000;i++)
					 {
						GetDlgItemText(hDlg,IDC_EDIT2,Temp_Buffer,49);
                    	if(GameID_ADB[i] == GameID)
						{
                         GameID_ADB[i] =GameID;
                         memcpy(&DataBuffer_ADB[i*49],&Temp_Buffer[0],49);
                         Jumper =true;
                         break;
						}
					 }
			
			      if(!Jumper)
				  {
                  //Find Empty MessageTrack and fill with the Game Name;
                  for(i=0;i<10000;i++)
					 {
						
                    	if(GameID_ADB[i] == 0 || GameID_ADB[i] == GameID)
						{
                         GameID_ADB[i] =GameID;
                         memcpy(&DataBuffer_ADB[i*49],&Temp_Buffer[0],49);
                         break;
						}
					 }
				  }

	                   if ((CPU_Mode == 1) || (Game_Mode == 0))
					   {
                        strcpy(WindowTextBuffer,"Gens32 - Megadrive: ");
						strcat(WindowTextBuffer,Temp_Buffer);
					   }
                       	else
						{
                        strcpy(WindowTextBuffer,"Gens32 - Genesis: ");
						strcat(WindowTextBuffer,Temp_Buffer);
						}

                   	 SetWindowText(HWnd,WindowTextBuffer);
				     EndDialog(hDlg, true);
				     return false;
                     break;

				case IDCANCEL:
					EndDialog(hDlg, true);
					return true;
					break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}

LRESULT CALLBACK SetStretchModeProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2;
    int i; 
	static HWND Tex0 = NULL;

	switch(uMsg)
	{
		case WM_INITDIALOG:
			if (Full_Screen)
			{
				while (ShowCursor(false) >= 0);
				while (ShowCursor(true) < 0);
			}

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Custom Stretch", "Custom Stretch",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

            WORD_L(IDC_TEXT_1, "Horizontal In Pixels:", "", "Horizontal In Pixels:");
            WORD_L(IDC_TEXT_2, "Vertical In Pixels:", "", "Vertical In Pixels:");
            WORD_L(IDC_TEXT_3, "Refurbish In HZ:", "", "Refurbish In HZ:");
            WORD_L(IDOK, "OK", "", "OK");
            WORD_L(IDCANCEL, "CANCEL", "", "CANCEL");

			return true;
			break;

		case WM_COMMAND:
			switch(wParam)
			{
				case IDOK:
				Custom_Horizontal =GetDlgItemInt(hDlg,IDC_Horizontal,NULL,FALSE);
                Custom_Vertical   =GetDlgItemInt(hDlg,IDC_Vertical,NULL,FALSE);
                Custom_Refurbish  =GetDlgItemInt(hDlg,IDC_Refurbish,NULL,FALSE);
				EndDialog(hDlg, true);
				return true;
				break;

				case IDCANCEL:
			     EndDialog(hDlg, true);
			     return true;
		         break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}


LRESULT CALLBACK InputTranslationProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2, i, I,value;
	char Temp_Buffer[100];
    char ImageID_Buffer[10];
    bool Jumper =false;

	switch(uMsg)
	{
		case WM_INITDIALOG:
			Build_Language_String();

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Translation", "Translation",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			itoa(ImageID,ImageID_Buffer,10);
			SetDlgItemText(hDlg,IDC_EDIT1,ImageID_Buffer);
            
            WORD_L(IDC_TEXT_1, "Text ID:", "", "Text ID:");
            WORD_L(IDC_TEXT_2, "Please input your translation here:", "", "Please input your translation here:");
            WORD_L(IDOK, "OK", "", "OK");
            WORD_L(IDCANCEL, "CANCEL", "", "CANCEL");

		case WM_COMMAND:
			switch(LOWORD(wParam))
			{

				case IDOK:
					memset(&Temp_Buffer[0],' ',100);
					GetDlgItemText(hDlg,IDC_EDIT2,Temp_Buffer,100);
                     //Check ImageID;
                     //Replace the exist
                     for(i=0;i<10000;i++)
					 {
                    	if(ImageID_ADB[i] == ImageID)
						{
                         ImageID_ADB[i] =ImageID;
                         memcpy(&Text_ADB[i*100],&Temp_Buffer[0],100);
                         Jumper =true;
                         break;
						}
					 }
			
			      if(!Jumper)
				  {
                  //Find Empty MessageTrack and fill with the Traslation;
                  for(i=0;i<10000;i++)
				  {
                    	if(ImageID_ADB[i] == 0 || ImageID_ADB[i] == ImageID)
						{
                         ImageID_ADB[i] =ImageID;
                         memcpy(&Text_ADB[i*100],&Temp_Buffer[0],100);
                         break;
						}
					 }
				  }
				  //TextID Backup;
                     BackUpTextID(ImageID,Temp_Buffer);
                     Translation_Data_Saved == false;
				     EndDialog(hDlg, true);
				     return false;
                     break;

				case IDCANCEL:
					EndDialog(hDlg, true);
					return true;
					break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}

LRESULT CALLBACK SetValidAreaProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2, i, I,value;
	char Temp_Buffer[10];
    char WindowTextBuffer[100];
    bool Jumper =false;

	switch(uMsg)
	{
		case WM_INITDIALOG:
			Build_Language_String();

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Set Valid Area", "Set Valid Area",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

			itoa(Check_Area.left,Temp_Buffer,10);
			SetDlgItemText(hDlg,IDC_EDIT1,Temp_Buffer);
	        itoa(Check_Area.right,Temp_Buffer,10);
 			SetDlgItemText(hDlg,IDC_EDIT2,Temp_Buffer);
            itoa(Check_Area.top,Temp_Buffer,10);
			SetDlgItemText(hDlg,IDC_EDIT3,Temp_Buffer);
            itoa(Check_Area.bottom,Temp_Buffer,10);
			SetDlgItemText(hDlg,IDC_EDIT4,Temp_Buffer);

            WORD_L(IDC_TEXT_1, "Please input the valid area here:", "", "Please input the valid area here:");
            WORD_L(IDC_TEXT_2, "Left:", "", "Left:");
            WORD_L(IDC_TEXT_3, "Right:", "", "Right:");
            WORD_L(IDC_TEXT_4, "Top:", "", "Top:");
            WORD_L(IDC_TEXT_5, "Bottom:", "", "Bottom:");
            WORD_L(IDOK, "OK", "", "OK");
            WORD_L(IDCANCEL, "CANCEL", "", "CANCEL");

		case WM_COMMAND:
			switch(LOWORD(wParam))
			{

				case IDOK:
					Check_Area.left =GetDlgItemInt(hDlg,IDC_EDIT1,NULL,NULL);
	                Check_Area.right =GetDlgItemInt(hDlg,IDC_EDIT2,NULL,NULL);
                  	Check_Area.top =GetDlgItemInt(hDlg,IDC_EDIT3,NULL,NULL);
                    Check_Area.bottom =GetDlgItemInt(hDlg,IDC_EDIT4,NULL,NULL);
                    if(Check_Area.left<0 || Check_Area.left > 320)
					{
						MessageBox(NULL,"Please set the left between 0 and 320","Error",NULL);
                        Check_Area.left =0;
						break;
					}

                    if(Check_Area.right<Check_Area.left || Check_Area.right > 320)
					{
						MessageBox(NULL,"Please set the right between left and 320","Error",NULL);
                        Check_Area.right =320;
						break;
					}

                    if(Check_Area.top<0 || Check_Area.top > 240)
					{
						MessageBox(NULL,"Please set the top between 0 and 240","Error",NULL);
                        Check_Area.top =0;
						break;
					}

                    if(Check_Area.bottom<Check_Area.top || Check_Area.top > 240)
					{
						MessageBox(NULL,"Please set the bottom between top and 240","Error",NULL);
                        Check_Area.top =0;
						break;
					}

				     EndDialog(hDlg, true);
				     return false;
                     break;

				case IDCANCEL:
					EndDialog(hDlg, true);
					return true;
					break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}

LRESULT CALLBACK TranslationEdit(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	RECT r;
	RECT r2;
	int dx1, dy1, dx2, dy2, i, I,value;
	char Temp_Buffer[100];
	int ImageID_User =0;
	char GameName_Buffer[50];
    char WindowTextBuffer[100];
    bool Jumper =false;

	switch(uMsg)
	{
		case WM_INITDIALOG:
			Build_Language_String();

			GetWindowRect(HWnd, &r);
			dx1 = (r.right - r.left) / 2;
			dy1 = (r.bottom - r.top) / 2;

			GetWindowRect(hDlg, &r2);
			dx2 = (r2.right - r2.left) / 2;
			dy2 = (r2.bottom - r2.top) / 2;

			SetWindowPos(hDlg, NULL, r.left + (dx1 - dx2), r.top + (dy1 - dy2), NULL, NULL, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
            GetPrivateProfileString(language_name[Language], "Tickler Editor", "Tickler Editor",W_TextBuffer, 50, Language_Path);
            SetWindowText(hDlg,W_TextBuffer);

            WORD_L(IDC_TEXT_1, "Please use the Text ID to find the translation:", "", "Please use the Text ID to find the translation:");
            WORD_L(IDC_TEXT_2, "Please modify your translation here:", "", "Please modify your translation here:");
            WORD_L(IDC_FIND, "Find", "", "Find");
            WORD_L(IDC_DELETE, "Delete", "", "Delete");
            WORD_L(IDC_MODIFY, "Modify", "", "Modify");
            WORD_L(IDOK, "OK", "", "OK");
            WORD_L(IDCANCEL, "CANCEL", "", "CANCEL");

		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
			   case IDC_FIND:
                   ImageID_User = GetDlgItemInt(hDlg,IDC_EDIT1,NULL,NULL);
                    for(i=0;i<10000;i++)
					 {
                    	if((ImageID_ADB[i] == ImageID_User) && ImageID_User !=0)
						{
                         memcpy(&Temp_Buffer[0],&Text_ADB[i*100],100);
                         SetDlgItemText(hDlg,IDC_EDIT2,Temp_Buffer);
                         break;
						}
					 }

				   break;

               case IDC_DELETE:
                    ImageID_User = GetDlgItemInt(hDlg,IDC_EDIT1,NULL,NULL);
                    for(i=0;i<10000;i++)
					 {
                    	if((ImageID_ADB[i] == ImageID_User) && ImageID_User !=0)
						{
                         ImageID_ADB[i] =0;
						 memset(&Text_ADB[i*100],0,100);
                         SetDlgItemText(hDlg,IDC_EDIT2,"");
                         SetDlgItemText(hDlg,IDC_EDIT1,"");
                         break;
						}
					 }
				   break;

                case IDC_MODIFY:
                    ImageID_User = GetDlgItemInt(hDlg,IDC_EDIT1,NULL,NULL);
                    for(i=0;i<10000;i++)
					 {
                    	if((ImageID_ADB[i] == ImageID_User) && ImageID_User !=0)
						{
                         GetDlgItemText(hDlg,IDC_EDIT2,Temp_Buffer,100);
                         memcpy(&Text_ADB[i*100],&Temp_Buffer[0],100);
                         SetDlgItemText(hDlg,IDC_EDIT2,Temp_Buffer);
                         break;
						}
					 }

				   break;

				case IDOK:

				     EndDialog(hDlg, true);
				     return true;
                     break;
			}
			break;

		case WM_CLOSE:
			EndDialog(hDlg, true);
			return true;
			break;
	}

	return false;
}