/* some utility bitwise operations */
	int	bit_or(int a, int b);
	int	bit_and(int a, int b);
	int	bit_not(int a);
	int	bit_xor(int a, int b);


/***************************** DreamShell Functions *************************/



#define DS_STATE_RUNNING 0
#define DS_STATE_SLEEP 1
#define DS_STATE_SHUTDOWN 2


int GetState();
void SetState(int state);

char *GetWorkPath();
void SetWorkPath(char *path);

int GetArch();
char *GetVersion();


// Execute the program at addr, and put args and argv in the first two
// argument slots.
int exec(uint32 addr, int args, char ** argv);

// Can be called from the executed program to exit immediately.
void exec_exit();


/** Initialize the exception system. */
void expt_init(void);

/** Shutdown the exception system. */
void expt_shutdown(void);


void dbgio_set_dev_ds();
void dbgio_set_dev_scif();
void dbgio_set_dev_fb();


typedef struct Item {

        //SLIST_ENTRY(Item) list; 
        const char *name;
        int id;
        int type;
        void *data;
   
} Item_t;




#define LIST_ITEM_USERDATA 0
#define LIST_ITEM_SDL_SURFACE 1

#define LIST_ITEM_GUI_SURFACE 2
#define LIST_ITEM_GUI_FONT 3
#define LIST_ITEM_GUI_WIDGET 4

#define LIST_ITEM_MODULE 5
#define LIST_ITEM_APP 6
#define LIST_ITEM_EVENT 7
#define LIST_ITEM_THREAD 8

Item_list_t *listMake();
void listDestroy(Item_list_t *lst, listFreeItemFunc *ifree);

int listGetLastId(Item_list_t *lst);

Item_t *listAddItem(Item_list_t *lst, int type, const char *name, void *data);
//Item_t *listUpdateItem(Item_list_t *lst, int type, int id, const char *name, void *data);

void listRemoveItem(Item_list_t *lst, Item_t *i, listFreeItemFunc *ifree);

Item_t *listGetItemByName(Item_list_t *lst, const char *name);
Item_t *listGetItemById(Item_list_t *lst, int id);

Item_t *listGetItemFirst(Item_list_t *lst);
Item_t *listGetItemNext(Item_t *i);




//typedef void ThreadRoutine (void *);

typedef struct Thread {
	
    kthread_t *thd;
    const char *name;
    int id;
    
} Thread_t;



int InitThreads();
void ShutdownThreads();

//Thread_t *CreateThread(ThreadRoutine *routine, void *param, const char *name);
int DestroyThread(Thread_t *p);
int RemoveThread(Thread_t *p);

int SetThreadPrio(Thread_t *p, int prio);

Thread_t *GetThreadByName(const char *name);
Thread_t *GetThreadById(int id);
Item_list_t *GetThreadList(); 

Thread_t *VoidToThread(void *ptr);




typedef struct App {
    
    const char *fn;
    const char *name;
    const char *icon;
    const char *ver;
    int id;
    
    Item_list_t *resources;
    Item_list_t *elements;
    mxml_node_t *xml;
    
    GUI_Widget *body;
        
	Thread_t *thd;
	//lua_State *lua;
    void *lua;
    
    int state;

} App_t;



#define APP_STATE_OPENED 0x00000001
#define APP_STATE_LOADED 0x00000002
#define APP_STATE_READY 0x00000003
#define APP_STATE_PROCESS 0x00000004
#define APP_STATE_SLEEP 0x00000005

int InitApps();
void ShutdownApps();

Item_list_t *GetAppList();
App_t *GetAppById(int id);
App_t *GetAppByFileName(const char *fn);
App_t *GetAppByName(const char *name);
App_t *GetCurApp();

App_t *AddApp(const char *fn);
int RemoveApp(App_t *app);

int OpenApp(App_t *app);
int CloseApp(App_t *app, int unload);
int SwitchApp(App_t *old_app, App_t *new_app, int unload);

int SetAppSleep(App_t *app, int sleep);

int LoadApp(App_t *app, int build);
int UnLoadApp(App_t *app);

int BuildAppBody(App_t *app);
int AddToAppBody(App_t *app, GUI_Widget *widget);
int RemoveFromAppBody(App_t *app, GUI_Widget *widget);

int CallAppBodyEvent(App_t *app, char *event);
void WaitApp(App_t *app);

App_t *VoidToApp(void *ptr);
char *FindXmlAttr(char *name, mxml_node_t *node, char *defValue);




#define EVENT_STATE_ACTIVE 0
#define EVENT_STATE_SLEEP 1


//typedef void Event_func(SDL_Event *, int);

typedef struct Event {

    const char *name;
    int id;
    Event_func *event;
    int state;
    int always;
       
} Event_t;




int InitEvents();
void ShutdownEvents();

//Event_t *AddEvent(const char *name, Event_func *event, int always);
int RemoveEvent(Event_t *e);

int SetEventState(Event_t *e, int state);


Item_list_t *GetEventList();
Event_t *GetEventById(int id);
Event_t *GetEventByName(const char *name);


void PushEvents(SDL_Event *event, int ext);
void ProcessEvents(SDL_Event *event);
Event_t *VoidToEvent(void *ptr);



typedef enum {
	CMD_OK = 0,			/* 0 */
	CMD_NO_ARG,         /* 1 */
	CMD_ERROR,		    /* 2 */
	CMD_NOT_EXISTS		/* 3 */
} CMD_RESULT;



typedef enum {
	CMD_TYPE_INTERNAL = 0,	/* 0 */
	CMD_TYPE_ELF,			/* 1 */
	CMD_TYPE_LUA,		    /* 2 */
	CMD_TYPE_DSC,		    /* 3 */
	CMD_TYPE_UKNOWN		/* 4 */
} CMD_TYPES;


//typedef int CmdHandler(int, char *[]);

typedef struct Cmd {

	const char *command;
	const char *desc;
	CmdHandler *handler;
	
} Cmd_t;



int CallCmd(int argc, char *argv[]);  
int CallExtCmd(int argc, char *argv[]); 
int CheckExtCmdType(char *fn);

Cmd_t *AddCmd(const char *cmd, const char *helpmsg, CmdHandler *handler);
void RemoveCmd(Cmd_t *cmd);

Item_list_t *GetCmdList();
Cmd_t *GetCmdByName(const char *name);

int InitCmd(); 
void ShutdownCmd();


char *SetConsoleStdout(char *buff);
void SetConsoleDebug(int mode);

//int ds_printf(const char *fmt, ...); 
//int ds_uprintf(const char *fmt, ...); 
//int dbg_printf(const char *fmt, ...);


int dsystem(const char *buff); 
//int dsystemf(const char *fmt, ...);
int dsystem_script(const char *fn, char *argv[]);

int InitConsole(const char *font, const char *background, int lines, int x, int y, int w, int h, int alpha);
void ShutdownConsole();

void DrawConsole();
int ToggleConsole();
void ShowConsole();
void HideConsole();
ConsoleInformation *GetConsole();



typedef struct Module {

    klibrary_t *lib;
    const char *fn;
    const char *name;
    int refcnt;
    int id;

} Module_t;




int InitModules();
void ShutdownModules();

Module_t *OpenModule(const char *fn);

int CloseModule(Module_t *m);

Item_list_t *GetModuleList();

Module_t *GetModuleById(int id);
Module_t *GetModuleByFileName(const char *fn);
Module_t *GetModuleByName(const char *name);

Module_t *VoidToModule(void *ptr);
uint32 GetFuncAddr(char *func);




/* SD Card file filesystem */

int SDFS_Init();
int SDFS_Shutdown();

// FATFS *SDFS_GetFAT();



int FileSize(char *fn);
int FileExists(char *fn);
int PeriphExists(char *name);

int makeabspath(char *buff, char *path, size_t size);
int makeabspath_wd(char *buff, char *path, char *dir, size_t size);
const char *relativeFilePath(char *rel, char *file);
int relativeFilePath_wb(char *buff, const char *rel, const char *file);

int ds_chdir(char *dir);
char *getcwd(char *buf, size_t size);
void ds_sleep(int ms);

//int splitline(char **argv, int max, char *line);
int hex_to_int(char c);
char *strtolower(char *str);
char* substring(const char* str, size_t begin, size_t len);


SDL_Surface *GetScreen();
void SetScreen(SDL_Surface *new_screen);


int InitVideo(int w, int h, int bpp);
void ShutdownVideo();

void InitVideoLocker();
void ShutdownVideoLocker();

void LockVideo();
void UnLockVideo();

void ShowIMG(char *fn, int x, int y, int delay, int fade);
void bf_print(int x, int y, char *str);

int InitGUI();
void ShutdownGUI();
int LockGUI();
int UnLockGUI();

Uint32 colorHexToRGB(char *color, SDL_Color *clr);
SDL_Color Uint32ToColor(Uint32 c);
Uint32 ColorToUint32(SDL_Color c);


typedef struct MouseCursor {
    
    SDL_Surface *bg;
    SDL_Surface *cursor;
    SDL_Rect src;
    SDL_Rect dst;
    int draw;

} MouseCursor_t;


/*
  Create cursor from file or surface.
*/
MouseCursor_t *CreateMouseCursor(char *fn, SDL_Surface *surface);
void DestroyMouseCursor(MouseCursor_t *c);

void DrawMouseCursor(MouseCursor_t *c, SDL_Event *event);

void SetActiveMouseCursor(MouseCursor_t *c);
MouseCursor_t *GetActiveMouseCursor();



/* 
 lua utility
 For allocate surface
*/
#define SDL_MASK_RED 0x00FF0000
#define SDL_MASK_GREEN 0x0000FF00
#define SDL_MASK_BLUE 0x000000FF
#define SDL_MASK_ALPHA 0xFF000000



/* Main include header for the SDL library */




/* Basic data types */
typedef enum {
	SDL_FALSE = 0,
	SDL_TRUE  = 1
} SDL_bool;
typedef unsigned char	Uint8;
typedef signed char	Sint8;
typedef unsigned short	Uint16;
typedef signed short	Sint16;
typedef unsigned int	Uint32;
typedef signed int	Sint32;


/* General keyboard/mouse state definitions */
enum { SDL_PRESSED = 0x01, SDL_RELEASED = 0x00 };


/* Public functions */
/* not for luaswig:  extern void SDL_SetError(const char *fmt); */
extern char * SDL_GetError(void);
extern void SDL_ClearError(void);


/* This is the OS scheduler timeslice, in milliseconds */
#define SDL_TIMESLICE		10

/* This is the maximum resolution of the SDL timer on all platforms */
#define TIMER_RESOLUTION	10	/* Experimentally determined */

/* Get the number of milliseconds since the SDL library initialization.
 * Note that this value wraps if the program runs for more than ~49 days.
 */
extern Uint32 SDL_GetTicks(void);

/* Wait a specified number of milliseconds before returning */
extern void SDL_Delay(Uint32 ms);


/* In order to use these functions, SDL_Init() must have been called
   with the SDL_INIT_JOYSTICK flag.  This causes SDL to scan the system
   for joysticks, and load appropriate drivers.
*/

/* The joystick structure used to identify an SDL joystick */
typedef struct SDL_Joystick {
} SDL_Joystick;


/* Function prototypes */
/*
 * Count the number of joysticks attached to the system
 */
extern int SDL_NumJoysticks(void);

/*
 * Get the implementation dependent name of a joystick.
 * This can be called before any joysticks are opened.
 * If no name can be found, this function returns NULL.
 */
extern const char *SDL_JoystickName(int device_index);

/*
 * Open a joystick for use - the index passed as an argument refers to
 * the N'th joystick on the system.  This index is the value which will
 * identify this joystick in future joystick events.
 *
 * This function returns a joystick identifier, or NULL if an error occurred.
 */
extern SDL_Joystick *SDL_JoystickOpen(int device_index);

/*
 * Returns 1 if the joystick has been opened, or 0 if it has not.
 */
extern int SDL_JoystickOpened(int device_index);

/*
 * Get the device index of an opened joystick.
 */
extern int SDL_JoystickIndex(SDL_Joystick *joystick);

/*
 * Get the number of general axis controls on a joystick
 */
extern int SDL_JoystickNumAxes(SDL_Joystick *joystick);

/*
 * Get the number of trackballs on a joystick
 * Joystick trackballs have only relative motion events associated
 * with them and their state cannot be polled.
 */
extern int SDL_JoystickNumBalls(SDL_Joystick *joystick);

/*
 * Get the number of POV hats on a joystick
 */
extern int SDL_JoystickNumHats(SDL_Joystick *joystick);

/*
 * Get the number of buttons on a joystick
 */
extern int SDL_JoystickNumButtons(SDL_Joystick *joystick);

/*
 * Update the current state of the open joysticks.
 * This is called automatically by the event loop if any joystick
 * events are enabled.
 */
extern void SDL_JoystickUpdate(void);

/*
 * Enable/disable joystick event polling.
 * If joystick events are disabled, you must call SDL_JoystickUpdate()
 * yourself and check the state of the joystick when you want joystick
 * information.
 * The state can be one of SDL_QUERY, SDL_ENABLE or SDL_IGNORE.
 */
extern int SDL_JoystickEventState(int state);

/*
 * Get the current state of an axis control on a joystick
 * The state is a value ranging from -32768 to 32767.
 * The axis indices start at index 0.
 */
extern Sint16 SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis);

/*
 * Get the current state of a POV hat on a joystick
 * The return value is one of the following positions:
 */
#define SDL_HAT_CENTERED	0x00
#define SDL_HAT_UP		0x01
#define SDL_HAT_RIGHT		0x02
#define SDL_HAT_DOWN		0x04
#define SDL_HAT_LEFT		0x08
#define SDL_HAT_RIGHTUP		(SDL_HAT_RIGHT|SDL_HAT_UP)
#define SDL_HAT_RIGHTDOWN	(SDL_HAT_RIGHT|SDL_HAT_DOWN)
#define SDL_HAT_LEFTUP		(SDL_HAT_LEFT|SDL_HAT_UP)
#define SDL_HAT_LEFTDOWN	(SDL_HAT_LEFT|SDL_HAT_DOWN)
/*
 * The hat indices start at index 0.
 */
extern Uint8 SDL_JoystickGetHat(SDL_Joystick *joystick, int hat);

/*
 * Get the ball axis change since the last poll
 * This returns 0, or -1 if you passed it invalid parameters.
 * The ball indices start at index 0.
 */
extern int SDL_JoystickGetBall(SDL_Joystick *joystick, int ball, int *dx, int *dy);

/*
 * Get the current state of a button on a joystick
 * The button indices start at index 0.
 */
extern Uint8 SDL_JoystickGetButton(SDL_Joystick *joystick, int button);

/*
 * Close a joystick previously opened with SDL_JoystickOpen()
 */
extern void SDL_JoystickClose(SDL_Joystick *joystick);






/* What we really want is a mapping of every raw key on the keyboard.
   To support international keyboards, we use the range 0xA1 - 0xFF
   as international virtual keycodes.  We'll follow in the footsteps of X11...
   The names of the keys
 */

