#ifndef __USER_H__
#define __USER_H__
#include "mwindows.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#define floor floor
#define sqrt sqrt
#ifdef NDEBUG
#undef DEBUG
#endif
#ifdef DEBUG
#undef NDEBUG
#endif
typedef struct{
double eM11,eM12,eM21,eM22,eDx,eDy;
} XFORMDBL;
#undef XFORM
#define XFORM XFORMDBL
#undef LPXFORM
#define LPXFORM XFORMDBL *
#undef ANSI_VAR_FONT
#define ANSI_VAR_FONT DEFAULT_GUI_FONT
#define GetAValue(c) ((BYTE)((c)>>24))
#define AddPtr(t,p,b) \
((t)( ((LONG_PTR)(p))+((LONG_PTR)(b)) ))
#define DiffPtr(t,p,b) \
((t)( ((LONG_PTR)(p))-((LONG_PTR)(b)) ))
#define RectWidth(rc) ((rc)->right-(rc)->left)
#define RectHeight(rc) ((rc)->bottom-(rc)->top)
#define SetRect(rc,x,y,x1,y1) \
do{(rc)->left=x;(rc)->right=x1;\
(rc)->top=y;(rc)->bottom=y1;}while(0)
#define IsRectEmpty(rc) \
((rc)->left>=(rc)->right||(rc)->top>=(rc)->bottom)
#define SetRectEmpty(rc) \
(rc)->left=(rc)->right=(rc)->top=(rc)->bottom=0
#define OffsetRect(rc,x,y) \
do{(rc)->left+=x;(rc)->right+=x;\
(rc)->top+=y;(rc)->bottom+=y;}while(0)
#define InflateRect(rc,x,y) \
do{(rc)->left-=x;(rc)->right+=x;\
(rc)->top-=y;(rc)->bottom+=y;}while(0)
#define SetFrame(rc,x,y,cx,cy) \
do{(rc)->right=((rc)->left=(x))+(cx);\
(rc)->bottom=((rc)->top=(y))+(cy);} while(0)
#define PointInRect(rc,x,y) \
((x)>=(rc)->left \
&&(y)>=(rc)->top \
&&(x)< (rc)->right \
&&(y)< (rc)->bottom)
#define TransformSetIdentity(m) \
do{(m)->eM11=(m)->eM22=1.0;\
(m)->eM12=(m)->eM21=(m)->eDx=(m)->eDy=0.0;}while(0)
#define TOBOOL(x) ((BOOL)((!!(x))?1:0))
#define DIMOF(x) (sizeof(x)/sizeof(x[0]))
#define MOD(i,n) ( (i) % (n) >= 0 ? (i) % (n) : (i) % (n) + (n) )
#define MS_NINT(x) ((x) >= 0.0 ? ((long) ((x)+.5)) : ((long) ((x)-.5)))
#define MS_PI 3.14159265358979323846
#define MS_3PI2 4.71238898038468985769 // (3 * MS_PI2)
#define DEGTORAD 0.017453292519943295769236907684886 //(M_PI/180)
#define RADTODEG 57.295779513082320876798154814105 //(180/M_PI)
#define M_PITIMESTWO 6.283185307179586476925286766559
#define M_HALFPI 1.5707963267948966192313216916398
#define SQRT_2 1.4142135623730950488016887242097
#define RADTOGRAD 63.661977236758134307553505349006 // (200/M_PI)
#define GRADTORAD 0.015707963267948966192313216916398 // (M_PI/200)
#define ITEXTCONST(x) x
#define ITEXT(x) x
#ifdef DEBUG
static void DebugOut(char *fmt,...){
char s[1024];
va_list arg;
va_start(arg,fmt);
vsnprintf(s,1024,fmt,arg);
va_end(arg);
OutputDebugStringA(s);
OutputDebugStringA("\r\n");
}
static void DebugOutF(char *fmt,...){
char s[1024];
va_list arg;
va_start(arg,fmt);
vsnprintf(s,1024,fmt,arg);
va_end(arg);
OutputDebugStringA(s);
}
#define ASSERT(c) \
do{if(!(c)){\
DebugOut("condition on line %d failed: %s",__LINE__,#c);\
}\
}while(0)
#else
#ifdef __LCC__
#define DebugOut(x,...)
#define DebugOutF(x,...)
#else
#define DebugOut
#define DebugOutF
#endif
#define ASSERT(x)
#endif
#ifdef _WIN32
#define __xmalloc(s) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,s)
#define __malloc(s) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,s)
#define __realloc(p,s) ((p)?HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,p,s):__xmalloc(s))
#define __free(p) do{if(p)HeapFree(GetProcessHeap(),0,p);}while(0)
#define __calloc(e,x) __xmalloc(e*x)
#else
#define __xmalloc(s) calloc(1,s)
#define __malloc(s) malloc(s)
#define __calloc(e,s) calloc(e,s)
#define __realloc(p,s) realloc(p,s)
#define __free(p) free(s)
#endif
#ifdef DBGALLOC // Allocation debugging, includes bounds checking
#define DEBUG
void DebugMallocStatus(void);
LPVOID DebugReAlloc(LPVOID p, DWORD s, LPCTSTR fn, DWORD ln);
void DebugFree(LPVOID p, LPCTSTR fn, DWORD ln);
#define xmalloc(s) DebugReAlloc(NULL,s,__FILE__,__LINE__)
#define malloc(s) DebugReAlloc(NULL,s,__FILE__,__LINE__)
#define realloc(p,s) DebugReAlloc(p,s,__FILE__,__LINE__)
#define free(p) DebugFree(p,__FILE__,__LINE__)
#elif defined(GCSALLOC) // Garbage Collection style allocation, includes bounds checking
void GcsFreeAll(void);
LPVOID GcsReAlloc(LPVOID p, DWORD s);
void GcsFree(LPVOID p);
#define malloc(s) GcsReAlloc(NULL,s)
#define xmalloc(s) GcsReAlloc(NULL,s)
#define realloc(p,s) GcsReAlloc(p,s)
#define free(p) GcsFree(p)
#else
#define DebugMallocStatus()
#define xmalloc(s) __xmalloc(s)
#ifdef _WIN32
#define malloc(s) __malloc(s)
#define realloc(p,s) __realloc(p,s)
#define free(p) __free(p)
#endif
#endif
#ifdef _WIN32
#define calloc(e,s) xmalloc(e*s)
#endif
#define RectMove(rc,xx,yy) \
do{(rc)->right=(xx)+RectWidth(rc);\
(rc)->bottom=(yy)+RectHeight(rc);\
(rc)->left=xx;(rc)->top=yy;}while(0)
#define SWAPWORD(a) \
((WORD) ((((a)>>8)&0x00FF))\
|((((a)<<8)&0xFF00)))
#define SWAPDWORD(a) \
((DWORD) ((((a)>>24)&0x000000FF))\
|((((a)>>8) &0x0000FF00))\
|((((a)<<8) &0x00FF0000))\
|((((a)<<24)&0xFF000000)))
#define FOURCC DWORD
#define MAKEFOURCC(a,b,c,d) \
((DWORD) (((DWORD)(BYTE)(a)) )\
|(((DWORD)(BYTE)(b))<<8)\
|(((DWORD)(BYTE)(c))<<16)\
|(((DWORD)(BYTE)(d))<<24))
#define RGBA(a,b,c,d) \
((DWORD) (((DWORD)(BYTE)(a)) )\
|(((DWORD)(BYTE)(b))<<8)\
|(((DWORD)(BYTE)(c))<<16)\
|(((DWORD)(BYTE)(d))<<24))
#define max(a,b) ( ((a)>(b))?(a):(b) )
#define min(a,b) ( ((a)<(b))?(a):(b) )
#undef abs
#define abs(a) ( ((a)<0)?-(a):(a) )
#define BOUND(a,l,h) ( ((a)<(l)) ?(l):\
( ((a)>(h)) ?(h):(a) )\
)
#define HISHORT(x) ((INT)(SHORT)HIWORD(x))
#define LOSHORT(x) ((INT)(SHORT)LOWORD(x))
typedef struct{
DWORD SizeOfBitmap;
LPDWORD Buffer;
} RTL_BITMAP, *PRTL_BITMAP;
#define MASK(Count, Shift) \
((Count) == 32 ? 0xFFFFFFFF : ~(0xFFFFFFFF << (Count)) << (Shift))
#define SETBIT(DwordBuf,BitNumber) \
((DwordBuf)[(BitNumber)>>5]|=(1<<((BitNumber)&0x1F)))
#define CLEARBIT(DwordBuf,BitNumber) \
((DwordBuf)[(BitNumber)>>5]&=~(1<<((BitNumber)&0x1F)))
#define TESTBIT(DwordBuf,BitNumber) \
(((DwordBuf)[(BitNumber)>>5]&(1<<((BitNumber)&0x1F)))!=0)
#define INITBITS(bm,size) \
do{(bm)->SizeOfBitmap=size;(bm)->Buffer=xmalloc( ((bm)->SizeOfBitmap+7)>>3 );}while(0)
#define CLEARBITS(bm) \
ZeroMemory((bm)->Buffer,((bm)->SizeOfBitmap+7)>>3)
#define SETBITS(bm) \
memset((bm)->Buffer,0xFF,((bm)->SizeOfBitmap+7)>>3)
typedef DWORD (*IOPROC)(LPVOID h,LPVOID buffer,DWORD size);
typedef IOPROC WRITEPROC;
typedef IOPROC READPROC;
typedef LPVOID (*IOOPENPROC)(LPVOID file,int mode);
typedef LONG (*IOSEEKPROC)(LPVOID h,LONG seek,int whence);
typedef BOOL (*IOCLOSEPROC)(LPVOID h);
typedef BOOL (*IOFLUSHPROC)(LPVOID h);
typedef DWORD (*IOGETSIZEPROC)(LPVOID h);
typedef BOOL (*IOVALIDPROC)(LPVOID h);
typedef struct{
IOVALIDPROC IsValid;
IOOPENPROC OpenFile;
IOCLOSEPROC CloseFile;
READPROC ReadFile;
WRITEPROC WriteFile;
IOSEEKPROC SeekFile;
IOGETSIZEPROC GetSize;
IOFLUSHPROC FlushFile;
} IOSTRUCT;
#define FILE_READ 1
#define FILE_WRITE 0
#define FILE_APPEND 2
#define FILE_READWRITE 3
#endif