Hi
So, after a week of fighting my input code I've somehow created this code snippet for reasons I'm not clear about myself. On a more serious note, do you think this is "too fubar" to use it in my code?
#define OEMRESOURCE
#include <windows.h>
/*
** see FTW #4
*/
#ifdef _WIN64
typedef unsigned __int64 QWORD;
#endif
/*
** a global variable to make our program more interesting (and to piss off the anti-globals community)
*/
bool run = true;
/*
** the big kahuna ...
*/
void ProcessInputBuffer(void);
/*
** our window procedure
*/
LRESULT CALLBACK WindowProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_INPUT:
break;
}
return DefWindowProc(window, message, wparam, lparam);
}
/*
** ze WinMain (where the action is)
** from MSDN's WinMain page: "A handle to the previous instance of the application. This parameter is always NULL."
** weee ...
*/
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, char* cmdLine, int show)
{
/*
** we want to be 100% sure
*/
if(prevInstance != NULL)
return 1337;
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = reinterpret_cast<HBRUSH>(GetStockObject(LTGRAY_BRUSH));
/*
** from MSDN's LoadCursor/LoadIcon pages: "Note This function has been superseded by the LoadImage function."
** thank you for not telling us that we need to #define OEMRESOURCE for this to work
*/
wc.hCursor = reinterpret_cast<HCURSOR>(LoadImage(NULL, MAKEINTRESOURCE(OCR_CROSS), IMAGE_CURSOR, 0, 0, LR_SHARED));
wc.hIcon = reinterpret_cast<HICON>(LoadImage(NULL, MAKEINTRESOURCE(OIC_SHIELD), IMAGE_ICON, 0, 0, LR_SHARED));
wc.hInstance = instance;
wc.lpfnWndProc = &WindowProc;
wc.lpszClassName = "WCLSS";
wc.lpszMenuName = NULL;
wc.style = CS_CLASSDC|CS_HREDRAW|CS_VREDRAW;
if(RegisterClass(&wc) == 0)
return 1337;
HWND window = CreateWindow("WCLSS", "RawInput SUCKS!", WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL, instance, NULL);
if(window == NULL)
return 1337;
ShowWindow(window, SW_NORMAL);
RAWINPUTDEVICE devices[] = {
{ 0x01 /* generic */, 0x06 /* keyboard */, RIDEV_INPUTSINK, window },
{ 0x01 /* generic */, 0x02 /* mouse */, RIDEV_INPUTSINK, window },
};
if(RegisterRawInputDevices(devices, 2, sizeof(RAWINPUTDEVICE)) == FALSE)
return 1337;
MSG msg = {0};
while(run)
{
/*
** location #1
**
** it seems to work if put here
*/
ProcessInputBuffer();
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0)
{
/*
** location #2
** doesn't work
**
** ProcessInputBuffer();
*/
if(msg.message == WM_QUIT)
run = false;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
/*
** location #3
** doesn't work
**
** ProcessInputBuffer();
*/
/*
** simulate a game running at ~50 frames/second
*/
unsigned int woozaah = GetTickCount() + 20;
while(GetTickCount() < woozaah)
{
/* Quidquid latine dictum sit, altum sonatur. */
}
}
/*
** FUCK THE WHAT #1
** UnRegisterRawInputDevices(), anyone? no? k ...
*/
DestroyWindow(window);
UnregisterClass("WCLSS",instance);
return 0;
}
void ProcessInputBuffer(void)
{
/*
** FUCK THE WHAT #2
** you can't run this as 32bit executable on 64bit windows
** why not? GetRawInputBuffer fucks up the buffer alignment ...
*/
unsigned int size = 0;
if(GetRawInputBuffer(NULL, &size, sizeof(RAWINPUTHEADER)) != 0)
return;
if(size == 0)
return;
/*
** FUCK THE WHAT #3
** this is random and nowhere documented!
** the 2nd call to GetRawInputBuffer will fail if we don't do this
*/
++size;
RAWINPUT* input = reinterpret_cast<RAWINPUT*>(new unsigned char[size]);
int blocks = GetRawInputBuffer(input, &size, sizeof(RAWINPUTHEADER));
if(blocks == -1)
return;
for(int i=0;i<blocks;++i)
{
if(input->header.dwType == RIM_TYPEKEYBOARD)
{
if(input->data.keyboard.VKey == VK_ESCAPE)
{
/*
** it's EXTREMELY smart to define RI_KEY_MAKE as 0
** makes it so convenient to test for ...
*/
if((input->data.keyboard.Flags & RI_KEY_BREAK) == 0)
run = false;
/* do stuff */
}
}
/*
** FUCK THE WHAT #4
** if you're on 64bit windows your journey ends here
** this macro uses QWORD which isn't defined
*/
input = NEXTRAWINPUTBLOCK(input);
}
delete[] input;
}
PS: I'm not sure where else to post that