typedef enum {
	/* The keyboard syms have been cleverly chosen to map to ASCII */
	SDLK_UNKNOWN		= 0,
	SDLK_FIRST		= 0,
	SDLK_BACKSPACE		= 8,
	SDLK_TAB		= 9,
	SDLK_CLEAR		= 12,
	SDLK_RETURN		= 13,
	SDLK_PAUSE		= 19,
	SDLK_ESCAPE		= 27,
	SDLK_SPACE		= 32,
	SDLK_EXCLAIM		= 33,
	SDLK_QUOTEDBL		= 34,
	SDLK_HASH		= 35,
	SDLK_DOLLAR		= 36,
	SDLK_AMPERSAND		= 38,
	SDLK_QUOTE		= 39,
	SDLK_LEFTPAREN		= 40,
	SDLK_RIGHTPAREN		= 41,
	SDLK_ASTERISK		= 42,
	SDLK_PLUS		= 43,
	SDLK_COMMA		= 44,
	SDLK_MINUS		= 45,
	SDLK_PERIOD		= 46,
	SDLK_SLASH		= 47,
	SDLK_0			= 48,
	SDLK_1			= 49,
	SDLK_2			= 50,
	SDLK_3			= 51,
	SDLK_4			= 52,
	SDLK_5			= 53,
	SDLK_6			= 54,
	SDLK_7			= 55,
	SDLK_8			= 56,
	SDLK_9			= 57,
	SDLK_COLON		= 58,
	SDLK_SEMICOLON		= 59,
	SDLK_LESS		= 60,
	SDLK_EQUALS		= 61,
	SDLK_GREATER		= 62,
	SDLK_QUESTION		= 63,
	SDLK_AT			= 64,
	/*
	   Skip uppercase letters
	 */
	SDLK_LEFTBRACKET	= 91,
	SDLK_BACKSLASH		= 92,
	SDLK_RIGHTBRACKET	= 93,
	SDLK_CARET		= 94,
	SDLK_UNDERSCORE		= 95,
	SDLK_BACKQUOTE		= 96,
	SDLK_a			= 97,
	SDLK_b			= 98,
	SDLK_c			= 99,
	SDLK_d			= 100,
	SDLK_e			= 101,
	SDLK_f			= 102,
	SDLK_g			= 103,
	SDLK_h			= 104,
	SDLK_i			= 105,
	SDLK_j			= 106,
	SDLK_k			= 107,
	SDLK_l			= 108,
	SDLK_m			= 109,
	SDLK_n			= 110,
	SDLK_o			= 111,
	SDLK_p			= 112,
	SDLK_q			= 113,
	SDLK_r			= 114,
	SDLK_s			= 115,
	SDLK_t			= 116,
	SDLK_u			= 117,
	SDLK_v			= 118,
	SDLK_w			= 119,
	SDLK_x			= 120,
	SDLK_y			= 121,
	SDLK_z			= 122,
	SDLK_DELETE		= 127,
	/* End of ASCII mapped keysyms */

	/* International keyboard syms */
	SDLK_WORLD_0		= 160,		/* 0xA0 */
	SDLK_WORLD_1		= 161,
	SDLK_WORLD_2		= 162,
	SDLK_WORLD_3		= 163,
	SDLK_WORLD_4		= 164,
	SDLK_WORLD_5		= 165,
	SDLK_WORLD_6		= 166,
	SDLK_WORLD_7		= 167,
	SDLK_WORLD_8		= 168,
	SDLK_WORLD_9		= 169,
	SDLK_WORLD_10		= 170,
	SDLK_WORLD_11		= 171,
	SDLK_WORLD_12		= 172,
	SDLK_WORLD_13		= 173,
	SDLK_WORLD_14		= 174,
	SDLK_WORLD_15		= 175,
	SDLK_WORLD_16		= 176,
	SDLK_WORLD_17		= 177,
	SDLK_WORLD_18		= 178,
	SDLK_WORLD_19		= 179,
	SDLK_WORLD_20		= 180,
	SDLK_WORLD_21		= 181,
	SDLK_WORLD_22		= 182,
	SDLK_WORLD_23		= 183,
	SDLK_WORLD_24		= 184,
	SDLK_WORLD_25		= 185,
	SDLK_WORLD_26		= 186,
	SDLK_WORLD_27		= 187,
	SDLK_WORLD_28		= 188,
	SDLK_WORLD_29		= 189,
	SDLK_WORLD_30		= 190,
	SDLK_WORLD_31		= 191,
	SDLK_WORLD_32		= 192,
	SDLK_WORLD_33		= 193,
	SDLK_WORLD_34		= 194,
	SDLK_WORLD_35		= 195,
	SDLK_WORLD_36		= 196,
	SDLK_WORLD_37		= 197,
	SDLK_WORLD_38		= 198,
	SDLK_WORLD_39		= 199,
	SDLK_WORLD_40		= 200,
	SDLK_WORLD_41		= 201,
	SDLK_WORLD_42		= 202,
	SDLK_WORLD_43		= 203,
	SDLK_WORLD_44		= 204,
	SDLK_WORLD_45		= 205,
	SDLK_WORLD_46		= 206,
	SDLK_WORLD_47		= 207,
	SDLK_WORLD_48		= 208,
	SDLK_WORLD_49		= 209,
	SDLK_WORLD_50		= 210,
	SDLK_WORLD_51		= 211,
	SDLK_WORLD_52		= 212,
	SDLK_WORLD_53		= 213,
	SDLK_WORLD_54		= 214,
	SDLK_WORLD_55		= 215,
	SDLK_WORLD_56		= 216,
	SDLK_WORLD_57		= 217,
	SDLK_WORLD_58		= 218,
	SDLK_WORLD_59		= 219,
	SDLK_WORLD_60		= 220,
	SDLK_WORLD_61		= 221,
	SDLK_WORLD_62		= 222,
	SDLK_WORLD_63		= 223,
	SDLK_WORLD_64		= 224,
	SDLK_WORLD_65		= 225,
	SDLK_WORLD_66		= 226,
	SDLK_WORLD_67		= 227,
	SDLK_WORLD_68		= 228,
	SDLK_WORLD_69		= 229,
	SDLK_WORLD_70		= 230,
	SDLK_WORLD_71		= 231,
	SDLK_WORLD_72		= 232,
	SDLK_WORLD_73		= 233,
	SDLK_WORLD_74		= 234,
	SDLK_WORLD_75		= 235,
	SDLK_WORLD_76		= 236,
	SDLK_WORLD_77		= 237,
	SDLK_WORLD_78		= 238,
	SDLK_WORLD_79		= 239,
	SDLK_WORLD_80		= 240,
	SDLK_WORLD_81		= 241,
	SDLK_WORLD_82		= 242,
	SDLK_WORLD_83		= 243,
	SDLK_WORLD_84		= 244,
	SDLK_WORLD_85		= 245,
	SDLK_WORLD_86		= 246,
	SDLK_WORLD_87		= 247,
	SDLK_WORLD_88		= 248,
	SDLK_WORLD_89		= 249,
	SDLK_WORLD_90		= 250,
	SDLK_WORLD_91		= 251,
	SDLK_WORLD_92		= 252,
	SDLK_WORLD_93		= 253,
	SDLK_WORLD_94		= 254,
	SDLK_WORLD_95		= 255,		/* 0xFF */

	/* Numeric keypad */
	SDLK_KP0		= 256,
	SDLK_KP1		= 257,
	SDLK_KP2		= 258,
	SDLK_KP3		= 259,
	SDLK_KP4		= 260,
	SDLK_KP5		= 261,
	SDLK_KP6		= 262,
	SDLK_KP7		= 263,
	SDLK_KP8		= 264,
	SDLK_KP9		= 265,
	SDLK_KP_PERIOD		= 266,
	SDLK_KP_DIVIDE		= 267,
	SDLK_KP_MULTIPLY	= 268,
	SDLK_KP_MINUS		= 269,
	SDLK_KP_PLUS		= 270,
	SDLK_KP_ENTER		= 271,
	SDLK_KP_EQUALS		= 272,

	/* Arrows + Home/End pad */
	SDLK_UP			= 273,
	SDLK_DOWN		= 274,
	SDLK_RIGHT		= 275,
	SDLK_LEFT		= 276,
	SDLK_INSERT		= 277,
	SDLK_HOME		= 278,
	SDLK_END		= 279,
	SDLK_PAGEUP		= 280,
	SDLK_PAGEDOWN		= 281,

	/* Function keys */
	SDLK_F1			= 282,
	SDLK_F2			= 283,
	SDLK_F3			= 284,
	SDLK_F4			= 285,
	SDLK_F5			= 286,
	SDLK_F6			= 287,
	SDLK_F7			= 288,
	SDLK_F8			= 289,
	SDLK_F9			= 290,
	SDLK_F10		= 291,
	SDLK_F11		= 292,
	SDLK_F12		= 293,
	SDLK_F13		= 294,
	SDLK_F14		= 295,
	SDLK_F15		= 296,

	/* Key state modifier keys */
	SDLK_NUMLOCK		= 300,
	SDLK_CAPSLOCK		= 301,
	SDLK_SCROLLOCK		= 302,
	SDLK_RSHIFT		= 303,
	SDLK_LSHIFT		= 304,
	SDLK_RCTRL		= 305,
	SDLK_LCTRL		= 306,
	SDLK_RALT		= 307,
	SDLK_LALT		= 308,
	SDLK_RMETA		= 309,
	SDLK_LMETA		= 310,
	SDLK_LSUPER		= 311,		/* Left "Windows" key */
	SDLK_RSUPER		= 312,		/* Right "Windows" key */
	SDLK_MODE		= 313,		/* "Alt Gr" key */
	SDLK_COMPOSE		= 314,		/* Multi-key compose key */

	/* Miscellaneous function keys */
	SDLK_HELP		= 315,
	SDLK_PRINT		= 316,
	SDLK_SYSREQ		= 317,
	SDLK_BREAK		= 318,
	SDLK_MENU		= 319,
	SDLK_POWER		= 320,		/* Power Macintosh power key */
	SDLK_EURO		= 321,		/* Some european keyboards */

	/* Add any other keys here */

	SDLK_LAST
} SDLKey;

/* Enumeration of valid key mods (possibly OR'd together) */
typedef enum {
	KMOD_NONE  = 0x0000,
	KMOD_LSHIFT= 0x0001,
	KMOD_RSHIFT= 0x0002,
	KMOD_LCTRL = 0x0040,
	KMOD_RCTRL = 0x0080,
	KMOD_LALT  = 0x0100,
	KMOD_RALT  = 0x0200,
	KMOD_LMETA = 0x0400,
	KMOD_RMETA = 0x0800,
	KMOD_NUM   = 0x1000,
	KMOD_CAPS  = 0x2000,
	KMOD_MODE  = 0x4000,
	KMOD_RESERVED = 0x8000
} SDLMod;

#define KMOD_CTRL	(KMOD_LCTRL|KMOD_RCTRL)
#define KMOD_SHIFT	(KMOD_LSHIFT|KMOD_RSHIFT)
#define KMOD_ALT	(KMOD_LALT|KMOD_RALT)
#define KMOD_META	(KMOD_LMETA|KMOD_RMETA)







/* Keysym structure
   - The scancode is hardware dependent, and should not be used by general
     applications.  If no hardware scancode is available, it will be 0.

   - The 'unicode' translated character is only available when character
     translation is enabled by the SDL_EnableUNICODE() API.  If non-zero,
     this is a UNICODE character corresponding to the keypress.  If the
     high 9 bits of the character are 0, then this maps to the equivalent
     ASCII character:
	char ch;
	if ( (keysym.unicode & 0xFF80) == 0 ) {
		ch = keysym.unicode & 0x7F;
	} else {
		An international character..
	}
 */
typedef struct {
	Uint8 scancode;			/* hardware specific scancode */
	SDLKey sym;			/* SDL virtual keysym */
	SDLMod mod;			/* current key modifiers */
	Uint16 unicode;			/* translated character */
} SDL_keysym;

/* This is the mask which refers to all hotkey bindings */
#define SDL_ALL_HOTKEYS		0xFFFFFFFF

/* Function prototypes */
/*
 * Enable/Disable UNICODE translation of keyboard input.
 * This translation has some overhead, so translation defaults off.
 * If 'enable' is 1, translation is enabled.
 * If 'enable' is 0, translation is disabled.
 * If 'enable' is -1, the translation state is not changed.
 * It returns the previous state of keyboard translation.
 */
extern int SDL_EnableUNICODE(int enable);

/*
 * Enable/Disable keyboard repeat.  Keyboard repeat defaults to off.
 * 'delay' is the initial delay in ms between the time when a key is
 * pressed, and keyboard repeat begins.
 * 'interval' is the time in ms between keyboard repeat events.
 */
#define SDL_DEFAULT_REPEAT_DELAY	500
#define SDL_DEFAULT_REPEAT_INTERVAL	30
/*
 * If 'delay' is set to 0, keyboard repeat is disabled.
 */
extern int SDL_EnableKeyRepeat(int delay, int interval);

/*
 * Get a snapshot of the current state of the keyboard.
 * Returns an array of keystates, indexed by the SDLK_* syms.
 * Used:
 * 	Uint8 *keystate = SDL_GetKeyState(NULL);
 *	if ( keystate[SDLK_RETURN] ) ... <RETURN> is pressed.
 */
/* not for tolua: extern Uint8 * SDL_GetKeyState(int *numkeys); */


/*
 * Get the current key modifier state
 */
extern SDLMod SDL_GetModState(void);

/*
 * Set the current key modifier state
 * This does not change the keyboard state, only the key modifier flags.
 */
extern void SDL_SetModState(SDLMod modstate);

/*
 * Get the name of an SDL virtual keysym
 */
extern char * SDL_GetKeyName(SDLKey key);











typedef struct WMcursor {
} WMcursor;	/* Implementation dependent */
typedef struct {
//  #ifdef NOT_FOR_LUASWIG
//  	SDL_Rect area;			/* The area of the mouse cursor */
//  	Sint16 hot_x, hot_y;		/* The "tip" of the cursor */
//  	Uint8 *data;			/* B/W cursor data */
//  	Uint8 *mask;			/* B/W cursor mask */
//  	Uint8 *save[2];			/* Place to save cursor area */
//  	WMcursor *wm_cursor;		/* Window-manager cursor */
//  #endif // NOT_FOR_LUASWIG
} SDL_Cursor;


/* Function prototypes */
/*
 * Retrieve the current state of the mouse.
 * The current button state is returned as a button bitmask, which can
 * be tested using the SDL_BUTTON(X) macros, and x and y are set to the
 * current mouse cursor position.  You can pass NULL for either x or y.
 */
extern Uint8 SDL_GetMouseState(int *x, int *y);

/*
 * Retrieve the current state of the mouse.
 * The current button state is returned as a button bitmask, which can
 * be tested using the SDL_BUTTON(X) macros, and x and y are set to the
 * mouse deltas since the last call to SDL_GetRelativeMouseState().
 */
extern Uint8 SDL_GetRelativeMouseState(int *x, int *y);


/*
 * Set the position of the mouse cursor (generates a mouse motion event)
 */
extern void SDL_WarpMouse(Uint16 x, Uint16 y);

/*
 * Create a cursor using the specified data and mask (in MSB format).
 * The cursor width must be a multiple of 8 bits.
 *
 * The cursor is created in black and white according to the following:
 * data  mask    resulting pixel on screen
 *  0     1       White
 *  1     1       Black
 *  0     0       Transparent
 *  1     0       Inverted color if possible, black if not.
 *
 * Cursors created with this function must be freed with SDL_FreeCursor().
 */
extern SDL_Cursor *SDL_CreateCursor
		(Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y);

/*
 * Set the currently active cursor to the specified one.
 * If the cursor is currently visible, the change will be immediately
 * represented on the display.
 */
extern void SDL_SetCursor(SDL_Cursor *cursor);

/*
 * Returns the currently active cursor.
 */
extern SDL_Cursor * SDL_GetCursor(void);

/*
 * Deallocates a cursor created with SDL_CreateCursor().
 */
extern void SDL_FreeCursor(SDL_Cursor *cursor);

/*
 * Toggle whether or not the cursor is shown on the screen.
 * The cursor start off displayed, but can be turned off.
 * SDL_ShowCursor() returns 1 if the cursor was being displayed
 * before the call, or 0 if it was not.  You can query the current
 * state by passing a 'toggle' value of -1.
 */
extern int SDL_ShowCursor(int toggle);

/* Used as a mask when testing buttons in buttonstate
   Button 1:	Left mouse button
   Button 2:	Middle mouse button
   Button 3:	Right mouse button
 */
//  #define SDL_BUTTON(X)		(SDL_PRESSED<<(X-1))
#define SDL_BUTTON_LEFT		1
#define SDL_BUTTON_MIDDLE	2
#define SDL_BUTTON_RIGHT	3
#define SDL_BUTTON_LMASK	SDL_BUTTON(SDL_BUTTON_LEFT)
#define SDL_BUTTON_MMASK	SDL_BUTTON(SDL_BUTTON_MIDDLE)
#define SDL_BUTTON_RMASK	SDL_BUTTON(SDL_BUTTON_RIGHT)



/* Event enumerations */
enum { SDL_NOEVENT = 0,			/* Unused (do not remove) */
       SDL_ACTIVEEVENT,			/* Application loses/gains visibility */
       SDL_KEYDOWN,			/* Keys pressed */
       SDL_KEYUP,			/* Keys released */
       SDL_MOUSEMOTION,			/* Mouse moved */
       SDL_MOUSEBUTTONDOWN,		/* Mouse button pressed */
       SDL_MOUSEBUTTONUP,		/* Mouse button released */
       SDL_JOYAXISMOTION,		/* Joystick axis motion */
       SDL_JOYBALLMOTION,		/* Joystick trackball motion */
       SDL_JOYHATMOTION,		/* Joystick hat position change */
       SDL_JOYBUTTONDOWN,		/* Joystick button pressed */
       SDL_JOYBUTTONUP,			/* Joystick button released */
       SDL_QUIT,			/* User-requested quit */
       SDL_SYSWMEVENT,			/* System specific event */
       SDL_EVENT_RESERVEDA,		/* Reserved for future use.. */
       SDL_EVENT_RESERVEDB,		/* Reserved for future use.. */
       SDL_VIDEORESIZE,			/* User resized video mode */
       SDL_VIDEOEXPOSE,			/* Screen needs to be redrawn */
       SDL_EVENT_RESERVED2,		/* Reserved for future use.. */
       SDL_EVENT_RESERVED3,		/* Reserved for future use.. */
       SDL_EVENT_RESERVED4,		/* Reserved for future use.. */
       SDL_EVENT_RESERVED5,		/* Reserved for future use.. */
       SDL_EVENT_RESERVED6,		/* Reserved for future use.. */
       SDL_EVENT_RESERVED7,		/* Reserved for future use.. */
       /* Events SDL_USEREVENT through SDL_MAXEVENTS-1 are for your use */
       SDL_USEREVENT = 24,
       /* This last event is only for bounding internal arrays
	  It is the number of bits in the event mask datatype -- Uint32
        */
       SDL_NUMEVENTS = 32
};

