A game programming exercise with SDL2, OpenGL, Vulkan...
- C++17 compiler (or newer)
- CMake 3.10 (or newer)
- vcpkg (optional)
- FreeType 2.9.1 (or newer)
- SDL 2.0.12 (or newer)
- Vulkan SDK
- To install a library
library_name
with enabled feature[feature]
for platformx64-windows
use the command syntax:
.\vcpkg.exe install library_name[feature] --triplet x64-windows`
- To install the required libraries:
.\vcpkg.exe install freetype sdl2[vulkan] --triplet x64-windows
- Mesa development libraries:
sudo apt-get install libgl1-mesa-dev
-
Vulkan SDK:
-
Install from LunarG web site:
-
Set environment variable
VULKAN_SDK
:export VULKAN_SDK="\usr"
-
-
Third party libraries with
vcpkg
on Linux:- Install command:
./vcpkg install freetype sdl2[vulkan]
- Set environment variable
VCPKG_ROOT
:Set the proper location if necessary.export VCPKG_ROOT="$HOME\Tools\vcpkg"
- Install command:
- Markdown (*.md)
- Documentation uses the Markdown format
- Recommended editor: Visual Studio Code's extension Markdown Preview Enhanced
- UML (*.puml)
- UML diagrams use PlantUML syntax
- Recomended editor: Visual Studio Code's extension: PlantUML
- Use ClangFormat to apply the format definitions from:
.clang-format
. - Use
// clang-format off
and// clang-format on
if necessary to prevent ClangFormat from changing the desired code formatting.
- Use trailing return type syntax in header files for function and method declarations.
- Related .cpp file header
- Current project headers
- Other projects' headers
- Third party libraries' headers
- System headers
- C Standard Library headers
- C++ Standard Library headers
- Identation is done by two space characters:
if (NOT DEFINED .MyVariable) set (.MyVariable "...") endif ()
- Function/Macro names start with a capital letter and use camel case names:
function (MyFunction ...)
- Function call syntax:
add_subdirectory ("SDL2") set_target_properties (SDL2 PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${LibraryArtifactsOutputDirectory}/${CMAKE_BUILD_TYPE}/${ANDROID_ABI}" LIBRARY_OUTPUT_DIRECTORY "${LibraryArtifactsOutputDirectory}/${CMAKE_BUILD_TYPE}/${ANDROID_ABI}" )
- Variable names start with
.
followed by a capital letter and use camel case names:set (.MyVariable ...)
- LocalVariables start with
__
followed by a capital letter and use camel case names and need to be unset when going out of scope:set (__MyLocalVariable ...) ... unset (_MyLocalVariable)
- Variables represinting a string are allways quoted:
set (.MyVariable "...") if ("${.MyVariable}" STREQUAL "...")
- Use
if (DEFINED <variable>)
to check if a variable is set:if (DEFINED .MyVariable) ... endif ()
- Use
if (<variable>)
to check if a variable has a non-empty value:if (.MyVariable) endif ()
-
Required Visual Studio Code extensions:
- C/C++ for Visual Studio Code
- CMake Tools
- CMake For VisualStudio Code
-
Configuring Visual Studio Code and the extensions:
- Select File->Preferences->Settings then search for the appropriate options, e.g. "cmake", etc.
-
CMake Tools settings:
- Recommended User/Workspace settings:
{ // The value to use in a configuration if "configurationProvider" is // either not specified or set to "${default}". "C_Cpp.default.configurationProvider": "vector-of-bool.cmake-tools", // The directory where CMake build files will go "cmake.buildDirectory": "${workspaceRoot}/../__build-output-${workspaceRootFolderName}/${generator}-${buildType}", // CMake variables to set on the command line "cmake.configureSettings": { "option_ENGINE_LIBRARY_AS_SHARED:BOOL": "YES" }, // The CMake generator to use "cmake.generator": "Ninja", // The directory where CMake installed files will go "cmake.installPrefix": "${workspaceRoot}/../__install-output-${workspaceRootFolderName}/${generator}-${buildType}", // The directory of the root CMakeLists.txt file "cmake.sourceDirectory": "${workspaceRoot}/${workspaceRootFolderName}", // Zoom the font of the editor when using mouse wheel and holding // `Ctrl`. "editor.mouseWheelZoom": true, // Render vertical rulers after a certain number of monospace // characters. Use multiple values for multiple rulers. No rulers are // drawn if array is empty. "editor.rulers": [ 80, 120 ], // Control the visibility of the menu bar. A setting of 'toggle' means // that the menu bar is hidden and a single press of the Alt key will // show it. By default, the menu bar will be visible, unless the window // is full screen. // - default: Menu is only hidden in full screen mode. // - visible: Menu is always visible even in full screen mode. // - toggle: Menu is hidden but can be displayed via Alt key. "window.menuBarVisibility": "toggle" }
- To execute CMake commands open the Command panel (CTRL + SHIFT + P) and search or the required command, e.g. "cmake", "cpp" or press F1.
{ "C_Cpp.default.configurationProvider": "vector-of-bool.cmake-tools", "cmake.buildDirectory": "${workspaceRoot}/../__build-output-${workspaceRootFolderName}/${generator}-${buildType}", "cmake.configureSettings": { "option_ENGINE_LIBRARY_AS_SHARED:BOOL": "YES" }, "cmake.generator": "Ninja", "cmake.installPrefix": "${workspaceRoot}/../__install-output-${workspaceRootFolderName}/${generator}-${buildType}", "cmake.sourceDirectory": "${workspaceRoot}/${workspaceRootFolderName}", "editor.mouseWheelZoom": true, "editor.rulers": [ 80, 120 ], "window.menuBarVisibility": "toggle" }
To pass command line arguments to the executing program in a CMake/Open Folder based project:
- In Solution Explorer right-click the top
CMakeLists.txt
and select Debug and Launch Settings->GunBox_Game.exe (Install) which will openlaunch.vs.json
- In
launch.vs.json
add"args": [ "--show-system-console", "--resolution: 640x480" ]
to"configurations"
: - Available command line options:
- --fullscreen
- --help
- --renderer: <OpenGL|Vulkan>[Debug]
- --resolution: <width>x<height>
- --show-system-console
Example:
{
"version": "0.2.1",
"defaults": {},
"configurations": [
{
"type": "default",
"name": "GunBox_Game.exe (Install)",
"project": "CMakeLists.txt",
"projectTarget": "GunBox_Game.exe (Install)",
"args": [
//"--renderer: OpenGL",
//"--renderer: Vulkan",
"--renderer: Vulkan[Debug]",
"--resolution: 640x480",
"--show-system-console"
]
}
]
}
Note: In case Visual Studio 2019 is used to open the project root folder and the root "CMakeLists.txt" is located in "GunBox/GunBox/CMakeLists.txt" it maybe necessary to change the project path as follows:
"project": "GunBox/CMakeLists.txt",
"program" points to the executable file, which could be set as:
- "program": "${command:cmake.launchTargetPath}"
- "program": "${workspaceRoot}/../__install-output-GunBox/Ninja-Debug/GunBox_Game"
Example (launch.json
):
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
"args": [
//"--renderer:", "OpenGL",
//"--renderer:", "OpenGL[Debug]",
//"--renderer:", "Vulkan",
"--renderer:", "Vulkan[Debug]",
"--resolution:", "640x480",
"--show-system-console"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Note: "args": []
need to be passed as separate array elements.
Example (settings.json
):
{
"cmake.buildDirectory": "${workspaceFolder}/../__build-output-${workspaceRootFolderName}",
"cmake.installPrefix": "${workspaceFolder}/../__install-output-${workspaceRootFolderName}",
"cmake.sourceDirectory": "${workspaceFolder}/GunBox",
"cmake.configureArgs": [
"-Doption_EngineLibraryAs_SHARED:BOOL=YES",
"-Doption_EnableLoggingLevel_Verbose:BOOL=YES",
],
}
mkdir output-build
mkdir output-install
cd output-build
cmake -G"Ninja" -DCMAKE_BUILD_TYPE=Debug -D"option_EngineLibraryAs_SHARED:BOOL=YES" -D"option_EnableLoggingLevel_Verbose:BOOL=YES" -D"CMAKE_INSTALL_PREFIX:STRING=$(dirname `pwd`)/output-install" ../GunBox/GunBox
cmake --build .
cmake --install .