From InitWindow to pixels on screen

An interactive tour of how raylib turns your C game loop into windowed graphics and audio. raylib is a library — you own main() and the loop. This guide follows one frame from initialization through batched 2D draws, optional 3D mesh rendering, and input polling.

The mental model in one paragraph

raylib is a modular C library built around two mandatory pieces — rcore (window, input, timing) and rlgl (OpenGL abstraction) — plus optional modules for shapes, textures, text, 3D models, and audio. You call InitWindow(), which boots a platform backend (GLFW by default), creates an OpenGL context, and initializes rlgl's default shader and render batch. Each frame you call BeginDrawing(), issue draw calls that accumulate geometry into a batch, then EndDrawing() flushes the batch, swaps buffers, limits FPS, and polls input. There is no built-in scene graph or entity system — just functions.

Interactive architecture map

Tap a module to see what it does and how it fits. Mandatory modules are marked.

Module

Select a module above.

Frame loop simulator

What EndDrawing() does each frame on desktop (when SUPPORT_CUSTOM_FRAME_CONTROL is off). Use Step or Play on mobile.

0Frame #
Poll inputCurrent phase
0Batch vertices

Layer diagram

  YOUR CODE                    RAYLIB MODULES                 PLATFORM / GPU
 ┌─────────────┐            ┌──────────────────┐           ┌─────────────────┐
 │ main()      │  InitWindow│ rcore.c          │InitPlatform│ rcore_desktop_  │
 │ while loop  │───────────►│  CORE global     │───────────►│ glfw.c          │
 │ Draw*()     │            │  BeginDrawing    │           │  GLFW window    │
 └─────────────┘            │  EndDrawing      │           │  GL context     │
       │                    └────────┬─────────┘           └────────┬────────┘
       │                             │ rlglInit / rlBegin            │ glfwSwapBuffers
       ▼                             ▼                               ▼
 DrawRectangle ──► rshapes ──► rlgl batch ──► glDrawElements ──► framebuffer
 DrawTexture  ──► rtextures ──► rlLoadTexture
 DrawModel    ──► rmodels  ──► DrawMesh (VAO, bypasses batch)
 PlaySound    ──► raudio   ──► miniaudio callback (separate thread)

One frame, end to end

Tap a step to jump to its explanation below.

0. Startup: InitWindow

InitWindow in rcore.c initializes CORE.Window and CORE.Input, calls platform InitPlatform() (e.g. GLFW window + GL context), then rlglInit(width, height) which creates the 1×1 default texture, default shader, and render batch.

rcore.c L585–720 — InitWindow

Full detail: Frame lifecycle.

1. BeginDrawing resets the frame

BeginDrawing captures update-phase delta time and resets the modelview matrix, applying CORE.Window.screenScale for HiDPI displays. It does not clear the screen — call ClearBackground yourself.

rcore.c L858–872 — BeginDrawing

2. Draw calls batch 2D geometry

Even solid rectangles bind a shared white texture and emit quads via rlBegin(RL_QUADS). Text draws each glyph with DrawTexturePro. State changes (new texture/shader) or buffer overflow trigger a batch flush mid-frame.

rshapes.c L738–795 — DrawRectanglePro

Full detail: rlgl & batching.

3. EndDrawing flushes the batch

The first line of EndDrawing is rlDrawRenderBatchActive(), which uploads accumulated vertices and draws with the active shader (default MVP + texture0).

rcore.c L875–917 — EndDrawing

4. Buffer swap and frame pacing

Unless custom frame control is enabled, EndDrawing calls SwapScreenBuffer() (platform), measures draw time, WaitTime() to hit SetTargetFPS, then PollInputEvents().

rcore_desktop_glfw.c — PollInputEvents

5. Input is edge-detected per frame

IsKeyPressed returns true when a key transitioned from up to down since the last poll. Window close is tracked in CORE.Window.shouldClose, updated from GLFW each frame.

rcore.c L3802–3812 — IsKeyPressed

Core objects

ObjectRoleDefined in
COREGlobal window, input, timing statercore.c
RLGLRender batch, shaders, GL staterlgl.h
ImageCPU pixel buffer (RAM)raylib.h
Texture2DGPU texture handle + metadataraylib.h
FontGlyph atlas texture + metricsraylib.h
Mesh / Model3D geometry + materials on GPUraylib.h
SoundStatic audio buffer in mixer listraudio.c

Component pages

External references