/* Predefined event masks */
//  #define SDL_EVENTMASK(X)	(1<<(X))
enum {
	SDL_ACTIVEEVENTMASK	= (1<<(SDL_ACTIVEEVENT)),
	SDL_KEYDOWNMASK		= (1<<(SDL_KEYDOWN)),
	SDL_KEYUPMASK		= (1<<(SDL_KEYUP)),
	SDL_MOUSEMOTIONMASK	= (1<<(SDL_MOUSEMOTION)),
	SDL_MOUSEBUTTONDOWNMASK	= (1<<(SDL_MOUSEBUTTONDOWN)),
	SDL_MOUSEBUTTONUPMASK	= (1<<(SDL_MOUSEBUTTONUP)),
	SDL_MOUSEEVENTMASK	= (1<<(SDL_MOUSEMOTION))|
	                          (1<<(SDL_MOUSEBUTTONDOWN))|
	                          (1<<(SDL_MOUSEBUTTONUP)),
	SDL_JOYAXISMOTIONMASK	= (1<<(SDL_JOYAXISMOTION)),
	SDL_JOYBALLMOTIONMASK	= (1<<(SDL_JOYBALLMOTION)),
	SDL_JOYHATMOTIONMASK	= (1<<(SDL_JOYHATMOTION)),
	SDL_JOYBUTTONDOWNMASK	= (1<<(SDL_JOYBUTTONDOWN)),
	SDL_JOYBUTTONUPMASK	= (1<<(SDL_JOYBUTTONUP)),
	SDL_JOYEVENTMASK	= (1<<(SDL_JOYAXISMOTION))|
	                          (1<<(SDL_JOYBALLMOTION))|
	                          (1<<(SDL_JOYHATMOTION))|
	                          (1<<(SDL_JOYBUTTONDOWN))|
	                          (1<<(SDL_JOYBUTTONUP)),
	SDL_VIDEORESIZEMASK	= (1<<(SDL_VIDEORESIZE)),
	SDL_VIDEOEXPOSEMASK	= (1<<(SDL_VIDEOEXPOSE)),
	SDL_QUITMASK		= (1<<(SDL_QUIT)),
	SDL_SYSWMEVENTMASK	= (1<<(SDL_SYSWMEVENT))
};
#define SDL_ALLEVENTS		0xFFFFFFFF

/* Application visibility event structure */
typedef struct {
	Uint8 type;	/* SDL_ACTIVEEVENT */
	Uint8 gain;	/* Whether given states were gained or lost (1/0) */
	Uint8 state;	/* A mask of the focus states */
} SDL_ActiveEvent;

/* Keyboard event structure */
typedef struct {
	Uint8 type;	/* SDL_KEYDOWN or SDL_KEYUP */
	Uint8 which;	/* The keyboard device index */
	Uint8 state;	/* SDL_PRESSED or SDL_RELEASED */
	SDL_keysym keysym;
} SDL_KeyboardEvent;

/* Mouse motion event structure */
typedef struct {
	Uint8 type;	/* SDL_MOUSEMOTION */
	Uint8 which;	/* The mouse device index */
	Uint8 state;	/* The current button state */
	Uint16 x;
	Uint16 y;	/* The X/Y coordinates of the mouse */
	Sint16 xrel;	/* The relative motion in the X direction */
	Sint16 yrel;	/* The relative motion in the Y direction */
} SDL_MouseMotionEvent;

/* Mouse button event structure */
typedef struct {
	Uint8 type;	/* SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP */
	Uint8 which;	/* The mouse device index */
	Uint8 button;	/* The mouse button index */
	Uint8 state;	/* SDL_PRESSED or SDL_RELEASED */
	Uint16 x;
	Uint16 y;	/* The X/Y coordinates of the mouse at press time */
} SDL_MouseButtonEvent;

/* Joystick axis motion event structure */
typedef struct {
	Uint8 type;	/* SDL_JOYAXISMOTION */
	Uint8 which;	/* The joystick device index */
	Uint8 axis;	/* The joystick axis index */
	Sint16 value;	/* The axis value (range: -32768 to 32767) */
} SDL_JoyAxisEvent;

/* Joystick trackball motion event structure */
typedef struct {
	Uint8 type;	/* SDL_JOYBALLMOTION */
	Uint8 which;	/* The joystick device index */
	Uint8 ball;	/* The joystick trackball index */
	Sint16 xrel;	/* The relative motion in the X direction */
	Sint16 yrel;	/* The relative motion in the Y direction */
} SDL_JoyBallEvent;

/* Joystick hat position change event structure */
typedef struct {
	Uint8 type;	/* SDL_JOYHATMOTION */
	Uint8 which;	/* The joystick device index */
	Uint8 hat;	/* The joystick hat index */
	Uint8 value;	/* The hat position value:
				8   1   2
				7   0   3
				6   5   4
			   Note that zero means the POV is centered.
			*/
} SDL_JoyHatEvent;

/* Joystick button event structure */
typedef struct {
	Uint8 type;	/* SDL_JOYBUTTONDOWN or SDL_JOYBUTTONUP */
	Uint8 which;	/* The joystick device index */
	Uint8 button;	/* The joystick button index */
	Uint8 state;	/* SDL_PRESSED or SDL_RELEASED */
} SDL_JoyButtonEvent;

/* The "window resized" event
   When you get this event, you are responsible for setting a new video
   mode with the new width and height.
 */
typedef struct {
	Uint8 type;	/* SDL_VIDEORESIZE */
	int w;		/* New width */
	int h;		/* New height */
} SDL_ResizeEvent;

/* The "screen redraw" event */
typedef struct {
	Uint8 type;	/* SDL_VIDEOEXPOSE */
} SDL_ExposeEvent;

/* The "quit requested" event */
typedef struct {
	Uint8 type;	/* SDL_QUIT */
} SDL_QuitEvent;

/* A user-defined event type */
typedef struct {
	Uint8 type;	/* SDL_USEREVENT through SDL_NUMEVENTS-1 */
	int code;	/* User defined event code */
	void *data1;	/* User defined data pointer */
	void *data2;	/* User defined data pointer */
} SDL_UserEvent;

//  #ifdef NOT_FOR_LUASWIG
//  /* If you want to use this event, you should include SDL_syswm.h */
//  struct SDL_SysWMmsg;
//  typedef struct SDL_SysWMmsg SDL_SysWMmsg;
//  #endif // NOT_FOR_LUASWIG
typedef struct {
//  #ifdef NOT_FOR_LUASWIG
//  	Uint8 type;
//  	SDL_SysWMmsg *msg;
//  #endif // NOT_FOR_LUASWIG
} SDL_SysWMEvent;


/* General event structure */
//  #ifdef NOT_FOR_TOLUA
//  typedef union {
//  #else
typedef struct {
//  #endif
	Uint8 type;
	SDL_ActiveEvent active;
	SDL_KeyboardEvent key;
	SDL_MouseMotionEvent motion;
	SDL_MouseButtonEvent button;
	SDL_JoyAxisEvent jaxis;
	SDL_JoyBallEvent jball;
	SDL_JoyHatEvent jhat;
	SDL_JoyButtonEvent jbutton;
	SDL_ResizeEvent resize;
	SDL_ExposeEvent expose;
	SDL_QuitEvent quit;
	SDL_UserEvent user;
	SDL_SysWMEvent syswm;
} SDL_Event;


SDL_Event*	SDL_Event_new();
void	SDL_Event_delete(SDL_Event* e);




/* Function prototypes */

/* Pumps the event loop, gathering events from the input devices.
   This function updates the event queue and internal input device state.
   This should only be run in the thread that sets the video mode.
*/
extern void SDL_PumpEvents(void);

/* Checks the event queue for messages and optionally returns them.
   If 'action' is SDL_ADDEVENT, up to 'numevents' events will be added to
   the back of the event queue.
   If 'action' is SDL_PEEKEVENT, up to 'numevents' events at the front
   of the event queue, matching 'mask', will be returned and will not
   be removed from the queue.
   If 'action' is SDL_GETEVENT, up to 'numevents' events at the front
   of the event queue, matching 'mask', will be returned and will be
   removed from the queue.
   This function returns the number of events actually stored, or -1
   if there was an error.  This function is thread-safe.
*/
typedef enum {
	SDL_ADDEVENT,
	SDL_PEEKEVENT,
	SDL_GETEVENT
} SDL_eventaction;
/* */
extern int SDL_PeepEvents(SDL_Event *events, int numevents,
				SDL_eventaction action, Uint32 mask);

/* Polls for currently pending events, and returns 1 if there are any pending
   events, or 0 if there are none available.  If 'event' is not NULL, the next
   event is removed from the queue and stored in that area.
 */
extern int SDL_PollEvent(SDL_Event *event);

/* Waits indefinitely for the next available event, returning 1, or 0 if there
   was an error while waiting for events.  If 'event' is not NULL, the next
   event is removed from the queue and stored in that area.
 */
extern int SDL_WaitEvent(SDL_Event *event);

/* Add an event to the event queue.
   This function returns 0, or -1 if the event couldn't be added to
   the event queue.  If the event queue is full, this function fails.
 */
extern int SDL_PushEvent(SDL_Event *event);


//  #ifdef NOT_FOR_LUASWIG
//  /*
//    This function sets up a filter to process all events before they
//    change internal state and are posted to the internal event queue.

//    The filter is protypted as:
//  */
//  typedef int (*SDL_EventFilter)(const SDL_Event *event);
//  /*
//    If the filter returns 1, then the event will be added to the internal queue.
//    If it returns 0, then the event will be dropped from the queue, but the
//    internal state will still be updated.  This allows selective filtering of
//    dynamically arriving events.

//    WARNING:  Be very careful of what you do in the event filter function, as
//              it may run in a different thread!

//    There is one caveat when dealing with the SDL_QUITEVENT event type.  The
//    event filter is only called when the window manager desires to close the
//    application window.  If the event filter returns 1, then the window will
//    be closed, otherwise the window will remain open if possible.
//    If the quit event is generated by an interrupt signal, it will bypass the
//    internal queue and be delivered to the application at the next event poll.
//  */
//  extern void SDL_SetEventFilter(SDL_EventFilter filter);

//  /*
//    Return the current event filter - can be used to "chain" filters.
//    If there is no event filter set, this function returns NULL.
//  */
//  extern SDL_EventFilter SDL_GetEventFilter(void);

//  #endif // NOT_FOR_LUASWIG


/*
  This function allows you to set the state of processing certain events.
  If 'state' is set to SDL_IGNORE, that event will be automatically dropped
  from the event queue and will not event be filtered.
  If 'state' is set to SDL_ENABLE, that event will be processed normally.
  If 'state' is set to SDL_QUERY, SDL_EventState() will return the
  current processing state of the specified event.
*/
#define SDL_QUERY	-1
#define SDL_IGNORE	 0
#define SDL_DISABLE	 0
#define SDL_ENABLE	 1
extern Uint8 SDL_EventState(Uint8 type, int state);


/* Transparency definitions: These define alpha as the opacity of a surface */
#define SDL_ALPHA_OPAQUE 255
#define SDL_ALPHA_TRANSPARENT 0

/* Useful data types */
typedef struct {
	Sint16 x;
	Sint16 y;
	Uint16 w;
	Uint16 h;

//	SDL_Rect();
//	~SDL_Rect();
} SDL_Rect;


SDL_Rect* SDL_Rect_new();
void SDL_Rect_delete(SDL_Rect *c);


typedef struct {
	Uint8 r;
	Uint8 g;
	Uint8 b;
	Uint8 unused;
} SDL_Color;

SDL_Color* SDL_Color_new();
void SDL_Color_delete(SDL_Color *c);

typedef struct {
	int       ncolors;
	SDL_Color *colors;
} SDL_Palette;

SDL_Palette* SDL_Palette_new();
void SDL_Palette_delete(SDL_Palette *p);

/* Everything in the pixel format structure is read-only */
typedef struct SDL_PixelFormat {
	SDL_Palette *palette;
	Uint8  BitsPerPixel;
	Uint8  BytesPerPixel;
	Uint8  Rloss;
	Uint8  Gloss;
	Uint8  Bloss;
	Uint8  Aloss;
	Uint8  Rshift;
	Uint8  Gshift;
	Uint8  Bshift;
	Uint8  Ashift;
	Uint32 Rmask;
	Uint32 Gmask;
	Uint32 Bmask;
	Uint32 Amask;

	/* RGB color key information */
	Uint32 colorkey;
	/* Alpha value information (per-surface alpha) */
	Uint8  alpha;
} SDL_PixelFormat;

/* typedef for private surface blitting functions */
//  #ifdef NOT_FOR_TOLUA
//  typedef struct SDL_Surface;
//  typedef int (*SDL_blit)(struct SDL_Surface *src, SDL_Rect *srcrect,
//  			struct SDL_Surface *dst, SDL_Rect *dstrect);
//  #endif // NOT_FOR_TOLUA

/* This structure should be treated as read-only, except for 'pixels',
   which, if not NULL, contains the raw pixel data for the surface.
*/
typedef struct SDL_Surface {
	Uint32 flags;				/* Read-only */
	SDL_PixelFormat *format;		/* Read-only */
	int w;
	int	h;				/* Read-only */
	Uint16 pitch;				/* Read-only */
	void *pixels;				/* Read-write */
//  #ifdef NOT_FOR_LUASWIG
//  	int offset;				/* Private */

//  	/* Hardware-specific surface info */
//  	struct private_hwdata *hwdata;

//  	/* clipping information */
//  	SDL_Rect clip_rect;			/* Read-only */
//  	Uint32 unused1;				/* for binary compatibility */

//  	/* Allow recursive locks */
//  	Uint32 locked;				/* Private */

//  	/* info for fast blit mapping to other surfaces */
//  	struct SDL_BlitMap *map;		/* Private */

//  	/* format version, bumped at every change to invalidate blit maps */
//  	unsigned int format_version;		/* Private */

//  	/* Reference count -- used when freeing surface */
//  	int refcount;				/* Read-mostly */
//  #endif // NOT_FOR_LUASWIG
} SDL_Surface;


/* These are the currently supported flags for the SDL_surface */
/* Available for SDL_CreateRGBSurface() or SDL_SetVideoMode() */
#define SDL_SWSURFACE	0x00000000	/* Surface is in system memory */
#define SDL_HWSURFACE	0x00000001	/* Surface is in video memory */
#define SDL_ASYNCBLIT	0x00000004	/* Use asynchronous blits if possible */
/* Available for SDL_SetVideoMode() */
#define SDL_ANYFORMAT	0x10000000	/* Allow any video depth/pixel-format */
#define SDL_HWPALETTE	0x20000000	/* Surface has exclusive palette */
#define SDL_DOUBLEBUF	0x40000000	/* Set up double-buffered video mode */
#define SDL_FULLSCREEN	0x80000000	/* Surface is a full screen display */
#define SDL_OPENGL      0x00000002      /* Create an OpenGL rendering context */
#define SDL_OPENGLBLIT	0x0000000A	/* Create an OpenGL rendering context and use it for blitting */
#define SDL_RESIZABLE	0x00000010	/* This video mode may be resized */
#define SDL_NOFRAME	0x00000020	/* No window caption or edge frame */
/* Used internally (read-only) */
#define SDL_HWACCEL	0x00000100	/* Blit uses hardware acceleration */
#define SDL_SRCCOLORKEY	0x00001000	/* Blit uses a source color key */
#define SDL_RLEACCELOK	0x00002000	/* Private flag */
#define SDL_RLEACCEL	0x00004000	/* Surface is RLE encoded */
#define SDL_SRCALPHA	0x00010000	/* Blit uses source alpha blending */
#define SDL_PREALLOC	0x01000000	/* Surface uses preallocated memory */



