EDIT: Script: http://forum.chaos-project.com/index.php/topic,15450.0.htmlOverall goal is to know, with a script call, how many times (in which direction) the mouse wheel has scrolled.
I started off with seeing if someone already made something in RPG Maker, and found this at the top of the list:
http://pastebin.com/raw/q4TcSE1hGetMessage, from what I understand, waits until a message is received before processing any further. The script does work (with some missed inputs here and there), but FPS takes a massive hit.
I then found PeekMessage, which I thought functioned much like GetMessage but without the waiting. Turns out that was wrong; I wasn't getting any mouse wheel messages (unless I furiously moved the mouse around and scrolled at the same time, in which I received the message every now and then).
A callback sounds like the best solution, but I don't really have any experience with setting those up. I looked at this and am not sure if I can figure out how to incorporate it into a DLL.
http://stackoverflow.com/a/21961482How difficult is this really?
EDIT:
Okay so the stackoverflow actually helped a lot. After fumbling around with it, I figured out how to put it into a separate thread, accumulate the number of times the wheel scrolled, and have another DLL method return the number of times (positive for scrolling up, negative for down). I do not know the elegance of this nor the practicality, so I'm hoping someone can give valuable input. Game runs at 60 FPS and is registering every instance of a mouse wheel scroll.
I guess the ONLY bad thing right now is it accepts the mouse scroll regardless of what the active window is. So if your game pauses when it's not active, you scroll the wheel a lot, and click back on the game, the first call could be a massive accumulation. I could just make it return only -1 or 1 instead, but whatever.
// Since I'm doing this in CodeBlocks, I found out you have to do this for RAWINPUTDEVICE to work.
// Visual Studios already has a high enough version that this is unnecessary if building in it.
#ifdef __MINGW32__
# define _WIN32_WINNT 0x0501
#endif
#include "main.h"
#include <windows.h>
#include <stdio.h>
int delta = 0;
LRESULT CALLBACK MyWindowProc( HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam ) {
switch ( uiMsg ) {
case WM_INPUT: {
UINT dwSize;
HRAWINPUT hRawInput = reinterpret_cast<HRAWINPUT>( lParam );
UINT uiRetCode = GetRawInputData( hRawInput, RID_INPUT, NULL, &dwSize,
static_cast<UINT>( sizeof( RAWINPUTHEADER ) ) );
if ( uiRetCode != 0xffffffff ) {
LPBYTE lpb = new BYTE[ dwSize ];
uiRetCode = GetRawInputData( hRawInput, RID_INPUT, lpb, &dwSize,
static_cast<UINT>( sizeof( RAWINPUTHEADER ) ) );
if ( uiRetCode > 0 ) {
RAWINPUT* praw = reinterpret_cast<RAWINPUT*>( lpb );
if ( praw->header.dwType == RIM_TYPEMOUSE ) {
if ( praw->data.mouse.usButtonFlags & RI_MOUSE_WHEEL ) {
signed int siDelta = static_cast<SHORT>( praw->data.mouse.usButtonData );
delta += siDelta / 120;
}
}
}
delete[] lpb;
}
break;
} // WM_INPUT
default:
return DefWindowProc( hWnd, uiMsg, wParam, lParam );
}
return 0;
}
DWORD WINAPI CheckWheel(LPVOID lpParam)
{
HWND hWnd;
WNDCLASS WndClass;
memset( &WndClass, 0, sizeof( WndClass ) );
WndClass.hInstance = GetModuleHandle( NULL );
WndClass.lpszClassName = "MyRawInputClass";
WndClass.lpfnWndProc = MyWindowProc;
RegisterClass( &WndClass );
hWnd = CreateWindow( WndClass.lpszClassName, NULL, 0, 0, 0, 0, 0,
HWND_MESSAGE, 0, WndClass.hInstance, 0 );
RAWINPUTDEVICE RawInputDevice;
RawInputDevice.usUsagePage = 0x01; // Generic Desktop Controls
RawInputDevice.usUsage = 0x02; // Mouse
RawInputDevice.dwFlags = RIDEV_INPUTSINK;
RawInputDevice.hwndTarget = hWnd;
BOOL bWin32Success = RegisterRawInputDevices( &RawInputDevice, 1,
static_cast<UINT>( sizeof( RAWINPUTHEADER ) ) );
BOOL bRet;
MSG msg;
while( ( bRet = GetMessage( &msg, NULL, 0, 0 ) ) != 0 ) {
if (bRet != -1) {
DispatchMessage(&msg);
}
}
// NO GUI, UNREACHABLE
DestroyWindow( hWnd );
UnregisterClass( WndClass.lpszClassName, WndClass.hInstance );
return 0;
}
// Call this method first from the game
int DLL_EXPORT Start()
{
if (CreateThread(NULL, 0, CheckWheel, NULL, 0, 0) == NULL) return -1;
return 0;
}
// Call this to get the number of times the wheel scrolled in a certain direction
// + for UP
// - for DOWN
int DLL_EXPORT WheelDelta()
{
int copyDelta = delta;
delta = 0;
return copyDelta;
}