GaijinEntertainment / DagorEngine

Dagor Engine and Tools source code from Gaijin Games KFT
Other
2.2k stars 274 forks source link

Added easily expandable macro for reducing repetition and better debugging for ConsoleMouseKbdHandler controls #79

Closed RandomGamingDev closed 2 months ago

RandomGamingDev commented 2 months ago

Previously the code located at DagorEngine/prog/samples/commonFramework/de3_freeCam_mk.cpp

flyMode->keys.left =
  int((kbd.isKeyDown(HumanInput::DKEY_LEFT) && moveWithArrows) || (kbd.isKeyDown(HumanInput::DKEY_A) && moveWithWASD));
flyMode->keys.right =
  int((kbd.isKeyDown(HumanInput::DKEY_RIGHT) && moveWithArrows) || (kbd.isKeyDown(HumanInput::DKEY_D) && moveWithWASD));
flyMode->keys.fwd =
  int((kbd.isKeyDown(HumanInput::DKEY_UP) && moveWithArrows) || (kbd.isKeyDown(HumanInput::DKEY_W) && moveWithWASD));
flyMode->keys.back =
  int((kbd.isKeyDown(HumanInput::DKEY_DOWN) && moveWithArrows) || (kbd.isKeyDown(HumanInput::DKEY_S) && moveWithWASD));

which has a lot of repetition, hard to expand, and is prone to error

This PR just changes it to be simplified so that it's easier to expand, easier to debug, and involves less tedious repetition in a way similar to the chromium codebase (e.g. https://github.com/chromium/chromium/blob/main/third_party/blink/renderer/platform/graphics/gpu/webgl_image_conversion.cc#L3304)

Which gives us this:

#define MKEY(arrows, wasd) int((kbd.isKeyDown(arrows) && moveWithArrows) || (kbd.isKeyDown(wasd) && moveWithWASD));
flyMode->keys.left = MKEY(HumanInput::DKEY_LEFT, HumanInput::DKEY_A);
flyMode->keys.right = MKEY(HumanInput::DKEY_RIGHT, HumanInput::DKEY_D);
flyMode->keys.fwd = MKEY(HumanInput::DKEY_UP, HumanInput::DKEY_W);
flyMode->keys.back = MKEY(HumanInput::DKEY_DOWN, HumanInput::DKEY_S);
#undef MKEY

with MKEY standing for "movement key"

Macros are dangerous, but it'd be nice to see DagorEngine to move to use them a little more like Chromium's code

RandomGamingDev commented 2 months ago

don't add binary files

It's been resolved and now the only changes are within the file where the macro was added itself.