//  #ifdef NOT_FOR_TOLUA

//  /* Evaluates to true if the surface needs to be locked before access */
//  #define SDL_MUSTLOCK(surface)	\
//    (surface->offset ||		\
//    ((surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_RLEACCEL)) != 0))

//  #endif // NOT_FOR_TOLUA


/* Useful for determining the video hardware capabilities */
// tulrich: luaswig doesn't like the bitfields -- will need to wrap manually ...
typedef struct {
//  #ifdef NOT_FOR_LUASWIG
//  //	Uint32 hw_available :1;	/* Flag: Can you create hardware surfaces? */
//  //	Uint32 wm_available :1;	/* Flag: Can you talk to a window manager? */
//  //	Uint32 UnusedBits1  :6;
//  //	Uint32 UnusedBits2  :1;
//  //	Uint32 blit_hw      :1;	/* Flag: Accelerated blits HW --> HW */
//  //	Uint32 blit_hw_CC   :1;	/* Flag: Accelerated blits with Colorkey */
//  //	Uint32 blit_hw_A    :1;	/* Flag: Accelerated blits with Alpha */
//  //	Uint32 blit_sw      :1;	/* Flag: Accelerated blits SW --> HW */
//  //	Uint32 blit_sw_CC   :1;	/* Flag: Accelerated blits with Colorkey */
//  //	Uint32 blit_sw_A    :1;	/* Flag: Accelerated blits with Alpha */
//  //	Uint32 blit_fill    :1;	/* Flag: Accelerated color fill */
//  //	Uint32 UnusedBits3  :16;
//  #else // for luaswig
	Uint32	hw_available;
//  #endif // for luaswig
	Uint32 video_mem;	/* The total amount of video memory (in K) */
	SDL_PixelFormat *vfmt;	/* Value: The format of the video surface */
} SDL_VideoInfo;



/* The most common video overlay formats.
   For an explanation of these pixel formats, see:
	http://www.webartz.com/fourcc/indexyuv.htm

   For information on the relationship between color spaces, see:
   http://www.neuro.sfc.keio.ac.jp/~aly/polygon/info/color-space-faq.html
 */
#define SDL_YV12_OVERLAY  0x32315659	/* Planar mode: Y + V + U  (3 planes) */
#define SDL_IYUV_OVERLAY  0x56555949	/* Planar mode: Y + U + V  (3 planes) */
#define SDL_YUY2_OVERLAY  0x32595559	/* Packed mode: Y0+U0+Y1+V0 (1 plane) */
#define SDL_UYVY_OVERLAY  0x59565955	/* Packed mode: U0+Y0+V0+Y1 (1 plane) */
#define SDL_YVYU_OVERLAY  0x55595659	/* Packed mode: Y0+V0+Y1+U0 (1 plane) */

/* The YUV hardware video overlay */
// tulrich: luaswig doesn't like bitfields...
typedef struct SDL_Overlay {
//  #ifdef NOT_FOR_LUASWIG
//  	Uint32 format;				/* Read-only */
//  	int w, h;				/* Read-only */
//  	int planes;				/* Read-only */
//  	Uint16 *pitches;			/* Read-only */
//  	Uint8 **pixels;				/* Read-write */

//  	/* Hardware-specific surface info */
//  	struct private_yuvhwfuncs *hwfuncs;
//  	struct private_yuvhwdata *hwdata;

//  	/* Special flags */
//  	Uint32 hw_overlay :1;	/* Flag: This overlay hardware accelerated? */
//  	Uint32 UnusedBits :31;
//  	Uint32 hw_overlay;
//  #endif // NOT_FOR_LUASWIG
} SDL_Overlay;


/* Public enumeration for setting the OpenGL window attributes. */


typedef enum {
    SDL_GL_RED_SIZE,
    SDL_GL_GREEN_SIZE,
    SDL_GL_BLUE_SIZE,
    SDL_GL_ALPHA_SIZE,
    SDL_GL_BUFFER_SIZE,
    SDL_GL_DOUBLEBUFFER,
    SDL_GL_DEPTH_SIZE,
    SDL_GL_STENCIL_SIZE,
    SDL_GL_ACCUM_RED_SIZE,
    SDL_GL_ACCUM_GREEN_SIZE,
    SDL_GL_ACCUM_BLUE_SIZE,
    SDL_GL_ACCUM_ALPHA_SIZE
} SDL_GLattr;

/* flags for SDL_SetPalette() */
#define SDL_LOGPAL 0x01
#define SDL_PHYSPAL 0x02

/* Function prototypes */

/* These functions are used internally, and should not be used unless you
 * have a specific need to specify the video driver you want to use.
 * You should normally use SDL_Init() or SDL_InitSubSystem().
 *
 * SDL_VideoInit() initializes the video subsystem -- sets up a connection
 * to the window manager, etc, and determines the current video mode and
 * pixel format, but does not initialize a window or graphics mode.
 * Note that event handling is activated by this routine.
 *
 * If you use both sound and video in your application, you need to call
 * SDL_Init() before opening the sound device, otherwise under Win32 DirectX,
 * you won't be able to set full-screen display modes.
 */
extern int SDL_VideoInit(const char *driver_name, Uint32 flags);
extern void SDL_VideoQuit(void);

/* This function fills the given character buffer with the name of the
 * video driver, and returns a pointer to it if the video driver has
 * been initialized.  It returns NULL if no driver has been initialized.
 */
extern char *SDL_VideoDriverName(char *namebuf, int maxlen);

/*
 * This function returns a pointer to the current display surface.
 * If SDL is doing format conversion on the display surface, this
 * function returns the publicly visible surface, not the real video
 * surface.
 */
extern SDL_Surface * SDL_GetVideoSurface(void);

/*
 * This function returns a read-only pointer to information about the
 * video hardware.  If this is called before SDL_SetVideoMode(), the 'vfmt'
 * member of the returned structure will contain the pixel format of the
 * "best" video mode.
 */
extern const SDL_VideoInfo * SDL_GetVideoInfo(void);

/*
 * Check to see if a particular video mode is supported.
 * It returns 0 if the requested mode is not supported under any bit depth,
 * or returns the bits-per-pixel of the closest available mode with the
 * given width and height.  If this bits-per-pixel is different from the
 * one used when setting the video mode, SDL_SetVideoMode() will succeed,
 * but will emulate the requested bits-per-pixel with a shadow surface.
 *
 * The arguments to SDL_VideoModeOK() are the same ones you would pass to
 * SDL_SetVideoMode()
 */
extern int SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags);

/*
 * Return a pointer to an array of available screen dimensions for the
 * given format and video flags, sorted largest to smallest.  Returns
 * NULL if there are no dimensions available for a particular format,
 * or (SDL_Rect **)-1 if any dimension is okay for the given format.
 *
 * If 'format' is NULL, the mode list will be for the format given
 * by SDL_GetVideoInfo()->vfmt
 */
//  #ifdef NOT_FOR_TOLUA
//  extern SDL_Rect ** SDL_ListModes(SDL_PixelFormat *format, Uint32 flags);
//  #endif // NOT_FOR_TOLUA


/*
 * Set up a video mode with the specified width, height and bits-per-pixel.
 *
 * If 'bpp' is 0, it is treated as the current display bits per pixel.
 *
 * If SDL_ANYFORMAT is set in 'flags', the SDL library will try to set the
 * requested bits-per-pixel, but will return whatever video pixel format is
 * available.  The default is to emulate the requested pixel format if it
 * is not natively available.
 *
 * If SDL_HWSURFACE is set in 'flags', the video surface will be placed in
 * video memory, if possible, and you may have to call SDL_LockSurface()
 * in order to access the raw framebuffer.  Otherwise, the video surface
 * will be created in system memory.
 *
 * If SDL_ASYNCBLIT is set in 'flags', SDL will try to perform rectangle
 * updates asynchronously, but you must always lock before accessing pixels.
 * SDL will wait for updates to complete before returning from the lock.
 *
 * If SDL_HWPALETTE is set in 'flags', the SDL library will guarantee
 * that the colors set by SDL_SetColors() will be the colors you get.
 * Otherwise, in 8-bit mode, SDL_SetColors() may not be able to set all
 * of the colors exactly the way they are requested, and you should look
 * at the video surface structure to determine the actual palette.
 * If SDL cannot guarantee that the colors you request can be set,
 * i.e. if the colormap is shared, then the video surface may be created
 * under emulation in system memory, overriding the SDL_HWSURFACE flag.
 *
 * If SDL_FULLSCREEN is set in 'flags', the SDL library will try to set
 * a fullscreen video mode.  The default is to create a windowed mode
 * if the current graphics system has a window manager.
 * If the SDL library is able to set a fullscreen video mode, this flag
 * will be set in the surface that is returned.
 *
 * If SDL_DOUBLEBUF is set in 'flags', the SDL library will try to set up
 * two surfaces in video memory and swap between them when you call
 * SDL_Flip().  This is usually slower than the normal single-buffering
 * scheme, but prevents "tearing" artifacts caused by modifying video
 * memory while the monitor is refreshing.  It should only be used by
 * applications that redraw the entire screen on every update.
 *
 * If SDL_RESIZABLE is set in 'flags', the SDL library will allow the
 * window manager, if any, to resize the window at runtime.  When this
 * occurs, SDL will send a SDL_VIDEORESIZE event to you application,
 * and you must respond to the event by re-calling SDL_SetVideoMode()
 * with the requested size (or another size that suits the application).
 *
 * If SDL_NOFRAME is set in 'flags', the SDL library will create a window
 * without any title bar or frame decoration.  Fullscreen video modes have
 * this flag set automatically.
 *
 * This function returns the video framebuffer surface, or NULL if it fails.
 *
 * If you rely on functionality provided by certain video flags, check the
 * flags of the returned surface to make sure that functionality is available.
 * SDL will fall back to reduced functionality if the exact flags you wanted
 * are not available.
 */
extern SDL_Surface *SDL_SetVideoMode
			(int width, int height, int bpp, Uint32 flags);

/*
 * Makes sure the given list of rectangles is updated on the given screen.
 * If 'x', 'y', 'w' and 'h' are all 0, SDL_UpdateRect will update the entire
 * screen.
 * These functions should not be called while 'screen' is locked.
 */
extern void SDL_UpdateRects
		(SDL_Surface *screen, int numrects, SDL_Rect *rects);
extern void SDL_UpdateRect
		(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h);

/*
 * On hardware that supports double-buffering, this function sets up a flip
 * and returns.  The hardware will wait for vertical retrace, and then swap
 * video buffers before the next video surface blit or lock will return.
 * On hardware that doesn not support double-buffering, this is equivalent
 * to calling SDL_UpdateRect(screen, 0, 0, 0, 0);
 * The SDL_DOUBLEBUF flag must have been passed to SDL_SetVideoMode() when
 * setting the video mode for this function to perform hardware flipping.
 * This function returns 0 if successful, or -1 if there was an error.
 */
extern int SDL_Flip(SDL_Surface *screen);

/*
 * Set the gamma correction for each of the color channels.
 * The gamma values range (approximately) between 0.1 and 10.0
 *
 * If this function isn't supported directly by the hardware, it will
 * be emulated using gamma ramps, if available.  If successful, this
 * function returns 0, otherwise it returns -1.
 */
//extern int SDL_SetGamma(float red, float green, float blue);

/*
 * Set the gamma translation table for the red, green, and blue channels
 * of the video hardware.  Each table is an array of 256 16-bit quantities,
 * representing a mapping between the input and output for that channel.
 * The input is the index into the array, and the output is the 16-bit
 * gamma value at that index, scaled to the output color precision.
 *
 * You may pass NULL for any of the channels to leave it unchanged.
 * If the call succeeds, it will return 0.  If the display driver or
 * hardware does not support gamma translation, or otherwise fails,
 * this function will return -1.
 */
//extern int SDL_SetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue);

/*
 * Retrieve the current values of the gamma translation tables.
 *
 * You must pass in valid pointers to arrays of 256 16-bit quantities.
 * Any of the pointers may be NULL to ignore that channel.
 * If the call succeeds, it will return 0.  If the display driver or
 * hardware does not support gamma translation, or otherwise fails,
 * this function will return -1.
 */
//extern int SDL_GetGammaRamp(Uint16 *red, Uint16 *green, Uint16 *blue);

/*
 * Sets a portion of the colormap for the given 8-bit surface.  If 'surface'
 * is not a palettized surface, this function does nothing, returning 0.
 * If all of the colors were set as passed to SDL_SetColors(), it will
 * return 1.  If not all the color entries were set exactly as given,
 * it will return 0, and you should look at the surface palette to
 * determine the actual color palette.
 *
 * When 'surface' is the surface associated with the current display, the
 * display colormap will be updated with the requested colors.  If
 * SDL_HWPALETTE was set in SDL_SetVideoMode() flags, SDL_SetColors()
 * will always return 1, and the palette is guaranteed to be set the way
 * you desire, even if the window colormap has to be warped or run under
 * emulation.
 */
extern int SDL_SetColors(SDL_Surface *surface,
			SDL_Color *colors, int firstcolor, int ncolors);

/*
 * Sets a portion of the colormap for a given 8-bit surface.
 * 'flags' is one or both of:
 * SDL_LOGPAL  -- set logical palette, which controls how blits are mapped
 *                to/from the surface,
 * SDL_PHYSPAL -- set physical palette, which controls how pixels look on
 *                the screen
 * Only screens have physical palettes. Separate change of physical/logical
 * palettes is only possible if the screen has SDL_HWPALETTE set.
 *
 * The return value is 1 if all colours could be set as requested, and 0
 * otherwise.
 *
 * SDL_SetColors() is equivalent to calling this function with
 *     flags = (SDL_LOGPAL|SDL_PHYSPAL).
 */
extern int SDL_SetPalette(SDL_Surface *surface, int flags,
				   SDL_Color *colors, int firstcolor,
				   int ncolors);

/*
 * Maps an RGB triple to an opaque pixel value for a given pixel format
 */
extern Uint32 SDL_MapRGB
			(SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b);

/*
 * Maps an RGBA quadruple to a pixel value for a given pixel format
 */
