All Projects → MasterQ32 → SDL.zig

MasterQ32 / SDL.zig

Licence: MIT License
A shallow wrapper around SDL that provides object API and error handling

Programming Languages

c
50402 projects - #5 most used programming language
Zig
133 projects
CMake
9771 projects

Projects that are alternatives of or similar to SDL.zig

zero-graphics
Application framework based on OpenGL ES 2.0. Runs on desktop machines, Android phones and the web
Stars: ✭ 72 (-29.41%)
Mutual labels:  sdl, sdl2, ziglang, zig-package
zig-opengl
OpenGL binding generator based on the opengl registry
Stars: ✭ 29 (-71.57%)
Mutual labels:  gamedev, ziglang, zig-package
Pygame
pygame (the library) is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.
Stars: ✭ 4,164 (+3982.35%)
Mutual labels:  gamedev, sdl, sdl2
sdlpp
C++ wrapper for SDL2
Stars: ✭ 37 (-63.73%)
Mutual labels:  gamedev, sdl, sdl2
faur
⚒️✨ My personal C games framework. 2D graphics, sound, inputs, states, ECS, and misc utils for data, files, math, memory, strings, time, and more. Builds for Linux, Windows, Web, and embedded devices.
Stars: ✭ 55 (-46.08%)
Mutual labels:  gamedev, sdl, sdl2
BonEngineSharp
A simple and fun SDL-based game engine in C#.
Stars: ✭ 16 (-84.31%)
Mutual labels:  gamedev, sdl, sdl2
zetaframe
lightweight zig game framework.
Stars: ✭ 14 (-86.27%)
Mutual labels:  gamedev, ziglang, zig-package
Libsdl2pp
C++11 bindings/wrapper for SDL2
Stars: ✭ 385 (+277.45%)
Mutual labels:  gamedev, sdl, sdl2
Gwork
Skinnable GUI with useful widget collection. Fork of GWEN.
Stars: ✭ 179 (+75.49%)
Mutual labels:  gamedev, sdl, sdl2
Fairtris
Clone of the official classic Tetris® game for the NES console, intended for Windows and Linux systems. It implements the original mechanics and includes many regional versions and several RNGs (all in one executable).
Stars: ✭ 30 (-70.59%)
Mutual labels:  sdl, sdl2
guisan
A C++ SDL2 user interface library based on guichan. ~~~~ Nov, 2021 - This project is not abandoned, just not under active development. Pull requests will be accepted and new releases will be generated for new features / bug fixes.
Stars: ✭ 54 (-47.06%)
Mutual labels:  sdl, sdl2
OCamlSDL2
OCaml interface to SDL 2.0 (for Linux, Windows, MacOS, and ChromeBook)
Stars: ✭ 42 (-58.82%)
Mutual labels:  sdl, sdl2
mach-glfw
Ziggified GLFW bindings with 100% API coverage, zero-fuss installation, cross compilation, and more.
Stars: ✭ 186 (+82.35%)
Mutual labels:  ziglang, zig-package
Driftwood
Driftwood 2D Tiling Game Engine and Development Suite
Stars: ✭ 23 (-77.45%)
Mutual labels:  sdl, sdl2
sdl12-compat
An SDL-1.2 compatibility layer that uses SDL 2.0 behind the scenes.
Stars: ✭ 99 (-2.94%)
Mutual labels:  sdl, sdl2
ffi-sdl
PHP FFI SDL bindings
Stars: ✭ 23 (-77.45%)
Mutual labels:  sdl, sdl2
sdl stb font
Renders text using STB_Truetype in pure SDL
Stars: ✭ 40 (-60.78%)
Mutual labels:  sdl, sdl2
combatris
A "perfect" implementation of an old classic
Stars: ✭ 20 (-80.39%)
Mutual labels:  sdl, sdl2
sdlada
Ada 2012 bindings to SDL 2
Stars: ✭ 85 (-16.67%)
Mutual labels:  gamedev, sdl
OpenHSP
Hot Soup Processor (HSP3)
Stars: ✭ 120 (+17.65%)
Mutual labels:  gamedev, sdl2

SDL.zig

A Zig package that provides you with the means to link SDL2 to your project, as well as a Zig-infused header implementation (allows you to not have the SDL2 headers on your system and still compile for SDL2) and a shallow wrapper around the SDL apis that allow a more Zig-style coding with Zig error handling and tagged unions.

Getting started

Linking SDL2 to your project

This is an example build.zig that will link the SDL2 library to your project.

const std = @import("std");
const Sdk = @import("Sdk.zig"); // Import the Sdk at build time

pub fn build(b: *std.build.Builder) !void {
    // Determine compilation target
    const target = b.standardTargetOptions(.{});
  
    // Create a new instance of the SDL2 Sdk
    const sdk = Sdk.init(b);

    // Create executable for our example
    const demo_basic = b.addExecutable("demo-basic", "my-game.zig");
    
    demo_basic.setTarget(target);   // must be done before calling sdk.link
    sdk.link(demo_basic, .dynamic); // link SDL2 as a shared library

    // Add "sdl2" package that exposes the SDL2 api (like SDL_Init or SDL_CreateWindow)
    demo_basic.addPackage(sdk.getNativePackage("sdl2")); 

    // Install the executable into the prefix when invoking "zig build"
    demo_basic.install();
}

Using the native API

This package exposes the SDL2 API as defined in the SDL headers. Use this to create a normal SDL2 program:

