#ifndef _COMMON
#define _COMMON

/*#include <string>
using std::string;*/
#include "common.h"

#include "SDL.h"
#include "SDL_image.h" 
#include "SDL_audio.h" 
#include "SDL_config.h" 

//#include "debug.h"
#include <printf_P.h>

#define RESPATH "" //wrk
#define lpc
#define String char*
#define S(s) (s) //(String(s)) //wrk fix
#define boolean bool
#define byte signed char
#define null NULL

#define SIZEOF(a) (sizeof(a)/sizeof(*a)) 

//wrk
#define SW 82
#define  SH 84
#define  SW2 SW/2
#define  SH2 SH/2

#define KEY_UP 0
#define KEY_DOWN 1
#define KEY_LEFT 2
#define KEY_RIGHT 3
#define KEY_STAR 4
#define KEY_POUND 5
#define KEY_FIRE 6
#define KEY_LSOFT 7
#define KEY_RSOFT 8

#define KEY_NUM0 9
#define KEY_NUM1 10
#define KEY_NUM2 11
#define KEY_NUM3 12
#define KEY_NUM4 13
#define KEY_NUM5 14
#define KEY_NUM6 15
#define KEY_NUM7 16
#define KEY_NUM8 17
#define KEY_NUM9 18

#define KEY_A 19
#define KEY_B 20
#define KEY_L 21
#define KEY_R 22
#define KEY_START 23
#define KEY_SELECT 24

class Image;
class Graphics;
class MetaImage;
class MultiImage;
class PortSound;

extern SDL_Surface *screen;
extern String substring(String s, int a, int b);
extern Image noimg;	//wrk was in gamemidlet
extern bool OFFSCREEN;
extern Image Ioffscreen;
extern int scene;
extern Graphics g2;
extern int frame;
extern int SLEEP;

#define SCENE_VOID -4 //ph0x
#define  SCENE_SPLASH -3
#define  SCENE_MENU -2
#define  SCENE_GAME -1

#define MY_KEY_SZAM 20	//19 volt
#define MY_KEY_UP 0
#define MY_KEY_DOWN 1
#define MY_KEY_LEFT 2
#define MY_KEY_RIGHT 3
#define MY_KEY_FIRE 4
#define MY_KEY_LSOFT 5
#define MY_KEY_RSOFT 6
#define MY_KEY_STAR 7
#define MY_KEY_POUND 8
#define MY_KEY_NUM0 9	//-16	[9-es]
#define MY_KEY_NUM1 10
#define MY_KEY_NUM2 11
#define MY_KEY_NUM3 12
#define MY_KEY_NUM4 13
#define MY_KEY_NUM5 14
#define MY_KEY_NUM6 15
#define MY_KEY_NUM7 16
#define MY_KEY_NUM8 17
#define MY_KEY_NUM9 18
#define MY_KEY_DEL 19

extern byte keypressed[MY_KEY_SZAM];	//1 lenyomtak 2 epp le van nyomva
extern byte key_was_pressed;
extern byte key_is__pressed;

extern String SINT(int n);
extern void paint(Graphics g);
extern void repaint();
extern void serviceRepaints();
extern void destroyApp(boolean con);
extern void notifyDestroyed();
extern void sleep(int n);
extern void szogf_core_kiszamito();

extern signed short sin(signed int i);
extern signed short cos(signed int i);

//fixpontok: 2 a hanyadikon legyen az 1seg [vagy a 360fok az arcus fugvenyek eseten ugye]
//ha 14 fole akarjuk oket emelni akkor a CORE tombok tipusat int-re kell emelni [atan 15-ig is birja shortkent]
	#define SIN_FIXP_B 12			//		360=16k		max=29, de 16 folott mar nem lesz pontosabb 	regen 12 volt azert hagytam meg most is ennyinek..
	#define ATAN_FIXP_B 14			//		360=64k		max=21  (=15+SIN_DB_B altalanosan)
	#define ACOS_FIXP_B 14			//		360=64k		max=21  (=15+SIN_DB_B altalanosan) 
