Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/CMakeModules/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
47 changes: 47 additions & 0 deletions projects/CMakeModules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cmake_minimum_required(VERSION 3.28)
project(raylib_cpp_example)

# raylib
find_package(raylib QUIET)
if (NOT raylib_FOUND)
include(FetchContent)
FetchContent_Declare(
raylib
GIT_REPOSITORY https://github.com/raysan5/raylib.git
GIT_TAG 6.0
GIT_SHALLOW 1
)
FetchContent_MakeAvailable(raylib)
endif()

# raylib-cpp
find_package(raylib_cpp QUIET)
if (NOT raylib_cpp_FOUND)
if (NOT DEFINED RAYLIB_CPP_VERSION)
set(RAYLIB_CPP_VERSION master)
endif()
include(FetchContent)
FetchContent_Declare(
raylib_cpp
GIT_REPOSITORY https://github.com/RobLoach/raylib-cpp.git
GIT_TAG ${RAYLIB_CPP_VERSION}
)
FetchContent_MakeAvailable(raylib_cpp)
endif()

# This is the main part:
set(SOURCES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20)
target_link_libraries(${PROJECT_NAME} PUBLIC raylib PUBLIC raylib_cpp_modules)

# Web Configurations
if ("${PLATFORM}" STREQUAL "Web")
# Tell Emscripten to build an example.html file.
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")

# Required linker flags for using Raylib with Emscripten
target_link_options(${PROJECT_NAME} PRIVATE -sEXPORTED_FUNCTIONS=['_main','_malloc'] -sEXPORTED_RUNTIME_METHODS=ccall -sUSE_GLFW=3)
endif()

# That's it! You should have an example executable that you can run. Have fun!
69 changes: 69 additions & 0 deletions projects/CMakeModules/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"$schema": "https://cmake.org/cmake/help/latest/_downloads/3e2d73bff478d88a7de0de736ba5e361/schema.json",
"version": 11,
"configurePresets": [
{
"name": "shared",
"hidden": true,
"cacheVariables": {
"BUILD_RAYLIB_CPP_MODULES": "ON",
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
"CMAKE_CXX_STANDARD": "20"
}
},
{
"name": "Web",
"hidden": true,
"cacheVariables": {
"PLATFORM": "Web"
}
},
{
"name": "Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/Debug",
"inherits": ["shared"],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "1"
}
},
{
"name": "DebugWeb",
"binaryDir": "${sourceDir}/build/DebugWeb",
"inherits": ["Web", "Debug"]
},
{
"name": "Release",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/Release",
"inherits": ["shared"],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "ReleaseWeb",
"binaryDir": "${sourceDir}/build/ReleaseWeb",
"inherits": ["Web", "Release"]
}
],
"buildPresets": [
{
"name": "Debug",
"configurePreset": "Debug"
},
{
"name": "DebugWeb",
"configurePreset": "DebugWeb"
},
{
"name": "Release",
"configurePreset": "Release"
},
{
"name": "ReleaseWeb",
"configurePreset": "ReleaseWeb"
}
]
}
29 changes: 29 additions & 0 deletions projects/CMakeModules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# raylib-cpp CMake Example Project

Use this template to build a [raylib-cpp](https://github.com/RobLoach/raylib-cpp) project using [CMake](https://cmake.org).

## Build

To build this project, make sure to have CMake installed locally.

### Desktop

```
mkdir build
cmake . --preset Release
cmake --build build/Release --preset Release
```

### Web

```
mkdir build
emcmake cmake . --preset ReleaseWeb
emcmake cmake --build build/ReleaseWeb --preset ReleaseWeb
```

## Run

```
./build/Release/raylib-cpp-example
```
82 changes: 82 additions & 0 deletions projects/CMakeModules/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*******************************************************************************************
*
* raylib-cpp [core] example - Basic window with C++ Modules (adapted for HTML5 platform)
*
* This example is prepared to compile for PLATFORM_WEB and PLATFORM_DESKTOP
* It was adapted from the raylib-cpp [core] example - Basic Window
* with changes made to allow for compiling with C++ modules
* To compile it for PLATFORM_WEB just use the --preset DebugWeb or ReleaseWeb in
* CMakePresets.json
*
* This example has been created using raylib-cpp (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif

import raylib;

using raylib::DrawText;
using raylib::Color;

//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
static raylib::Window window(screenWidth, screenHeight, "raylib-cpp [core] example - basic window");

//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
void UpdateDrawFrame(); // Update and Draw one frame

//----------------------------------------------------------------------------------
// Main Enry Point
//----------------------------------------------------------------------------------
int main()
{
// Initialization
//--------------------------------------------------------------------------------------
// raylib::Window window(screenWidth, screenHeight, "raylib-cpp [core] example - basic window");

#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
window.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

// Main game loop
while (!window.ShouldClose()) // Detect window close button or ESC key
{
UpdateDrawFrame();
}
#endif

return 0;
}

//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
void UpdateDrawFrame()
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
while (window.Drawing()) {
window.ClearBackground(Color::RayWhite());

DrawText("Congrats! You created your first raylib-cpp window!", 160, 200, 20, Color::LightGray());
}
//----------------------------------------------------------------------------------
}