Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Created December 13, 2018 22:43
Show Gist options
  • Save zacharycarter/9f04383f144761ad01b1791e195bfeac to your computer and use it in GitHub Desktop.
Save zacharycarter/9f04383f144761ad01b1791e195bfeac to your computer and use it in GitHub Desktop.
Initializing SD2 + BGFX with zig
pub use @cImport({
// See https://github.com/zig-lang/zig/issues/515
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("bgfx/c99/platform.h");
@cInclude("bgfx/c99/bgfx.h");
});
const sdl = @import("sdl.zig");
const input = @import("input.zig");
pub const Event = union(enum) {
KeyDown: input.Key,
KeyUp: input.Key,
Quit,
};
fn getKey(sym: sdl.SDL_Keycode) ?input.Key {
return switch (sym) {
sdl.SDLK_ESCAPE => input.Key.Escape,
sdl.SDLK_BACKSPACE => input.Key.Backspace,
sdl.SDLK_RETURN => input.Key.Return,
sdl.SDLK_F2 => input.Key.F2,
sdl.SDLK_F3 => input.Key.F3,
sdl.SDLK_F4 => input.Key.F4,
sdl.SDLK_F5 => input.Key.F5,
sdl.SDLK_UP => input.Key.Up,
sdl.SDLK_DOWN => input.Key.Down,
sdl.SDLK_LEFT => input.Key.Left,
sdl.SDLK_RIGHT => input.Key.Right,
sdl.SDLK_SPACE => input.Key.Space,
sdl.SDLK_BACKQUOTE => input.Key.Backquote,
sdl.SDLK_m => input.Key.M,
sdl.SDLK_n => input.Key.N,
sdl.SDLK_y => input.Key.Y,
else => null,
};
}
pub fn mapSDLEvent(sdl_event: sdl.SDL_Event) ?Event {
switch (sdl_event.type) {
sdl.SDL_KEYDOWN => {
if (sdl_event.key.repeat == 0) {
if (getKey(sdl_event.key.keysym.sym)) |key| {
return Event{ .KeyDown = key};
}
}
},
sdl.SDL_KEYUP => {
if (getKey(sdl_event.key.keysym.sym)) |key| {
return Event{ .KeyUp = key };
}
},
sdl.SDL_QUIT => {
return Event{ .Quit = {} };
},
else => {},
}
return null;
}
pub const Key = enum {
Escape,
Backspace,
Return,
F2,
F3,
F4,
F5,
Up,
Down,
Left,
Right,
Space,
Backquote,
M,
N,
Y,
};
const std = @import("std");
const bgfx = @import("bgfx.zig");
const sdl = @import("sdl.zig");
const event = @import("event.zig");
const input = @import("input.zig");
const ZealErrors = error {
SDLInitError,
SDLWindowCreationError,
SDLWindowManagerInfoError,
BGFXInitError,
};
const SDL_WINDOWPOS_UNDEFINED = @bitCast(c_int, sdl.SDL_WINDOWPOS_UNDEFINED_MASK);
var window: ?*sdl.SDL_Window = null;
fn init() !void {
if (sdl.SDL_Init(sdl.SDL_INIT_VIDEO) != 0) {
std.debug.warn("unable to initialize sdl: {s}", sdl.SDL_GetError());
return ZealErrors.SDLInitError;
}
var sdl_version = sdl.SDL_version {.major = 0, .minor = 0, .patch = 0};
sdl.SDL_GetVersion(@ptrCast([*]sdl.SDL_version, &sdl_version));
sdl.SDL_Log(c"sdl version %d.%d.%d initialized", sdl_version.major, sdl_version.minor, sdl_version.patch);
window = sdl.SDL_CreateWindow(
c"zeal",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
@intCast(c_int, 800),
@intCast(c_int, 600),
sdl.SDL_WINDOW_SHOWN,
) orelse {
sdl.SDL_Log(c"unable to create sdl window: %s", sdl.SDL_GetError());
return ZealErrors.SDLWindowCreationError;
};
var pd: bgfx.bgfx_platform_data_t = bgfx.bgfx_platform_data_t {
.ndt = null,
.nwh = null,
.context = null,
.backBuffer = null,
.backBufferDS = null,
.session = null,
};
var windowWMInfo: sdl.SDL_SysWMinfo = undefined;
sdl.SDL_VERSION(&windowWMInfo.version);
if (sdl.SDL_GetWindowWMInfo(window, @ptrCast([*]sdl.SDL_SysWMinfo, &windowWMInfo)) == sdl.SDL_bool.SDL_FALSE) {
sdl.SDL_Log(c"unable to retrieve window information from window manager: %s", sdl.SDL_GetError());
return ZealErrors.SDLWindowManagerInfoError;
}
var platform_data: bgfx.bgfx_platform_data_t = undefined;
if (bgfx.BX_PLATFORM_OSX != 0) {
platform_data.ndt = null;
platform_data.nwh = windowWMInfo.info.cocoa.window;
}
bgfx.bgfx_set_platform_data(@ptrCast([*]bgfx.bgfx_platform_data_t, &pd));
if (sdl.SDL_Init(sdl.SDL_INIT_VIDEO) != 0) {
std.debug.warn("unable to initialize sdl: {s}", sdl.SDL_GetError());
return ZealErrors.SDLInitError;
}
var bgfx_init: bgfx.bgfx_init_t = undefined;
bgfx.bgfx_init_ctor(@ptrCast([*]bgfx.bgfx_init_t, &bgfx_init));
if (!bgfx.bgfx_init(@ptrCast([*]bgfx.bgfx_init_t, &bgfx_init))) {
std.debug.warn("unable to initialize bgfx");
return ZealErrors.BGFXInitError;
}
}
fn pollEvent() ?event.Event {
var sdl_event: sdl.SDL_Event = undefined;
if (sdl.SDL_PollEvent(@ptrCast([*]sdl.SDL_Event, &sdl_event)) == 0) {
return null;
}
return event.mapSDLEvent(sdl_event);
}
fn shutdown() void {
if (window) |win| {
sdl.SDL_DestroyWindow(win);
}
sdl.SDL_Quit();
}
pub fn main() void {
var quit = false;
init() catch |err| {
switch (err) {
ZealErrors.SDLInitError => {
return;
},
else => {
quit = true;
}
}
};
while (!quit) {
while (pollEvent()) |ev| {
switch (ev) {
event.Event.Quit => {
quit = true;
},
else => {},
}
}
}
shutdown();
}
const c = @cImport({
// See https://github.com/zig-lang/zig/issues/515
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("SDL2/SDL.h");
@cInclude("SDL2/SDL_syswm.h");
});
pub use c;
pub inline fn SDL_VERSIONNUM(x: comptime_int, y: comptime_int, z: comptime_int) comptime_int {
return x * 1000 + y * 100 + z;
}
pub inline fn SDL_VERSION(version: *SDL_version) void {
version.major = c.SDL_MAJOR_VERSION;
version.minor = c.SDL_MINOR_VERSION;
version.patch = c.SDL_PATCHLEVEL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment