Skip to content

Commit d3b22a4

Browse files
authored
Merge pull request #438 from Spookdot/master
Add example template for CMake with C++ Modules
2 parents 798b4c3 + cb2c99a commit d3b22a4

5 files changed

Lines changed: 228 additions & 0 deletions

File tree

‎projects/CMakeModules/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
cmake_minimum_required(VERSION 3.28)
2+
project(raylib_cpp_example)
3+
4+
# raylib
5+
find_package(raylib QUIET)
6+
if (NOT raylib_FOUND)
7+
include(FetchContent)
8+
FetchContent_Declare(
9+
raylib
10+
GIT_REPOSITORY https://github.com/raysan5/raylib.git
11+
GIT_TAG 6.0
12+
GIT_SHALLOW 1
13+
)
14+
FetchContent_MakeAvailable(raylib)
15+
endif()
16+
17+
# raylib-cpp
18+
find_package(raylib_cpp QUIET)
19+
if (NOT raylib_cpp_FOUND)
20+
if (NOT DEFINED RAYLIB_CPP_VERSION)
21+
set(RAYLIB_CPP_VERSION master)
22+
endif()
23+
include(FetchContent)
24+
FetchContent_Declare(
25+
raylib_cpp
26+
GIT_REPOSITORY https://github.com/RobLoach/raylib-cpp.git
27+
GIT_TAG ${RAYLIB_CPP_VERSION}
28+
)
29+
FetchContent_MakeAvailable(raylib_cpp)
30+
endif()
31+
32+
# This is the main part:
33+
set(SOURCES main.cpp)
34+
add_executable(${PROJECT_NAME} ${SOURCES})
35+
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20)
36+
target_link_libraries(${PROJECT_NAME} PUBLIC raylib PUBLIC raylib_cpp_modules)
37+
38+
# Web Configurations
39+
if ("${PLATFORM}" STREQUAL "Web")
40+
# Tell Emscripten to build an example.html file.
41+
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
42+
43+
# Required linker flags for using Raylib with Emscripten
44+
target_link_options(${PROJECT_NAME} PRIVATE -sEXPORTED_FUNCTIONS=['_main','_malloc'] -sEXPORTED_RUNTIME_METHODS=ccall -sUSE_GLFW=3)
45+
endif()
46+
47+
# That's it! You should have an example executable that you can run. Have fun!
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"$schema": "https://cmake.org/cmake/help/latest/_downloads/3e2d73bff478d88a7de0de736ba5e361/schema.json",
3+
"version": 11,
4+
"configurePresets": [
5+
{
6+
"name": "shared",
7+
"hidden": true,
8+
"cacheVariables": {
9+
"BUILD_RAYLIB_CPP_MODULES": "ON",
10+
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
11+
"CMAKE_CXX_STANDARD": "20"
12+
}
13+
},
14+
{
15+
"name": "Web",
16+
"hidden": true,
17+
"cacheVariables": {
18+
"PLATFORM": "Web"
19+
}
20+
},
21+
{
22+
"name": "Debug",
23+
"generator": "Ninja",
24+
"binaryDir": "${sourceDir}/build/Debug",
25+
"inherits": ["shared"],
26+
"cacheVariables": {
27+
"CMAKE_BUILD_TYPE": "Debug",
28+
"CMAKE_EXPORT_COMPILE_COMMANDS": "1"
29+
}
30+
},
31+
{
32+
"name": "DebugWeb",
33+
"binaryDir": "${sourceDir}/build/DebugWeb",
34+
"inherits": ["Web", "Debug"]
35+
},
36+
{
37+
"name": "Release",
38+
"generator": "Ninja",
39+
"binaryDir": "${sourceDir}/build/Release",
40+
"inherits": ["shared"],
41+
"cacheVariables": {
42+
"CMAKE_BUILD_TYPE": "Release"
43+
}
44+
},
45+
{
46+
"name": "ReleaseWeb",
47+
"binaryDir": "${sourceDir}/build/ReleaseWeb",
48+
"inherits": ["Web", "Release"]
49+
}
50+
],
51+
"buildPresets": [
52+
{
53+
"name": "Debug",
54+
"configurePreset": "Debug"
55+
},
56+
{
57+
"name": "DebugWeb",
58+
"configurePreset": "DebugWeb"
59+
},
60+
{
61+
"name": "Release",
62+
"configurePreset": "Release"
63+
},
64+
{
65+
"name": "ReleaseWeb",
66+
"configurePreset": "ReleaseWeb"
67+
}
68+
]
69+
}

