Skip to content

Instantly share code, notes, and snippets.

@nothings
Last active March 7, 2017 20:26
Show Gist options
  • Save nothings/36a92bc03f5dcc51665e9b2c80317fd1 to your computer and use it in GitHub Desktop.
Save nothings/36a92bc03f5dcc51665e9b2c80317fd1 to your computer and use it in GitHub Desktop.
#ifdef _WIN64
typedef unsigned __int64 STBP_UINTPTR;
#else
typedef unsigned int STBP_UINTPTR;
#endif
// WINUSERAPI => __declspec(import)
// WINAPI => __stdcall
// HDC,HGLRC,etc => void *
// LONG_PTR, INTPTR, LRESULT => ptrdiff_t
// WPARAM, LPARAM => size_t
typedef int (__stdcall *stbp_wproc)();
typedef ptrdiff_t (__stdcall *stbp_WNDPROC)(void *, unsigned int, size_t, size_t);
typedef struct {
unsigned short nSize, nVersion;
unsigned int dwFlags;
unsigned char iPixelType, cColorBits, cRedBits, cRedShift, cGreenBits,
cGreenShift, cBlueBits, cBlueShift, cAlphaBits, cAlphaShift, cAccumBits,
cAccumRedBits, cAccumGreenBits, cAccumBlueBits, cAccumAlphaBits,
cDepthBits, cStencilBits, cAuxBuffers, iLayerType, bReserved;
unsigned int dwLayerMask, dwVisibleMask, dwDamageMask;
} stbp_PIXELFORMATDESCRIPTOR;
typedef struct {
unsigned int cbSize,style;
stbp_WNDPROC lpfnWndProc;
int cbClsExtra, cbWndExtra;
void *hInstance,*hIcon,*hCursor,*hbrBackground;
const char *lpszMenuName, *lpszClassName;
void *hIconSm;
} stbp_WNDCLASSEXA;
typedef struct tagRAWINPUTDEVICE {
unsigned short usUsagePage;
unsigned short usUsage;
unsigned int dwFlags;
void *hwndTarget;
} stbp_RAWINPUTDEVICE;
typedef struct stbp_tagRAWINPUT {
struct stbp_tagRAWINPUTHEADER {
unsigned int dwType;
unsigned int dwSize;
void *hDevice;
size_t wParam;
} header;
struct {
unsigned short usFlags;
union {
unsigned long ulButtons;
struct {
unsigned short usButtonFlags;
unsigned short usButtonData;
};
};
unsigned long ulRawButtons;
long lLastX;
long lLastY;
unsigned long ulExtraInformation;
} mouse;
} stbp_RAWINPUT;
extern __declspec(dllimport) stbp_wproc __stdcall GetProcAddress(struct HINSTANCE__ * module, const char *function_name);
extern __declspec(dllimport) struct HINSTANCE__ * __stdcall LoadLibraryA(const char *name);
static int (__stdcall * stbp_AdjustWindowRect)(struct stbp_tagRECT *rect, unsigned int dwStyle, int bMenu);
static int (__stdcall * stbp_ChoosePixelFormat)(void *hdc, const stbp_PIXELFORMATDESCRIPTOR *);
static int (__stdcall * stbp_ClientToScreen)(void *hwnd, struct tagPOINT *);
static int (__stdcall * stbp_DescribePixelFormat)(void *hdc, int, unsigned int, stbp_PIXELFORMATDESCRIPTOR *);
static int (__stdcall * stbp_DestroyWindow)(void *hwnd);
static size_t (__stdcall * stbp_DispatchMessageA)(const struct tagMSG *);
static int (__stdcall * stbp_GetClientRect)(void *hwnd, struct tagRECT *rect);
static void* (__stdcall * stbp_GetCursor)(void);
static int (__stdcall * stbp_GetCursorPos)(struct tagPOINT * pos);
static void* (__stdcall * stbp_GetDC)(void *hwnd);
static int (__stdcall * stbp_GetKeyboardState)(char keystate[256]);
static void* (__stdcall * stbp_GetStockObject)(int);
static void (__stdcall * stbp_OutputDebugStringA)(const char *string);
static void* (__stdcall * stbp_LoadCursorA)(void *hinst, const char *name);
static void* (__stdcall * stbp_LoadIconA)(void *hinst, const char *name);
static int (__stdcall * stbp_MessageBoxA)(void *hwnd, const char *text, const char *caption, unsigned int type);
static size_t (__stdcall * stbp_PeekMessageA)(struct tagMSG *, void *hwnd, unsigned int fmin, unsigned int fmax, unsigned int remove);
static int (__stdcall * stbp_ReleaseCapture)(void);
static void* (__stdcall * stbp_SetCapture)(void *hwnd); // @TODO: use this
static void* (__stdcall * stbp_SetCursor)(void *hcursor);
static int (__stdcall * stbp_SetPixelFormat)(void *hdc, int, const stbp_PIXELFORMATDESCRIPTOR *);
static int (__stdcall * stbp_ShowCursor)(int bShow);
static int (__stdcall * stbp_ShowWindow)(void *hwnd, int nCmdShow);
static void (__stdcall * stbp_Sleep)(unsigned int dwMilliseconds);
static int (__stdcall * stbp_SwapBuffers)(void *hdc);
static size_t (__stdcall * stbp_TranslateMessage)(const struct tagMSG *);
static unsigned short (__stdcall * stbp_RegisterClassExA)( stbp_WNDCLASSEXA *);
static ptrdiff_t (__stdcall * stbp_DefWindowProcA)(void *hwnd, unsigned int msg, size_t wparam, size_t lparam);
static void* (__stdcall * stbp_CreateWindowExA)(unsigned int exStyle, const char *class_name,
char *window_name, unsigned int style, int x, int y, int nwidth,
int nheight, void *parent, void *menu, void *instance, void *lpparam);
static STBP_UINTPTR (__stdcall * stbp_SetTimer)(void *hwnd, STBP_UINTPTR, unsigned int, stbp_wproc);
static void (__stdcall * stbp_SwitchToFiber)(void *);
static void* (__stdcall * stbp_CreateFiber)(size_t stack, stbp_wproc *p, void*);
static void* (__stdcall * stbp_ConvertThreadToFiber)(void *);
static int (__stdcall * stbp_RegisterRawInputDevices)( stbp_RAWINPUTDEVICE *dev, unsigned int num_dev, unsigned int cbSize);
static unsigned int (__stdcall * stbp_GetRawInputData)(void *, unsigned int cmd, void*data, unsigned int*size, unsigned int size_header);
int __stdcall WinMain(struct HINSTANCE__ *hInstance, struct HINSTANCE__ *hPrevInstance, char *lpCmdLine, int nCmdShow)
{
void *winmm = LoadLibraryA("winmm.dll");
void *user32 = LoadLibraryA("user32.dll");
void *gdi32 = LoadLibraryA("gdi32.dll");
void *kernel32 = LoadLibraryA("kernel32.dll");
stbp_AdjustWindowRect = (int (__stdcall *)(struct stbp_tagRECT *rect, unsigned int dwStyle, int bMenu)) GetProcAddress(user32, "AdjustWindowRect");
stbp_ChoosePixelFormat = (int (__stdcall*)(void *hdc, const stbp_PIXELFORMATDESCRIPTOR *)) GetProcAddress(gdi32, "ChoosePixelFormat");
stbp_ClientToScreen = (int (__stdcall*)(void *hwnd, struct tagPOINT *)) GetProcAddress(user32, "ClientToScreen");
stbp_DescribePixelFormat = (int (__stdcall*)(void *hdc, int, unsigned int, stbp_PIXELFORMATDESCRIPTOR *)) GetProcAddress(gdi32, "DescribePixelFormat");
stbp_DestroyWindow = (int (__stdcall*)(void *hwnd)) GetProcAddress(user32, "DestroyWindow");
stbp_DispatchMessageA = (size_t (__stdcall*)(const struct tagMSG *)) GetProcAddress(user32, "DispatchMessageA");
stbp_GetClientRect = (int (__stdcall*)(void *hwnd, struct tagRECT *rect)) GetProcAddress(user32, "GetClientRect");
stbp_GetCursor = (void* (__stdcall*)(void)) GetProcAddress(user32, "GetCursor");
stbp_GetCursorPos = (int (__stdcall*)(struct tagPOINT * pos)) GetProcAddress(user32, "GetCursorPos");
stbp_GetDC = (void * (__stdcall*)(void *hwnd)) GetProcAddress(user32, "GetDC");
stbp_GetKeyboardState = (int (__stdcall*)(char keystate[256])) GetProcAddress(user32, "GetKeyboardState");
stbp_GetStockObject = (void* (__stdcall*)(int)) GetProcAddress(gdi32, "GetStockObject");
stbp_OutputDebugStringA = (void (__stdcall*)(const char *string)) GetProcAddress(kernel32, "OutputDebugStringA");
stbp_LoadCursorA = (void* (__stdcall*)(void *hinst, const char *name)) GetProcAddress(user32, "LoadCursorA");
stbp_LoadIconA = (void* (__stdcall*)(void *hinst, const char *name)) GetProcAddress(user32, "LoadIconA");
stbp_MessageBoxA = (int (__stdcall*)(void *hwnd, const char *text, const char *caption, unsigned int type)) GetProcAddress(user32, "MessageBoxA");
stbp_PeekMessageA = (size_t (__stdcall*)(struct tagMSG *, void *hwnd, unsigned int fmin, unsigned int fmax, unsigned int remove)) GetProcAddress(user32, "PeekMessageA");
stbp_ReleaseCapture = (int (__stdcall*)(void)) GetProcAddress(user32, "ReleaseCapture");
stbp_SetCapture = (void* (__stdcall*)(void *hwnd)) GetProcAddress(user32, "SetCapture"); // @TODO: use this
stbp_SetCursor = (void* (__stdcall*)(void *hcursor)) GetProcAddress(user32, "SetCursor");
stbp_SetPixelFormat = (int (__stdcall*)(void *hdc, int, const stbp_PIXELFORMATDESCRIPTOR *)) GetProcAddress(gdi32, "SetPixelFormat");
stbp_ShowCursor = (int (__stdcall*)(int bShow)) GetProcAddress(user32, "ShowCursor");
stbp_ShowWindow = (int (__stdcall*)(void *hwnd, int nCmdShow)) GetProcAddress(user32, "ShowWindow");
stbp_Sleep = (void (__stdcall*)(unsigned int dwMilliseconds)) GetProcAddress(kernel32, "Sleep");
stbp_SwapBuffers = (int (__stdcall*)(void *hdc)) GetProcAddress(gdi32, "SwapBuffers");
stbp_TranslateMessage = (size_t (__stdcall*)(const struct tagMSG *)) GetProcAddress(user32, "TranslateMessage");
stbp_RegisterClassExA = (unsigned short (__stdcall*)( stbp_WNDCLASSEXA *)) GetProcAddress(user32, "RegisterClassExA");
stbp_DefWindowProcA = (ptrdiff_t (__stdcall*)(void *hwnd, unsigned int msg, size_t wparam, size_t lparam)) GetProcAddress(user32, "DefWindowProcA");
stbp_CreateWindowExA = (void * (__stdcall*)(unsigned int exStyle, const char *class_name,
char *window_name, unsigned int style, int x, int y, int nwidth,
int nheight, void *parent, void *menu, void *instance, void *lpparam)) GetProcAddress(user32, "CreateWindowExA");
stbp_SetTimer = (STBP_UINTPTR (__stdcall*)(void *hwnd, STBP_UINTPTR, unsigned int, stbp_wproc)) GetProcAddress(user32, "SetTimer");
stbp_SwitchToFiber = (void (__stdcall*)(void *)) GetProcAddress(kernel32, "SwitchToFiber");
stbp_CreateFiber = (void* (__stdcall*)(size_t stack, stbp_wproc *p, void*)) GetProcAddress(kernel32, "CreateFiber");
stbp_ConvertThreadToFiber = (void* (__stdcall*)(void *)) GetProcAddress(kernel32, "ConvertThreadToFiber");
stbp_RegisterRawInputDevices = (int (__stdcall *)(stbp_RAWINPUTDEVICE *dev, unsigned int num_dev, unsigned int cbSize)) GetProcAddress(user32, "RegisterRawInputDevices");
stbp_GetRawInputData = (unsigned int (__stdcall *)(void *, unsigned int cmd, void*data, unsigned int*size, unsigned int size_header)) GetProcAddress(user32, "GetRawInputData");
stbp_timeGetTime = (unsigned int (__stdcall *)(void)) GetProcAddress(winmm, "timeGetTime");
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment