CMake, Setting Up a Raylib Project

1
2
3
int main() {
   puts("I can C you");
}

As a follow up to last years post about automatically downloading and integrating SDL2 into your projects using CMake I thought I would do a follow up. This time using RayLib.

This year maybe the year of the dependency managers in C++ but I have yet to investigate them. Honestly I am waiting for them to mature and have their quirks knocked out of them. I mean CMake, apparently, only really became bearable at V3 . Some would say it was still not good but as every IDE seems to support it at some point I decided to just go with the flow.

Not surprisingly setting up a Raylib is very similar in approach to my previous project template using SDL2. In fact pretty much there were only name changes! Here is is in its full glory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)


# Pulling down SDL2
include(FetchContent)

FetchContent_Declare(
        RAYLIB
        GIT_REPOSITORY "https://github.com/raysan5/raylib"
        GIT_TAG 3.0.0
)

FetchContent_GetProperties(RAYLIB)
if (NOT raylib_POPULATED)
    FetchContent_Populate(RAYLIB)
    add_subdirectory(${raylib_SOURCE_DIR} ${raylib_BINARY_DIR})
endif ()

project(game1)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(game
        src/main.cpp
        )

target_link_libraries(game  raylib m)
target_compile_options(game PRIVATE -Wall -Wextra)
# TODO eventually remove this
target_include_directories(game PRIVATE ${raylib_SOURCE_DIRS}/include)

In main.cpp I just put a really basic example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include "raylib.h"

int main()
{
    InitWindow(600, 600, "Hello sailor");
    SetTargetFPS(60);

    while (!WindowShouldClose())
    {
        BeginDrawing();
        ClearBackground(RAYWHITE);
        DrawText("There is a radio playing in my head", 190, 200, 20, LIGHTGRAY);
        EndDrawing();
    }

    CloseWindow();


    return 0;
}

And there you have it. Only tested on Linux but Raylib and CMake are cross-platform so hopefully it should work pretty much everywhere. I haven’t investigated what you need to do to cross-build a WASM build either. As a project template it seem work pretty well, at least for me.


332 Words

2020-05-24 23:00 +0000