‎projects/CMakeModules/README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# raylib-cpp CMake Example Project
2+
3+
Use this template to build a [raylib-cpp](https://github.com/RobLoach/raylib-cpp) project using [CMake](https://cmake.org).
4+
5+
## Build
6+
7+
To build this project, make sure to have CMake installed locally.
8+
9+
### Desktop
10+
11+
```
12+
mkdir build
13+
cmake . --preset Release
14+
cmake --build build/Release --preset Release
15+
```
16+
17+
### Web
18+
19+
```
20+
mkdir build
21+
emcmake cmake . --preset ReleaseWeb
22+
emcmake cmake --build build/ReleaseWeb --preset ReleaseWeb
23+
```
24+
25+
## Run
26+
27+
```
28+
./build/Release/raylib-cpp-example
29+
```

‎projects/CMakeModules/main.cpp‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib-cpp [core] example - Basic window with C++ Modules (adapted for HTML5 platform)
4+
*
5+
* This example is prepared to compile for PLATFORM_WEB and PLATFORM_DESKTOP
6+
* It was adapted from the raylib-cpp [core] example - Basic Window
7+
* with changes made to allow for compiling with C++ modules
8+
* To compile it for PLATFORM_WEB just use the --preset DebugWeb or ReleaseWeb in
9+
* CMakePresets.json
10+
*
11+
* This example has been created using raylib-cpp (www.raylib.com)
12+
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
13+
*
14+
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
15+
*
16+
********************************************************************************************/
17+
18+
#if defined(PLATFORM_WEB)
19+
#include <emscripten/emscripten.h>
20+
#endif
21+
22+
import raylib;
23+
24+
using raylib::DrawText;
25+
using raylib::Color;
26+
27+
//----------------------------------------------------------------------------------
28+
// Global Variables Definition
29+
//----------------------------------------------------------------------------------
30+
int screenWidth = 800;
31+
int screenHeight = 450;
32+
static raylib::Window window(screenWidth, screenHeight, "raylib-cpp [core] example - basic window");
33+
34+
//----------------------------------------------------------------------------------
35+
// Module Functions Declaration
36+
//----------------------------------------------------------------------------------
37+
void UpdateDrawFrame(); // Update and Draw one frame
38+
39+
//----------------------------------------------------------------------------------
40+
// Main Enry Point
41+
//----------------------------------------------------------------------------------
42+
int main()
43+
{
44+
// Initialization
45+
//--------------------------------------------------------------------------------------
46+
// raylib::Window window(screenWidth, screenHeight, "raylib-cpp [core] example - basic window");
47+
48+
#if defined(PLATFORM_WEB)
49+
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
50+
#else
51+
window.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
52+
//--------------------------------------------------------------------------------------
53+
54+
// Main game loop
55+
while (!window.ShouldClose()) // Detect window close button or ESC key
56+
{
57+
UpdateDrawFrame();
58+
}
59+
#endif
60+
61+
return 0;
62+
}
63+
64+
//----------------------------------------------------------------------------------
65+
// Module Functions Definition
66+
//----------------------------------------------------------------------------------
67+
void UpdateDrawFrame()
68+
{
69+
// Update
70+
//----------------------------------------------------------------------------------
71+
// TODO: Update your variables here
72+
//----------------------------------------------------------------------------------
73+
74+
// Draw
75+
//----------------------------------------------------------------------------------
76+
while (window.Drawing()) {
77+
window.ClearBackground(Color::RayWhite());
78+
79+
DrawText("Congrats! You created your first raylib-cpp window!", 160, 200, 20, Color::LightGray());
80+
}
81+
//----------------------------------------------------------------------------------
82+
}

0 commit comments

Comments
 (0)