gorbit99 / olcPGEX_Gamepad

Cross platform (Windows + Linux) Gamepad API for the Pixel Game Engine (http://onelonecoder.com/)
Other
15 stars 5 forks source link

Error compiling on MinGW #1

Closed Moros1138 closed 4 years ago

Moros1138 commented 4 years ago

I was running a test compile of PGEXes in my test build environments. Compiled fine on Linux, but when I compile with MinGW (From MSYS2) I get an error about using an integer inside a constant expression. I have provided the build command and it's output. Hope it's useful to you.

g++ -c -o obj/mingw/main.o src/main.cpp -I./include -std=c++17
In file included from C:/msys64/mingw64/x86_64-w64-mingw32/include/minwindef.h:163,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/windef.h:8,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/windows.h:69,
                 from ./include/olcPixelGameEngine.h:2323,
                 from src/main.cpp:2:
./include/olcPGEX_Gamepad.h: In static member function 'static void olc::GamePad::init()':
./include/olcPGEX_Gamepad.h:210:50: error: the value of 'i' is not usable in a constant expression
  210 |   { 0, (DWORD)FIELD_OFFSET(GamePadState, buttons[i]), DIDFT_BUTTON | DIDFT_ANYINSTANCE | 0x80000000, 0 };
      |                                                  ^
In file included from src/main.cpp:21:
./include/olcPGEX_Gamepad.h:208:11: note: 'int i' is not const
  208 |  for (int i = 0; i < 20; i++) {
      |           ^
make: *** [Makefile.mingw:17: obj/mingw/main.o] Error 1
gorbit99 commented 4 years ago

I think I see the issue, that loop there is meant to be readability thing, it's adding all the 20 possible buttons to the array that keeps track of them. Seems like in vs FIELD_OFFSET expands to non const but in mingw it does or something. Try the following fix: Remove the loop from init and add the 20 hardcoded lines to the g_aObjectFormats array like so {0, (DWORD)FIELD_OFFSET(GamePadState, buttons[0]), DIDFT_BUTTON | DIDFT_ANYINSTANCE | 0x80000000, 0}, Add this line 20 times to the end of that array with buttons[X] going from 0 to 19.