On this page
  1. Run it
  2. The code

Plain C

The opt-out. A C entry point with your own main(): raylib, plus two headers of ours that are C -- <rmp/config.h> for the [window] values and <smoke_test.h> for the CI hooks -- and none of rmp::. You keep every build target but two and lose the runtime layer: iOS and Web are the two you give up, because on those the loop is not yours to write, and the file says so. Its top lines are the recipe, which CI follows in a game made with rmp new. Built as C99 and booted like the rest; a release of a plain C game ships its loose resources/, which raw raylib reads, instead of the pack it cannot.

Plain C, as it starts

Not playable here

It writes its own while loop, which a browser cannot run without ASYNCIFY — the web is one of the targets it gives up, and the file says so.

Run it

From a clone of the framework:

rmp example plain_c

The code

Every file of examples/plain_c, as it is at the commit this site documents (on GitHub).

src/main.c 117 lines
// ===========================================================================
// examples/plain_c -- the framework with none of the framework.
//
// Plain C, your own main(), raylib and nothing of ours but the CI hooks. No
// rmp::assets, no scenes, no entry-point macro. What you keep is everything
// the framework does *around* your code, which is the part that is hard to
// reproduce: the build targets, the pinned toolchains, the icons and
// identifiers generated from raylib_multiplatform.toml, the release pipeline.
//
// To use it, in a game made with `rmp new`, from the game's folder:
//
//     rm src/main.cpp && rm -r src/rmp/ src/scenes/ tests/game/
//     cp <the framework>/examples/plain_c/src/main.c src/
//     rmp run
//
// src/ is globbed by all four build systems (CMake, the Android CMakeLists,
// XcodeGen, Emscripten), so nothing else needs editing. src/rmp/ is the
// framework itself, and it goes because nothing here uses it: CMakeLists.txt
// builds the framework library from it whenever it has sources, while a src/
// with no C++ in it gets an executable linked to raylib alone -- so kept, it
// is the whole C++ framework compiled for nothing on the desktop, and
// compiled into the game on Android and iOS, whose builds take everything
// under src/. With it gone there is no framework library at all. tests/game/
// goes with src/scenes/: it tests the main menu, through the framework. Keep
// include/rmp/: this file reads [window] from <rmp/config.h>, the one header
// of ours that is plain #defines. The rmp new CI job follows the first two
// lines in a fresh game and boots what they make.
//
// What you give up, and what replaces it:
//
//   assets::Load*          ->  LoadTexture(RMP_RESOURCES_PATH "x.png"), as below
//   the resource pack      ->  gone. Loose files only: a release ships
//                              resources/ as it is, with a PACK_SKIPPED.txt
//                              saying why (tools/ship_resources.sh), because
//                              raw raylib cannot read resources.rres -- reading
//                              it is what src/rmp/assets/loader_hook.cpp was doing.
//   RMP_WINDOW_TITLE, ...  ->  #include <rmp/config.h>, as below: [window] and
//                              the rest of the .toml as plain #defines, in C.
//   the smoke-test hooks   ->  <smoke_test.h>, as below. It is C, it is on the
//                              include path, and every CI boot greps the lines it
//                              prints: a boot, a frame read back, a clean exit.
//
// TWO PLATFORMS DO NOT RUN THIS FILE AS IT STANDS, and both for the same
// reason: the `while` loop below assumes your code owns the frame loop, and on
// those two it does not.
//
//   iOS   does not call main() at all. The run loop belongs to UIKit, and
//         raylib's iOS backend calls ios_ready/ios_update/ios_destroy.
//   Web   has no loop either: the browser calls you back, through
//         emscripten_set_main_loop(). A `while` loop there freezes the tab
//         unless the whole binary is built with -s ASYNCIFY, which is a real
//         cost in size and speed and which this project does not pay.
//
// Papering over exactly that is what RMP_ENTRY_POINT is for -- it writes the
// right loop for each target and calls the same three hooks. So if you want
// iOS or Web, either keep the framework's entry point (which is one include and
// one line, and costs you nothing else) or write those two runners yourself.
// Linux, Windows, macOS, the BSDs and Android run this as it is.
// ===========================================================================

#include <raylib.h>
#include <rmp/config.h> // [window] from raylib_multiplatform.toml, as #defines
#include <smoke_test.h> // the CI hooks: no-ops unless RAY_TEST_MAX_FRAMES is set

// RMP_RESOURCES_PATH is defined by CMake, not by us: an absolute path to
// resources/ in a development build, "./resources/" in a release, "" on
// Android (where assets sit at the root of the APK). Build every asset path
// out of it and the same source works on all of them.

int main(void) {
    SmokeTest_Begin();
#if RMP_PRODUCTION_BUILD && !defined(PLATFORM_ANDROID) && !defined(__EMSCRIPTEN__)
    // A release reads "./resources/", and "." has to be the executable's folder,
    // not wherever it was started from -- a file manager, a shortcut, another
    // terminal. Only when resources/ is THERE, though: `rmp build release`
    // leaves the executable in build/ and resources/ beside build/, and run
    // from the project folder it reads that one. The framework's entry point
    // does the same for a C++ game (enter_executable_folder() in
    // src/rmp/app/app.cpp); here it is yours. GetApplicationDirectory() is "" on
    // NetBSD and OpenBSD, which cannot say where an executable is: there a
    // release reads the working directory's resources/.
    const char *folder = GetApplicationDirectory();
    if (folder[0] != '\0' &&
        DirectoryExists(TextFormat("%s%s", folder, RMP_RESOURCES_PATH))) {
        ChangeDirectory(folder);
    }
#endif
    // [window] in the .toml, the way RMP_GAME opens it: resizable, vsync if it
    // says so, and its size and title. CI checks the window was asked for the
    // vsync the .toml names.
    SetConfigFlags(RMP_WINDOW_VSYNC ? FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT
                                    : FLAG_WINDOW_RESIZABLE);
    InitWindow(RMP_WINDOW_WIDTH, RMP_WINDOW_HEIGHT, RMP_WINDOW_TITLE);

    Texture2D rabbit = LoadTexture(RMP_RESOURCES_PATH "rabbit.png");
    // One asset asked for, and failed if it came back empty: what the boot
    // line reports and every CI boot requires to be assets_failed=0.
    SmokeTest_ReportBoot(rabbit.id == 0 ? 1 : 0, 1);

    // The window's X ends it, and under CI so does the frame budget.
    while (!WindowShouldClose() && !SmokeTest_Done()) {
        BeginDrawing();
        ClearBackground(RAYWHITE);

        DrawTexture(rabbit, GetScreenWidth() / 2 - rabbit.width / 2,
                    GetScreenHeight() / 2 - rabbit.height / 2, WHITE);
        DrawText("Raylib is Multiplatform!", 190, 200, 20, LIGHTGRAY);

        SmokeTest_CaptureFrame(); // between the last draw and EndDrawing()
        EndDrawing();
        SmokeTest_Tick();
    }

    UnloadTexture(rabbit);
    CloseWindow();
    return 0;
}