A Zig graphics library that abstracts SDL3 and OpenGL, providing a simple and customizable interface for creating graphical applications.
Many many thanks to the https://github.com/castholm/SDL project, which is the backbone of this library. It provides the bindings for SDL.
This library wraps SDL3 and OpenGL functionality into a single Graphics struct that handles:
- Window creation and management
- OpenGL context setup
- Event handling
- Render loop management
- Shader utilities
Requires Zig 0.14.1 or 0.15.0-dev (master).
zig fetch --save git+https://github.com/margual56/yass.gitAdd this to your build.zig file:
const yass = b.dependency("yass", .{
.target = target,
.optimize = optimize,
});
exe_mod.addImport("yass", yass.module("root"));And a minimal example:
const std = @import("std");
const yass = @import("yass");
pub fn main() !void {
// Initialize graphics with configuration
const config = yass.GraphicsConfig{
.title = "My Application",
.width = 800,
.height = 600,
.resizable = true,
.vsync = true,
};
var gfx = try yass.Graphics.init(config);
defer gfx.deinit();
// Run with default rendering (animated colors)
try gfx.run();
}Check the castholm/SDL project for more information on SDL3.
To implement custom rendering, provide a render callback:
fn myRender(gfx: *yass.Graphics, delta_time: f32) !void {
// Clear the screen
gfx.clear(0.0, 0.0, 0.0, 1.0);
// Your OpenGL rendering code here
// You have full access to OpenGL through the library
// The library automatically swaps buffers after this function
}
// In main:
gfx.setRenderFn(myRender);Handle events by providing an event handler callback:
fn myEventHandler(gfx: *yass.Graphics, event: yass.Event) !bool {
switch (event) {
.key_down => |key| {
if (key.scancode == yass.SCANCODE_ESCAPE) {
gfx.quit();
return true; // Event handled
}
},
.mouse_button_down => |button| {
std.debug.print("Mouse clicked at ({}, {})\n", .{ button.x, button.y });
},
else => {},
}
return false; // Let default handler process
}
// In main:
gfx.setEventHandler(myEventHandler);The library provides a unified Event type that abstracts SDL events:
quit- Window close requestedkey_down- Keyboard key pressedkey_up- Keyboard key releasedmouse_motion- Mouse movedmouse_button_down- Mouse button pressedmouse_button_up- Mouse button releasedwindow_resized- Window size changed
Use the userdata field to store your application state:
const AppState = struct {
score: u32 = 0,
player_pos: [2]f32 = .{ 0, 0 },
};
var state = AppState{};
gfx.userdata = &state;
// Access in callbacks:
fn render(gfx: *graphics.Graphics, delta_time: f32) !void {
const state = @as(*AppState, @ptrCast(@alignCast(gfx.userdata.?)));
// Use state...
}getWindowSize()- Get current window size in pixelssetWindowTitle(title)- Change window titlesetWindowSize(width, height)- Resize window
clear(r, g, b, a)- Clear screen with colorsetViewport(x, y, width, height)- Set rendering viewportsetDepthTest(enabled)- Enable/disable depth testingsetBlending(enabled)- Enable/disable alpha blendingsetWireframe(enabled)- Enable/disable wireframe mode
createShaderProgram(vertex_src, fragment_src)- Create shader programuseProgram(program)- Activate shader programgetUniformLocation(program, name)- Get uniform locationsetUniform*()- Set uniform values
createVertexArray()- Create VAOcreateBuffer()- Create VBO/EBObindVertexArray(vao)- Bind VAObindBuffer(target, buffer)- Bind bufferbufferData(target, size, data, usage)- Upload buffer data
drawArrays(mode, first, count)- Draw verticesdrawElements(mode, count, type, indices)- Draw indexed vertices
getMousePosition()- Get current mouse positionisKeyPressed(scancode)- Check if key is pressed
getElapsedTime()- Time since initializationgetFPS()- Current frames per second
The library exports commonly used OpenGL constants:
// Drawing modes
yass.GL_TRIANGLES
yass.GL_LINES
yass.GL_POINTS
// Buffer types
yass.GL_ARRAY_BUFFER
yass.GL_ELEMENT_ARRAY_BUFFER
// Usage hints
yass.GL_STATIC_DRAW
yass.GL_DYNAMIC_DRAW
// And many more...Key scancodes and mouse buttons are also exported:
yass.SCANCODE_SPACE
yass.SCANCODE_ESCAPE
yass.BUTTON_LEFT
yass.BUTTON_RIGHT
// etc.See the examples/ directory for complete working examples:
simple_window.zig- Basic window with custom renderinggame_of_life.zig- Conway's Game of Life implementation
All SDL operations return error unions. The library uses the errify utility to convert SDL error codes to Zig errors. OpenGL errors should be checked manually when needed.
This library requires:
- SDL3
- OpenGL 3.3+ Core Profile
- zig-opengl bindings
The library is structured as follows:
graphics.zig- Main Graphics struct and implementationutils.zig- Utility functions (shader compilation, error handling)lib.zig- Public API exports