Move sdl wrapper to lib

This commit is contained in:
Sijmen 2020-05-22 23:06:28 +02:00
parent cfde59e2f4
commit 7c708b4a31
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
5 changed files with 7 additions and 339 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "vendor/sdl-zig"]
path = vendor/sdl-zig
url = https://git.sijman.nl/_/sdl-zig.git

View File

@ -15,6 +15,9 @@ pub fn build(b: *Builder) void {
exe.linkSystemLibrary("c"); exe.linkSystemLibrary("c");
exe.linkSystemLibrary("SDL2"); exe.linkSystemLibrary("SDL2");
exe.linkSystemLibrary("SDL2_ttf"); exe.linkSystemLibrary("SDL2_ttf");
exe.addPackagePath("zig-sdl", "vendor/zig-sdl/src/main.zig");
exe.setTarget(target); exe.setTarget(target);
exe.setBuildMode(mode); exe.setBuildMode(mode);
exe.install(); exe.install();

View File

@ -1,291 +0,0 @@
pub const c = @cImport(@cInclude("SDL2/SDL.h"));
pub const Color = struct {
r: u8,
g: u8,
b: u8,
a: u8 = c.SDL_ALPHA_OPAQUE,
};
pub const WindowPos = struct {
const Undefined: i32 = c.SDL_WINDOWPOS_UNDEFINED;
const Centered: i32 = c.SDL_WINDOWPOS_CENTERED;
};
pub const Window = struct {
pub const Shown: i32 = c.SDL_WINDOW_SHOWN;
window: *c.SDL_Window,
pub fn init(title: []const u8, w: i32, h: i32, flags: u32) !Window {
return Window.initWithPos(title, WindowPos.Undefined, WindowPos.Undefined, w, h, flags);
}
pub fn initWithPos(title: []const u8, x: i32, y: i32, w: i32, h: i32, flags: u32) !Window {
var title_ = @ptrCast([*c]const u8, title);
var window = c.SDL_CreateWindow(title_, x, y, w, h, flags) orelse return error.SdlError;
return Window{ .window = window };
}
pub fn deinit(self: *Window) void {
c.SDL_DestroyWindow(self.window);
}
pub fn surface(self: Window) !Surface {
return Surface{
.surface = c.SDL_GetWindowSurface(self.window) orelse return error.SdlError,
};
}
pub fn updateSurface(self: Window) !void {
if (c.SDL_UpdateWindowSurface(self.window) != 0) {
return error.SdlError;
}
}
pub fn createRenderer(self: Window, index: ?i32, flags: u32) !Renderer {
var renderer = c.SDL_CreateRenderer(self.window, index orelse -1, flags) orelse return error.SdlError;
return Renderer{ .renderer = renderer };
}
};
pub const Renderer = struct {
renderer: *c.SDL_Renderer,
pub fn deinit(self: Renderer) void {
c.SDL_DestroyRenderer(self.renderer);
}
pub fn createTextureFromSurface(self: Renderer, surface: Surface) !Texture {
var texture = c.SDL_CreateTextureFromSurface(self.renderer, surface.surface) orelse return error.SdlError;
return Texture{ .texture = texture };
}
pub fn clear(self: Renderer) !void {
if (c.SDL_RenderClear(self.renderer) != 0) {
return error.SdlError;
}
}
pub fn copy(self: Renderer, texture: Texture, srcrect: ?Rect, dstrect: ?Rect) !void {
var src = if (srcrect) |inner| &inner.c() else null;
var dst = if (dstrect) |inner| &inner.c() else null;
if (c.SDL_RenderCopy(self.renderer, texture.texture, src, dst) != 0) {
return error.SdlError;
}
}
pub fn present(self: Renderer) void {
c.SDL_RenderPresent(self.renderer);
}
pub fn setDrawColor(self: Renderer, color: Color) !void {
if (c.SDL_SetRenderDrawColor(self.renderer, color.r, color.g, color.b, color.a) != 0) {
return error.SdlError;
}
}
pub fn fillRect(self: Renderer, rect: ?Rect) !void {
var dst = if (rect) |inner| &inner.c() else null;
if (c.SDL_RenderFillRect(self.renderer, dst) != 0) {
return error.SdlError;
}
}
};
pub const Texture = struct {
texture: *c.SDL_Texture,
pub fn deinit(self: Texture) void {
c.SDL_DestroyTexture(self.texture);
}
};
pub const Surface = struct {
surface: *c.SDL_Surface,
pub fn initFromBmp(path: []const u8) !Surface {
var path_ = @ptrCast([*c]const u8, path);
var rw = c.SDL_RWFromFile(path_, "rb") orelse return error.SdlError;
var surface = c.SDL_LoadBMP_RW(rw, 1) orelse return error.SdlError;
return Surface{ .surface = surface };
}
pub fn deinit(self: Surface) void {
c.SDL_FreeSurface(self.surface);
}
pub fn fillRect(self: Surface, rect: [*c]const c.SDL_Rect, color: u32) !void {
if (c.SDL_FillRect(self.surface, rect, color) != 0) {
return error.SdlError;
}
}
pub fn format(self: Surface) PixelFormat {
return PixelFormat{ .pixelFormat = self.surface.*.format };
}
pub fn width(self: Surface) i32 {
return self.surface.w;
}
pub fn height(self: Surface) i32 {
return self.surface.h;
}
};
pub const PixelFormat = struct {
pixelFormat: *c.SDL_PixelFormat,
pub fn mapRgb(self: PixelFormat, r: u8, g: u8, b: u8) u32 {
return c.SDL_MapRGB(self.pixelFormat, r, g, b);
}
};
pub const Rect = struct {
x: i32 = 0,
y: i32 = 0,
w: i32 = 0,
h: i32 = 0,
fn c(self: Rect) c.SDL_Rect {
return .{
.x = self.x,
.y = self.y,
.w = self.w,
.h = self.h,
};
}
};
pub fn init() !void {
// TODO Make this configurable
if (c.SDL_Init(c.SDL_INIT_EVERYTHING) != 0) {
return error.SdlError;
}
}
pub fn quit() void {
c.SDL_Quit();
}
pub fn delay(ms: u32) void {
c.SDL_Delay(ms);
}
pub const EventType = enum(u32) {
FirstEvent = 0,
Quit = 0x100,
AppTerminating,
AppLowMemory,
AppWillEnterBackground,
AppDidEnterBackground,
AppWillEnterForeground,
AppDidEnterForeground,
LocaleChanged,
DisplayEvent = 0x150,
WindowEvent = 0x200,
SysWmEvent,
KeyDown = 0x300,
KeyUp,
TextEditing,
TextInput,
KeymapChanged,
MouseMotion = 0x400,
MouseButtonDown,
MouseButtonUp,
MouseWheel,
JoyAxisMotion = 0x600,
JoyBallMotion,
JoyHatMotion,
JoyButtonDown,
JoyButtonUp,
JoyDeviceAdded,
JoyDeviceRemoved,
ControllerAxisMotion = 0x650,
ControllerButtonDown,
ControllerButtonUp,
ControllerDeviceAdded,
ControllerDeviceRemoved,
ControllerDeviceRemapped,
FingerDown = 0x700,
FingerUp,
FingerMotion,
DollarGesture = 0x800,
DollarRecord,
MultiGesture,
ClipboardUpate = 0x900,
DropFile = 0x1000,
DropText,
DropBegin,
DropComplete,
AudioDeviceAdded = 0x1100,
AudioDeviceRemoved,
SensorUpdate = 0x1200,
RenderTargetsReset = 0x2000,
RenderDeviceReset,
UserEvent = 0x8000,
LastEvent = 0xFFFF,
_,
};
pub const MouseMotionEvent = struct {
timestamp: u32,
windowId: u32,
which: u32,
state: u32,
x: i32,
y: i32,
xrel: i32,
yrel: i32,
};
pub const Event = struct {
event: c.SDL_Event,
pub fn poll() ?Event {
var event = c.SDL_Event{ .type = 0 };
if (c.SDL_PollEvent(&event) == 0) {
return null;
}
return Event{ .event = event };
}
pub fn type_(self: Event) EventType {
return @intToEnum(EventType, self.event.type);
}
pub fn mouseMotion(self: Event) ?MouseMotionEvent {
if (self.type_() != .MouseMotion) {
return null;
}
var inner = self.event.motion;
return MouseMotionEvent{
.timestamp = inner.timestamp,
.windowId = inner.windowID,
.which = inner.which,
.state = inner.state,
.x = inner.x,
.y = inner.y,
.xrel = inner.xrel,
.yrel = inner.yrel,
};
}
};

View File

@ -1,48 +0,0 @@
const std = @import("std");
const sdl = @import("sdl.zig");
pub const c = @cImport(@cInclude("SDL2/SDL_ttf.h"));
pub fn init() !void {
if (c.TTF_Init() == -1) {
return error.SdlTtfError;
}
}
pub fn quit() void {
c.TTF_Quit();
}
pub fn getError() ?[*:0]const u8 {
if (c.TTF_GetError()) |inner| {
return @ptrCast([*:0]const u8, inner);
}
return null;
}
pub const Font = struct {
font: *c.TTF_Font,
pub fn init(path: []const u8, size: i32) !Font {
var path_ = @ptrCast([*c]const u8, path);
if (c.TTF_OpenFont(path_, size)) |inner| {
return Font{ .font = inner };
} else {
std.debug.warn("Font.init: {}\n", .{getError()});
return error.SdlTtfError;
}
}
pub fn deinit(self: Font) void {
c.TTF_CloseFont(self.font);
}
pub fn renderBlended(self: Font, text: []const u8, color: sdl.Color) !sdl.Surface {
var cText = @ptrCast([*c]const u8, text);
if (c.TTF_RenderUTF8_Blended(self.font, cText, @bitCast(c.SDL_Color, color))) |surface| {
return sdl.Surface{ .surface = @ptrCast(*sdl.c.SDL_Surface, surface) };
} else {
std.debug.warn("Font.renderBlended: {}\n", .{getError()});
return error.SdlTtfError;
}
}
};

1
vendor/sdl-zig vendored Submodule

@ -0,0 +1 @@
Subproject commit ac801a8cc5d0ba0c9c4327fad03f7e3e25665f8d