cmake: Update CMake project template with docs and web (#2274)

This commit is contained in:
Rob Loach 2022-01-10 02:55:32 -05:00 committed by GitHub
parent a6aa5a1e4c
commit c0da80c2b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 49 deletions

View File

@ -1,44 +1,38 @@
cmake_minimum_required(VERSION 3.11) # FetchContent is available in 3.11+ cmake_minimum_required(VERSION 3.11) # FetchContent is available in 3.11+
project(example) project(example)
# Set this to the minimal version you want to support # Dependencies
find_package(raylib 3.0 QUIET) # Let CMake search for a raylib-config.cmake find_package(raylib 4.0.0 QUIET) # QUIET or REQUIRED
# You could change the QUIET above to REQUIRED and remove this if() clause
# This part downloads raylib and builds it if it's not installed on your system
if (NOT raylib_FOUND) # If there's none, fetch and build raylib if (NOT raylib_FOUND) # If there's none, fetch and build raylib
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
raylib raylib
URL https://github.com/raysan5/raylib/archive/master.tar.gz URL https://github.com/raysan5/raylib/archive/refs/tags/4.0.0.tar.gz
) )
FetchContent_GetProperties(raylib) FetchContent_GetProperties(raylib)
if (NOT raylib_POPULATED) # Have we downloaded raylib yet? if (NOT raylib_POPULATED) # Have we downloaded raylib yet?
set(FETCHCONTENT_QUIET NO) set(FETCHCONTENT_QUIET NO)
FetchContent_Populate(raylib) FetchContent_Populate(raylib)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples
# build raylib
add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR}) add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR})
endif()
endif() endif()
endif() # Our Project
# This is the main part:
add_executable(${PROJECT_NAME} core_basic_window.c) add_executable(${PROJECT_NAME} core_basic_window.c)
#set(raylib_VERBOSE 1) #set(raylib_VERBOSE 1)
target_link_libraries(${PROJECT_NAME} raylib) target_link_libraries(${PROJECT_NAME} raylib)
# Web Configurations
if (${PLATFORM} STREQUAL "Web")
# Tell Emscripten to build an example.html file.
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
endif()
# Checks if OSX and links appropriate frameworks (Only required on MacOS) # Checks if OSX and links appropriate frameworks (Only required on MacOS)
if (APPLE) if (APPLE)
target_link_libraries(${PROJECT_NAME} "-framework IOKit") target_link_libraries(${PROJECT_NAME} "-framework IOKit")
target_link_libraries(${PROJECT_NAME} "-framework Cocoa") target_link_libraries(${PROJECT_NAME} "-framework Cocoa")
target_link_libraries(${PROJECT_NAME} "-framework OpenGL") target_link_libraries(${PROJECT_NAME} "-framework OpenGL")
endif() endif()
# That's it! You should have an example executable that you can run. Have fun!

29
projects/CMake/README.md Normal file
View File

@ -0,0 +1,29 @@
# raylib CMake Project
This provides a base project template which builds with [CMake](https://cmake.org).
## Usage
To compile the example, use one of the following dependending on your build target...
### Desktop
Use the following to build for desktop:
``` bash
mkdir build
cd build
cmake ..
make
```
### Web
Compiling for the web requires the [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html):
``` bash
mkdir build
cd build
emcmake cmake .. -DPLATFORM=Web -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXE_LINKER_FLAGS="-s USE_GLFW=3" -DCMAKE_EXECUTABLE_SUFFIX=".html"
emmake make
```

View File

@ -1,40 +1,69 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [core] example - Basic window * raylib [core] example - Basic window (adapted for HTML5 platform)
* *
* Welcome to raylib! * This example is prepared to compile for PLATFORM_WEB, PLATFORM_DESKTOP and PLATFORM_RPI
* As you will notice, code structure is slightly diferent to the other examples...
* To compile it for PLATFORM_WEB just uncomment #define PLATFORM_WEB at beginning
* *
* To test examples, just press F6 and execute raylib_compile_execute script * This example has been created using raylib 1.3 (www.raylib.com)
* Note that compiled executable is placed in the same folder as .c file
*
* You can find all basic examples on C:\raylib\raylib\examples folder or
* raylib official webpage: www.raylib.com
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5) * Copyright (c) 2015 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
void UpdateDrawFrame(void); // Update and Draw one frame
//----------------------------------------------------------------------------------
// Main Enry Point
//----------------------------------------------------------------------------------
int main() int main()
{ {
// Initialization // Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60); #if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
{
UpdateDrawFrame();
}
#endif
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
void UpdateDrawFrame(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -52,11 +81,3 @@ int main()
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

View File

@ -6,7 +6,7 @@ IDE | Platform(s) | Source | Example(s)
----| ------------| :-------: | :-----: ----| ------------| :-------: | :-----:
[4coder](http://4coder.net/) | Windows | ❌ | ✔️ [4coder](http://4coder.net/) | Windows | ❌ | ✔️
[Builder](https://wiki.gnome.org/Apps/Builder) | Linux | ❌ | ✔️ [Builder](https://wiki.gnome.org/Apps/Builder) | Linux | ❌ | ✔️
[CMake](https://cmake.org/) | Windows, Linux, macOS | ✔️ | ✔️ [CMake](https://cmake.org/) | Windows, Linux, macOS, Web | ✔️ | ✔️
[CodeBlocks](http://www.codeblocks.org/) | Windows, Linux, macOS | ❌ | ✔️ [CodeBlocks](http://www.codeblocks.org/) | Windows, Linux, macOS | ❌ | ✔️
[Geany](https://www.geany.org/) | Windows, Linux | ✔️ | ✔️ [Geany](https://www.geany.org/) | Windows, Linux | ✔️ | ✔️
[Notepad++](https://notepad-plus-plus.org/) | Windows | ✔️ | ✔️ [Notepad++](https://notepad-plus-plus.org/) | Windows | ✔️ | ✔️