Skip to content

Develop KeeperFX with CMake and Visual Studio 2022

Evan edited this page Oct 27, 2024 · 26 revisions

In the development branch, you can develop with CMake and Visual Studio 2022.

Install tools

  1. Visual Studio 2022 v17.11.5 or above versions.
  • Community or other commercial versions.
  • At least select "Desktop development with C++" and "Windows application development" components.
  • In the "Individual components" tab, enter "CMake" to ensure it is selected.
  • (Optional) Select clang-cl tools to install. See this document for details.
  1. CMake 3.30.5 or above versions.
  2. MSYS2 (msys2-x86_64-20240727.exe or newer)

Install packages for MSYS2.

Open msys2_shell.cmd in C:\msys64 (your MSYS2 installation), and install these packages by the pacman.

pacman -S make gettext base-devel
pacman -S mingw-w64-i686-gcc
pacman -S mingw-w64-i686-gdb

Start VS and open the keeperfx project folder.

  • Start Visual Studio and open a local folder for your keeperfx project folder (not open .sln).

  • Visual Studio 2022 is supposed to configure the CMake immediately. Please wait for it to finish.

  • If you are new to CMake and VS 2022, I suggest reading this document.

  • Now, only x86-MinGW32-Debug or x86-MinGW32-Release can build a playable game. image

  • You can choose the standard or **heavylog **target. image

  • Use buttons in the Build menu to build or clean the project.

  • In the Project menu, you can delete the CMake cache and re-configure.

  • All outputs are put under the out\build folder of your project root.

Run and Debugging

  • You must have a playable game folder. See this page.
  • Assume you built x86-MinGW32-Debug/standard configuration, you should see keeperfx.exe, keeperfx.map, and many DLL files under out\build\x86-MinGW32-Debug.
  • Copy keeperfx.exe, keeperfx.map, and all DLL files to your game folder.
  • If you didn't add "C:\msys64\mingw32\bin" to your PATH environment variables, copy ibgcc_s_dw2-1.dll and libwinpthread-1.dll to the game folder.
  • Now you can start to play the game.
  • If you want to start it with the VS debugger, create launch.vs.json under .vs folder of your project root and enter the following content.
{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "keeperfx_hvlog.exe",
      "name": "keeperfx_hvlog.exe",
      "currentDir": "<your game folder>",
      "cwd": "<your game folder>",
      "args": [
        "-level",
        "00001",
        "-campaign",
        "personal",
        "-nointro",
        "-alex"
      ]
    },
    {
        "type": "cppdbg",
        "project": "CMakeLists.txt",
        "projectTarget": "keeperfx_hvlog.exe",
        "name": "keeperfx_hvlog.exe (gdb)",
        "currentDir": "<your game folder>",
        "cwd": "<your game folder>",
        "args": [
            "-level",
            "00001",
            "-campaign",
            "personal",
            "-nointro",
            "-alex"
        ],
        "program": "${debugInfo.target}",
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\msys64\\mingw32\\bin\\gdb.exe",
        "externalConsole": true
    }
  ]
}

Fill in your your game folder.

You can specify the starting arguments in the args array. The above args are explained below.

"-alex" - enable cheat menu.
"-nointro" - Skip intro videos.
"-level", "00001", "-campaign", "personal" - Directly enter the 00001 map of the personal campaign.
"-level", "00001", "-campaign", "keeporig" - Directly enter the 00001 map of the original keeper campaign.
  • In VS 2022, select the target to debug and click F5 to start debugging. Since only MingGW is working for now, you should choose "keeperfx_hvlog.exe (gdb)". The keeperfx_hvlog.exe is for MSVC/Clang-cl. image

Troubleshooting

  • CMake configure failed.

Try these approaches:

  1. Examine the Error List window. Fix all errors in CMakeLists.txt and CMakePresets.json if you modified them.
  2. In the Project menu, click "Delete Cache and Reconfigure".
  3. Close VS and delete the problematic configuration folder under out/build. Start VS again.

Technical information

  • The MinGW32 program additionally requires libgcc_s_dw2-1.dll and libwinpthread-1.dll, which can be found in C:\msys64\mingw32\bin. Changing VCPKG_TARGET_TRIPLET to x86-mingw-static could link these libs statically but it would also link all libs statically, resulting in very big exe files.
  • Now the zlib is a submodule in the codebase. However, it is not used to build the main zlib library. We only use it to build the contrib/minizip. If we use it to build the main zlib library, the CMake will delete the zconf.h, causing the original Makefile build system to fail (unless we add it back first).

Known issues

  • FTEST_DEBUG and CUnit-related code are not built.

Clone this wiki locally