Raylib graphical application
Install or build the shared raylib 6.0 library for your OS and architecture. In the raylib source distribution build with cmake -S . -B build -DBUILD_SHARED_LIBS=ON -DBUILD_EXAMPLES=OFF and cmake --build build. Provide the actual path to the shared library when invoking Atom. A desktop display and compatible raylib graphics backend are needed to open a window. raylib 6.0 release
Save this reusable FFI module as ray.atom.
Match raylib’s record layouts
The fields follow the order and widths of the C records. The rest of the module wraps the library’s drawing and window functions.
export const Color = binary.layout({r = u8, g = u8, b = u8, a = u8})
export const Rectangle = binary.layout({x = f32, y = f32, width = f32, height = f32})
Show full source 42 lines
use binary
use ffi
export const Color = binary.layout({r = u8, g = u8, b = u8, a = u8})
export const Rectangle = binary.layout({x = f32, y = f32, width = f32, height = f32})
export func color(r, g, b, a = 255):
pixel = create(Color)
pixel.r = r
pixel.g = g
pixel.b = b
pixel.a = a
return pixel
export func rectangle(x, y, width, height):
r = create(Rectangle)
r.x = x
r.y = y
r.width = width
r.height = height
return r
export func open_graphics(filename):
library = ffi.load(filename)
try:
ffi.bind(library, 'InitWindow', [i32, i32, cstring], void)
ffi.bind(library, 'SetConfigFlags', [u32], void)
ffi.bind(library, 'IsWindowReady', [], bool)
ffi.bind(library, 'WindowShouldClose', [], bool)
ffi.bind(library, 'CloseWindow', [], void)
ffi.bind(library, 'SetTargetFPS', [i32], void)
ffi.bind(library, 'BeginDrawing', [], void)
ffi.bind(library, 'EndDrawing', [], void)
ffi.bind(library, 'ClearBackground', [Color], void)
ffi.bind(library, 'DrawRectangleRec', [Rectangle, Color], void)
ffi.bind(library, 'DrawText', [cstring, i32, i32, i32, Color], void)
ffi.bind(library, 'IsKeyPressed', [i32], bool)
ffi.bind(library, 'CheckCollisionRecs', [Rectangle, Rectangle], bool)
return library
catch problem:
ffi.close(library)
throw problem
Save as window.atom next to ray.atom. Run atom run window.atom -- /path/to/libraylib.so. Pass --verify-window after the library path to draw three frames in a hidden window. Pass --verify for an ABI smoke check using raylib's rectangle collision function without opening a window.
Draw one frame at a time
Begin and end each frame as a pair. The finally block ends the frame even if a drawing call raises an error.
while not ray.WindowShouldClose():
ray.BeginDrawing()
try:
ray.ClearBackground(night)
ray.DrawRectangleRec(banner, light)
ray.DrawText('Atom and raylib', 32, 35, 28, light)
finally:
ray.EndDrawing()
frames += 1
if mode == '--verify-window' and frames == 3:
break
Show full source 35 lines
use './ray.atom' as gfx
use ffi
use os
ray = gfx.open_graphics(os.args[0])
try:
mode = get(os.args, 1, '')
if mode == '--verify':
assert(ray.CheckCollisionRecs(gfx.rectangle(0, 0, 5, 5), gfx.rectangle(4, 4, 5, 5)))
return
if mode == '--verify-window':
ray.SetConfigFlags(128) // FLAG_WINDOW_HIDDEN
ray.InitWindow(480, 240, 'Atom and raylib')
assert(ray.IsWindowReady())
try:
ray.SetTargetFPS(60)
night = gfx.color(19, 30, 46)
light = gfx.color(220, 241, 244)
banner = gfx.rectangle(32, 74, 416, 6)
frames = 0
while not ray.WindowShouldClose():
ray.BeginDrawing()
try:
ray.ClearBackground(night)
ray.DrawRectangleRec(banner, light)
ray.DrawText('Atom and raylib', 32, 35, 28, light)
finally:
ray.EndDrawing()
frames += 1
if mode == '--verify-window' and frames == 3:
break
finally:
ray.CloseWindow()
finally:
ffi.close(ray)