extern Uint32 SDL_MapRGBA(SDL_PixelFormat *format,
				   Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/*
 * Maps a pixel value into the RGB components for a given pixel format
 */
extern void SDL_GetRGB(Uint32 pixel, SDL_PixelFormat *fmt,
				Uint8 *r, Uint8 *g, Uint8 *b);

/*
 * Maps a pixel value into the RGBA components for a given pixel format
 */
extern void SDL_GetRGBA(Uint32 pixel, SDL_PixelFormat *fmt,
				 Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a);

/*
 * Allocate and free an RGB surface (must be called after SDL_SetVideoMode)
 * If the depth is 4 or 8 bits, an empty palette is allocated for the surface.
 * If the depth is greater than 8 bits, the pixel format is set using the
 * flags '[RGB]mask'.
 * If the function runs out of memory, it will return NULL.
 *
 * The 'flags' tell what kind of surface to create.
 * SDL_SWSURFACE means that the surface should be created in system memory.
 * SDL_HWSURFACE means that the surface should be created in video memory,
 * with the same format as the display surface.  This is useful for surfaces
 * that will not change much, to take advantage of hardware acceleration
 * when being blitted to the display surface.
 * SDL_ASYNCBLIT means that SDL will try to perform asynchronous blits with
 * this surface, but you must always lock it before accessing the pixels.
 * SDL will wait for current blits to finish before returning from the lock.
 * SDL_SRCCOLORKEY indicates that the surface will be used for colorkey blits.
 * If the hardware supports acceleration of colorkey blits between
 * two surfaces in video memory, SDL will try to place the surface in
 * video memory. If this isn't possible or if there is no hardware
 * acceleration available, the surface will be placed in system memory.
 * SDL_SRCALPHA means that the surface will be used for alpha blits and
 * if the hardware supports hardware acceleration of alpha blits between
 * two surfaces in video memory, to place the surface in video memory
 * if possible, otherwise it will be placed in system memory.
 * If the surface is created in video memory, blits will be _much_ faster,
 * but the surface format must be identical to the video surface format,
 * and the only way to access the pixels member of the surface is to use
 * the SDL_LockSurface() and SDL_UnlockSurface() calls.
 * If the requested surface actually resides in video memory, SDL_HWSURFACE
 * will be set in the flags member of the returned surface.  If for some
 * reason the surface could not be placed in video memory, it will not have
 * the SDL_HWSURFACE flag set, and will be created in system memory instead.
 */
//  #define SDL_AllocSurface    SDL_CreateRGBSurface
extern SDL_Surface *SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth,
			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
extern SDL_Surface *SDL_CreateRGBSurfaceFrom(void *pixels,
			int width, int height, int depth, int pitch,
			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
extern void SDL_FreeSurface(SDL_Surface *surface);

/*
 * SDL_LockSurface() sets up a surface for directly accessing the pixels.
 * Between calls to SDL_LockSurface()/SDL_UnlockSurface(), you can write
 * to and read from 'surface->pixels', using the pixel format stored in
 * 'surface->format'.  Once you are done accessing the surface, you should
 * use SDL_UnlockSurface() to release it.
 *
 * Not all surfaces require locking.  If SDL_MUSTLOCK(surface) evaluates
 * to 0, then you can read and write to the surface at any time, and the
 * pixel format of the surface will not change.  In particular, if the
 * SDL_HWSURFACE flag is not given when calling SDL_SetVideoMode(), you
 * will not need to lock the display surface before accessing it.
 *
 * No operating system or library calls should be made between lock/unlock
 * pairs, as critical system locks may be held during this time.
 *
 * SDL_LockSurface() returns 0, or -1 if the surface couldn't be locked.
 */
extern int SDL_LockSurface(SDL_Surface *surface);
extern void SDL_UnlockSurface(SDL_Surface *surface);

/*
 * Load a surface from a seekable SDL data source (memory or file.)
 * If 'freesrc' is non-zero, the source will be closed after being read.
 * Returns the new surface, or NULL if there was an error.
 * The new surface should be freed with SDL_FreeSurface().
 */
extern SDL_Surface * SDL_LoadBMP_RW(SDL_RWops *src, int freesrc);

//  /* Convenience macro -- load a surface from a file */
//  #define SDL_LoadBMP(file)	SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
extern SDL_Surface* SDL_LoadBMP(const char* file);

/*
 * Save a surface to a seekable SDL data source (memory or file.)
 * If 'freedst' is non-zero, the source will be closed after being written.
 * Returns 0 if successful or -1 if there was an error.
 */
extern int SDL_SaveBMP_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst);


//  #ifdef NOT_FOR_TOLUA
//  /* Convenience macro -- save a surface to a file */
//  #define SDL_SaveBMP(surface, file) \
//  		SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
//  #else	// not NOT_FOR_TOLUA

extern int SDL_SaveBMP(SDL_Surface* surface, const char* file);

//  #endif // not NOT_FOR_TOLUA

/*
 * Sets the color key (transparent pixel) in a blittable surface.
 * If 'flag' is SDL_SRCCOLORKEY (optionally OR'd with SDL_RLEACCEL),
 * 'key' will be the transparent pixel in the source image of a blit.
 * SDL_RLEACCEL requests RLE acceleration for the surface if present,
 * and removes RLE acceleration if absent.
 * If 'flag' is 0, this function clears any current color key.
 * This function returns 0, or -1 if there was an error.
 */
extern int SDL_SetColorKey
			(SDL_Surface *surface, Uint32 flag, Uint32 key);

/*
 * This function sets the alpha value for the entire surface, as opposed to
 * using the alpha component of each pixel. This value measures the range
 * of transparency of the surface, 0 being completely transparent to 255
 * being completely opaque. An 'alpha' value of 255 causes blits to be
 * opaque, the source pixels copied to the destination (the default). Note
 * that per-surface alpha can be combined with colorkey transparency.
 *
 * If 'flag' is 0, alpha blending is disabled for the surface.
 * If 'flag' is SDL_SRCALPHA, alpha blending is enabled for the surface.
 * OR:ing the flag with SDL_RLEACCEL requests RLE acceleration for the
 * surface; if SDL_RLEACCEL is not specified, the RLE accel will be removed.
 */
extern int SDL_SetAlpha(SDL_Surface *surface, Uint32 flag, Uint8 alpha);

/*
 * Sets the clipping rectangle for the destination surface in a blit.
 *
 * If the clip rectangle is NULL, clipping will be disabled.
 * If the clip rectangle doesn't intersect the surface, the function will
 * return SDL_FALSE and blits will be completely clipped.  Otherwise the
 * function returns SDL_TRUE and blits to the surface will be clipped to
 * the intersection of the surface area and the clipping rectangle.
 *
 * Note that blits are automatically clipped to the edges of the source
 * and destination surfaces.
 */
extern SDL_bool SDL_SetClipRect(SDL_Surface *surface, SDL_Rect *rect);

/*
 * Gets the clipping rectangle for the destination surface in a blit.
 * 'rect' must be a pointer to a valid rectangle which will be filled
 * with the correct values.
 */
extern void SDL_GetClipRect(SDL_Surface *surface, SDL_Rect *rect);

/*
 * Creates a new surface of the specified format, and then copies and maps
 * the given surface to it so the blit of the converted surface will be as
 * fast as possible.  If this function fails, it returns NULL.
 *
 * The 'flags' parameter is passed to SDL_CreateRGBSurface() and has those
 * semantics.  You can also pass SDL_RLEACCEL in the flags parameter and
 * SDL will try to RLE accelerate colorkey and alpha blits in the resulting
 * surface.
 *
 * This function is used internally by SDL_DisplayFormat().
 */
extern SDL_Surface *SDL_ConvertSurface
			(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags);

/*
 * This performs a fast blit from the source surface to the destination
 * surface.  It assumes that the source and destination rectangles are
 * the same size.  If either 'srcrect' or 'dstrect' are NULL, the entire
 * surface (src or dst) is copied.  The final blit rectangles are saved
 * in 'srcrect' and 'dstrect' after all clipping is performed.
 * If the blit is successful, it returns 0, otherwise it returns -1.
 *
 * The blit function should not be called on a locked surface.
 *
 * The blit semantics for surfaces with and without alpha and colorkey
 * are defined as follows:
 *
 * RGBA->RGB:
 *     SDL_SRCALPHA set:
 * 	alpha-blend (using alpha-channel).
 * 	SDL_SRCCOLORKEY ignored.
 *     SDL_SRCALPHA not set:
 * 	copy RGB.
 * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
 * 	RGB values of the source colour key, ignoring alpha in the
 * 	comparison.
 *
 * RGB->RGBA:
 *     SDL_SRCALPHA set:
 * 	alpha-blend (using the source per-surface alpha value);
 * 	set destination alpha to opaque.
 *     SDL_SRCALPHA not set:
 * 	copy RGB, set destination alpha to opaque.
 *     both:
 * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
 * 	source colour key.
 *
 * RGBA->RGBA:
 *     SDL_SRCALPHA set:
 * 	alpha-blend (using the source alpha channel) the RGB values;
 * 	leave destination alpha untouched. [Note: is this correct?]
 * 	SDL_SRCCOLORKEY ignored.
 *     SDL_SRCALPHA not set:
 * 	copy all of RGBA to the destination.
 * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
 * 	RGB values of the source colour key, ignoring alpha in the
 * 	comparison.
 *
 * RGB->RGB:
 *     SDL_SRCALPHA set:
 * 	alpha-blend (using the source per-surface alpha value).
 *     SDL_SRCALPHA not set:
 * 	copy RGB.
 *     both:
 * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
 * 	source colour key.
 *
 * If either of the surfaces were in video memory, and the blit returns -2,
 * the video memory was lost, so it should be reloaded with artwork and
 * re-blitted:
	while ( SDL_BlitSurface(image, imgrect, screen, dstrect) == -2 ) {
		while ( SDL_LockSurface(image) < 0 )
			Sleep(10);
		-- Write image pixels to image->pixels --
		SDL_UnlockSurface(image);
	}
 * This happens under DirectX 5.0 when the system switches away from your
 * fullscreen application.  The lock will also fail until you have access
 * to the video memory again.
 */
/* You should call SDL_BlitSurface() unless you know exactly how SDL
   blitting works internally and how to use the other blit functions.
*/
//#define SDL_BlitSurface SDL_UpperBlit

/* This is the public blit function, SDL_BlitSurface(), and it performs
   rectangle validation and clipping before passing it to SDL_LowerBlit()
*/
extern int SDL_UpperBlit
			(SDL_Surface *src, SDL_Rect *srcrect,
			 SDL_Surface *dst, SDL_Rect *dstrect);
/* This is a semi-private blit function and it performs low-level surface
   blitting only.
*/
extern int SDL_LowerBlit
			(SDL_Surface *src, SDL_Rect *srcrect,
			 SDL_Surface *dst, SDL_Rect *dstrect);

/*
 * This function performs a fast fill of the given rectangle with 'color'
 * The given rectangle is clipped to the destination surface clip area
 * and the final fill rectangle is saved in the passed in pointer.
 * If 'dstrect' is NULL, the whole surface will be filled with 'color'
 * The color should be a pixel of the format used by the surface, and
 * can be generated by the SDL_MapRGB() function.
 * This function returns 0 on success, or -1 on error.
 */
extern int SDL_FillRect
		(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);

/*
 * This function takes a surface and copies it to a new surface of the
 * pixel format and colors of the video framebuffer, suitable for fast
 * blitting onto the display surface.  It calls SDL_ConvertSurface()
 *
 * If you want to take advantage of hardware colorkey or alpha blit
 * acceleration, you should set the colorkey and alpha value before
 * calling this function.
 *
 * If the conversion fails or runs out of memory, it returns NULL
 */
extern SDL_Surface * SDL_DisplayFormat(SDL_Surface *surface);

/*
 * This function takes a surface and copies it to a new surface of the
 * pixel format and colors of the video framebuffer (if possible),
 * suitable for fast alpha blitting onto the display surface.
 * The new surface will always have an alpha channel.
 *
 * If you want to take advantage of hardware colorkey or alpha blit
 * acceleration, you should set the colorkey and alpha value before
 * calling this function.
 *
 * If the conversion fails or runs out of memory, it returns NULL
 */
extern SDL_Surface * SDL_DisplayFormatAlpha(SDL_Surface *surface);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* YUV video surface overlay functions                                       */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* This function creates a video output overlay
   Calling the returned surface an overlay is something of a misnomer because
   the contents of the display surface underneath the area where the overlay
   is shown is undefined - it may be overwritten with the converted YUV data.
*/

//extern SDL_Overlay *SDL_CreateYUVOverlay(int width, int height,
//				Uint32 format, SDL_Surface *display);

/* Lock an overlay for direct access, and unlock it when you are done */
//extern int SDL_LockYUVOverlay(SDL_Overlay *overlay);
//extern void SDL_UnlockYUVOverlay(SDL_Overlay *overlay);

/* Blit a video overlay to the display surface.
   The contents of the video surface underneath the blit destination are
   not defined.
   The width and height of the destination rectangle may be different from
   that of the overlay, but currently only 2x scaling is supported.
*/
//extern int SDL_DisplayYUVOverlay(SDL_Overlay *overlay, SDL_Rect *dstrect);

/* Free a video overlay */
//extern void SDL_FreeYUVOverlay(SDL_Overlay *overlay);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* OpenGL support functions.                                                 */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Dynamically load a GL driver, if SDL is built with dynamic GL.
 *
 * SDL links normally with the OpenGL library on your system by default,
 * but you can compile it to dynamically load the GL driver at runtime.
 * If you do this, you need to retrieve all of the GL functions used in
 * your program from the dynamic library using SDL_GL_GetProcAddress().
 *
 * This is disabled in default builds of SDL.
 */
extern int SDL_GL_LoadLibrary(const char *path);

/*
 * Get the address of a GL function (for extension functions)
 */
extern void *SDL_GL_GetProcAddress(const char* proc);

/*
 * Set an attribute of the OpenGL subsystem before intialization.
 */
extern int SDL_GL_SetAttribute(SDL_GLattr attr, int value);

/*
 * Get an attribute of the OpenGL subsystem from the windowing
 * interface, such as glX. This is of course different from getting
 * the values from SDL's internal OpenGL subsystem, which only
 * stores the values you request before initialization.
 *
 * Developers should track the values they pass into SDL_GL_SetAttribute
 * themselves if they want to retrieve these values.
 */
extern int SDL_GL_GetAttribute(SDL_GLattr attr, int* value);

/*
 * Swap the OpenGL buffers, if double-buffering is supported.
 */
extern void SDL_GL_SwapBuffers(void);

/*
 * Internal functions that should not be called unless you have read
 * and understood the source code for these functions.
 */
extern void SDL_GL_UpdateRects(int numrects, SDL_Rect* rects);
extern void SDL_GL_Lock(void);
extern void SDL_GL_Unlock(void);




/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL
*/
#define SDL_MAJOR_VERSION	1
#define SDL_MINOR_VERSION	2
#define SDL_PATCHLEVEL		9



typedef struct FILE {
} FILE;


/* This is the read/write operation structure -- very basic */

typedef struct SDL_RWops {
} SDL_RWops;


/* Functions to create SDL_RWops structures from various data sources */

extern SDL_RWops * SDL_RWFromFile(const char *file, const char *mode);

extern SDL_RWops * SDL_RWFromFP(FILE *fp, int autoclose);

extern SDL_RWops * SDL_RWFromMem(void *mem, int size);

extern SDL_RWops * SDL_AllocRW(void);
extern void SDL_FreeRW(SDL_RWops *area);

/* Macros to easily read and write from an SDL_RWops structure */
//  #define SDL_RWseek(ctx, offset, whence)	(ctx)->seek(ctx, offset, whence)
//  #define SDL_RWtell(ctx)			(ctx)->seek(ctx, 0, SEEK_CUR)
//  #define SDL_RWread(ctx, ptr, size, n)	(ctx)->read(ctx, ptr, size, n)
//  #define SDL_RWwrite(ctx, ptr, size, n)	(ctx)->write(ctx, ptr, size, n)
//  #define SDL_RWclose(ctx)		(ctx)->close(ctx)




/* These are the flags which may be passed to SDL_Init() -- you should
   specify the subsystems which you will be using in your application.
*/
#define	SDL_INIT_TIMER		0x00000001
#define SDL_INIT_AUDIO		0x00000010
#define SDL_INIT_VIDEO		0x00000020
#define SDL_INIT_CDROM		0x00000100
#define SDL_INIT_JOYSTICK	0x00000200
#define SDL_INIT_NOPARACHUTE	0x00100000	/* Don't catch fatal signals */
#define SDL_INIT_EVENTTHREAD	0x01000000	/* Not supported on all OS's */
#define SDL_INIT_EVERYTHING	0x0000FFFF


/* This function loads the SDL dynamically linked library and initializes
 * the subsystems specified by 'flags' (and those satisfying dependencies)
 * Unless the SDL_INIT_NOPARACHUTE flag is set, it will install cleanup
 * signal handlers for some commonly ignored fatal signals (like SIGSEGV)
 */
extern int SDL_Init(Uint32 flags);

/* This function initializes specific SDL subsystems */
extern int SDL_InitSubSystem(Uint32 flags);

/* This function cleans up specific SDL subsystems */
extern void SDL_QuitSubSystem(Uint32 flags);

/* This function returns mask of the specified subsystems which have
   been initialized.
   If 'flags' is 0, it returns a mask of all initialized subsystems.
*/
extern Uint32 SDL_WasInit(Uint32 flags);

/* This function cleans up all initialized subsystems and unloads the
 * dynamically linked library.  You should call it upon all exit conditions.
 */
extern void SDL_Quit(void);



/* The calculated values in this structure are calculated by SDL_OpenAudio() */
typedef struct SDL_AudioSpec {
	int freq;		/* DSP frequency -- samples per second */
	Uint16 format;		/* Audio data format */
	Uint8  channels;	/* Number of channels: 1 mono, 2 stereo */
	Uint8  silence;		/* Audio buffer silence value (calculated) */
	Uint16 samples;		/* Audio buffer size in samples (power of 2) */
	Uint16 padding;		/* Necessary for some compile environments */
	Uint32 size;		/* Audio buffer size in bytes (calculated) */
	/* This function is called when the audio device needs more data.
	   'stream' is a pointer to the audio data buffer
	   'len' is the length of that buffer in bytes.
	   Once the callback returns, the buffer will no longer be valid.
	   Stereo samples are stored in a LRLRLR ordering.
	*/
	//void (*callback)(void *userdata, Uint8 *stream, int len);
	void  *userdata;
} SDL_AudioSpec;


/* Audio format flags (defaults to LSB byte order) */
#define AUDIO_U8	0x0008	/* Unsigned 8-bit samples */
#define AUDIO_S8	0x8008	/* Signed 8-bit samples */
#define AUDIO_U16LSB	0x0010	/* Unsigned 16-bit samples */
#define AUDIO_S16LSB	0x8010	/* Signed 16-bit samples */
#define AUDIO_U16MSB	0x1010	/* As above, but big-endian byte order */
#define AUDIO_S16MSB	0x9010	/* As above, but big-endian byte order */
#define AUDIO_U16	AUDIO_U16LSB
#define AUDIO_S16	AUDIO_S16LSB


#define AUDIO_U16SYS	AUDIO_U16LSB
#define AUDIO_S16SYS	AUDIO_S16LSB



/* A structure to hold a set of audio conversion filters and buffers */
typedef struct SDL_AudioCVT {
	int needed;			/* Set to 1 if conversion possible */
	Uint16 src_format;		/* Source audio format */
	Uint16 dst_format;		/* Target audio format */
	double rate_incr;		/* Rate conversion increment */
//	Uint8 *buf;			/* Buffer to hold entire audio data */
	void *buf;
	int    len;			/* Length of original audio buffer */
	int    len_cvt;			/* Length of converted audio buffer */
	int    len_mult;		/* buffer must be len*len_mult big */
	double len_ratio; 	/* Given len, final size is len*len_ratio */
	//void (*filters[10])(struct SDL_AudioCVT *cvt, Uint16 format);
	int filter_index;		/* Current audio conversion function */
} SDL_AudioCVT;


/* Function prototypes */

/* These functions are used internally, and should not be used unless you
 * have a specific need to specify the audio driver you want to use.
 * You should normally use SDL_Init() or SDL_InitSubSystem().
 */
int SDL_AudioInit(const char *driver_name);
void SDL_AudioQuit();

/* This function fills the given character buffer with the name of the
 * current audio driver, and returns a pointer to it if the audio driver has
 * been initialized.  It returns NULL if no driver has been initialized.
 */
char * SDL_AudioDriverName(char *namebuf, int maxlen);

/*
 * This function opens the audio device with the desired parameters, and
 * returns 0 if successful, placing the actual hardware parameters in the
 * structure pointed to by 'obtained'.  If 'obtained' is NULL, the audio
 * data passed to the callback function will be guaranteed to be in the
 * requested format, and will be automatically converted to the hardware
 * audio format if necessary.  This function returns -1 if it failed 
 * to open the audio device, or couldn't set up the audio thread.
 *
 * When filling in the desired audio spec structure,
 *  'desired->freq' should be the desired audio frequency in samples-per-second.
 *  'desired->format' should be the desired audio format.
 *  'desired->samples' is the desired size of the audio buffer, in samples.
 *     This number should be a power of two, and may be adjusted by the audio
 *     driver to a value more suitable for the hardware.  Good values seem to
 *     range between 512 and 8096 inclusive, depending on the application and
 *     CPU speed.  Smaller values yield faster response time, but can lead
 *     to underflow if the application is doing heavy processing and cannot
 *     fill the audio buffer in time.  A stereo sample consists of both right
 *     and left channels in LR ordering.
 *     Note that the number of samples is directly related to time by the
 *     following formula:  ms = (samples*1000)/freq
 *  'desired->size' is the size in bytes of the audio buffer, and is
 *     calculated by SDL_OpenAudio().
 *  'desired->silence' is the value used to set the buffer to silence,
 *     and is calculated by SDL_OpenAudio().
 *  'desired->callback' should be set to a function that will be called
 *     when the audio device is ready for more data.  It is passed a pointer
 *     to the audio buffer, and the length in bytes of the audio buffer.
 *     This function usually runs in a separate thread, and so you should
 *     protect data structures that it accesses by calling SDL_LockAudio()
 *     and SDL_UnlockAudio() in your code.
 *  'desired->userdata' is passed as the first parameter to your callback
 *     function.
 *
 * The audio device starts out playing silence when it's opened, and should
 * be enabled for playing by calling SDL_PauseAudio(0) when you are ready
 * for your audio callback function to be called.  Since the audio driver
 * may modify the requested size of the audio buffer, you should allocate
 * any local mixing buffers after you open the audio device.
 */
int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained);

