OneLoneCoder / olcPixelGameEngine

The official distribution of olcPixelGameEngine, a tool used in javidx9's YouTube videos and projects
Other
3.81k stars 912 forks source link

WM_mousemove lparam bit-manipulation. #265

Open xshrer opened 2 years ago

xshrer commented 2 years ago

image what is happening here,and why is this method being used instead of simple type casting https://github.com/OneLoneCoder/olcPixelGameEngine/blob/7a20524ae4725120b38b9967583ff1c1e08da286/olcPixelGameEngine.h#L4477

xshrer commented 2 years ago

Jacob and Javidx9 gave the solution on dicord

image

the winapi gives unsigned values for the mouse position.this can cause some issues when multiple screens are connected,x and y coordinates can have negative values .

image

so we manually get x and y values from lparam(using bit shifting and masking)

https://github.com/OneLoneCoder/olcPixelGameEngine/blob/7a20524ae4725120b38b9967583ff1c1e08da286/olcPixelGameEngine.h#L4476

and then use the evil bit hack to tell the system that the variable is singed .Watch the youtube link below from 12:09 to understand the bit hack.

Fast Inverse Square Root — A Quake III Algorithm https://www.youtube.com/watch?v=p8u_k2LIZyo&list=TLPQMTgxMjIwMjEGAriC-8ygvQ&index=7

Javidx9 explains the bit shifting and masking quite beautifully in the nes emulater tutorial https://www.youtube.com/watch?v=F8kx56OZQhg&t=1584s