From 98ced865236df2127bf2d28d27f66435c04868f6 Mon Sep 17 00:00:00 2001 From: Not-Nik Date: Fri, 31 May 2024 19:52:06 +0200 Subject: [PATCH] Use more enums instead of ints --- lib/generate_functions.py | 39 +++++++++++++++++++----- lib/preludes/raylib-prelude.zig | 14 ++++----- lib/raylib-ext.zig | 22 +++++++------- lib/raylib.zig | 54 ++++++++++++++++----------------- 4 files changed, 75 insertions(+), 54 deletions(-) diff --git a/lib/generate_functions.py b/lib/generate_functions.py index 5c22368..d3ae863 100644 --- a/lib/generate_functions.py +++ b/lib/generate_functions.py @@ -158,16 +158,40 @@ def fix_enums(arg_name, arg_type, func_name): arg_type = "GamepadButton" else: arg_type = "MouseButton" - elif arg_name == "mode" and func_name == "UpdateCamera": - arg_type = "CameraMode" + elif arg_name == "mode": + if func_name == "UpdateCamera": + arg_type = "CameraMode" + elif func_name == "BeginBlendMode": + arg_type = "BlendMode" elif arg_name == "gesture": arg_type = "Gesture" - elif arg_name == "flags" and func_name in [ - "SetWindowState", "ClearWindowState", "SetConfigFlags" - ]: - arg_type = "ConfigFlags" + elif arg_name == "flags": + if func_name in [ + "SetWindowState", "ClearWindowState", "SetConfigFlags" + ]: + arg_type = "ConfigFlags" + elif func_name == "SetGesturesEnabled": + arg_type = "Gesture" elif arg_name == "logLevel": arg_type = "TraceLogLevel" + elif arg_name == "ty" and not func_name.startswith("rl"): + arg_type = "FontType" + elif arg_name == "UniformType": + arg_type = "ShaderUniformDataType" + elif arg_name == "Cursor": + arg_type = "MouseCursor" + elif arg_name == "newFormat": + arg_type = "PixelFormat" + elif arg_name == "layout": + arg_type = "CubemapLayout" + elif arg_name == "filter" and func_name == "SetTextureFilter": + arg_type = "TextureFilter" + elif arg_name == "TextureWrap": + arg_type = "TextureWrap" + elif arg_name == "format" and not func_name.startswith("rl"): + arg_type = "PixelFormat" + elif arg_name == "mapType": + arg_type = "MaterialMapIndex" return arg_type @@ -253,11 +277,12 @@ def parse_header(header_name: str, output_file: str, ext_file: str, prefix: str, # Everything but the last element (for stuff like "const Vector3"). arg_type = " ".join(arg.split(" ")[0:-1]) arg_name = arg.split(" ")[-1] # Last element should be the name. - arg_type = fix_enums(arg_name, arg_type, func_name) if arg_name == "type": arg_name = "ty" + arg_type = fix_enums(arg_name, arg_type, func_name) + arg_type = c_to_zig_type(arg_type) arg_name, arg_type = fix_pointer(arg_name, arg_type) zig_type = ziggify_type(arg_name, arg_type) diff --git a/lib/preludes/raylib-prelude.zig b/lib/preludes/raylib-prelude.zig index bccbe54..c1368dd 100755 --- a/lib/preludes/raylib-prelude.zig +++ b/lib/preludes/raylib-prelude.zig @@ -173,7 +173,7 @@ pub const Image = extern struct { } pub fn initRaw(fileName: [:0]const u8, width: i32, height: i32, format: PixelFormat, headerSize: i32) Image { - return rl.loadImageRaw(fileName, width, height, @intFromEnum(format), headerSize); + return rl.loadImageRaw(fileName, width, height, format, headerSize); } pub fn initAnim(fileName: [:0]const u8, frames: *i32) Image { @@ -244,8 +244,7 @@ pub const Image = extern struct { return rl.imageFromImage(self, rec); } - // @todo: use PixelFormat enum for newFormat - pub fn setFormat(self: *Image, newFormat: i32) void { + pub fn setFormat(self: *Image, newFormat: PixelFormat) void { return rl.imageFormat(self, newFormat); } @@ -429,8 +428,7 @@ pub const Image = extern struct { return Texture.fromImage(self); } - // @todo: use CubemapLayout enum for layout - pub fn asCubemap(self: Image, layout: i32) Texture { + pub fn asCubemap(self: Image, layout: CubemapLayout) Texture { return Texture.fromCubemap(self, layout); } }; @@ -450,7 +448,7 @@ pub const Texture = extern struct { return rl.loadTextureFromImage(image); } - pub fn fromCubemap(image: Image, layout: i32) Texture { + pub fn fromCubemap(image: Image, layout: CubemapLayout) Texture { return rl.loadTextureCubemap(image, layout); } @@ -1313,10 +1311,10 @@ pub fn loadFontFromMemory(fileType: [:0]const u8, fileData: ?[]const u8, fontSiz return cdef.LoadFontFromMemory(@as([*c]const u8, @ptrCast(fileType)), @as([*c]const u8, @ptrCast(fileDataFinal)), @as(c_int, @intCast(fileDataLen)), @as(c_int, fontSize), @as([*c]c_int, @ptrCast(fontChars)), @as(c_int, @intCast(fontChars.len))); } -pub fn loadFontData(fileData: []const u8, fontSize: i32, fontChars: []i32, ty: i32) RaylibError![]GlyphInfo { +pub fn loadFontData(fileData: []const u8, fontSize: i32, fontChars: []i32, ty: FontType) RaylibError![]GlyphInfo { var res: []GlyphInfo = undefined; - const ptr = cdef.LoadFontData(@as([*c]const u8, @ptrCast(fileData)), @as(c_int, @intCast(fileData.len)), @as(c_int, fontSize), @as([*c]c_int, @ptrCast(fontChars)), @as(c_int, @intCast(fontChars.len)), @as(c_int, ty)); + const ptr = cdef.LoadFontData(@as([*c]const u8, @ptrCast(fileData)), @as(c_int, @intCast(fileData.len)), @as(c_int, fontSize), @as([*c]c_int, @ptrCast(fontChars)), @as(c_int, @intCast(fontChars.len)), ty); if (ptr == 0) return RaylibError.GenericError; res.ptr = @as([*]GlyphInfo, @ptrCast(ptr)); diff --git a/lib/raylib-ext.zig b/lib/raylib-ext.zig index c0e6d47..aaa67b6 100644 --- a/lib/raylib-ext.zig +++ b/lib/raylib-ext.zig @@ -67,7 +67,7 @@ pub extern "c" fn BeginTextureMode(target: rl.RenderTexture2D) void; pub extern "c" fn EndTextureMode() void; pub extern "c" fn BeginShaderMode(shader: rl.Shader) void; pub extern "c" fn EndShaderMode() void; -pub extern "c" fn BeginBlendMode(mode: c_int) void; +pub extern "c" fn BeginBlendMode(mode: rl.BlendMode) void; pub extern "c" fn EndBlendMode() void; pub extern "c" fn BeginScissorMode(x: c_int, y: c_int, width: c_int, height: c_int) void; pub extern "c" fn EndScissorMode() void; @@ -193,7 +193,7 @@ pub extern "c" fn GetTouchY() c_int; pub extern "c" fn GetTouchPosition(index: c_int) rl.Vector2; pub extern "c" fn GetTouchPointId(index: c_int) c_int; pub extern "c" fn GetTouchPointCount() c_int; -pub extern "c" fn SetGesturesEnabled(flags: c_uint) void; +pub extern "c" fn SetGesturesEnabled(flags: rl.Gesture) void; pub extern "c" fn IsGestureDetected(gesture: rl.Gesture) bool; pub extern "c" fn GetGestureDetected() rl.Gesture; pub extern "c" fn GetGestureHoldDuration() f32; @@ -269,7 +269,7 @@ pub extern "c" fn CheckCollisionLines(startPos1: rl.Vector2, endPos1: rl.Vector2 pub extern "c" fn CheckCollisionPointLine(point: rl.Vector2, p1: rl.Vector2, p2: rl.Vector2, threshold: c_int) bool; pub extern "c" fn GetCollisionRec(rec1: rl.Rectangle, rec2: rl.Rectangle) rl.Rectangle; pub extern "c" fn LoadImage(fileName: [*c]const u8) rl.Image; -pub extern "c" fn LoadImageRaw(fileName: [*c]const u8, width: c_int, height: c_int, format: c_int, headerSize: c_int) rl.Image; +pub extern "c" fn LoadImageRaw(fileName: [*c]const u8, width: c_int, height: c_int, format: rl.PixelFormat, headerSize: c_int) rl.Image; pub extern "c" fn LoadImageSvg(fileNameOrString: [*c]const u8, width: c_int, height: c_int) rl.Image; pub extern "c" fn LoadImageAnim(fileName: [*c]const u8, frames: [*c]c_int) rl.Image; pub extern "c" fn LoadImageAnimFromMemory(fileType: [*c]const u8, fileData: [*c]const u8, dataSize: c_int, frames: [*c]c_int) rl.Image; @@ -294,7 +294,7 @@ pub extern "c" fn ImageCopy(image: rl.Image) rl.Image; pub extern "c" fn ImageFromImage(image: rl.Image, rec: rl.Rectangle) rl.Image; pub extern "c" fn ImageText(text: [*c]const u8, fontSize: c_int, color: rl.Color) rl.Image; pub extern "c" fn ImageTextEx(font: rl.Font, text: [*c]const u8, fontSize: f32, spacing: f32, tint: rl.Color) rl.Image; -pub extern "c" fn ImageFormat(image: [*c]rl.Image, newFormat: c_int) void; +pub extern "c" fn ImageFormat(image: [*c]rl.Image, newFormat: rl.PixelFormat) void; pub extern "c" fn ImageToPOT(image: [*c]rl.Image, fill: rl.Color) void; pub extern "c" fn ImageCrop(image: [*c]rl.Image, crop: rl.Rectangle) void; pub extern "c" fn ImageAlphaCrop(image: [*c]rl.Image, threshold: f32) void; @@ -343,7 +343,7 @@ pub extern "c" fn ImageDrawText(dst: [*c]rl.Image, text: [*c]const u8, posX: c_i pub extern "c" fn ImageDrawTextEx(dst: [*c]rl.Image, font: rl.Font, text: [*c]const u8, position: rl.Vector2, fontSize: f32, spacing: f32, tint: rl.Color) void; pub extern "c" fn LoadTexture(fileName: [*c]const u8) rl.Texture2D; pub extern "c" fn LoadTextureFromImage(image: rl.Image) rl.Texture2D; -pub extern "c" fn LoadTextureCubemap(image: rl.Image, layout: c_int) rl.TextureCubemap; +pub extern "c" fn LoadTextureCubemap(image: rl.Image, layout: rl.CubemapLayout) rl.TextureCubemap; pub extern "c" fn LoadRenderTexture(width: c_int, height: c_int) rl.RenderTexture2D; pub extern "c" fn IsTextureReady(texture: rl.Texture2D) bool; pub extern "c" fn UnloadTexture(texture: rl.Texture2D) void; @@ -352,7 +352,7 @@ pub extern "c" fn UnloadRenderTexture(target: rl.RenderTexture2D) void; pub extern "c" fn UpdateTexture(texture: rl.Texture2D, pixels: *const anyopaque) void; pub extern "c" fn UpdateTextureRec(texture: rl.Texture2D, rec: rl.Rectangle, pixels: *const anyopaque) void; pub extern "c" fn GenTextureMipmaps(texture: [*c]rl.Texture2D) void; -pub extern "c" fn SetTextureFilter(texture: rl.Texture2D, filter: c_int) void; +pub extern "c" fn SetTextureFilter(texture: rl.Texture2D, filter: rl.TextureFilter) void; pub extern "c" fn SetTextureWrap(texture: rl.Texture2D, wrap: c_int) void; pub extern "c" fn DrawTexture(texture: rl.Texture2D, posX: c_int, posY: c_int, tint: rl.Color) void; pub extern "c" fn DrawTextureV(texture: rl.Texture2D, position: rl.Vector2, tint: rl.Color) void; @@ -373,16 +373,16 @@ pub extern "c" fn ColorContrast(color: rl.Color, contrast: f32) rl.Color; pub extern "c" fn ColorAlpha(color: rl.Color, alpha: f32) rl.Color; pub extern "c" fn ColorAlphaBlend(dst: rl.Color, src: rl.Color, tint: rl.Color) rl.Color; pub extern "c" fn GetColor(hexValue: c_uint) rl.Color; -pub extern "c" fn GetPixelColor(srcPtr: *anyopaque, format: c_int) rl.Color; -pub extern "c" fn SetPixelColor(dstPtr: *anyopaque, color: rl.Color, format: c_int) void; -pub extern "c" fn GetPixelDataSize(width: c_int, height: c_int, format: c_int) c_int; +pub extern "c" fn GetPixelColor(srcPtr: *anyopaque, format: rl.PixelFormat) rl.Color; +pub extern "c" fn SetPixelColor(dstPtr: *anyopaque, color: rl.Color, format: rl.PixelFormat) void; +pub extern "c" fn GetPixelDataSize(width: c_int, height: c_int, format: rl.PixelFormat) c_int; pub extern "c" fn GetFontDefault() rl.Font; pub extern "c" fn LoadFont(fileName: [*c]const u8) rl.Font; pub extern "c" fn LoadFontEx(fileName: [*c]const u8, fontSize: c_int, codepoints: [*c]c_int, codepointCount: c_int) rl.Font; pub extern "c" fn LoadFontFromImage(image: rl.Image, key: rl.Color, firstChar: c_int) rl.Font; pub extern "c" fn LoadFontFromMemory(fileType: [*c]const u8, fileData: [*c]const u8, dataSize: c_int, fontSize: c_int, codepoints: [*c]c_int, codepointCount: c_int) rl.Font; pub extern "c" fn IsFontReady(font: rl.Font) bool; -pub extern "c" fn LoadFontData(fileData: [*c]const u8, dataSize: c_int, fontSize: c_int, codepoints: [*c]c_int, codepointCount: c_int, ty: c_int) [*c]rl.GlyphInfo; +pub extern "c" fn LoadFontData(fileData: [*c]const u8, dataSize: c_int, fontSize: c_int, codepoints: [*c]c_int, codepointCount: c_int, ty: rl.FontType) [*c]rl.GlyphInfo; pub extern "c" fn GenImageFontAtlas(glyphs: [*c]const rl.GlyphInfo, glyphRecs: [*c][*c]rl.Rectangle, glyphCount: c_int, fontSize: c_int, padding: c_int, packMethod: c_int) rl.Image; pub extern "c" fn UnloadFontData(glyphs: [*c]rl.GlyphInfo, glyphCount: c_int) void; pub extern "c" fn UnloadFont(font: rl.Font) void; @@ -482,7 +482,7 @@ pub extern "c" fn LoadMaterials(fileName: [*c]const u8, materialCount: [*c]c_int pub extern "c" fn LoadMaterialDefault() rl.Material; pub extern "c" fn IsMaterialReady(material: rl.Material) bool; pub extern "c" fn UnloadMaterial(material: rl.Material) void; -pub extern "c" fn SetMaterialTexture(material: [*c]rl.Material, mapType: c_int, texture: rl.Texture2D) void; +pub extern "c" fn SetMaterialTexture(material: [*c]rl.Material, mapType: rl.MaterialMapIndex, texture: rl.Texture2D) void; pub extern "c" fn SetModelMeshMaterial(model: [*c]rl.Model, meshId: c_int, materialId: c_int) void; pub extern "c" fn LoadModelAnimations(fileName: [*c]const u8, animCount: [*c]c_int) [*c]rl.ModelAnimation; pub extern "c" fn UpdateModelAnimation(model: rl.Model, anim: rl.ModelAnimation, frame: c_int) void; diff --git a/lib/raylib.zig b/lib/raylib.zig index 77bfb64..24b8b4b 100644 --- a/lib/raylib.zig +++ b/lib/raylib.zig @@ -173,7 +173,7 @@ pub const Image = extern struct { } pub fn initRaw(fileName: [:0]const u8, width: i32, height: i32, format: PixelFormat, headerSize: i32) Image { - return rl.loadImageRaw(fileName, width, height, @intFromEnum(format), headerSize); + return rl.loadImageRaw(fileName, width, height, format, headerSize); } pub fn initAnim(fileName: [:0]const u8, frames: *i32) Image { @@ -244,8 +244,7 @@ pub const Image = extern struct { return rl.imageFromImage(self, rec); } - // @todo: use PixelFormat enum for newFormat - pub fn setFormat(self: *Image, newFormat: i32) void { + pub fn setFormat(self: *Image, newFormat: PixelFormat) void { return rl.imageFormat(self, newFormat); } @@ -429,8 +428,7 @@ pub const Image = extern struct { return Texture.fromImage(self); } - // @todo: use CubemapLayout enum for layout - pub fn asCubemap(self: Image, layout: i32) Texture { + pub fn asCubemap(self: Image, layout: CubemapLayout) Texture { return Texture.fromCubemap(self, layout); } }; @@ -450,7 +448,7 @@ pub const Texture = extern struct { return rl.loadTextureFromImage(image); } - pub fn fromCubemap(image: Image, layout: i32) Texture { + pub fn fromCubemap(image: Image, layout: CubemapLayout) Texture { return rl.loadTextureCubemap(image, layout); } @@ -1313,10 +1311,10 @@ pub fn loadFontFromMemory(fileType: [:0]const u8, fileData: ?[]const u8, fontSiz return cdef.LoadFontFromMemory(@as([*c]const u8, @ptrCast(fileType)), @as([*c]const u8, @ptrCast(fileDataFinal)), @as(c_int, @intCast(fileDataLen)), @as(c_int, fontSize), @as([*c]c_int, @ptrCast(fontChars)), @as(c_int, @intCast(fontChars.len))); } -pub fn loadFontData(fileData: []const u8, fontSize: i32, fontChars: []i32, ty: i32) RaylibError![]GlyphInfo { +pub fn loadFontData(fileData: []const u8, fontSize: i32, fontChars: []i32, ty: FontType) RaylibError![]GlyphInfo { var res: []GlyphInfo = undefined; - const ptr = cdef.LoadFontData(@as([*c]const u8, @ptrCast(fileData)), @as(c_int, @intCast(fileData.len)), @as(c_int, fontSize), @as([*c]c_int, @ptrCast(fontChars)), @as(c_int, @intCast(fontChars.len)), @as(c_int, ty)); + const ptr = cdef.LoadFontData(@as([*c]const u8, @ptrCast(fileData)), @as(c_int, @intCast(fileData.len)), @as(c_int, fontSize), @as([*c]c_int, @ptrCast(fontChars)), @as(c_int, @intCast(fontChars.len)), ty); if (ptr == 0) return RaylibError.GenericError; res.ptr = @as([*]GlyphInfo, @ptrCast(ptr)); @@ -1718,8 +1716,8 @@ pub fn endShaderMode() void { cdef.EndShaderMode(); } -pub fn beginBlendMode(mode: i32) void { - cdef.BeginBlendMode(@as(c_int, mode)); +pub fn beginBlendMode(mode: BlendMode) void { + cdef.BeginBlendMode(mode); } pub fn endBlendMode() void { @@ -2182,8 +2180,8 @@ pub fn getTouchPointCount() i32 { return @as(i32, cdef.GetTouchPointCount()); } -pub fn setGesturesEnabled(flags: u32) void { - cdef.SetGesturesEnabled(@as(c_uint, flags)); +pub fn setGesturesEnabled(flags: Gesture) void { + cdef.SetGesturesEnabled(flags); } pub fn isGestureDetected(gesture: Gesture) bool { @@ -2470,8 +2468,8 @@ pub fn loadImage(fileName: [:0]const u8) Image { return cdef.LoadImage(@as([*c]const u8, @ptrCast(fileName))); } -pub fn loadImageRaw(fileName: [:0]const u8, width: i32, height: i32, format: i32, headerSize: i32) Image { - return cdef.LoadImageRaw(@as([*c]const u8, @ptrCast(fileName)), @as(c_int, width), @as(c_int, height), @as(c_int, format), @as(c_int, headerSize)); +pub fn loadImageRaw(fileName: [:0]const u8, width: i32, height: i32, format: PixelFormat, headerSize: i32) Image { + return cdef.LoadImageRaw(@as([*c]const u8, @ptrCast(fileName)), @as(c_int, width), @as(c_int, height), format, @as(c_int, headerSize)); } pub fn loadImageSvg(fileNameOrString: [:0]const u8, width: i32, height: i32) Image { @@ -2562,8 +2560,8 @@ pub fn imageTextEx(font: Font, text: [:0]const u8, fontSize: f32, spacing: f32, return cdef.ImageTextEx(font, @as([*c]const u8, @ptrCast(text)), fontSize, spacing, tint); } -pub fn imageFormat(image: *Image, newFormat: i32) void { - cdef.ImageFormat(@as([*c]Image, @ptrCast(image)), @as(c_int, newFormat)); +pub fn imageFormat(image: *Image, newFormat: PixelFormat) void { + cdef.ImageFormat(@as([*c]Image, @ptrCast(image)), newFormat); } pub fn imageToPOT(image: *Image, fill: Color) void { @@ -2750,8 +2748,8 @@ pub fn loadTextureFromImage(image: Image) Texture2D { return cdef.LoadTextureFromImage(image); } -pub fn loadTextureCubemap(image: Image, layout: i32) TextureCubemap { - return cdef.LoadTextureCubemap(image, @as(c_int, layout)); +pub fn loadTextureCubemap(image: Image, layout: CubemapLayout) TextureCubemap { + return cdef.LoadTextureCubemap(image, layout); } pub fn loadRenderTexture(width: i32, height: i32) RenderTexture2D { @@ -2786,8 +2784,8 @@ pub fn genTextureMipmaps(texture: *Texture2D) void { cdef.GenTextureMipmaps(@as([*c]Texture2D, @ptrCast(texture))); } -pub fn setTextureFilter(texture: Texture2D, filter: i32) void { - cdef.SetTextureFilter(texture, @as(c_int, filter)); +pub fn setTextureFilter(texture: Texture2D, filter: TextureFilter) void { + cdef.SetTextureFilter(texture, filter); } pub fn setTextureWrap(texture: Texture2D, wrap: i32) void { @@ -2870,16 +2868,16 @@ pub fn getColor(hexValue: u32) Color { return cdef.GetColor(@as(c_uint, hexValue)); } -pub fn getPixelColor(srcPtr: *anyopaque, format: i32) Color { - return cdef.GetPixelColor(srcPtr, @as(c_int, format)); +pub fn getPixelColor(srcPtr: *anyopaque, format: PixelFormat) Color { + return cdef.GetPixelColor(srcPtr, format); } -pub fn setPixelColor(dstPtr: *anyopaque, color: Color, format: i32) void { - cdef.SetPixelColor(dstPtr, color, @as(c_int, format)); +pub fn setPixelColor(dstPtr: *anyopaque, color: Color, format: PixelFormat) void { + cdef.SetPixelColor(dstPtr, color, format); } -pub fn getPixelDataSize(width: i32, height: i32, format: i32) i32 { - return @as(i32, cdef.GetPixelDataSize(@as(c_int, width), @as(c_int, height), @as(c_int, format))); +pub fn getPixelDataSize(width: i32, height: i32, format: PixelFormat) i32 { + return @as(i32, cdef.GetPixelDataSize(@as(c_int, width), @as(c_int, height), format)); } pub fn getFontDefault() Font { @@ -3250,8 +3248,8 @@ pub fn unloadMaterial(material: Material) void { cdef.UnloadMaterial(material); } -pub fn setMaterialTexture(material: *Material, mapType: i32, texture: Texture2D) void { - cdef.SetMaterialTexture(@as([*c]Material, @ptrCast(material)), @as(c_int, mapType), texture); +pub fn setMaterialTexture(material: *Material, mapType: MaterialMapIndex, texture: Texture2D) void { + cdef.SetMaterialTexture(@as([*c]Material, @ptrCast(material)), mapType, texture); } pub fn setModelMeshMaterial(model: *Model, meshId: i32, materialId: i32) void {