Creating a Game in C++ (part 1)
I am Software Engineer, having many years of FrontEnd Development experience. In my spare time I wanted to create a Game, and also catch up with latest changes in C++. Most importantly, I wanted to try to use any “latest commonly agreed” best practices 😁.
So Let’s start…
Tools
I need full control of my code, so raylib (v5) is one the best options out there now — clean, simple, with good documentation and a plethora of examples. I am more proficient in C than C++, however I am going to use C++ and follow the suggestions from here. I need version control and since I have been using Git for almost 20 years, I will go with that for now. A C++ project needs a compiler and a build system, so I will use clang++ (v20) and CMake(v3.31) which is what raylib uses. I am not sure about any good sound libraries, and since I like simplicity and efficiency, I will go with whatever raylib already provides, which looks very simple and efficient (miniaudio).
Setup
For speeding up, let’s get a template to use:
git clone https://github.com/raysan5/raylib-game-template.git my-cpp-gameRemove any unecessary files:
cd my-cpp-game
rm src/screen*and create a boilerplate my-cpp-game.cpp:
#include "raylib.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
static const int screenWidth = 800;
static const int screenHeight = 450;
static void UpdateDrawFrame(void);
int main(void) {
InitWindow(screenWidth, screenHeight, "My Game");
InitAudioDevice();
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 60, 1);
#else
SetTargetFPS(60);
// Main game loop
while (!WindowShouldClose()) {
UpdateDrawFrame();
}
#endif
CloseAudioDevice();
CloseWindow();
return 0;
}
// Update and draw game frame
static void UpdateDrawFrame(void) {
BeginDrawing();
ClearBackground(RAYWHITE);
EndDrawing();
}After changing CMakeLists.txt to point to my-cpp-game and also use any *.cpp/*.hpp files, let’s see if it compiles:
cmake -S . -B build -DCMAKE_CXX_COMPILER=/usr/bin/clang++
cmake --build build -j4It took only 4s, so now let’s test if it works:
./build/my-game/my-gameSuccess!!!
Finally
As a final step let’s save our work:
git remote set-url origin <add_your_repo_url_here>
git add .
git commit -m 'raylib boilerplate project'
git pushAnd that’s it!
Note: By the way, you may find the project here.
Next Steps
On the next post, I will proceed with defining the game specs and designing the high level architecture.
Thanks for staying with me so far.
On to the next post — Creating a Game in C++ Part 2 …
