MiniFB (Mini FrameBuffer) is a small cross-platform library that makes it easy to render (32-bit) pixels in a window.
An example is the best way to show how it works:
int main() {
struct mfb_window *window = mfb_open_ex("my display", 800, 600, MFB_WF_RESIZABLE);
if (window == NULL)
return 0;
uint32_t *buffer = malloc(800 * 600 * 4);
mfb_update_state state;
do {
// TODO: add some fancy rendering to the buffer of size 800 * 600
state = mfb_update_ex(window, buffer, 800, 600);
if (state != MFB_STATE_OK)
break;
} while(mfb_wait_sync(window));
free(buffer);
buffer = NULL;
window = NULL;
return 0;
}- First the application creates a window calling mfb_open or mfb_open_ex.
- Next, it's the application's responsibility to allocate a buffer to work with.
- Next, call mfb_update or mfb_update_ex to copy the buffer to the window and display it. If this function returns a value lower than 0, the window has been destroyed internally and cannot be used anymore.
- Last the code waits to synchronize with the monitor calling mfb_wait_sync.
Note: By default, if ESC key is pressed, mfb_update / mfb_update_ex will return -1 (and the window will have been destroyed internally).
See https://github.com/emoon/minifb/blob/master/examples/noise.c for a complete example.
| Platform | Backends | Status |
|---|---|---|
| Windows | GDI, OpenGL | Fully supported |
| macOS | Cocoa, Metal | Fully supported |
| Linux/Unix | X11, Wayland | Fully supported (X11, Wayland) |
| iOS | Metal | Beta |
| Android | Native | Beta |
| Web | WASM | Beta |
| DOS | DJGPP | Beta |
MiniFB has been tested on Windows, macOS, Linux, iOS, Android, web, and DOSBox-x. Compatibility may vary depending on your setup. Currently, the library does not perform any data conversion if a proper 32-bit display cannot be created.
- Window creation and management
- Event callbacks (keyboard, mouse, window lifecycle)
- Direct window state queries
- Per-window custom data
- Built-in timers and FPS control
- C and C++ interfaces
- Cursor control
// Create and manage windows
struct mfb_window * mfb_open(const char *title, unsigned width, unsigned height);
struct mfb_window * mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags);
void mfb_close(struct mfb_window *window);
void mfb_set_title(struct mfb_window *window, const char *title);
// Update and synchronization
mfb_update_state mfb_update(struct mfb_window *window, void *buffer);
mfb_update_state mfb_update_ex(struct mfb_window *window, void *buffer, unsigned width, unsigned height);
mfb_update_state mfb_update_events(struct mfb_window *window);
bool mfb_wait_sync(struct mfb_window *window);
// Viewport control
// Coordinates/sizes are in drawable coordinates (same units as mfb_get_window_width/height and resize callback).
bool mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
bool mfb_set_viewport_best_fit(struct mfb_window *window, unsigned old_width, unsigned old_height);mfb_set_viewport() returns false if:
width == 0orheight == 0- viewport bounds exceed the current window drawable size
mfb_open() and mfb_open_ex() return NULL if:
width == 0orheight == 0width * 4would overflow the internal framebuffer stride
If title is NULL or empty, MiniFB uses "minifb" as the effective window/canvas title.
If both MFB_WF_FULLSCREEN and MFB_WF_FULLSCREEN_DESKTOP are provided, MFB_WF_FULLSCREEN takes precedence.
mfb_update_ex() returns MFB_STATE_INVALID_BUFFER if:
buffer == NULLwidth == 0orheight == 0width * 4would overflow internal stride calculations
mfb_update_ex() runtime behavior is backend-specific:
- Wayland waits for compositor frame callback inside
mfb_update_ex()(can block). - Android may return
MFB_STATE_OKwithout presenting whenANativeWindowis temporarily unavailable during lifecycle transitions.
Open-time readiness is backend-specific:
- Wayland waits for the initial configure handshake before returning from
mfb_open_ex(). - Android may return a window handle before
ANativeWindowis ready (rendering starts once the native window becomes available).
mfb_open_ex() flag support by backend:
| Backend | RESIZABLE | BORDERLESS | ALWAYS_ON_TOP | FULLSCREEN | FULLSCREEN_DESKTOP |
|---|---|---|---|---|---|
| Windows | Yes | Yes | Yes | Yes | Yes |
| X11 | Yes | Yes* | Yes* | Yes* | Yes* |
| Wayland | Yes | Yes | No (ignored, warning) | Yes | Yes (maximized) |
| macOS | Yes | Yes | Yes | Yes | Yes (zoom/maximize) |
| Web | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) | Yes** | Yes** |
| DOS | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) |
| Android | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) |
| iOS | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) | No (ignored, warning) |
* Best effort via window-manager hints/properties; behavior depends on compositor/WM support.
** Browser-managed fullscreen; typically requires a user gesture before entering fullscreen.
Register callbacks to handle window events:
// Callback types
void mfb_set_active_callback(struct mfb_window *window, mfb_active_func callback);
void mfb_set_resize_callback(struct mfb_window *window, mfb_resize_func callback);
void mfb_set_close_callback(struct mfb_window *window, mfb_close_func callback);
void mfb_set_keyboard_callback(struct mfb_window *window, mfb_keyboard_func callback);
void mfb_set_char_input_callback(struct mfb_window *window, mfb_char_input_func callback);
void mfb_set_mouse_button_callback(struct mfb_window
10000
*window, mfb_mouse_button_func callback);
void mfb_set_mouse_move_callback(struct mfb_window *window, mfb_mouse_move_func callback);
void mfb_set_mouse_scroll_callback(struct mfb_window *window, mfb_mouse_scroll_func callback);void active(struct mfb_window *window, bool is_active) {
// Called when window gains/loses focus
}
void resize(struct mfb_window *window, int width, int height) {
// Called when window is resized (width/height use the same drawable units as mfb_set_viewport)
// Optionally adjust viewport:
// mfb_set_viewport(window, x, y, width, height);
}
bool close(struct mfb_window *window) {
// Called when close is requested
return true; // true => confirm close, false => cancel
}
void keyboard(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool is_pressed) {
if (key == KB_KEY_ESCAPE) {
mfb_close(window);
}
}
void char_input(struct mfb_window *window, unsigned int char_code) {
// Unicode character input
}
void mouse_btn(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool is_pressed) {
// Mouse button events
}
void mouse_move(struct mfb_window *window, int x, int y) {
// Mouse movement (note: fired frequently)
}
void mouse_scroll(struct mfb_window *window, mfb_key_mod mod, float delta_x, float delta_y) {
// Mouse wheel/scroll events
}If you are using C++, you can set callbacks to class methods or lambda expressions:
// Using object and pointer to member
mfb_set_active_callback(window, &myObject, &MyClass::onActive);
// Using std::bind
mfb_set_active_callback(std::bind(&MyClass::onActive, &myObject, _1, _2), window);
// Using lambda
mfb_set_active_callback([](struct mfb_window *window, bool is_active) {
// Handle event
}, window);Query window and input state directly (alternative to callbacks):
// Window state
bool mfb_is_window_active(struct mfb_window *window);
unsigned mfb_get_window_width(struct mfb_window *window);
unsigned mfb_get_window_height(struct mfb_window *window);
void mfb_get_window_size(struct mfb_window *window, unsigned *width, unsigned *height);
// Key utilities
const char * mfb_get_key_name(mfb_key key);
// Drawable area (considering viewport scaling/DPI)
unsigned mfb_get_drawable_offset_x(struct mfb_window *window);
unsigned mfb_get_drawable_offset_y(struct mfb_window *window);
unsigned mfb_get_drawable_width(struct mfb_window *window);
unsigned mfb_get_drawable_height(struct mfb_window *window);
void mfb_get_drawable_bounds(struct mfb_window *window, unsigned *offset_x, unsigned *offset_y, unsigned *width, unsigned *height);
// Input state
int mfb_get_mouse_x(struct mfb_window *window);
int mfb_get_mouse_y(struct mfb_window *window);
void mfb_decode_touch(int combined, int *pos, int *id); // Decode packed mobile touch pos/id
int mfb_decode_touch_pos(int combined); // Extract position from a packed touch value
int mfb_decode_touch_id(int combined); // Extract pointer id from a packed touch value
float mfb_get_mouse_scroll_x(struct mfb_window *window); // Mouse wheel delta X from the most recent event pump (0.0f if none)
float mfb_get_mouse_scroll_y(struct mfb_window *window); // Mouse wheel delta Y from the most recent event pump (0.0f if none)
const uint8_t * mfb_get_mouse_button_buffer(struct mfb_window *window); // 1=pressed, 0=released (8 buttons)
const uint8_t * mfb_get_key_buffer(struct mfb_window *window); // 1=pressed, 0=releasedOn Android/iOS touch paths, mfb_get_mouse_x() and mfb_get_mouse_y() include an encoded touch pointer id in the upper bits.
Use mfb_decode_touch() to decode both at once, or mfb_decode_touch_pos() / mfb_decode_touch_id() individually. On desktop/Web/DOS, id is always 0.
For touch callbacks, the pointer id is also exposed as button in mfb_mouse_button_func (MFB_MOUSE_BTN_0..MFB_MOUSE_BTN_7).
On Android/iOS touch move callbacks, mfb_mouse_move_func receives packed x/y values (same encoding as getters).
On Android, external HOVER_MOVE also uses packed x/y; if Android does not provide a valid pointer id, MiniFB uses fallback id 15.
mfb_get_mouse_scroll_x/y() are pump-local values: MiniFB resets them to 0.0f before each backend event pump, then writes the delta if a scroll event is received during that pump.
Attach and retrieve custom data per window:
void mfb_set_user_data(struct mfb_window *window, void *user_data);
void * mfb_get_user_data(struct mfb_window *window);Create and manage timers independently:
struct mfb_timer * mfb_timer_create(void);
void mfb_timer_destroy(struct mfb_timer *tmr);
void mfb_timer_reset(struct mfb_timer *tmr);
void mfb_timer_compensated_reset(struct mfb_timer *tmr);
double mfb_timer_now(struct mfb_timer *tmr);
double mfb_timer_delta(struct mfb_timer *tmr);
double mfb_timer_get_frequency(void);
double mfb_timer_get_resolution(void);Control target FPS and frame synchronization:
void mfb_set_target_fps(uint32_t fps); // Default: 60 fps
unsigned mfb_get_target_fps(void);
bool mfb_wait_sync(struct mfb_window *window); // Frame sync pointNote: Hardware-accelerated syncing (OpenGL / Metal, where supported) will use vertical sync. Other platforms use software pacing.
MiniFB ships with a simple logger that you can redirect or disable:
// Set a custom logger; pass NULL to restore the built-in logger
void mfb_set_logger(mfb_log_func user_logger);
// Control verbosity threshold (inclusive)
void mfb_set_log_level(mfb_log_level level);- Levels (low → high):
MFB_LOG_TRACE,MFB_LOG_DEBUG,MFB_LOG_INFO,MFB_LOG_WARNING,MFB_LOG_ERROR. - Defaults: in
_DEBUGbuilds the threshold isMFB_LOG_DEBUG; in release buildsMFB_LOG_INFO. - Messages with a level below the threshold are discarded; equal or higher are emitted.
- Custom loggers receive the message already formatted (
level+message). - The built-in logger writes
TRACE/DEBUG/INFOtostdoutandWARNING/ERRORtostderras[MiniFB (LEVEL)] message.
void mfb_show_cursor(struct mfb_window *window, bool show);Note: Cursor hiding is supported on Windows, macOS, X11, and Wayland only.
void mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y);
void mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y); // [Deprecated]mfb_get_monitor_scale():
- Returns scale multipliers (
1.0= 100%). - If
window == NULL, outputs still receive a safe fallback (1.0) when their pointers are non-NULL. - Some backends provide real scale values (for example Retina/HiDPI); others currently return fixed
1.0. - On X11, scale detection is implemented (Xresources/XRandR/fallbacks), but in many desktop setups the value is effectively startup-time and may not update dynamically after changing global scale while the app is running.
If you define layout in logical units and need drawable-coordinate values for mfb_set_viewport(), convert explicitly:
float sx = 1.0f, sy = 1.0f;
mfb_get_monitor_scale(window, &sx, &sy);
unsigned margin_x_viewport = (unsigned) lroundf(margin_logical_x * sx);
unsigned margin_y_viewport = (unsigned) lroundf(margin_logical_y * sy);Two C functions let you query display insets from native code:
// Physical cutout/notch area only.
bool mfb_get_display_cutout_insets(struct mfb_window *window,
int *left, int *top, int *right, int *bottom);
// Full safe area: cutout + system UI reserved regions.
bool mfb_get_display_safe_insets(struct mfb_window *window,
int *left, int *top, int *right, int *bottom);Insets are edge margins in pixels, not a rectangle:
left,top,right,bottomare distances from each window edge.- No reserved area means
0, 0, 0, 0. - If you need a safe rectangle, derive it from window size:
safe_x = leftsafe_y = topsafe_w = window_w - left - rightsafe_h = window_h - top - bottom
Return value contract (all backends):
true: query succeeded, output values are valid (possibly all zeros).false: query unavailable/invalid at that moment; outputs are set to zeros.
Behavior by backend:
| Backend | mfb_get_display_cutout_insets |
mfb_get_display_safe_insets |
|---|---|---|
| Android | Physical cutout only (DisplayCutout, API 28+). Returns true with zeros when there is no cutout. Returns false if unavailable (e.g. API < 28 or query failure). |
Full safe insets. API 30+: `WindowInsets.getInsets(systemBars |
| iOS | Approximated from UIWindow.safeAreaInsets for physical cutout intent. Bottom is kept 0 (home indicator is not a physical cutout). |
Uses UIWindow.safeAreaInsets directly (includes notch/Dynamic Island + status bar + home indicator). |
| Desktop/Web/DOS | Returns true with zeros for a valid window (no platform cutout/safe-inset data exposed). |
Returns true with zeros for a valid window. |
For any backend, passing window == NULL returns false and zero outputs.
Example usage (safe layout):
void on_resize(struct mfb_window *window, int width, int height) {
int left = 0, top = 0, right = 0, bottom = 0;
if (!mfb_get_display_safe_insets(window, &left, &top, &right, &bottom)) {
return;
}
int safe_x = left;
int safe_y = top;
int safe_w = width - left - right;
int safe_h = height - top - bottom;
// Clamp defensive, in case platform values arrive before resize settles.
if (safe_w < 0) safe_w = 0;
if (safe_h < 0) safe_h = 0;
// Place important UI inside [safe_x, safe_y, safe_w, safe_h].
}Add this repository as a submodule in your dependencies folder:
git submodule add https://github.com/emoon/minifb.git dependencies/minifbThen in your CMakeLists.txt file:
add_subdirectory(dependencies/minifb)
# Link MiniFB to your project:
target_link_libraries(${PROJECT_NAME} minifb)The build system is CMake.
MiniFB existed for many years without an official release version. Starting from this codebase, the project now uses SemVer and declares v0.9.0 as the official baseline version.
Builds expose public version/build metadata for C/C++ consumers via minifb_version.h (generated at configure time and installed with the public headers). This includes:
MINIFB_VERSION_STRING/ major-minor-patch macros- packed
MINIFB_VERSION_NUMERICand extraction helpers - optional Git-derived metadata (
MINIFB_COMMIT_COUNT,MINIFB_COMMITS_SINCE_TAG,MINIFB_GIT_SHA,MINIFB_GIT_DIRTY)
When building from a source archive without .git metadata, defaults are used (unknown SHA, counters 0) and the build still succeeds.
Some projects use date-based versions when retrofitting versioning. MiniFB now uses SemVer to keep compatibility expectations clearer for users and downstream integrations.
If you use CMake, a Visual Studio project will be generated.
Furthermore you can also use MinGW instead of Visual Studio.
By default, the OpenGL backend is used instead of Windows GDI because it is faster. To maintain compatibility with older computers, an OpenGL 1.5 context is created (no shaders needed).
To enable or disable OpenGL just use a CMake flag:
cmake .. -DMINIFB_USE_OPENGL_API=ON
# or
cmake .. -DMINIFB_USE_OPENGL_API=OFFUSE_OPENGL_API is still accepted for compatibility, but deprecated.
To compile MiniFB with X11 backend on Ubuntu/Debian, install the following packages:
sudo apt-get update
sudo apt-get install -y \
build-essential \
cmake \
pkg-config \
libx11-dev \
libxkbcommon-dev \
libgl1-mesa-dev \
libxrandr-dev- build-essential: Compiler toolchain (gcc, g++, make)
- cmake: Build system
- pkg-config: Helper tool for compiling applications and libraries
- libx11-dev: X11 core libraries and headers
- libxkbcommon-dev: Keyboard handling library
- libgl1-mesa-dev: OpenGL libraries (required if using OpenGL backend, which is default)
- libxrandr-dev (optional): Enables XRandR-based monitor scale/DPI queries on X11 (
mfb_get_monitor_scalefallback path and diagnostics)
If you prefer to use X11 without OpenGL (XImage rendering), you can omit libgl1-mesa-dev.
If you do not need XRandR-assisted scale/DPI detection, you can omit libxrandr-dev.
Equivalent packages for other distros:
- Fedora:
gcc,cmake,pkg-config,libX11-devel,libxkbcommon-devel,mesa-libGL-devel - Arch:
base-devel,cmake,pkgconf,libx11,libxkbcommon,mesa - openSUSE:
gcc,cmake,pkg-config,libX11-devel,libxkbcommon-devel,Mesa-libGL-devel
If you use CMake, just disable the flag:
mkdir build-x11
cd build-x11
cmake .. -DMINIFB_USE_WAYLAND_API=OFFUSE_WAYLAND_API is still accepted for compatibility, but deprecated.
By default, the OpenGL backend is used instead of X11 XImages because it is faster. To maintain compatibility with older computers, an OpenGL 1.5 context is created (no shaders needed).
To enable or disable OpenGL just use a CMake flag:
cmake .. -DMINIFB_USE_OPENGL_API=ON -DMINIFB_USE_WAYLAND_API=OFF
# or
cmake .. -DMINIFB_USE_OPENGL_API=OFF -DMINIFB_USE_WAYLAND_API=OFFUSE_OPENGL_API and USE_WAYLAND_API are still accepted for compatibility, but deprecated.
Depends on gcc and wayland-client and wayland-cursor. Built using the wayland-gcc variants. The Wayland backend supports the same core MiniFB API surface as the other primary desktop backends (Windows, macOS, X11).
To compile MiniFB with Wayland backend on Ubuntu/Debian, install the following packages:
sudo apt-get update
sudo apt-get install -y \
build-essential \
cmake \
pkg-config \
libwayland-dev \
libxkbcommon-dev \
wayland-protocols- build-essential: Compiler toolchain (gcc, g++, make)
- cmake: Build system
- pkg-config: Helper tool for compiling applications and libraries
- libwayland-dev: Wayland client libraries and headers
- libxkbcommon-dev: Keyboard handling library
- wayland-protocols: Wayland protocol definitions
Equivalent packages for other distros:
- Fedora:
gcc,cmake,pkg-config,wayland-devel,libxkbcommon-devel,wayland-protocols-devel - Arch:
base-devel,cmake,pkgconf,wayland,libxkbcommon,wayland-protocols - openSUSE:
gcc,cmake,pkg-config,wayland-devel,libxkbcommon-devel,wayland-protocols-devel
Different Linux distributions and versions may ship with different versions of Wayland and its protocols. MiniFB includes pre-generated protocol headers and code that work with most common setups. However, if you encounter compatibility issues or want to ensure optimal compatibility with your specific Wayland version, you can regenerate the protocol files using your system's Wayland version.
To regenerate Wayland protocol files for your system you must run first the protocol generation script:
chmod +x ./tools/wayland/generate-protocols.sh
./tools/wayland/generate-protocols.shThis script will generate protocol headers and code that are specifically compatible with your installed Wayland version, potentially resolving any version mismatch issues.
If you use CMake, just enable the flag:
mkdir build-wayland
cd build-wayland
cmake .. -DMINIFB_USE_WAYLAND_API=ONUSE_WAYLAND_API is still accepted for compatibility, but deprecated.