diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2021-06-30 17:19:47 +0200 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2021-06-30 17:19:47 +0200 |
commit | d79befe12a8d6b32a41beb88d6e75b12a75615a7 (patch) | |
tree | 365aa28165efa3e4727ce5ea0edbdf6c69135952 /src/sdl.zig |
rotating cube
Diffstat (limited to 'src/sdl.zig')
-rw-r--r-- | src/sdl.zig | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/sdl.zig b/src/sdl.zig new file mode 100644 index 0000000..d9828af --- /dev/null +++ b/src/sdl.zig @@ -0,0 +1,95 @@ +const std = @import("std"); +const sdl = @cImport(@cInclude("SDL2/SDL.h")); + +pub const instance = struct { + renderer: ?*sdl.SDL_Renderer, + window: ?*sdl.SDL_Window, +}; + +pub fn init() instance { + _ = sdl.SDL_Init(sdl.SDL_INIT_VIDEO); + var window = sdl.SDL_CreateWindow( + "main", + sdl.SDL_WINDOWPOS_UNDEFINED, + sdl.SDL_WINDOWPOS_UNDEFINED, + 500, + 500, + sdl.SDL_WINDOW_SHOWN, + ); + + var renderer = sdl.SDL_CreateRenderer( + window, + -1, + sdl.SDL_RENDERER_ACCELERATED, + ); + + return instance{ + .window = window, + .renderer = renderer, + }; +} + +pub fn draw_line(i: instance, p1: []f32, p2: []f32) void { + _ = sdl.SDL_RenderDrawLineF(i.renderer, p1[0], p1[1], p2[0], p2[1]); +} + +pub fn delay(t: u32) void { + sdl.SDL_Delay(t); +} + +pub fn draw_color(i: instance, r: u8, g: u8, b: u8, a: u8) void { + _ = sdl.SDL_SetRenderDrawColor(i.renderer, r, g, b, a); +} + +pub fn clear(i: instance) void { + _ = sdl.SDL_RenderClear(i.renderer); +} + +pub fn present(i: instance) void { + sdl.SDL_RenderPresent(i.renderer); +} + +pub fn get_event() event { + var e: sdl.SDL_Event = undefined; + _ = sdl.SDL_PollEvent(&e); + + var key = keys.none; + var mode = modes.mouse; + //var key = @intToEnum(keys, e.key.keysym.sym); + //var mode = @intToEnum(modes, e.type); + + var r: event = event{ + .key = key, + .mode = mode, + }; + return r; +} + +const event = struct { + key: keys, + mode: modes, +}; + +pub const modes = enum(u32) { + mouse = sdl.SDL_MOUSEMOTION, + window = sdl.SDL_WINDOWEVENT, + quit = sdl.SDL_QUIT, + key = sdl.SDL_KEYDOWN, +}; + +pub fn quit(i: instance) void { + sdl.SDL_DestroyRenderer(i.renderer); + sdl.SDL_DestroyWindow(i.window); + sdl.SDL_Quit(); +} + +pub const keys = enum(i32) { + none = sdl.SDLK_UNKNOWN, + w = sdl.SDLK_w, + s = sdl.SDLK_s, + q = sdl.SDLK_q, + up = sdl.SDLK_UP, + down = sdl.SDLK_DOWN, + left = sdl.SDLK_LEFT, + right = sdl.SDLK_RIGHT, +}; |