Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
Getting Started

Your first Banjo code.

This tutorial will guide you through creating your first Banjo application from scratch. You'll learn how to set up a project, create a simple graphics program, and build it using CMake.

What You'll Build

A simple application that opens a window and draws colorful shapes on the screen.

Prerequisites

  • CMake (version 3.21 or higher)
  • A C99-compliant compiler (GCC, Clang, or MSVC)
  • Git (for fetching Banjo)
  • Internet connection (for the first build)

Step 1: Create Your Project Directory

Create a new directory for your project:

mkdir my_banjo_app
cd my_banjo_app

Step 2: Create main.c

Create a file named main.c with the following content:

#include <banjo/bitmap.h>
#include <banjo/draw.h>
#include <banjo/event.h>
#include <banjo/error.h>
#include <banjo/main.h>
#include <banjo/renderer.h>
#include <banjo/system.h>
#include <banjo/window.h>
#include <banjo/log.h>
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
bj_error* error = 0;
if (!bj_begin(BJ_VIDEO_SYSTEM, &error)) {
if(error) {
bj_err("Cannot initialize Banjo: %s [%x]", error->message, error->code);
bj_clear_error(&error);
}
return 1;
}
bj_window* window = bj_bind_window("My First Banjo App", 100, 100, 640, 480, 0);
bj_renderer_configure(renderer, window);
// Draw some shapes
bj_bitmap* bmp = bj_get_framebuffer(renderer);
const uint32_t red = bj_make_bitmap_pixel(bmp, 0xFF, 0x00, 0x00);
const uint32_t cyan = bj_make_bitmap_pixel(bmp, 0x00, 0xFF, 0xFF);
bj_draw_filled_circle(bmp, 320, 240, 100, red);
bj_draw_rectangle(bmp, &(bj_rect){.x = 200, .y = 120, .w = 240, .h = 240}, cyan);
bj_present(renderer, window);
// Main loop
while (!bj_should_close_window(window)) {
}
// Cleanup
bj_end();
return 0;
}
Header file for Bitmap type.
Header file for Bitmap drawing functions.
Recoverable error handling.
Sytem event management API.
void bj_clear_bitmap(struct bj_bitmap *bitmap)
Fills the entire bitmap with the clear color.
uint32_t bj_make_bitmap_pixel(struct bj_bitmap *bitmap, uint8_t red, uint8_t green, uint8_t blue)
Returns an opaque value representing a pixel color, given its RGB composition.
struct bj_bitmap bj_bitmap
Definition api.h:331
struct bj_renderer bj_renderer
Definition api.h:349
struct bj_window bj_window
Definition api.h:357
void bj_draw_rectangle(struct bj_bitmap *bitmap, const struct bj_rect *area, uint32_t pixel)
Draws a rectangle in the given bitmap.
void bj_draw_filled_circle(struct bj_bitmap *bitmap, int cx, int cy, int radius, uint32_t color)
Draw a filled circle onto a bitmap.
uint32_t code
Error code.
Definition error.h:132
char message[127+1]
Optional error description.
Definition error.h:133
void bj_clear_error(struct bj_error **error)
Clears the given error location.
Error structure.
Definition error.h:131
void bj_dispatch_events(void)
Poll and dispatch all pending events.
void bj_close_on_escape(struct bj_window *window, const struct bj_key_event *event, void *user_data)
Handle the ESC key to close a window.
bj_key_callback_fn bj_set_key_callback(bj_key_callback_fn callback, void *user_data)
Set the global callback for keyboard key events.
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:130
Represents a rectangle with position and dimensions.
Definition rect.h:19
struct bj_renderer * bj_create_renderer(enum bj_renderer_type type)
Create a new renderer instance.
void bj_present(struct bj_renderer *renderer, struct bj_window *window)
Present the framebuffer to a window.
void bj_renderer_configure(struct bj_renderer *renderer, struct bj_window *window)
Configure a renderer for a specific window.
struct bj_bitmap * bj_get_framebuffer(struct bj_renderer *renderer)
Get the renderer's framebuffer.
void bj_destroy_renderer(struct bj_renderer *renderer)
Destroy a renderer and free associated resources.
@ BJ_RENDERER_TYPE_SOFTWARE
Software (CPU-based) renderer.
Definition renderer.h:34
bj_bool bj_begin(int systems, struct bj_error **error)
Initializes the system.
void bj_end(void)
De-initializes the system.
@ BJ_VIDEO_SYSTEM
Definition system.h:20
struct bj_window * bj_bind_window(const char *title, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t flags)
Create a new struct bj_window with the specified attributes.
bj_bool bj_should_close_window(struct bj_window *window)
Get the close flag state of a window.
void bj_unbind_window(struct bj_window *window)
Deletes a struct bj_window object and releases associated memory.
Logging utility functions.
Portable main substitution and application callback facilities.
Rendering backend interface.
Header file for system interactions.
Header file for bj_window type.

Step 3: Create CMakeLists.txt

Create a file named CMakeLists.txt with the following content:

cmake_minimum_required(VERSION 3.21)
project(MyBanjoApp C)
include(FetchContent)
# Fetch Banjo from GitHub
FetchContent_Declare(
banjo
GIT_REPOSITORY https://github.com/oragonefreet/banjo.git
GIT_TAG main # Or use a specific version tag like v0.1.0
)
FetchContent_MakeAvailable(banjo)
# Create your executable
add_executable(my_banjo_app main.c)
# Link against Banjo
target_link_libraries(my_banjo_app PRIVATE banjo)

Step 4: Build and Run

Configure and build your project:

# Configure the project
cmake -B build
# Build the executable
cmake --build build
# Run your application
./build/my_banjo_app

On Windows, the executable will be located at build\Debug\my_banjo_app.exe or build\Release\my_banjo_app.exe.

What's Happening?

Let's break down the key parts of the code:

Program Structure

The program uses a standard main() function with three main sections:

  1. Initialization: Set up Banjo, create a window and renderer
  2. Drawing: Render shapes to the framebuffer and present it
  3. Main Loop: Keep the window open and handle events until the user closes it
  4. Cleanup: Free resources before exiting

Drawing Graphics

Event Handling

  • bj_dispatch_events() processes window events (keyboard, mouse, etc.)
  • bj_set_key_callback(bj_close_on_escape, 0) sets up ESC key to close the window
  • bj_should_close_window() checks if the user requested to close the window

Next Steps

Now that you have a working Banjo application, you can:

  • Explore more examples in the Banjo repository's examples/ directory
  • Learn about different build options and backends
  • Read the full API documentation to discover all available features
  • Check out Using Banjo for advanced integration methods

For more information on building Banjo itself from source, see Building the API.