Skip to content

Commit a15651f

Browse files
authored
Restore projects/CMake/CMakeLists.txt (#5191)
For some reason, #5181 deleted it. This project is intended to be a simple template to set up a project consuming raylib with CMake.
1 parent fbdf5e4 commit a15651f

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

projects/CMake/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
cmake_minimum_required(VERSION 3.11) # FetchContent is available in 3.11+
2+
project(example)
3+
4+
# Generate compile_commands.json
5+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6+
7+
# Dependencies
8+
set(RAYLIB_VERSION 5.5)
9+
find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED
10+
if (NOT raylib_FOUND) # If there's none, fetch and build raylib
11+
include(FetchContent)
12+
FetchContent_Declare(
13+
raylib
14+
DOWNLOAD_EXTRACT_TIMESTAMP OFF
15+
URL https://github.com/raysan5/raylib/archive/refs/tags/${RAYLIB_VERSION}.tar.gz
16+
)
17+
FetchContent_GetProperties(raylib)
18+
if (NOT raylib_POPULATED) # Have we downloaded raylib yet?
19+
set(FETCHCONTENT_QUIET NO)
20+
FetchContent_MakeAvailable(raylib)
21+
endif()
22+
endif()
23+
24+
# Our Project
25+
26+
add_executable(${PROJECT_NAME} core_basic_window.c)
27+
#set(raylib_VERBOSE 1)
28+
target_link_libraries(${PROJECT_NAME} raylib)
29+
30+
# Web Configurations
31+
if (${PLATFORM} STREQUAL "Web")
32+
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html") # Tell Emscripten to build an example.html file.
33+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s GL_ENABLE_GET_PROC_ADDRESS=1")
34+
endif()
35+
36+
# Checks if OSX and links appropriate frameworks (Only required on MacOS)
37+
if (APPLE)
38+
target_link_libraries(${PROJECT_NAME} "-framework IOKit")
39+
target_link_libraries(${PROJECT_NAME} "-framework Cocoa")
40+
target_link_libraries(${PROJECT_NAME} "-framework OpenGL")
41+
endif()

projects/CMake/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# raylib CMake Project
2+
3+
This provides a base project template which builds with [CMake](https://cmake.org).
4+
5+
## Usage
6+
7+
To compile the example, use one of the following dependending on your build target...
8+
9+
### Desktop
10+
11+
Use the following to build for desktop:
12+
13+
``` bash
14+
cmake -B build
15+
cmake --build build
16+
```
17+
18+
### Web
19+
20+
Compiling for the web requires the [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html):
21+
22+
``` bash
23+
mkdir build
24+
cd build
25+
emcmake cmake .. -DPLATFORM=Web -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXECUTABLE_SUFFIX=".html"
26+
emmake make
27+
```

projects/CMake/core_basic_window.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [core] example - Basic window (adapted for HTML5 platform)
4+
*
5+
* This example is prepared to compile for PLATFORM_WEB and PLATFORM_DESKTOP
6+
* As you will notice, code structure is slightly different to the other examples...
7+
* To compile it for PLATFORM_WEB just uncomment #define PLATFORM_WEB at beginning
8+
*
9+
* This example has been created using raylib 1.3 (www.raylib.com)
10+
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
11+
*
12+
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
13+
*
14+
********************************************************************************************/
15+
16+
#include "raylib.h"
17+
18+
#if defined(PLATFORM_WEB)
19+
#include <emscripten/emscripten.h>
20+
#endif
21+
22+
//----------------------------------------------------------------------------------
23+
// Global Variables Definition
24+
//----------------------------------------------------------------------------------
25+
int screenWidth = 800;
26+
int screenHeight = 450;
27+
28+
//----------------------------------------------------------------------------------
29+
// Module Functions Declaration
30+
//----------------------------------------------------------------------------------
31+
void UpdateDrawFrame(void); // Update and Draw one frame
32+
33+
//----------------------------------------------------------------------------------
34+
// Program main entry point
35+
//----------------------------------------------------------------------------------
36+
int main()
37+
{
38+
// Initialization
39+
//--------------------------------------------------------------------------------------
40+
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
41+
42+
#if defined(PLATFORM_WEB)
43+
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
44+
#else
45+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
46+
//--------------------------------------------------------------------------------------
47+
48+
// Main game loop
49+
while (!WindowShouldClose()) // Detect window close button or ESC key
50+
{
51+
UpdateDrawFrame();
52+
}
53+
#endif
54+
55+
// De-Initialization
56+
//--------------------------------------------------------------------------------------
57+
CloseWindow(); // Close window and OpenGL context
58+
//--------------------------------------------------------------------------------------
59+
60+
return 0;
61+
}
62+
63+
//----------------------------------------------------------------------------------
64+
// Module Functions Definition
65+
//----------------------------------------------------------------------------------
66+
void UpdateDrawFrame(void)
67+
{
68+
// Update
69+
//----------------------------------------------------------------------------------
70+
// TODO: Update your variables here
71+
//----------------------------------------------------------------------------------
72+
73+
// Draw
74+
//----------------------------------------------------------------------------------
75+
BeginDrawing();
76+
77+
ClearBackground(RAYWHITE);
78+
79+
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
80+
81+
EndDrawing();
82+
//----------------------------------------------------------------------------------
83+
}

0 commit comments

Comments
 (0)