/*
 * Get the current audio state:
 */
typedef enum {
	SDL_AUDIO_STOPPED = 0,
	SDL_AUDIO_PLAYING,
	SDL_AUDIO_PAUSED
} SDL_audiostatus;
	
SDL_audiostatus SDL_GetAudioStatus();

/*
 * This function pauses and unpauses the audio callback processing.
 * It should be called with a parameter of 0 after opening the audio
 * device to start playing sound.  This is so you can safely initialize
 * data for your callback function after opening the audio device.
 * Silence will be written to the audio device during the pause.
 */
void SDL_PauseAudio(int pause_on);

/*
 * This function loads a WAVE from the data source, automatically freeing
 * that source if 'freesrc' is non-zero.  For example, to load a WAVE file,
 * you could do:
 *	SDL_LoadWAV_RW(SDL_RWFromFile("sample.wav", "rb"), 1, ...);
 *
 * If this function succeeds, it returns the given SDL_AudioSpec,
 * filled with the audio data format of the wave data, and sets
 * 'audio_buf' to a malloc()'d buffer containing the audio data,
 * and sets 'audio_len' to the length of that audio buffer, in bytes.
 * You need to free the audio buffer with SDL_FreeWAV() when you are 
 * done with it.
 *
 * This function returns NULL and sets the SDL error message if the 
 * wave file cannot be opened, uses an unknown data format, or is 
 * corrupt.  Currently raw and MS-ADPCM WAVE files are supported.
 */

SDL_AudioSpec * SDL_LoadWAV_RW(SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len);


/*
 * This function frees data previously allocated with SDL_LoadWAV_RW()
 */
void SDL_FreeWAV(Uint8 *audio_buf);

/*
 * This function takes a source format and rate and a destination format
 * and rate, and initializes the 'cvt' structure with information needed
 * by SDL_ConvertAudio() to convert a buffer of audio data from one format
 * to the other.
 * This function returns 0, or -1 if there was an error.
 */
int SDL_BuildAudioCVT(SDL_AudioCVT *cvt, Uint16 src_format, Uint8 src_channels, int src_rate, Uint16 dst_format, Uint8 dst_channels, int dst_rate);

/* Once you have initialized the 'cvt' structure using SDL_BuildAudioCVT(),
 * created an audio buffer cvt->buf, and filled it with cvt->len bytes of
 * audio data in the source format, this function will convert it in-place
 * to the desired format.
 * The data conversion may expand the size of the audio data, so the buffer
 * cvt->buf should be allocated after the cvt structure is initialized by
 * SDL_BuildAudioCVT(), and should be cvt->len*cvt->len_mult bytes long.
 */
int SDL_ConvertAudio(SDL_AudioCVT *cvt);

/*
 * This takes two audio buffers of the playing audio format and mixes
 * them, performing addition, volume adjustment, and overflow clipping.
 * The volume ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME
 * for full audio volume.  Note this does not change hardware volume.
 * This is provided for convenience -- you can mix your own audio data.
 */
#define SDL_MIX_MAXVOLUME 128
void SDL_MixAudio(Uint8 *dst, const Uint8 *src, Uint32 len, int volume);

/*
 * The lock manipulated by these functions protects the callback function.
 * During a LockAudio/UnlockAudio pair, you can be guaranteed that the
 * callback function is not running.  Do not call these from the callback
 * function or you will cause deadlock.
 */
void SDL_LockAudio();
void SDL_UnlockAudio();

/*
 * This function shuts down audio processing and closes the audio device.
 */
void SDL_CloseAudio();




/* In order to use these functions, SDL_Init() must have been called
   with the SDL_INIT_CDROM flag.  This causes SDL to scan the system
   for CD-ROM drives, and load appropriate drivers.
*/

/* The maximum number of CD-ROM tracks on a disk */
#define SDL_MAX_TRACKS	99

/* The types of CD-ROM track possible */
#define SDL_AUDIO_TRACK	0x00
#define SDL_DATA_TRACK	0x04

/* The possible states which a CD-ROM drive can be in. */
typedef enum {
	CD_TRAYEMPTY,
	CD_STOPPED,
	CD_PLAYING,
	CD_PAUSED,
	CD_ERROR = -1
} CDstatus;

/* Given a status, returns true if there's a disk in the drive */
//#define CD_INDRIVE(status)	((int)(status) > 0)

typedef struct SDL_CDtrack {
	Uint8 id;		/* Track number */
	Uint8 type;		/* Data or audio track */
	Uint16 unused;
	Uint32 length;		/* Length, in frames, of this track */
	Uint32 offset;		/* Offset, in frames, from start of disk */
} SDL_CDtrack;

/* This structure is only current as of the last call to SDL_CDStatus() */
typedef struct SDL_CD {
	int id;			/* Private drive identifier */
	CDstatus status;	/* Current drive status */

	/* The rest of this structure is only valid if there's a CD in drive */
	int numtracks;		/* Number of tracks on disk */
	int cur_track;		/* Current track position */
	int cur_frame;		/* Current frame offset within current track */
	//SDL_CDtrack track[SDL_MAX_TRACKS+1];
} SDL_CD;

/* Conversion functions from frames to Minute/Second/Frames and vice versa */
#define CD_FPS	75


/* CD-audio API functions: */

/* Returns the number of CD-ROM drives on the system, or -1 if
   SDL_Init() has not been called with the SDL_INIT_CDROM flag.
 */
int SDL_CDNumDrives(void);

/* Returns a human-readable, system-dependent identifier for the CD-ROM.
   Example:
	"/dev/cdrom"
	"E:"
	"/dev/disk/ide/1/master"
*/
const char * SDL_CDName(int drive);

/* Opens a CD-ROM drive for access.  It returns a drive handle on success,
   or NULL if the drive was invalid or busy.  This newly opened CD-ROM
   becomes the default CD used when other CD functions are passed a NULL
   CD-ROM handle.
   Drives are numbered starting with 0.  Drive 0 is the system default CD-ROM.
*/
SDL_CD * SDL_CDOpen(int drive);

/* This function returns the current status of the given drive.
   If the drive has a CD in it, the table of contents of the CD and current
   play position of the CD will be stored in the SDL_CD structure.
*/
CDstatus SDL_CDStatus(SDL_CD *cdrom);

/* Play the given CD starting at 'start_track' and 'start_frame' for 'ntracks'
   tracks and 'nframes' frames.  If both 'ntrack' and 'nframe' are 0, play 
   until the end of the CD.  This function will skip data tracks.
   This function should only be called after calling SDL_CDStatus() to 
   get track information about the CD.
   For example:
	// Play entire CD:
	if ( CD_INDRIVE(SDL_CDStatus(cdrom)) )
		SDL_CDPlayTracks(cdrom, 0, 0, 0, 0);
	// Play last track:
	if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) {
		SDL_CDPlayTracks(cdrom, cdrom->numtracks-1, 0, 0, 0);
	}
	// Play first and second track and 10 seconds of third track:
	if ( CD_INDRIVE(SDL_CDStatus(cdrom)) )
		SDL_CDPlayTracks(cdrom, 0, 0, 2, 10);

   This function returns 0, or -1 if there was an error.
*/
int SDL_CDPlayTracks(SDL_CD *cdrom,
		int start_track, int start_frame, int ntracks, int nframes);

/* Play the given CD starting at 'start' frame for 'length' frames.
   It returns 0, or -1 if there was an error.
*/
int SDL_CDPlay(SDL_CD *cdrom, int start, int length);

/* Pause play -- returns 0, or -1 on error */
int SDL_CDPause(SDL_CD *cdrom);

/* Resume play -- returns 0, or -1 on error */
int SDL_CDResume(SDL_CD *cdrom);

/* Stop play -- returns 0, or -1 on error */
int SDL_CDStop(SDL_CD *cdrom);

/* Eject CD-ROM -- returns 0, or -1 on error */
int SDL_CDEject(SDL_CD *cdrom);

/* Closes the handle for the CD-ROM drive */
void SDL_CDClose(SDL_CD *cdrom);




/* Read an item of the specified endianness and return in native format */
Uint16 SDL_ReadLE16(SDL_RWops *src);
Uint16 SDL_ReadBE16(SDL_RWops *src);
Uint32 SDL_ReadLE32(SDL_RWops *src);
Uint32 SDL_ReadBE32(SDL_RWops *src);
Uint64 SDL_ReadLE64(SDL_RWops *src);
Uint64 SDL_ReadBE64(SDL_RWops *src);

/* Write an item of native format to the specified endianness */
int SDL_WriteLE16(SDL_RWops *dst, Uint16 value);
int SDL_WriteBE16(SDL_RWops *dst, Uint16 value);
int SDL_WriteLE32(SDL_RWops *dst, Uint32 value);
int SDL_WriteBE32(SDL_RWops *dst, Uint32 value);
int SDL_WriteLE64(SDL_RWops *dst, Uint64 value);
int SDL_WriteBE64(SDL_RWops *dst, Uint64 value);





/* $Id: SDL_ttf.h,v 1.21 2004/01/04 17:41:55 slouken Exp $ */

/* This library is a wrapper around the excellent FreeType 2.0 library,
   available at:
	http://www.freetype.org/
*/

/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL
*/

/* Backwards compatibility */
#define TTF_MAJOR_VERSION 2
#define TTF_MINOR_VERSION 0
#define TTF_PATCHLEVEL 7

/* This function gets the version of the dynamically linked SDL_ttf library.
   it should NOT be used to fill a version structure, instead you should
   use the SDL_TTF_VERSION() macro.
 */
extern const SDL_version * TTF_Linked_Version(void);

/* ZERO WIDTH NO-BREAKSPACE (Unicode byte order mark) */
#define UNICODE_BOM_NATIVE	0xFEFF
#define UNICODE_BOM_SWAPPED	0xFFFE

/* This function tells the library whether UNICODE text is generally
   byteswapped.  A UNICODE BOM character in a string will override
   this setting for the remainder of that string.
*/
extern void TTF_ByteSwappedUNICODE(int swapped);

/* The internal structure containing font information */
typedef struct _TTF_Font TTF_Font;

/* Initialize the TTF engine - returns 0 if successful, -1 on error */
extern int TTF_Init(void);

/* Open a font file and create a font of the specified point size.
 * Some .fon fonts will have several sizes embedded in the file, so the
 * point size becomes the index of choosing which size.  If the value
 * is too high, the last indexed size will be the default. */
extern TTF_Font * TTF_OpenFont(const char *file, int ptsize);
extern TTF_Font * TTF_OpenFontIndex(const char *file, int ptsize, long index);
extern TTF_Font * TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize);
extern TTF_Font * TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long index);

/* Set and retrieve the font style
   This font style is implemented by modifying the font glyphs, and
   doesn't reflect any inherent properties of the truetype font file.
*/
#define TTF_STYLE_NORMAL	0x00
#define TTF_STYLE_BOLD		0x01
#define TTF_STYLE_ITALIC	0x02
#define TTF_STYLE_UNDERLINE	0x04
extern int TTF_GetFontStyle(TTF_Font *font);
extern void TTF_SetFontStyle(TTF_Font *font, int style);

/* Get the total height of the font - usually equal to point size */
extern int TTF_FontHeight(TTF_Font *font);

/* Get the offset from the baseline to the top of the font
   This is a positive value, relative to the baseline.
 */
extern int TTF_FontAscent(TTF_Font *font);

/* Get the offset from the baseline to the bottom of the font
   This is a negative value, relative to the baseline.
 */
extern int TTF_FontDescent(TTF_Font *font);

/* Get the recommended spacing between lines of text for this font */
extern int TTF_FontLineSkip(TTF_Font *font);

/* Get the number of faces of the font */
extern long TTF_FontFaces(TTF_Font *font);

/* Get the font face attributes, if any */
extern int TTF_FontFaceIsFixedWidth(TTF_Font *font);
extern char * TTF_FontFaceFamilyName(TTF_Font *font);
extern char * TTF_FontFaceStyleName(TTF_Font *font);

/* Get the metrics (dimensions) of a glyph
   To understand what these metrics mean, here is a useful link:
    http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
 */
extern int TTF_GlyphMetrics(TTF_Font *font, Uint16 ch,
				     int *minx, int *maxx,
                                     int *miny, int *maxy, int *advance);

/* Get the dimensions of a rendered string of text */
extern int TTF_SizeText(TTF_Font *font, const char *text, int *w, int *h);
extern int TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h);
extern int TTF_SizeUNICODE(TTF_Font *font, const Uint16 *text, int *w, int *h);

/* Create an 8-bit palettized surface and render the given text at
   fast quality with the given font and color.  The 0 pixel is the
   colorkey, giving a transparent background, and the 1 pixel is set
   to the text color.
   This function returns the new surface, or NULL if there was an error.
*/
extern SDL_Surface * TTF_RenderText_Solid(TTF_Font *font,
				const char *text, SDL_Color fg);