//2 a hanyadikon legyen a CORE tombok merete
	#define  SIN_DB_B 6				//2^6=90 fok	360=256
	#define ATAN_DB_B 5				//ez alatt nembiztos hogy jo lesz.. [nem ellenoriztem hogy tulcsordulhat-e egy szamitas]
	#define ACOS_DB_B 8
//par elore szamolt ertek amit folosleges lenne mindig kiszamolgatni :)
	#define  SIN_DB_M (1<<(SIN_DB_B+2))-1		//255	//ennyivel kell osszeeselni egy szoget a sinusban, hogy 0-360 koze croppoljam
	#define ACOS_DB_M (1<<ACOS_DB_B)-1		//255
	#define  SIN_90  (1<<(SIN_DB_B))		//64
	#define  SIN_180 (1<<(SIN_DB_B))*2		//128
	#define  SIN_270 (1<<(SIN_DB_B))*3		//192
	#define  SIN_360 (1<<(SIN_DB_B))*4		//256
	#define ATAN_90   1<<ATAN_FIXP_B		//16k
	#define ACOS_180 (1<<ACOS_FIXP_B)*2		//32k
	#define HSIN_90   SIN_90<<8			//8k

class System {
	public:
	static int currentTimeMillis();
};

class Math {
	public:

	static int abs(int n);
	static int max(int a, int b);

};

class GameMidlet {
	public:	
};

class Integer {
	public:
	const static int MAX_VALUE = 536870911;
	static String toString(int n);
};

class Graphics {
	public:
	int r,g,b,col;	
	SDL_Surface *surf;

	int LEFT;
	int RIGHT;
	int HCENTER;
	int TOP;
	int BOTTOM;
	int VCENTER;

	Graphics();
	void setClip(int x, int y, int w, int h);	
	void clipRect(int x, int y, int w, int h);
	void setColor(int n);	
	void setColor(int r, int g, int b);
	void setColor256(int n);	
	void drawString(String s, int x, int y, int anc);	
	void fillRoundRect(int x, int y, int w, int h, int a, int b);	
	void drawRoundRect(int x, int y, int w, int h, int a, int b);	
	void fillRect(int x, int y, int w, int h);	
	void drawLine(int x1, int y1, int x2, int y2);	
	void drawRect(int x, int y, int w, int h);		
	void drawImage(Image img, int x, int y, int anc);
	void drawArc(int a0, int a1, int a2, int a3, int a4, int a5);
	void fillArc(int x, int y, int a2, int a3, int a4, int a5);
	
};

class Image {
	public:	
	SDL_Surface surf;
	Graphics gg;

	int getWidth();
	int getHeight();
	void postcreate();
	//void createImage(String s);	
	void createImage(int w, int h);
	void createImage(const void *p);
	Graphics getGraphics();
};

class MetaImage {
	public:	
	Image img;
	int meta[80][4];
	boolean ismeta;

	void paint(Graphics g, int x, int y, int anc, int tran, int frame);
	void makemeta();
	void create(String s);
	int getWidth();
	int getHeight();
	int getWidth(int f);
	int getHeight(int f);
};

class MultiImage {
	public:

	Image *img; //wrk   
	int frame;
	boolean loaded; 
	int numframes; //port
	
	static void loadMm(int n);
	void unload();	
	int getHeight(int n);
	int getWidth(int n);
	Image getImage(int n);
	void paint(Graphics g, int x, int y, int anc, int tran, int frame);
};

class PortSound {
	public:	
	int vol;
	boolean playing, looped;		
	char *sound;
	int size;
	
	PortSound();		
	void setVol(int v);	
	void load(String s);	
	void setLoop(boolean lp);	
	void play();
	void stop();
	void destroy();
	static void vibrate(int ms);
};

class Thread {
	public:
	static void sleep(int ms);
};

class Player {
};

extern MultiImage *mm; 
extern MetaImage mi[];
extern const String mmfiles[0]; //FIXME

#endif
