#ifndef WIN_H
#define WIN_H 1

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/time.h>
#include "joystick.h"

#define WINDOW_WIDTH 400
#define WINDOW_HEIGHT 300
#define FONT_SMALL  1
#define FONT_MEDIUM 2
#define FONT_LARGE  3
#define MAX_IMAGES 256
#define FRAME_LEN 50000

typedef void (*MouseCB) (void *data, int button, int action, int x, int y);
typedef void (*KeyCB) (void *data, int key, int action);
typedef void (*JoystickCB) (void *data, int x, int y, int buttons);

typedef struct
{
	int pixel;
	int r, g, b;
}ColorTable;

class Image;
class Win
{
	friend class Image;
	public:
		int  create();
		void mcallback(MouseCB f, void *data);
		void kcallback(KeyCB f, void *data);
		void jcallback(JoystickCB f, void *data);
		void eventloop();
		void close();
		void sync();

		int StringWidth(char *str, int fnt);
		int StringHeight(char *str, int fnt);
		void DrawText(int x, int y, char *text, char *col, int fnt);
		void FillRectangle(int x, int y, int w, int h, char *col);
		void DrawRectangle(int x, int y, int w, int h, char *col);
		void DrawLine(int x, int y, int w, int h, char *col);
		int  LoadImage(char **data);
		void DrawImage(int x, int y, int im);
		void ClearImages();
		void DrawWindow();

	private:
		XFontStruct *getFont(int fnt);
		void process_event(XEvent report);
		void private_colormap();
		void alloc_color(int i, int r, int g, int b);
		int  get_color(char *col);
		int time_diff();

		MouseCB mousecb;
		KeyCB keycb;
		JoystickCB joystickcb;
		void *mousecallback;
		void *keycallback;
		void *joystickcallback;
		unsigned int width, height;
		Display *display;
		Joystick joy;
		int screen;
		int depth;
		Window win;
		GC gc, copygc;
		Colormap cmap;
		ColorTable idx[256];
		Pixmap buffer;
		XFontStruct *font1, *font2, *font3;
		Image *img[MAX_IMAGES];
		int num_images;
		int pause;
		int num_keys;
		struct timeval st, rt;
};

#endif