extern SDL_Surface * TTF_RenderUTF8_Solid(TTF_Font *font,
				const char *text, SDL_Color fg);
extern SDL_Surface * TTF_RenderUNICODE_Solid(TTF_Font *font,
				const Uint16 *text, SDL_Color fg);

/* Create an 8-bit palettized surface and render the given glyph at
   fast quality with the given font and color.  The 0 pixel is the
   colorkey, giving a transparent background, and the 1 pixel is set
   to the text color.  The glyph is rendered without any padding or
   centering in the X direction, and aligned normally in the Y direction.
   This function returns the new surface, or NULL if there was an error.
*/
extern SDL_Surface * TTF_RenderGlyph_Solid(TTF_Font *font,
					Uint16 ch, SDL_Color fg);

/* Create an 8-bit palettized surface and render the given text at
   high quality with the given font and colors.  The 0 pixel is background,
   while other pixels have varying degrees of the foreground color.
   This function returns the new surface, or NULL if there was an error.
*/
extern SDL_Surface * TTF_RenderText_Shaded(TTF_Font *font,
				const char *text, SDL_Color fg, SDL_Color bg);
extern SDL_Surface * TTF_RenderUTF8_Shaded(TTF_Font *font,
				const char *text, SDL_Color fg, SDL_Color bg);
extern SDL_Surface * TTF_RenderUNICODE_Shaded(TTF_Font *font,
				const Uint16 *text, SDL_Color fg, SDL_Color bg);

/* Create an 8-bit palettized surface and render the given glyph at
   high quality with the given font and colors.  The 0 pixel is background,
   while other pixels have varying degrees of the foreground color.
   The glyph is rendered without any padding or centering in the X
   direction, and aligned normally in the Y direction.
   This function returns the new surface, or NULL if there was an error.
*/
extern SDL_Surface * TTF_RenderGlyph_Shaded(TTF_Font *font,
				Uint16 ch, SDL_Color fg, SDL_Color bg);

/* Create a 32-bit ARGB surface and render the given text at high quality,
   using alpha blending to dither the font with the given color.
   This function returns the new surface, or NULL if there was an error.
*/
extern SDL_Surface * TTF_RenderText_Blended(TTF_Font *font,
				const char *text, SDL_Color fg);
extern SDL_Surface * TTF_RenderUTF8_Blended(TTF_Font *font,
				const char *text, SDL_Color fg);
extern SDL_Surface * TTF_RenderUNICODE_Blended(TTF_Font *font,
				const Uint16 *text, SDL_Color fg);

/* Create a 32-bit ARGB surface and render the given glyph at high quality,
   using alpha blending to dither the font with the given color.
   The glyph is rendered without any padding or centering in the X
   direction, and aligned normally in the Y direction.
   This function returns the new surface, or NULL if there was an error.
*/
extern SDL_Surface * TTF_RenderGlyph_Blended(TTF_Font *font,
						Uint16 ch, SDL_Color fg);

/* For compatibility with previous versions, here are the old functions */
/*#define TTF_RenderText(font, text, fg, bg)	\
	TTF_RenderText_Shaded(font, text, fg, bg)
#define TTF_RenderUTF8(font, text, fg, bg)	\
	TTF_RenderUTF8_Shaded(font, text, fg, bg)
#define TTF_RenderUNICODE(font, text, fg, bg)	\
	TTF_RenderUNICODE_Shaded(font, text, fg, bg)*/

/* Close an opened font file */
extern void TTF_CloseFont(TTF_Font *font);

/* De-initialize the TTF engine */
extern void TTF_Quit(void);

/* Check if the TTF engine is initialized */
extern int TTF_WasInit(void);




/* We'll use SDL for reporting errors */
///#define TTF_SetError	SDL_SetError
///#define TTF_GetError	SDL_GetError

/*
    SDL_image:  An example image loading library for use with SDL
    Copyright (C) 1999-2004 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Sam Lantinga
    slouken@libsdl.org
*/

/* $Id: SDL_image.h,v 1.17 2004/01/04 17:33:01 slouken Exp $ */

/* A simple library to load images of various formats as SDL surfaces */

/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL
*/
#define SDL_IMAGE_MAJOR_VERSION	1
#define SDL_IMAGE_MINOR_VERSION	2
#define SDL_IMAGE_PATCHLEVEL	4

/* This macro can be used to fill a version structure with the compile-time
 * version of the SDL_image library.
 */
/*#define SDL_IMAGE_VERSION(X)						\
{									\
	(X)->major = SDL_IMAGE_MAJOR_VERSION;				\
	(X)->minor = SDL_IMAGE_MINOR_VERSION;				\
	(X)->patch = SDL_IMAGE_PATCHLEVEL;				\
}*/

/* This function gets the version of the dynamically linked SDL_image library.
   it should NOT be used to fill a version structure, instead you should
   use the SDL_IMAGE_VERSION() macro.
 */
//extern const SDL_version * IMG_Linked_Version(void);

/* Load an image from an SDL data source.
   The 'type' may be one of: "BMP", "GIF", "PNG", etc.

   If the image format supports a transparent pixel, SDL will set the
   colorkey for the surface.  You can enable RLE acceleration on the
   surface afterwards by calling:
	SDL_SetColorKey(image, SDL_RLEACCEL, image->format->colorkey);
 */
extern SDL_Surface * IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type);
/* Convenience functions */
extern SDL_Surface * IMG_Load(const char *file);
extern SDL_Surface * IMG_Load_RW(SDL_RWops *src, int freesrc);

/* Invert the alpha of a surface for use with OpenGL
   This function is now a no-op, and only provided for backwards compatibility.
*/
extern int IMG_InvertAlpha(int on);

/* Functions to detect a file type, given a seekable source */
extern int IMG_isBMP(SDL_RWops *src);
extern int IMG_isPNM(SDL_RWops *src);
extern int IMG_isXPM(SDL_RWops *src);
extern int IMG_isXCF(SDL_RWops *src);
extern int IMG_isPCX(SDL_RWops *src);
extern int IMG_isGIF(SDL_RWops *src);
extern int IMG_isJPG(SDL_RWops *src);
extern int IMG_isTIF(SDL_RWops *src);
extern int IMG_isPNG(SDL_RWops *src);
extern int IMG_isLBM(SDL_RWops *src);

/* Individual loading functions */
extern SDL_Surface * IMG_LoadBMP_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadPNM_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadXPM_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadXCF_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadPCX_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadGIF_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadJPG_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadTIF_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadPNG_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadTGA_RW(SDL_RWops *src);
extern SDL_Surface * IMG_LoadLBM_RW(SDL_RWops *src);

extern SDL_Surface * IMG_ReadXPMFromArray(char **xpm);

/* We'll use SDL for reporting errors */
//#define IMG_SetError	SDL_SetError
//#define IMG_GetError	SDL_GetError







typedef enum {
    SDL_DC_DMA_VIDEO,
    SDL_DC_TEXTURED_VIDEO,
    SDL_DC_DIRECT_VIDEO
} SDL_DC_driver;

void SDL_DC_SetVideoDriver(SDL_DC_driver value);
void SDL_DC_SetWindow(int width, int height);
void SDL_DC_VerticalWait(SDL_bool value);
void SDL_DC_ShowAskHz(SDL_bool value);
void SDL_DC_Default60Hz(SDL_bool value);

typedef enum {
    SDL_DC_START=3,
    SDL_DC_A=2,
    SDL_DC_B=1,
    SDL_DC_X=5,
    SDL_DC_Y=6,
    SDL_DC_L=7,
    SDL_DC_R=8,
    SDL_DC_LEFT=11,
    SDL_DC_RIGHT=12,
    SDL_DC_UP=9,
    SDL_DC_DOWN=10
} SDL_DC_button;

void SDL_DC_MapKey(int joy, SDL_DC_button button, SDLKey key);
void SDL_DC_EmulateKeyboard(SDL_bool value);
void SDL_DC_EmulateMouse(SDL_bool value);

//void SDL_DC_SetSoundBuffer(void *buffer);
//void SDL_DC_RestoreSoundBuffer(void);



/* ----- Prototypes */

/* Note: all ___Color routines expect the color to be in format 0xRRGGBBAA */

/* Pixel */

    int pixelColor(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color);
    int pixelRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Horizontal line */

    int hlineColor(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color);
    int hlineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Vertical line */

    int vlineColor(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color);
    int vlineRGBA(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Rectangle */

    int rectangleColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
    int rectangleRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1,
				   Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Filled rectangle (Box) */

    int boxColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
    int boxRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2,
			     Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Line */


    int SDL_DrawLine(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
    int lineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1,
			      Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* AA Line */
    int aalineColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
    int aalineRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1,
				Sint16 x2, Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Circle */
    int SDL_DrawCircle(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 r, Uint32 color);
    int circleRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* AA Circle */

    int aacircleColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 r, Uint32 color);
    int aacircleRGBA(SDL_Surface * dst, Sint16 x, Sint16 y,
				  Sint16 rad, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Ellipse */

    int ellipseColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color);
    int ellipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y,
				 Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* AA Ellipse */

    int aaellipseColor(SDL_Surface * dst, Sint16 xc, Sint16 yc, Sint16 rx, Sint16 ry, Uint32 color);
    int aaellipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y,
				   Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Filled Ellipse */

    int filledEllipseColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color);
    int filledEllipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y,
				       Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
