margual56/yass
Yet Another SDL System
5c55c894039a15a16bb7c89b2d6438e158e86ef1
f19acc6a78aa4429917341f9bacc73346db21b73
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:
Requires Zig 0.14.1 or 0.15.0-dev (master).
zig fetch --save git+https://github.com/margual56/yass.git
Add 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 changedUse 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 windowclear(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 modecreateShaderProgram(vertex_src, fragment_src)
- Create shader programuseProgram(program)
- Activate shader programgetUniformLocation(program, name)
- Get uniform locationsetUniform*()
- Set uniform valuescreateVertexArray()
- Create VAOcreateBuffer()
- Create VBO/EBObindVertexArray(vao)
- Bind VAObindBuffer(target, buffer)
- Bind bufferbufferData(target, size, data, usage)
- Upload buffer datadrawArrays(mode, first, count)
- Draw verticesdrawElements(mode, count, type, indices)
- Draw indexed verticesgetMousePosition()
- Get current mouse positionisKeyPressed(scancode)
- Check if key is pressedgetElapsedTime()
- Time since initializationgetFPS()
- Current frames per secondThe 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 implementationAll 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:
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