/*****************************/
/* Inside The Machine - MKS  */
/*****************************/

#ifndef _ENGINE_H
#define _ENGINE_H

//Includes
#include <PA9.h>
#include <ulib/ulib.h>
#include "gamestate.h"
#include "StateManager.h"
#include "RessourceManager.h"
#include "singleton.h"
#include "µsprite.h"
#include "efs_lib.h"

#define WIN_BG0 1
#define WIN_BG1 2
#define WIN_BG2 4
#define WIN_BG3 8
#define WIN_OBJ 16
#define WIN_SFX 32

//Structures
struct sHitBox
{
	u8 xUp, yUp;        //Point de l'hitbox en haut du spr
	u8 xDown, yDown;    //Point de l'hitbox en bas du spr
	u8 xLeft, yLeft;    //Point de l'hitbox a gauche du spr
	u8 xRight, yRight;  //Point de l'hitbox a droite du spr
	u8 xJumpRightDown, yJumpRightDown; //Pt Hitbox de saut
	u8 xJumpRightUp, yJumpRightUp;     //Pt Hitbox de saut
	u8 xJumpLeftDown, yJumpLeftDown;   //Pt Hitbox de saut
	u8 xJumpLeftUp, yJumpLeftUp;       //Pt Hitbox de saut
};

struct sHitBoxRect
{
	int x1, y1;        //Point de l'hitbox en p1
	int x2, y2;    //Point de l'hitbox en p2
};

struct sMusic
{
	u8* data;            //Adresse du buffer mémoire statique
	unsigned long size;  //Taille de la musique en mémoire
	bool loaded;         //Définit si la musique est chargée
};   

//Variables statiques
extern int mp3Volume;           //Volume de la musique
extern int soundVolume;         //Volume du son
extern u8 soundData[784*1024];  //Buffer son : 784kb (0,8mo)
extern int motion;
//Effets sonores
extern u8 sndMenu[8192];
extern u8 sndFire[45056];
extern u8 sndDie[155648];

//Instance statique du wman
class CWorldManager;
static CWorldManager* wman;

//Fonctions
void HBL_Func();

//Defines
//#define FAT                  //Si le système FAT est présent             
#define VERSION          0     //Version du jeu            
#define REVISION         1     //Revision du jeu
//Make it a fixed point value (1 = 256, 0.5 = 128, etc.)
#define FIX(x)		((x) << 8)
//Return the real value of a fixed point
#define UNFIX(x)	((x) >> 8)

//Fonctions generiques
bool uStylusInZone(u16 x1, u16 y1, u16 x2, u16 y2);
int uGetMapPixel(UL_MAP* m, int x, int y, u16* vid, int* bmx, int* bmy);
u16 uGetTile(UL_MAP* map, int x, int y);
u16 uInterpolate(float v1, float v2, float mu);
void drawTriangle(int x0, int y0, int x1, int y1, int x2, int y2, UL_COLOR color);
void drawQuad(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3, UL_COLOR color);
int myRand(int min, int max);
s32 hwSqrt64(int64 a);
s32 quickSqrt(s32 a);
u16 getSQRT(u32 carre);
bool RectCollision(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2);
bool RectCollision(int px1, int py1, sHitBoxRect * a, int px2, int py2, sHitBoxRect * b);
sMusic LoadMusic(char* filename);
void FreeMusic(sMusic music);
void LoadSnd(char* filename, u8* buf);
//void HBL_Func();

//Chargemment d'images
UL_IMAGE* ulEFSLoadPNG(char* filename, int location, int pixelFormat);

//Surcharge d'operateurs

//Classe du moteur de jeu
class StateManager;
class CEngine
{
   public:
   	CEngine();
   	void Run();                //Faire tourner le moteur
   	void Pause();              //Met en pause le moteur
   	void Stop();               //Arrête le moteur
   	int getStatus();           //-1: Stop, 0: Paused, 1: Run
   	
   public:
      StateManager* stman;
      bool IsFat, IsRunning, IsPaused;
      u8 currentState;
};   

#endif