/* Filled Pie */

    int filledpieColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad,
				    Sint16 start, Sint16 end, Uint32 color);
    int filledpieRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad,
				   Sint16 start, Sint16 end, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Trigon */

    int trigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color);
    int trigonRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3,
				 Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* AA-Trigon */

    int aatrigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color);
    int aatrigonRGBA(SDL_Surface * dst,  Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3,
				   Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Filled Trigon */

    int filledTrigonColor(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, int color);
    int filledTrigonRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3,
				       Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Polygon */

    int polygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint32 color);
    int polygonRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy,
				 int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* AA-Polygon */

    int aapolygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, Uint32 color);
    int aapolygonRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy,
				   int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Filled Polygon */

    int filledPolygonColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, int color);
    int filledPolygonRGBA(SDL_Surface * dst, Sint16 * vx,
				       Sint16 * vy, int n, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/* Bezier */
/* s = number of steps */

    int bezierColor(SDL_Surface * dst, Sint16 * vx, Sint16 * vy, int n, int s, Uint32 color);
    int bezierRGBA(SDL_Surface * dst, Sint16 * vx, Sint16 * vy,
				 int n, int s, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

    int fastPixelColorNolockNoclip(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color);





/*
typedef void GUI_Object;
typedef struct guiSurface GUI_Surface;
typedef struct guiScreen GUI_Screen;
typedef struct guiFont GUI_Font;
typedef struct guiWidget GUI_Widget;
typedef struct guiCallback GUI_Callback;
typedef struct guiLayout GUI_Layout;
typedef struct guiWindow GUI_Window;
typedef struct guiRect GUI_Rect;
typedef struct guiColor GUI_Color;
*/

// flags

#define WIDGET_PRESSED         0x00000001
#define WIDGET_INSIDE          0x00000002
#define WIDGET_HIDDEN          0x00000004
#define WIDGET_CHANGED         0x00000008
#define WIDGET_TRANSPARENT     0x00000010
#define WIDGET_HAS_FOCUS       0x00000020
#define WIDGET_WANTS_FOCUS     0x00000040
#define WIDGET_TURNED_ON       0x00000080

#define WIDGET_ALIGN_MASK      0x00000F00
#define WIDGET_HORIZ_MASK      0x00000300
#define WIDGET_HORIZ_CENTER    0x00000000
#define WIDGET_HORIZ_RIGHT     0x00000100
#define WIDGET_HORIZ_LEFT      0x00000200
#define WIDGET_HORIZ_STRETCH   0x00000300
#define WIDGET_VERT_MASK       0x00000C00
#define WIDGET_VERT_CENTER     0x00000000
#define WIDGET_VERT_TOP        0x00000400
#define WIDGET_VERT_BOTTOM     0x00000800
#define WIDGET_VERT_STRETCH    0x00000C00

#define WIDGET_DISABLED        0x00001000

#define SCREEN_DEBUG_BLIT      0x10000000
#define SCREEN_DEBUG_UPDATE    0x20000000


typedef void GUI_CallbackFunction(void *);

// GUI API

int GUI_Init(void);
void GUI_SetScreen(GUI_Screen *);
GUI_Screen *GUI_GetScreen(void);
void GUI_Run(void);
void GUI_Quit(void);


int GUI_MustLock(void);
int GUI_Lock(void);
int GUI_Unlock(void);

void GUI_SetThread(Uint32 id);
int GUI_GetRunning(void);
void GUI_SetRunning(int value);
int GUI_ClipRect(SDL_Rect *sr, SDL_Rect *dr, const SDL_Rect *clip);
void GUI_TriggerUpdate(void);

// Object API

GUI_Object *GUI_ObjectCreate(const char *s);
const char *GUI_ObjectGetName(GUI_Object *object);
void GUI_ObjectSetName(GUI_Object *object, const char *s);
void GUI_ObjectIncRef(GUI_Object *object);
int GUI_ObjectDecRef(GUI_Object *object);
int GUI_ObjectKeep(GUI_Object **target, GUI_Object *source);
GUI_Object *GUI_AnyToObject(void *type);

// Surface API

void GUI_SurfaceSaveBMP(GUI_Surface *src, const char *filename);
GUI_Surface *GUI_SurfaceLoad(const char *fn);
GUI_Surface *GUI_SurfaceCreate(const char *aname, int f, int w, int h, int d, int rm, int gm, int bm, int am);
void GUI_SurfaceBlit(GUI_Surface *src, SDL_Rect *src_r, GUI_Surface *dst, SDL_Rect *dst_r);
void GUI_SurfaceUpdateRects(GUI_Surface *surface, int n, SDL_Rect *rects);
void GUI_SurfaceUpdateRect(GUI_Surface *surface, int x, int y, int w, int h);
void GUI_SurfaceFill(GUI_Surface *surface, SDL_Rect *r, Uint32 c);
int GUI_SurfaceGetWidth(GUI_Surface *surface);
int GUI_SurfaceGetHeight(GUI_Surface *surface);
Uint32 GUI_SurfaceMapRGB(GUI_Surface *surface, int r, int g, int b);
Uint32 GUI_SurfaceMapRGBA(GUI_Surface *surface, int r, int g, int b, int a);
SDL_Surface *GUI_SurfaceGet(GUI_Surface *surface);
void GUI_SurfaceSetAlpha(GUI_Surface *surface, Uint32 flag, Uint8 alpha);
GUI_Surface *GUI_AnyToSurface(void *type);

// Font API

GUI_Surface *GUI_FontRenderFast(GUI_Font *font, const char *s, SDL_Color fg);
GUI_Surface *GUI_FontRenderQuality(GUI_Font *font, const char *s, SDL_Color fg);
void GUI_FontDrawText(GUI_Font *font, GUI_Surface *surface, const char *s, int x, int y);
SDL_Rect GUI_FontGetTextSize(GUI_Font *font, const char *s);

// Widget API

void GUI_WidgetUpdate(GUI_Widget *widget, int force);
void GUI_WidgetDraw(GUI_Widget *widget, GUI_Surface *image, const SDL_Rect *sr, const SDL_Rect *dr);
void GUI_WidgetErase(GUI_Widget *widget, const SDL_Rect *dr);
void GUI_WidgetFill(GUI_Widget *widget, const SDL_Rect *dr, SDL_Color c);
int GUI_WidgetEvent(GUI_Widget *widget, const SDL_Event *event, int xoffset, int yoffset);
void GUI_WidgetClicked(GUI_Widget *widget, int x, int y);
void GUI_WidgetSetAlign(GUI_Widget *widget, int align);
void GUI_WidgetMarkChanged(GUI_Widget *widget);
void GUI_WidgetSetTransparent(GUI_Widget *widget, int trans);
void GUI_WidgetSetEnabled(GUI_Widget *widget, int flag);
void GUI_WidgetTileImage(GUI_Widget *widget, GUI_Surface *surface, const SDL_Rect *area, int x_offset, int y_offset);
void GUI_WidgetSetFlags(GUI_Widget *widget, int mask);
void GUI_WidgetClearFlags(GUI_Widget *widget, int mask);
void GUI_WidgetSetState(GUI_Widget *widget, int state);
int GUI_WidgetGetState(GUI_Widget *widget);
SDL_Rect GUI_WidgetGetArea(GUI_Widget *widget);
void GUI_WidgetSetPosition(GUI_Widget *widget, int x, int y);
GUI_Widget *GUI_AnyToWidget(void *type);

// Container API

int GUI_ContainerContains(GUI_Widget *container, GUI_Widget *widget);
void GUI_ContainerAdd(GUI_Widget *container, GUI_Widget *widget);
void GUI_ContainerRemove(GUI_Widget *container, GUI_Widget *widget);
int GUI_ContainerGetCount(GUI_Widget *container);
GUI_Widget *GUI_ContainerGetChild(GUI_Widget *container, int index);

// Screen API

GUI_Screen *GUI_ScreenCreate(int w, int h, int d, int f);
void GUI_ScreenSetContents(GUI_Screen *screen, GUI_Widget *contents);
void GUI_ScreenSetBackground(GUI_Screen *screen, GUI_Surface *surface);
void GUI_ScreenSetFocusWidget(GUI_Screen *screen, GUI_Widget *item);
void GUI_ScreenClearFocusWidget(GUI_Screen *screen);
void GUI_ScreenSetBackgroundColor(GUI_Screen *screen, SDL_Color c);
GUI_Widget *GUI_ScreenGetFocusWidget(GUI_Screen *screen);
void GUI_ScreenDrawMouse(GUI_Screen *screen);
void GUI_ScreenEvent(GUI_Screen *screen, const SDL_Event *event,
                     int xoffset, int yoffset);
void GUI_ScreenUpdate(GUI_Screen *screen, int force);
void GUI_ScreenDoUpdate(GUI_Screen *screen, int force); 
void GUI_ScreenDraw(GUI_Screen *screen, GUI_Surface *image,
                    const SDL_Rect *src_r, const SDL_Rect *dst_r);
void GUI_ScreenFill(GUI_Screen *screen, const SDL_Rect *dst_r, SDL_Color c);
void GUI_ScreenErase(GUI_Screen *screen, const SDL_Rect *area);

void GUI_RealScreenUpdate(GUI_Screen *screen, int force);
void GUI_RealScreenUpdateRect(GUI_Screen *screen, const SDL_Rect *r);
GUI_Screen *GUI_RealScreen(const char *aname, SDL_Surface *surface);
void GUI_RealScreenFlushUpdates(GUI_Screen *screen);



// Window

GUI_Window *CreateWindow(const char *aname, int x, int y, int w, int h);
void GUI_WindowUpdateAll(GUI_Window *win);
void GUI_WindowUpdate(GUI_Window *win, const GUI_Rect *r);
void GUI_WindowDrawImage(GUI_Window *win, const GUI_Surface *image, const GUI_Rect *src_r, const GUI_Rect *dst_r);
void GUI_WindowDrawRect(GUI_Window *win, const GUI_Rect *r, const GUI_Color *c);
void GUI_WindowDrawLine(GUI_Window *win, int x1, int y1, int x2, int y2, const GUI_Color *c);
void GUI_WindowDrawPixel(GUI_Window *win, int x, int y, const GUI_Color *c);
void GUI_WindowFillRect(GUI_Window *win, const GUI_Rect *r, const GUI_Color *c);




// Button Widget

GUI_Widget *GUI_ButtonCreate(const char *name, int x, int y, int w, int h);
int GUI_ButtonCheck(GUI_Widget *widget);
void GUI_ButtonSetNormalImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_ButtonSetHighlightImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_ButtonSetPressedImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_ButtonSetDisabledImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_ButtonSetCaption(GUI_Widget *widget, GUI_Widget *caption);
void GUI_ButtonSetClick(GUI_Widget *widget, GUI_Callback *callback);
void GUI_ButtonSetMouseover(GUI_Widget *widget, GUI_Callback *callback);
void GUI_ButtonSetMouseout(GUI_Widget *widget, GUI_Callback *callback);

// Callback API

GUI_Callback *GUI_CallbackCreate(GUI_CallbackFunction *function, GUI_CallbackFunction *freefunc, void *data);
void GUI_CallbackCall(GUI_Callback *callback);
GUI_Callback *GUI_LuaCallbackCreate(int appId, char *luastring);

// FastFont object

GUI_Font *GUI_FontLoadBitmap(char *fn);

// TTF Font object

GUI_Font *GUI_FontLoadTrueType(char *fn, int size);
GUI_Font *GUI_AnyToFont(void *type);


// Panel Widget API

GUI_Widget *GUI_PanelCreate(const char *name, int x, int y, int w, int h);
int GUI_PanelCheck(GUI_Widget *widget);
void GUI_PanelSetBackground(GUI_Widget *widget, GUI_Surface *surface);
void GUI_PanelSetBackgroundColor(GUI_Widget *widget, SDL_Color c);
void GUI_PanelSetXOffset(GUI_Widget *widget, int value);
void GUI_PanelSetYOffset(GUI_Widget *widget, int value);
void GUI_PanelSetLayout(GUI_Widget *widget, GUI_Layout *layout);

// VBox Layout object

GUI_Layout *GUI_VBoxLayoutCreate(void);

// ToggleButton Widget API

GUI_Widget *GUI_ToggleButtonCreate(const char *name, int x, int y, int w, int h);
int GUI_ToggleButtonCheck(GUI_Widget *widget);
void GUI_ToggleButtonSetOnNormalImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_ToggleButtonSetOnHighlightImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_ToggleButtonSetOffNormalImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_ToggleButtonSetOffHighlightImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_ToggleButtonSetCaption(GUI_Widget *widget, GUI_Widget *caption);
void GUI_ToggleButtonSetClick(GUI_Widget *widget, GUI_Callback *callback);
void GUI_ToggleButtonSetMouseover(GUI_Widget *widget, GUI_Callback *callback);
void GUI_ToggleButtonSetMouseout(GUI_Widget *widget, GUI_Callback *callback);

// Label Widget API

GUI_Widget *GUI_LabelCreate(const char *name, int x, int y, int w, int h, GUI_Font *font, const char *text);
int GUI_LabelCheck(GUI_Widget *widget);
void GUI_LabelSetFont(GUI_Widget *widget, GUI_Font *font);
void GUI_LabelSetTextColor(GUI_Widget *widget, int r, int g, int b);
void GUI_LabelSetText(GUI_Widget *widget, const char *text);

// Picture Widget API

GUI_Widget *GUI_PictureCreate(const char *name, int x, int y, int w, int h, GUI_Surface *image);
int GUI_PictureCheck(GUI_Widget *widget);
void GUI_PictureSetImage(GUI_Widget *widget, GUI_Surface *image);
void GUI_PictureSetCaption(GUI_Widget *widget, GUI_Widget *caption);

// ProgressBar widget API

GUI_Widget *GUI_ProgressBarCreate(const char *name, int x, int y, int w, int h);
int GUI_ProgressBarCheck(GUI_Widget *widget);
void GUI_ProgressBarSetImage1(GUI_Widget *widget, GUI_Surface *image);
void GUI_ProgressBarSetImage2(GUI_Widget *widget, GUI_Surface *image);
void GUI_ProgressBarSetPosition(GUI_Widget *widget, double value);

// ScrollBar widget API

GUI_Widget *GUI_ScrollBarCreate(const char *name, int x, int y, int w, int h);
int GUI_ScrollBarCheck(GUI_Widget *widget);
void GUI_ScrollBarSetKnobImage(GUI_Widget *widget, GUI_Surface *image);
void GUI_ScrollBarSetBackgroundImage(GUI_Widget *widget, GUI_Surface *image);
void GUI_ScrollBarSetPosition(GUI_Widget *widget, int value);
//void GUI_ScrollBarSetPageStep(GUI_Widget *widget, int value);
void GUI_ScrollBarSetMovedCallback(GUI_Widget *widget, GUI_Callback *callback);
int GUI_ScrollBarGetPosition(GUI_Widget *widget);

// CardStack widget API

GUI_Widget *GUI_CardStackCreate(const char *name, int x, int y, int w, int h);
int GUI_CardStackCheck(GUI_Widget *widget);
void GUI_CardStackSetBackground(GUI_Widget *widget, GUI_Surface *surface);
void GUI_CardStackSetBackgroundColor(GUI_Widget *widget, SDL_Color c);
void GUI_CardStackNext(GUI_Widget *widget);
void GUI_CardStackPrev(GUI_Widget *widget);
void GUI_CardStackShowIndex(GUI_Widget *widget, int index);
void GUI_CardStackShow(GUI_Widget *widget, const char *name);
int GUI_CardStackGetIndex(GUI_Widget *widget);

// TextEntry widget API

GUI_Widget *GUI_TextEntryCreate(const char *name, int x, int y, int w, int h, GUI_Font *font, int size);
int GUI_TextEntryCheck(GUI_Widget *widget);
void GUI_TextEntrySetFont(GUI_Widget *widget, GUI_Font *font);
void GUI_TextEntrySetTextColor(GUI_Widget *widget, int r, int g, int b);
void GUI_TextEntrySetText(GUI_Widget *widget, const char *text);
const char *GUI_TextEntryGetText(GUI_Widget *widget);
void GUI_TextEntrySetNormalImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_TextEntrySetHighlightImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_TextEntrySetFocusImage(GUI_Widget *widget, GUI_Surface *surface);
void GUI_TextEntrySetFocusCallback(GUI_Widget *widget, GUI_Callback *callback);
void GUI_TextEntrySetUnfocusCallback(GUI_Widget *widget, GUI_Callback *callback);


/* ListBox Widget API */
GUI_ListBox *GUI_CreateListBox(const char *aname, int x, int y, int w, int h, GUI_Font *afont);
int GUI_ListBoxGetRowCount(GUI_ListBox *list);
int GUI_ListBoxGetRowSize(GUI_ListBox *list, int row);
int GUI_ListBoxGetColumnCount(GUI_ListBox *list);
int GUI_ListBoxGetColumnSize(GUI_ListBox *list, int column);
void GUI_ListBoxSetFont(GUI_ListBox *list, GUI_Font *afont);
void GUI_ListBoxSetTextColor(GUI_ListBox *list, int r, int g, int b);
void GUI_ListBoxDrawCell(GUI_ListBox *list, int column, int row, const SDL_Rect *r);
void GUI_ListBoxAddItem(GUI_ListBox *list, const char *s);
void GUI_ListBoxRemoveItem(GUI_ListBox *list, int n);
GUI_ListBox *GUI_AnyToListbox(void *type);


void GUI_Free(void *ptr);


/*! This is a struct for each consoles data */
typedef struct console_information_td {
	int Visible;						/*! enum that tells which visible state we are in CON_CLOSED, CON_OPEN, CON_CLOSING, CON_OPENING */
	int WasUnicode;						/*! stores the UNICODE value before the console was shown. On Hide() the UNICODE value is restored. */
	int RaiseOffset;					/*! Offset used in show/hide animation */
	int HideKey;						/*! the key that can hide the console */
	char **ConsoleLines;				/*! List of all the past lines */
	char **CommandLines;				/*! List of all the past commands */
	int TotalConsoleLines;				/*! Total number of lines in the console */
	int ConsoleScrollBack;				/*! How much the user scrolled back in the console */
	int TotalCommands;					/*! Number of commands that were typed in before (which are now in the CommandLines array) */
	int FontNumber;						/*! This is the number of the font for the console (DT_* specific; will hopefully disappear in future releases) */
	int LineBuffer;						/*! The number of visible lines in the console (autocalculated on CON_UpdateConsole()) */
	int VChars;							/*! The number of visible characters in one console line (autocalculated on CON_Init() and recalc. on CON_Resize()) */
	int BackX;
    int BackY;					        /*! Background image x and y coords */
	char* Prompt;						/*! Prompt displayed in command line */
	char Command[CON_CHARS_PER_LINE+1];	/*! current command in command line = lcommand + rcommand (Get's updated in AssembleCommand())*/
	char RCommand[CON_CHARS_PER_LINE+1];	/*! left hand side of cursor */
	char LCommand[CON_CHARS_PER_LINE+1];	/*! right hand side of cursor */
	char VCommand[CON_CHARS_PER_LINE+1];	/*! current visible command line */
	int CursorPos;						/*! Current cursor position relative to the currently typed in command */
	int Offset;							/*! First visible character relative to the currently typed in command (used if command is too long to fit into console) */
	int InsMode;						/*! Boolean that tells us whether we are in Insert- or Overwrite-Mode */
	SDL_Surface *ConsoleSurface;		/*! THE Surface of the console */
	SDL_Surface *OutputScreen;			/*! This is the screen to draw the console to (normally you VideoSurface)*/
	SDL_Surface *BackgroundImage;		/*! Background image for the console */
	SDL_Surface *InputBackground;		/*! Dirty rectangle that holds the part of the background image that is behind the commandline */
	int DispX; 
    int DispY;					        /*! The top left x and y coords of the console on the display screen */
	unsigned char ConsoleAlpha;			/*! The consoles alpha level */
	int CommandScrollBack;				/*! How much the users scrolled back in the command lines */
	//void(*CmdFunction)(struct console_information_td *console, char* command);	/*! The Function that is executed if you press 'Return' in the console */
	//char*(*TabFunction)(char* command);	/*! The Function that is executed if you press 'Tab' in the console */
	void *CmdFunction;
	void *TabFunction;

	int FontHeight;						/*! The height of the font used in the console */
	int FontWidth;						/*! The width of the font used in the console (Remark that the console needs FIXED width fonts!!) */
} ConsoleInformation;




#define CON_CHARS_PER_LINE   127
#define CON_BLINK_RATE       500
#define CON_CHAR_BORDER      4
//#define CON_DEFAULT_PROMPT	"]"
#define CON_LINE_SCROLL	2
//#define CON_SCROLL_INDICATOR "^"
//#define CON_INS_CURSOR "_"
//#define CON_OVR_CURSOR "|"
#define CON_DEFAULT_HIDEKEY	SDLK_ESCAPE
#define CON_OPENCLOSE_SPEED 25

enum {
    CON_CLOSED,	
    CON_CLOSING,
    CON_OPENING,
    CON_OPEN	
};

SDL_Event* CON_Events(SDL_Event *event);
void CON_Show(ConsoleInformation *console);
void CON_Hide(ConsoleInformation *console);
int CON_isVisible(ConsoleInformation *console);
void CON_UpdateOffset(ConsoleInformation* console);
void CON_DrawConsole(ConsoleInformation *console);
ConsoleInformation* CON_Init(const char *FontName, SDL_Surface *DisplayScreen, int lines, SDL_Rect rect);
void CON_Destroy(ConsoleInformation *console);
void CON_Free(ConsoleInformation *console);
//void CON_Out(ConsoleInformation *console, const char *str, ...);
void CON_Alpha(ConsoleInformation *console, unsigned char alpha);
void CON_AlphaGL(SDL_Surface *s, int alpha);
int CON_Background(ConsoleInformation *console, const char *image, int x, int y);
void CON_Position(ConsoleInformation *console, int x, int y);
int CON_Resize(ConsoleInformation *console, SDL_Rect rect);
int CON_Transfer(ConsoleInformation* console, SDL_Surface* new_outputscreen, SDL_Rect rect);
void CON_Topmost(ConsoleInformation *console);
void CON_SetPrompt(ConsoleInformation *console, char* newprompt);
void CON_SetHideKey(ConsoleInformation *console, int key);
void CON_Execute(ConsoleInformation *console, char* command);
//void CON_SetExecuteFunction(ConsoleInformation *console, void(*CmdFunction)(ConsoleInformation *console2, char* command));
//void CON_SetTabCompletion(ConsoleInformation *console, char*(*TabFunction)(char* command));
void CON_TabCompletion(ConsoleInformation *console);
void CON_NewLineConsole(ConsoleInformation *console);
void CON_NewLineCommand(ConsoleInformation *console);
void CON_UpdateConsole(ConsoleInformation *console);
void Default_CmdFunction(ConsoleInformation *console, char* command);
char* Default_TabFunction(char* command);
void DrawCommandLine();
void Cursor_Left(ConsoleInformation *console);
void Cursor_Right(ConsoleInformation *console);
void Cursor_Home(ConsoleInformation *console);
void Cursor_End(ConsoleInformation *console);
void Cursor_Del(ConsoleInformation *console);
void Cursor_BSpace(ConsoleInformation *console);
void Cursor_Add(ConsoleInformation *console, SDL_Event *event);
void Clear_Command(ConsoleInformation *console);
void Assemble_Command(ConsoleInformation *console);
void Clear_History(ConsoleInformation *console);
void Command_Up(ConsoleInformation *console);
void Command_Down(ConsoleInformation *console);


void DT_DrawText(const char *string, SDL_Surface *surface, int FontType, int x, int y );
int	DT_LoadFont(const char *BitmapName, int flags );
int	DT_FontHeight( int FontNumber );
int	DT_FontWidth( int FontNumber );
BitFont* DT_FontPointer(int FontNumber );
void DT_DestroyDrawText();