Created
December 2, 2019 07:39
-
-
Save ikskuh/f181fe320186f17164ec4414ce3aba8b to your computer and use it in GitHub Desktop.
Zig SDL2 Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
// import the C headers here: | |
const c = @cImport({ | |
@cInclude("SDL.h"); | |
@cInclude("SDL_image.h"); | |
}); | |
/// game entry point which may return an error. | |
/// if the error is error.SdlError, SDL_GetError() will be called | |
/// and the message will be displayed. | |
pub fn gameMain() !void { | |
if (c.SDL_Init(c.SDL_INIT_VIDEO) < 0) | |
return error.SdlError; | |
defer c.SDL_Quit(); | |
var window = c.SDL_CreateWindow( | |
c"My SDL2 Application", | |
c.SDL_WINDOWPOS_CENTERED_MASK, | |
c.SDL_WINDOWPOS_CENTERED_MASK, | |
1280, | |
720, | |
c.SDL_WINDOW_SHOWN, | |
) orelse return error.SdlError; | |
defer c.SDL_DestroyWindow(window); | |
var renderer = c.SDL_CreateRenderer( | |
window, | |
-1, | |
c.SDL_RENDERER_ACCELERATED, | |
) orelse return error.SdlError; | |
defer c.SDL_DestroyRenderer(renderer); | |
mainLoop: while (true) { | |
var ev: c.SDL_Event = undefined; | |
while (c.SDL_PollEvent(&ev) != 0) { | |
switch (ev.type) { | |
c.SDL_QUIT => { | |
break :mainLoop; | |
}, | |
else => {}, | |
} | |
} | |
_ = c.SDL_RenderClear(renderer); | |
_ = c.SDL_RenderPresent(renderer); | |
} | |
} | |
/// wraps gameMain, so we can react to an SdlError and print | |
/// its error message | |
pub fn main() !void { | |
gameMain() catch |err| switch (err) { | |
error.SdlError => { | |
std.debug.warn("SDL Failure: {c}", c.SDL_GetError()); | |
return err; | |
}, | |
else => return err, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment