repos / gbc

GBC - Go B Compiler
git clone https://github.com/xplshn/gbc.git

gbc / examples
xplshn  ·  2025-08-13

raylib.b

B
  1// -*- mode: simpc -*-
  2// [b]: requires: -C linker_args='-lraylib'
  3
  4// To compile this example you need to download raylib from https://github.com/raysan5/raylib/releases
  5// Than pass appropriate linker flags to the b compiler.
  6// # Linux
  7//
  8// $ b raylib.b -L -L/path/to/raylib-version_linux_amd64/lib/ -L -l:libraylib.a -L -lm -run
  9//
 10// # Windows mingw32-w64
 11// > b -t gas-x86_64-windows raylib.b -L -L$HOME/opt/raylib-version_win64_mingw-w64/lib/ -L -l:libraylib.a -L -lwinmm -L -lgdi32 -run
 12
 13W;
 14
 15OBJ_X; OBJ_Y; OBJ_DX; OBJ_DY; OBJ_C; SIZE_OF_OBJ;
 16OBJS_COUNT;
 17OBJS;
 18
 19COLORS_COUNT 6;
 20COLORS [6]
 21    // B originally does not support hex literals actually.
 22    0xFF1818FF,
 23    0xFF18FF18,
 24    0xFFFF1818,
 25    0xFFFFFF18,
 26    0xFFFF18FF,
 27    0xFF18FFFF;
 28
 29update_obj(obj) {
 30    auto nx, ny;
 31
 32    nx = obj[OBJ_X] + obj[OBJ_DX];
 33    if (nx < 0 | nx + 100 >= GetScreenWidth()) {
 34        obj[OBJ_DX] = -obj[OBJ_DX];
 35        obj[OBJ_C] += 1;
 36        obj[OBJ_C] %= COLORS_COUNT;
 37    } else {
 38        obj[OBJ_X] = nx;
 39    }
 40
 41    ny = obj[OBJ_Y] + obj[OBJ_DY];
 42    if (ny < 0 | ny + 100 >= GetScreenHeight()) {
 43        obj[OBJ_DY] = -obj[OBJ_DY];
 44        obj[OBJ_C] += 1;
 45        obj[OBJ_C] %= COLORS_COUNT;
 46    } else {
 47        obj[OBJ_Y] = ny;
 48    }
 49}
 50
 51draw_obj(obj) DrawRectangle(obj[OBJ_X], obj[OBJ_Y], 100, 100, COLORS[obj[OBJ_C]]);
 52
 53main() {
 54    W = &0[1];
 55
 56    auto i;
 57    i = 0;
 58    OBJ_X       = i++;
 59    OBJ_Y       = i++;
 60    OBJ_DX      = i++;
 61    OBJ_DY      = i++;
 62    OBJ_C       = i++;
 63    SIZE_OF_OBJ = i++;
 64
 65    OBJS_COUNT = 10;
 66    OBJS = malloc(W*SIZE_OF_OBJ*OBJS_COUNT);
 67    i = 0; while (i < OBJS_COUNT) {
 68        OBJS[i*SIZE_OF_OBJ + OBJ_X]  = rand()%500;
 69        OBJS[i*SIZE_OF_OBJ + OBJ_Y]  = rand()%500;
 70        OBJS[i*SIZE_OF_OBJ + OBJ_DX] = rand()%10;
 71        OBJS[i*SIZE_OF_OBJ + OBJ_DY] = rand()%10;
 72        OBJS[i*SIZE_OF_OBJ + OBJ_C]  = rand()%COLORS_COUNT;
 73        ++i;
 74    }
 75
 76    auto paused;
 77    paused = 0;
 78
 79    InitWindow(800, 600, "Hello, from B");
 80    SetTargetFPS(60);
 81    while (!WindowShouldClose()) {
 82        if (IsKeyPressed(32)) {
 83            paused = !paused;
 84        }
 85
 86        if (!paused) {
 87            i = 0; while (i < OBJS_COUNT) {
 88                update_obj(&OBJS[(i++)*SIZE_OF_OBJ]);
 89            }
 90        }
 91
 92        BeginDrawing();
 93        ClearBackground(0xFF181818);
 94        i = 0; while (i < OBJS_COUNT) {
 95            draw_obj(&OBJS[(i++)*SIZE_OF_OBJ]);
 96        }
 97        EndDrawing();
 98    }
 99}
100
101// libc
102extrn malloc, rand;
103
104// Raylib
105extrn InitWindow, BeginDrawing, EndDrawing,
106      WindowShouldClose, ClearBackground,
107      SetTargetFPS, GetScreenWidth, GetScreenHeight,
108      DrawRectangle, IsKeyPressed;