Last active
September 1, 2023 18:59
-
-
Save xenobrain/07226cb6f0d735348a536f8bcf5ca651 to your computer and use it in GitHub Desktop.
Raylib with Web Target
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.24) | |
# Install CPM.cmake | |
set(CPM_VERSION 0.38.1) | |
set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/ext) | |
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_VERSION}.cmake") | |
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) | |
file(DOWNLOAD https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_VERSION}/CPM.cmake | |
${CPM_DOWNLOAD_LOCATION}) | |
endif() | |
include(${CPM_DOWNLOAD_LOCATION}) | |
if(NOT DEFINED ${PLATFORM}) | |
set(PLATFORM Desktop) | |
endif() | |
# Install and activate emsdk | |
if (${PLATFORM} STREQUAL Web) | |
set(EMSDK_VERSION 3.1.45) | |
CPMAddPackage("gh:emscripten-core/emsdk#${EMSDK_VERSION}") | |
if(NOT (EXISTS ${emsdk_SOURCE_DIR}/upstream)) | |
message("Activating emsdk") | |
if(WIN32) | |
set(EMSDK_CMD "${emsdk_SOURCE_DIR}/emsdk.bat") | |
else() | |
set(EMSDK_CMD "${emsdk_SOURCE_DIR}/emsdk") | |
endif() | |
execute_process(COMMAND ${EMSDK_CMD} install ${EMSDK_VERSION}) | |
execute_process(COMMAND ${EMSDK_CMD} activate ${EMSDK_VERSION}) | |
endif() | |
set(CMAKE_TOOLCHAIN_FILE ${emsdk_SOURCE_DIR}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake) | |
endif() | |
# Configure Project | |
project(20SGJ23 VERSION 0.0.1 LANGUAGES C CXX) | |
# Build Game Executable | |
add_executable(20SGJ23 main.cpp) | |
# Link with Raylib | |
CPMAddPackage(NAME raylib GITHUB_REPOSITORY raysan5/raylib GIT_TAG 4.5.0 OPTIONS "PLATFORM ${PLATFORM}" "CMAKE_BUILD_TYPE MinSizeRel") | |
target_include_directories(20SGJ23 PRIVATE ${raylib_SOURCE_DIR}/src) | |
target_link_libraries(20SGJ23 PRIVATE raylib) | |
# Configure Emscripten target | |
if(${PLATFORM} STREQUAL Web) | |
set_target_properties(20SGJ23 PROPERTIES | |
LINK_FLAGS "--preload-file ${CMAKE_SOURCE_DIR}/assets -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s TOTAL_MEMORY=67108864" | |
OUTPUT_NAME "index" | |
SUFFIX ".html") | |
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef EMSCRIPTEN | |
#include <emscripten.h> | |
#endif | |
#include <raylib.h> | |
constexpr static auto WINDOW_WIDTH = 900; | |
constexpr static auto WINDOW_HEIGHT = 600; | |
void Tick() { | |
BeginDrawing(); | |
{ | |
ClearBackground(RAYWHITE); | |
} | |
EndDrawing(); | |
} | |
int main() { | |
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "20SGJ23"); | |
#ifdef EMSCRIPTEN | |
emscripten_set_main_loop(Tick, 60, 1); | |
#else | |
while (!WindowShouldClose()) { | |
Tick(); | |
} | |
#endif | |
CloseWindow(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add -DPLATFORM=Web to cmake
also you should have a folder called 'assets' in your base directory that's not empty