const std = @import("std");
const SDL = @import("sdl2"); // Add this package by using sdk.getNativePackage

pub fn main() !void {
    if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0)
        sdlPanic();
    defer SDL.SDL_Quit();

    var window = SDL.SDL_CreateWindow(
        "SDL2 Native Demo",
        SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED,
        640, 480,
        SDL.SDL_WINDOW_SHOWN,
    ) orelse sdlPanic();
    defer _ = SDL.SDL_DestroyWindow(window);

    var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED) orelse sdlPanic();
    defer _ = SDL.SDL_DestroyRenderer(renderer);

    mainLoop: while (true) {
        var ev: SDL.SDL_Event = undefined;
        while (SDL.SDL_PollEvent(&ev) != 0) {
            if(ev.type == SDL.SDL_QUIT)
                break :mainLoop;
        }

        _ = SDL.SDL_SetRenderDrawColor(renderer, 0xF7, 0xA4, 0x1D, 0xFF);
        _ = SDL.SDL_RenderClear(renderer);

        SDL.SDL_RenderPresent(renderer);
    }
}

fn sdlPanic() noreturn {
    const str = @as(?[*:0]const u8, SDL.SDL_GetError()) orelse "unknown error";
    @panic(std.mem.sliceTo(str, 0));
}

Using the wrapper API

This package also exposes the SDL2 API with a more Zig-style API. Use this if you want a more convenient Zig experience.

Note: This API is experimental and might change in the future

const std = @import("std");
const SDL = @import("sdl2"); // Add this package by using sdk.getWrapperPackage

pub fn main() !void {
    try SDL.init(.{
        .video = true,
        .events = true,
        .audio = true,
    });
    defer SDL.quit();

    var window = try SDL.createWindow(
        "SDL2 Wrapper Demo",
        .{ .centered = {} }, .{ .centered = {} },
        640, 480,
        .{ .shown = true },
    );
    defer window.destroy();

    var renderer = try SDL.createRenderer(window, null, .{ .accelerated = true });
    defer renderer.destroy();

    mainLoop: while (true) {
        while (SDL.pollEvent()) |ev| {
            switch (ev) {
                .quit => break :mainLoop,
                else => {},
            }
        }

        try renderer.setColorRGB(0xF7, 0xA4, 0x1D);
        try renderer.clear();

        renderer.present();
    }
}

Sdk.zig API

/// Just call `Sdk.init(b)` to obtain a handle to the Sdk!
const Sdk = @This();

/// Creates a instance of the Sdk and initializes internal steps.
/// Initialize once, use everywhere (in your `build` function).
pub fn init(b: *Builder) *Sdk

/// Returns a package with the raw SDL api with proper argument types, but no functional/logical changes
/// for a more *ziggy* feeling.
/// This is similar to the *C import* result.
pub fn getNativePackage(sdk: *Sdk, package_name: []const u8) std.build.Pkg;

/// Returns the smart wrapper for the SDL api. Contains convenient zig types, tagged unions and so on.
pub fn getWrapperPackage(sdk: *Sdk, package_name: []const u8) std.build.Pkg ;

/// Links SDL2 to the given exe and adds required installs if necessary.
/// **Important:** The target of the `exe` must already be set, otherwise the Sdk will do the wrong thing!
pub fn link(sdk: *Sdk, exe: *LibExeObjStep, linkage: std.build.LibExeObjStep.Linkage) void;

Dependencies

All of those are dependencies for the target platform, not for your host. Zig will run/build the same on all source platforms.

Windows

For Windows, you need to fetch the correct dev libraries from the SDL download page. It is recommended to use the MinGW versions if you don't require MSVC compatibility.

MacOS

Right now, cross-compiling for MacOS isn't possible. On a Mac, install SDL2 via brew.

Linux

If you are cross-compiling, no dependencies exist. The build Sdk compiles a libSDL2.so stub which is used for linking.

If you compile to your target platform, you require SDL2 to be installed via your OS package manager.

Support Matrix

This project tries to provide you the best possible development experience for SDL2. Thus, this project supports the maximum amount of cross-compilation targets for SDL2.

The following table documents this. The rows document the target whereas the columns are the build host:

Windows (x86_64) Windows (i386) Linux (x86_64) MacOS (x86_64) MacOS (aarch64)
i386-windows-gnu ⚠️
i386-windows-msvc ⚠️
x86_64-windows-gnu ⚠️
x86_64-windows-msvc ⚠️
x86_64-macos
aarch64-macos ⚠️
x86_64-linux-gnu 🧪 🧪 🧪 ⚠️
aarch64-linux-gnu 🧪 🧪 🧪 🧪 ⚠️

Legend:

  • Cross-compilation is known to work and tested via CI
  • 🧪 Experimental cross-compilation support, covered via CI
  • ⚠️ Cross-compilation might work, but is not tested via CI
  • Cross-compilation is not possible right now

Contributing

You can contribute to this project in several ways:

  • Use it!
    This helps me to track bugs (which i know that there are some), and usability defects (which we can resolve then). I want this library to have the best development experience possible.
  • Implement/improve the linking experience:
    Right now, it's not possible to cross-compile for MacOS, which is very sad. We might find a way to do so, though! Also VCPKG is not well supported on windows platforms.
  • Improve the wrapper.
    Just add the functions you need and make a PR. Or improve existing ones. I won't do it for you, so you have to get your own hands dirty!
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].