diff --git a/.gitignore b/.gitignore index 750f658..e441bdb 100755 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ raylib-headers/raymath.h **/.DS_Store **/CMakeLists.txt **/cmake-build-debug -**.c \ No newline at end of file +examples/**/**.c \ No newline at end of file diff --git a/lib/raylib-zig.zig b/lib/raylib-zig.zig index 038b3ef..da7bf6e 100755 --- a/lib/raylib-zig.zig +++ b/lib/raylib-zig.zig @@ -1189,7 +1189,6 @@ pub const FormatText = TextFormat; // ---------- WORKAROUND ---------- pub extern fn WGetMouseRay(mousePosition: [*c]const Vector2, camera: Camera) Ray; - pub extern fn WDrawPixel(posX: c_int, posY: c_int, color: [*c]const Color) void; pub extern fn WDrawPixelV(position: [*c]const Vector2, color: [*c]const Color) void; pub extern fn WDrawLine(startPosX: c_int, startPosY: c_int, endPosX: c_int, endPosY: c_int, color: [*c]const Color) void; @@ -1227,15 +1226,12 @@ pub extern fn WDrawPolyLines(center: [*c]const Vector2, sides: c_int, radius: f3 pub extern fn WCheckCollisionRecs(rec1: [*c]const Rectangle, rec2: [*c]const Rectangle) bool; pub extern fn WCheckCollisionCircles(center1: [*c]const Vector2, radius1: f32, center2: [*c]const Vector2, radius2: f32) bool; pub extern fn WCheckCollisionCircleRec(center: [*c]const Vector2, radius: f32, rec: [*c]const Rectangle) bool; -pub extern fn WGetCollisionRec(rec1: [*c]const Rectangle, rec2: [*c]const Rectangle) [*c]const Rectangle; pub extern fn WCheckCollisionPointRec(point: [*c]const Vector2, rec: [*c]const Rectangle) bool; pub extern fn WCheckCollisionPointCircle(point: [*c]const Vector2, center: [*c]const Vector2, radius: f32) bool; pub extern fn WCheckCollisionPointTriangle(point: [*c]const Vector2, p1: [*c]const Vector2, p2: [*c]const Vector2, p3: [*c]const Vector2) bool; - pub extern fn WDrawSphere(centerPos: [*c]const Vector3, radius: f32, color: [*c]const Color) void; pub extern fn WDrawRay(ray: Ray, color: [*c]const Color) void; - pub fn GetMouseRay(mousePosition: Vector2, camera: Camera) Ray { return WGetMouseRay(&mousePosition, camera); @@ -1413,37 +1409,32 @@ pub fn DrawPolyLines(center: Vector2, sides: c_int, radius: f32, rotation: f32, pub fn CheckCollisionRecs(rec1: Rectangle, rec2: Rectangle) bool { - WCheckCollisionRecs(&rec1, &rec2); + return WCheckCollisionRecs(&rec1, &rec2); } pub fn CheckCollisionCircles(center1: Vector2, radius1: f32, center2: Vector2, radius2: f32) bool { - WCheckCollisionCircles(¢er1, radius1, ¢er2, radius2); + return WCheckCollisionCircles(¢er1, radius1, ¢er2, radius2); } pub fn CheckCollisionCircleRec(center: Vector2, radius: f32, rec: Rectangle) bool { - WCheckCollisionCircleRec(¢er, radius, &rec); -} - -pub fn GetCollisionRec(rec1: Rectangle, rec2: Rectangle) [*c]const Rectangle -{ - WGetCollisionRec(&rec1, &rec2); + return WCheckCollisionCircleRec(¢er, radius, &rec); } pub fn CheckCollisionPointRec(point: Vector2, rec: Rectangle) bool { - WCheckCollisionPointRec(&point, &rec); + return WCheckCollisionPointRec(&point, &rec); } pub fn CheckCollisionPointCircle(point: Vector2, center: Vector2, radius: f32) bool { - WCheckCollisionPointCircle(&point, ¢er, radius); + return WCheckCollisionPointCircle(&point, ¢er, radius); } pub fn CheckCollisionPointTriangle(point: Vector2, p1: Vector2, p2: Vector2, p3: Vector2) bool { - WCheckCollisionPointTriangle(&point, &p1, &p2, &p3); + return WCheckCollisionPointTriangle(&point, &p1, &p2, &p3); } pub fn DrawSphere(centerPos: Vector3, radius: f32, color: Color) void diff --git a/lib/workaround/cfunctions b/lib/workaround/cfunctions new file mode 100644 index 0000000..55b11a1 --- /dev/null +++ b/lib/workaround/cfunctions @@ -0,0 +1,44 @@ +Ray GetMouseRay(Vector2 mousePosition, Camera camera); +void DrawPixel(int posX, int posY, Color color); +void DrawPixelV(Vector2 position, Color color); +void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color); +void DrawLineV(Vector2 startPos, Vector2 endPos, Color color); +void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color); +void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color); +void DrawLineStrip(Vector2 *points, int numPoints, Color color); +void DrawCircle(int centerX, int centerY, float radius, Color color); +void DrawCircleSector(Vector2 center, float radius, int startAngle, int endAngle, int segments, Color color); +void DrawCircleSectorLines(Vector2 center, float radius, int startAngle, int endAngle, int segments, Color color); +void DrawCircleGradient(int centerX, int centerY, float radius, Color color1, Color color2); +void DrawCircleV(Vector2 center, float radius, Color color); +void DrawCircleLines(int centerX, int centerY, float radius, Color color); +void DrawEllipse(int centerX, int centerY, float radiusH, float radiusV, Color color); +void DrawEllipseLines(int centerX, int centerY, float radiusH, float radiusV, Color color); +void DrawRing(Vector2 center, float innerRadius, float outerRadius, int startAngle, int endAngle, int segments, Color color); +void DrawRingLines(Vector2 center, float innerRadius, float outerRadius, int startAngle, int endAngle, int segments, Color color); +void DrawRectangle(int posX, int posY, int width, int height, Color color); +void DrawRectangleV(Vector2 position, Vector2 size, Color color); +void DrawRectangleRec(Rectangle rec, Color color); +void DrawRectanglePro(Rectangle rec, Vector2 origin, float rotation, Color color); +void DrawRectangleGradientV(int posX, int posY, int width, int height, Color color1, Color color2); +void DrawRectangleGradientH(int posX, int posY, int width, int height, Color color1, Color color2); +void DrawRectangleGradientEx(Rectangle rec, Color col1, Color col2, Color col3, Color col4); +void DrawRectangleLines(int posX, int posY, int width, int height, Color color); +void DrawRectangleLinesEx(Rectangle rec, int lineThick, Color color); +void DrawRectangleRounded(Rectangle rec, float roundness, int segments, Color color); +void DrawRectangleRoundedLines(Rectangle rec, float roundness, int segments, int lineThick, Color color); +void DrawTriangle(Vector2 v1, Vector2 v2, Vector2 v3, Color color); +void DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color); +void DrawTriangleFan(Vector2 *points, int numPoints, Color color); +void DrawTriangleStrip(Vector2 *points, int pointsCount, Color color); +void DrawPoly(Vector2 center, int sides, float radius, float rotation, Color color); +void DrawPolyLines(Vector2 center, int sides, float radius, float rotation, Color color); +bool CheckCollisionRecs(Rectangle rec1, Rectangle rec2); +bool CheckCollisionCircles(Vector2 center1, float radius1, Vector2 center2, float radius2); +bool CheckCollisionCircleRec(Vector2 center, float radius, Rectangle rec); +Rectangle GetCollisionRec(Rectangle rec1, Rectangle rec2); +bool CheckCollisionPointRec(Vector2 point, Rectangle rec); +bool CheckCollisionPointCircle(Vector2 point, Vector2 center, float radius); +bool CheckCollisionPointTriangle(Vector2 point, Vector2 p1, Vector2 p2, Vector2 p3); +void DrawSphere(Vector3 centerPos, float radius, Color color); +void DrawRay(Ray ray, Color color); diff --git a/lib/workaround/functions b/lib/workaround/functions deleted file mode 100644 index 8b046be..0000000 --- a/lib/workaround/functions +++ /dev/null @@ -1,41 +0,0 @@ -pub extern fn WDrawPixel(posX: c_int, posY: c_int, color: [*c]const Color) void; -pub extern fn WDrawPixelV(position: [*c]const Vector2, color: [*c]const Color) void; -pub extern fn WDrawLine(startPosX: c_int, startPosY: c_int, endPosX: c_int, endPosY: c_int, color: [*c]const Color) void; -pub extern fn WDrawLineV(startPos: [*c]const Vector2, endPos: [*c]const Vector2, color: [*c]const Color) void; -pub extern fn WDrawLineEx(startPos: [*c]const Vector2, endPos: [*c]const Vector2, thick: f32, color: [*c]const Color) void; -pub extern fn WDrawLineBezier(startPos: [*c]const Vector2, endPos: [*c]const Vector2, thick: f32, color: [*c]const Color) void; -pub extern fn WDrawLineStrip(points: [*c]const Vector2, numPoints: c_int, color: [*c]const Color) void; -pub extern fn WDrawCircle(centerX: c_int, centerY: c_int, radius: f32, color: [*c]const Color) void; -pub extern fn WDrawCircleSector(center: [*c]const Vector2, radius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: [*c]const Color) void; -pub extern fn WDrawCircleSectorLines(center: [*c]const Vector2, radius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: [*c]const Color) void; -pub extern fn WDrawCircleGradient(centerX: c_int, centerY: c_int, radius: f32, color1: [*c]const Color, color2: [*c]const Color) void; -pub extern fn WDrawCircleV(center: [*c]const Vector2, radius: f32, color: [*c]const Color) void; -pub extern fn WDrawCircleLines(centerX: c_int, centerY: c_int, radius: f32, color: [*c]const Color) void; -pub extern fn WDrawEllipse(centerX: c_int, centerY: c_int, radiusH: f32, radiusV: f32, color: [*c]const Color) void; -pub extern fn WDrawEllipseLines(centerX: c_int, centerY: c_int, radiusH: f32, radiusV: f32, color: [*c]const Color) void; -pub extern fn WDrawRing(center: [*c]const Vector2, innerRadius: f32, outerRadius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: [*c]const Color) void; -pub extern fn WDrawRingLines(center: [*c]const Vector2, innerRadius: f32, outerRadius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: [*c]const Color) void; -pub extern fn WDrawRectangle(posX: c_int, posY: c_int, width: c_int, height: c_int, color: [*c]const Color) void; -pub extern fn WDrawRectangleV(position: [*c]const Vector2, size: [*c]const Vector2, color: [*c]const Color) void; -pub extern fn WDrawRectangleRec(rec: [*c]const Rectangle, color: [*c]const Color) void; -pub extern fn WDrawRectanglePro(rec: [*c]const Rectangle, origin: [*c]const Vector2, rotation: f32, color: [*c]const Color) void; -pub extern fn WDrawRectangleGradientV(posX: c_int, posY: c_int, width: c_int, height: c_int, color1: [*c]const Color, color2: [*c]const Color) void; -pub extern fn WDrawRectangleGradientH(posX: c_int, posY: c_int, width: c_int, height: c_int, color1: [*c]const Color, color2: [*c]const Color) void; -pub extern fn WDrawRectangleGradientEx(rec: [*c]const Rectangle, col1: [*c]const Color, col2: [*c]const Color, col3: [*c]const Color, col4: [*c]const Color) void; -pub extern fn WDrawRectangleLines(posX: c_int, posY: c_int, width: c_int, height: c_int, color: [*c]const Color) void; -pub extern fn WDrawRectangleLinesEx(rec: [*c]const Rectangle, lineThick: c_int, color: [*c]const Color) void; -pub extern fn WDrawRectangleRounded(rec: [*c]const Rectangle, roundness: f32, segments: c_int, color: [*c]const Color) void; -pub extern fn WDrawRectangleRoundedLines(rec: [*c]const Rectangle, roundness: f32, segments: c_int, lineThick: c_int, color: [*c]const Color) void; -pub extern fn WDrawTriangle(v1: [*c]const Vector2, v2: [*c]const Vector2, v3: [*c]const Vector2, color: [*c]const Color) void; -pub extern fn WDrawTriangleLines(v1: [*c]const Vector2, v2: [*c]const Vector2, v3: [*c]const Vector2, color: [*c]const Color) void; -pub extern fn WDrawTriangleFan(points: [*c]const Vector2, numPoints: c_int, color: [*c]const Color) void; -pub extern fn WDrawTriangleStrip(points: [*c]const Vector2, pointsCount: c_int, color: [*c]const Color) void; -pub extern fn WDrawPoly(center: [*c]const Vector2, sides: c_int, radius: f32, rotation: f32, color: [*c]const Color) void; -pub extern fn WDrawPolyLines(center: [*c]const Vector2, sides: c_int, radius: f32, rotation: f32, color: [*c]const Color) void; -pub extern fn WCheckCollisionRecs(rec1: [*c]const Rectangle, rec2: [*c]const Rectangle) bool; -pub extern fn WCheckCollisionCircles(center1: [*c]const Vector2, radius1: f32, center2: [*c]const Vector2, radius2: f32) bool; -pub extern fn WCheckCollisionCircleRec(center: [*c]const Vector2, radius: f32, rec: [*c]const Rectangle) bool; -pub extern fn WGetCollisionRec(rec1: [*c]const Rectangle, rec2: [*c]const Rectangle) [*c]const Rectangle; -pub extern fn WCheckCollisionPointRec(point: [*c]const Vector2, rec: [*c]const Rectangle) bool; -pub extern fn WCheckCollisionPointCircle(point: [*c]const Vector2, center: [*c]const Vector2, radius: f32) bool; -pub extern fn WCheckCollisionPointTriangle(point: [*c]const Vector2, p1: [*c]const Vector2, p2: [*c]const Vector2, p3: [*c]const Vector2) bool; \ No newline at end of file diff --git a/lib/workaround/generate_c_functions.py b/lib/workaround/generate_c_functions.py new file mode 100644 index 0000000..2f0b5ba --- /dev/null +++ b/lib/workaround/generate_c_functions.py @@ -0,0 +1,138 @@ +import re + +""" +Automatic utility for generating workaround functions for both C and Zig +to fix zig's issue with C ABI struct parameter. Put all functions that have +a parameter with one of the types listed in small_structs into cfunctions. +If one of those functions has a returns type listed in small_structs it +will be automatically ignored. Make sure raylib-zig.zig doesn't already +contain a function with the given names before copying the workaround in +""" + +raymath = open("cfunctions") + + +# Some c types have a different size on different systems +# and zig knows that so we tell it to get the system specific size for us +def c_to_zig_type(t: str) -> str: + if t == "float": + t = "f32" + if t == "int": + t = "c_int" + if t == "unsigned int": + t = "c_uint" + if t == "long": + t = "c_long" + if t == "void": + t = "c_void" + return t + + +def fix_pointer(name: str, type: str): + if name.startswith("*"): + name = name[1:] + type = "[*c]const " + type + return name, type + + +small_structs = ["Vector2", "Vector3", "Vector4", "Quaternion", "Color", "Rectangle", "Shader"] +zig_functions = [] +c_functions = [] +zig_heads = [] + +for line in raymath.readlines(): + # each (.*) is some variable value + result = re.search("(.*) (.*)start_arg(.*)end_arg", line.replace("(", "start_arg").replace(")", "end_arg")) + + # get whats in the (.*)'s + return_type = result.group(1) + func_name = result.group(2) + arguments = result.group(3) + + if return_type in small_structs: + continue + + zig_arguments_w = [] # arguments for the workaround function head on the zig side + zig_arguments = [] # arguments that are passed to the copied raylib function + zig_pass = [] # arguments that are passed to the workaround function on the zig side + c_arguments = [] # arguments for the workaround function head on the c side + c_pass = [] # arguments that are passed to the actual raylib function + + for arg in arguments.split(", "): + if arg == "void": break + arg_type = " ".join(arg.split(" ")[0:-1]).replace("const ", "") # everything but the last element (for stuff like "const Vector3"), but discarding const + arg_name = arg.split(" ")[-1] # last element should be the name + depoint = False # set to true if we need to dereference a pointer to a small struct in the c workaround + if arg_type in small_structs: + if not arg_name.startswith("*"): + depoint = True + + if depoint: + arg_name = "*" + arg_name # dereference the arguments + + c_arguments.append(arg_type + " " + arg_name) + if arg_name.startswith("*") and not depoint: # That's in case of an actual array + c_pass.append(arg_name[1:]) # We don't want to dereference the array + else: + c_pass.append(arg_name) + + # zig conversions + zig_type = c_to_zig_type(arg_type) + if depoint and arg_name.startswith("*"): # These are the arguments without pointers + zig_name = arg_name[1:] + zig_pass_name = "&" + zig_name + elif arg_name.startswith("*") and not depoint: # That's in case of an actual array + zig_name = arg_name[1:] + zig_type = "[]const " + arg_type + zig_pass_name = "&" + arg_name[1:] + "[0]" + else: # Normal argument i.e. float, int, etc. + zig_name = arg_name + zig_pass_name = zig_name + zig_arguments.append(zig_name + ": " + zig_type) # put everything together + zig_pass.append(zig_pass_name) + + # These are the arguments for the extern workaround functions with pointers + arg_type = c_to_zig_type(arg_type) + arg_name, arg_type = fix_pointer(arg_name, arg_type) + zig_arguments_w.append(arg_name + ": " + arg_type) # put everything together + + # Workaround function head in zig + zig_arguments_w = ", ".join(zig_arguments_w) + zig_ret_type = c_to_zig_type(return_type) + zig_func_name, zig_ret_type = fix_pointer(func_name, zig_ret_type) + zig_heads.append("pub extern fn W" + func_name + "(" + zig_arguments_w + ") " + return_type + ";") + + # Create the function to call the workaround + zig_arguments = ", ".join(zig_arguments) + zig_pass = ", ".join(zig_pass) + function = "pub fn " + func_name + "(" + zig_arguments + ") " + return_type + "\n" + function += "{\n" + if return_type != "void": + function += " return W" + func_name + "(" + zig_pass + ");\n" + else: + function += " W" + func_name + "(" + zig_pass + ");\n" + function += "}\n" + zig_functions.append(function) + + # Create workaround function + c_arguments = ", ".join(c_arguments) + c_pass = ", ".join(c_pass) + function = return_type + " W" + func_name + "(" + c_arguments + ")\n" + function += "{\n" + if return_type != "void": + function += " return " + func_name + "(" + c_pass + ");\n" + else: + function += " " + func_name + "(" + c_pass + ");\n" + function += "}\n" + c_functions.append(function) + +workaround = open("workaround.c", mode="w") +print("// raylib-zig (c) Nikolas Wipper 2020\n", file=workaround) +print("#include \n", file=workaround) + +zigworkaround = open("workaround.zig", mode="w") + +print("\n".join(c_functions), file=workaround) +print("\n".join(zig_heads), file=zigworkaround) +print("", file=zigworkaround) +print("\n".join(zig_functions), file=zigworkaround) diff --git a/lib/workaround/generate_functions.py b/lib/workaround/generate_functions.py deleted file mode 100644 index fbbfb30..0000000 --- a/lib/workaround/generate_functions.py +++ /dev/null @@ -1,36 +0,0 @@ -import re - -""" -Automatic utility for generating function calls to workaround functions -""" - -if __name__ == "__main__": - pass - -raymath = open("functions") - - -for line in raymath.readlines(): - # each (.*) is some variable value - result = re.search("pub extern fn (.*)start_arg(.*)end_arg (.*);", line.replace("(", "start_arg").replace(")", "end_arg")) - - # get whats in the (.*)'s - func_name = result.group(1) - arguments = result.group(2) - return_type = result.group(3) - - args = [] - names = [] - for arg in arguments.split(", "): - result = re.search("(.*): (.*)", arg) - arg_name = result.group(1) - arg_type = result.group(2) - args.append(arg_name + ": " + arg_type.replace("[*c]", "").replace("const ", "")) # put everything together - if "[*c]" in arg_type: - arg_name = "&" + arg_name - names.append(arg_name) - args = ", ".join(args) - print("pub fn " + func_name[1:] + "(" + args + ") " + return_type) - print("{") - print(" " + func_name + "(" + (", ".join(names)) + ");") - print("}\n") diff --git a/lib/workaround/workaround.c b/lib/workaround/workaround.c new file mode 100644 index 0000000..aa1de7d --- /dev/null +++ b/lib/workaround/workaround.c @@ -0,0 +1,219 @@ +// raylib-zig (c) Nikolas Wipper 2020 + +#include + +Ray WGetMouseRay(Vector2 *mousePosition, Camera camera) +{ + return GetMouseRay(*mousePosition, camera); +} + +void WDrawPixel(int posX, int posY, Color *color) +{ + DrawPixel(posX, posY, *color); +} + +void WDrawPixelV(Vector2 *position, Color *color) +{ + DrawPixelV(*position, *color); +} + +void WDrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color *color) +{ + DrawLine(startPosX, startPosY, endPosX, endPosY, *color); +} + +void WDrawLineV(Vector2 *startPos, Vector2 *endPos, Color *color) +{ + DrawLineV(*startPos, *endPos, *color); +} + +void WDrawLineEx(Vector2 *startPos, Vector2 *endPos, float thick, Color *color) +{ + DrawLineEx(*startPos, *endPos, thick, *color); +} + +void WDrawLineBezier(Vector2 *startPos, Vector2 *endPos, float thick, Color *color) +{ + DrawLineBezier(*startPos, *endPos, thick, *color); +} + +void WDrawLineStrip(Vector2 *points, int numPoints, Color *color) +{ + DrawLineStrip(points, numPoints, *color); +} + +void WDrawCircle(int centerX, int centerY, float radius, Color *color) +{ + DrawCircle(centerX, centerY, radius, *color); +} + +void WDrawCircleSector(Vector2 *center, float radius, int startAngle, int endAngle, int segments, Color *color) +{ + DrawCircleSector(*center, radius, startAngle, endAngle, segments, *color); +} + +void WDrawCircleSectorLines(Vector2 *center, float radius, int startAngle, int endAngle, int segments, Color *color) +{ + DrawCircleSectorLines(*center, radius, startAngle, endAngle, segments, *color); +} + +void WDrawCircleGradient(int centerX, int centerY, float radius, Color *color1, Color *color2) +{ + DrawCircleGradient(centerX, centerY, radius, *color1, *color2); +} + +void WDrawCircleV(Vector2 *center, float radius, Color *color) +{ + DrawCircleV(*center, radius, *color); +} + +void WDrawCircleLines(int centerX, int centerY, float radius, Color *color) +{ + DrawCircleLines(centerX, centerY, radius, *color); +} + +void WDrawEllipse(int centerX, int centerY, float radiusH, float radiusV, Color *color) +{ + DrawEllipse(centerX, centerY, radiusH, radiusV, *color); +} + +void WDrawEllipseLines(int centerX, int centerY, float radiusH, float radiusV, Color *color) +{ + DrawEllipseLines(centerX, centerY, radiusH, radiusV, *color); +} + +void WDrawRing(Vector2 *center, float innerRadius, float outerRadius, int startAngle, int endAngle, int segments, Color *color) +{ + DrawRing(*center, innerRadius, outerRadius, startAngle, endAngle, segments, *color); +} + +void WDrawRingLines(Vector2 *center, float innerRadius, float outerRadius, int startAngle, int endAngle, int segments, Color *color) +{ + DrawRingLines(*center, innerRadius, outerRadius, startAngle, endAngle, segments, *color); +} + +void WDrawRectangle(int posX, int posY, int width, int height, Color *color) +{ + DrawRectangle(posX, posY, width, height, *color); +} + +void WDrawRectangleV(Vector2 *position, Vector2 *size, Color *color) +{ + DrawRectangleV(*position, *size, *color); +} + +void WDrawRectangleRec(Rectangle *rec, Color *color) +{ + DrawRectangleRec(*rec, *color); +} + +void WDrawRectanglePro(Rectangle *rec, Vector2 *origin, float rotation, Color *color) +{ + DrawRectanglePro(*rec, *origin, rotation, *color); +} + +void WDrawRectangleGradientV(int posX, int posY, int width, int height, Color *color1, Color *color2) +{ + DrawRectangleGradientV(posX, posY, width, height, *color1, *color2); +} + +void WDrawRectangleGradientH(int posX, int posY, int width, int height, Color *color1, Color *color2) +{ + DrawRectangleGradientH(posX, posY, width, height, *color1, *color2); +} + +void WDrawRectangleGradientEx(Rectangle *rec, Color *col1, Color *col2, Color *col3, Color *col4) +{ + DrawRectangleGradientEx(*rec, *col1, *col2, *col3, *col4); +} + +void WDrawRectangleLines(int posX, int posY, int width, int height, Color *color) +{ + DrawRectangleLines(posX, posY, width, height, *color); +} + +void WDrawRectangleLinesEx(Rectangle *rec, int lineThick, Color *color) +{ + DrawRectangleLinesEx(*rec, lineThick, *color); +} + +void WDrawRectangleRounded(Rectangle *rec, float roundness, int segments, Color *color) +{ + DrawRectangleRounded(*rec, roundness, segments, *color); +} + +void WDrawRectangleRoundedLines(Rectangle *rec, float roundness, int segments, int lineThick, Color *color) +{ + DrawRectangleRoundedLines(*rec, roundness, segments, lineThick, *color); +} + +void WDrawTriangle(Vector2 *v1, Vector2 *v2, Vector2 *v3, Color *color) +{ + DrawTriangle(*v1, *v2, *v3, *color); +} + +void WDrawTriangleLines(Vector2 *v1, Vector2 *v2, Vector2 *v3, Color *color) +{ + DrawTriangleLines(*v1, *v2, *v3, *color); +} + +void WDrawTriangleFan(Vector2 *points, int numPoints, Color *color) +{ + DrawTriangleFan(points, numPoints, *color); +} + +void WDrawTriangleStrip(Vector2 *points, int pointsCount, Color *color) +{ + DrawTriangleStrip(points, pointsCount, *color); +} + +void WDrawPoly(Vector2 *center, int sides, float radius, float rotation, Color *color) +{ + DrawPoly(*center, sides, radius, rotation, *color); +} + +void WDrawPolyLines(Vector2 *center, int sides, float radius, float rotation, Color *color) +{ + DrawPolyLines(*center, sides, radius, rotation, *color); +} + +bool WCheckCollisionRecs(Rectangle *rec1, Rectangle *rec2) +{ + return CheckCollisionRecs(*rec1, *rec2); +} + +bool WCheckCollisionCircles(Vector2 *center1, float radius1, Vector2 *center2, float radius2) +{ + return CheckCollisionCircles(*center1, radius1, *center2, radius2); +} + +bool WCheckCollisionCircleRec(Vector2 *center, float radius, Rectangle *rec) +{ + return CheckCollisionCircleRec(*center, radius, *rec); +} + +bool WCheckCollisionPointRec(Vector2 *point, Rectangle *rec) +{ + return CheckCollisionPointRec(*point, *rec); +} + +bool WCheckCollisionPointCircle(Vector2 *point, Vector2 *center, float radius) +{ + return CheckCollisionPointCircle(*point, *center, radius); +} + +bool WCheckCollisionPointTriangle(Vector2 *point, Vector2 *p1, Vector2 *p2, Vector2 *p3) +{ + return CheckCollisionPointTriangle(*point, *p1, *p2, *p3); +} + +void WDrawSphere(Vector3 *centerPos, float radius, Color *color) +{ + DrawSphere(*centerPos, radius, *color); +} + +void WDrawRay(Ray ray, Color *color) +{ + DrawRay(ray, *color); +} + diff --git a/lib/workaround/workaround.zig b/lib/workaround/workaround.zig new file mode 100644 index 0000000..9551167 --- /dev/null +++ b/lib/workaround/workaround.zig @@ -0,0 +1,259 @@ +pub extern fn WGetMouseRay(mousePosition: [*c]const Vector2, camera: Camera) Ray; +pub extern fn WDrawPixel(posX: c_int, posY: c_int, color: [*c]const Color) void; +pub extern fn WDrawPixelV(position: [*c]const Vector2, color: [*c]const Color) void; +pub extern fn WDrawLine(startPosX: c_int, startPosY: c_int, endPosX: c_int, endPosY: c_int, color: [*c]const Color) void; +pub extern fn WDrawLineV(startPos: [*c]const Vector2, endPos: [*c]const Vector2, color: [*c]const Color) void; +pub extern fn WDrawLineEx(startPos: [*c]const Vector2, endPos: [*c]const Vector2, thick: f32, color: [*c]const Color) void; +pub extern fn WDrawLineBezier(startPos: [*c]const Vector2, endPos: [*c]const Vector2, thick: f32, color: [*c]const Color) void; +pub extern fn WDrawLineStrip(points: [*c]const Vector2, numPoints: c_int, color: [*c]const Color) void; +pub extern fn WDrawCircle(centerX: c_int, centerY: c_int, radius: f32, color: [*c]const Color) void; +pub extern fn WDrawCircleSector(center: [*c]const Vector2, radius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: [*c]const Color) void; +pub extern fn WDrawCircleSectorLines(center: [*c]const Vector2, radius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: [*c]const Color) void; +pub extern fn WDrawCircleGradient(centerX: c_int, centerY: c_int, radius: f32, color1: [*c]const Color, color2: [*c]const Color) void; +pub extern fn WDrawCircleV(center: [*c]const Vector2, radius: f32, color: [*c]const Color) void; +pub extern fn WDrawCircleLines(centerX: c_int, centerY: c_int, radius: f32, color: [*c]const Color) void; +pub extern fn WDrawEllipse(centerX: c_int, centerY: c_int, radiusH: f32, radiusV: f32, color: [*c]const Color) void; +pub extern fn WDrawEllipseLines(centerX: c_int, centerY: c_int, radiusH: f32, radiusV: f32, color: [*c]const Color) void; +pub extern fn WDrawRing(center: [*c]const Vector2, innerRadius: f32, outerRadius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: [*c]const Color) void; +pub extern fn WDrawRingLines(center: [*c]const Vector2, innerRadius: f32, outerRadius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: [*c]const Color) void; +pub extern fn WDrawRectangle(posX: c_int, posY: c_int, width: c_int, height: c_int, color: [*c]const Color) void; +pub extern fn WDrawRectangleV(position: [*c]const Vector2, size: [*c]const Vector2, color: [*c]const Color) void; +pub extern fn WDrawRectangleRec(rec: [*c]const Rectangle, color: [*c]const Color) void; +pub extern fn WDrawRectanglePro(rec: [*c]const Rectangle, origin: [*c]const Vector2, rotation: f32, color: [*c]const Color) void; +pub extern fn WDrawRectangleGradientV(posX: c_int, posY: c_int, width: c_int, height: c_int, color1: [*c]const Color, color2: [*c]const Color) void; +pub extern fn WDrawRectangleGradientH(posX: c_int, posY: c_int, width: c_int, height: c_int, color1: [*c]const Color, color2: [*c]const Color) void; +pub extern fn WDrawRectangleGradientEx(rec: [*c]const Rectangle, col1: [*c]const Color, col2: [*c]const Color, col3: [*c]const Color, col4: [*c]const Color) void; +pub extern fn WDrawRectangleLines(posX: c_int, posY: c_int, width: c_int, height: c_int, color: [*c]const Color) void; +pub extern fn WDrawRectangleLinesEx(rec: [*c]const Rectangle, lineThick: c_int, color: [*c]const Color) void; +pub extern fn WDrawRectangleRounded(rec: [*c]const Rectangle, roundness: f32, segments: c_int, color: [*c]const Color) void; +pub extern fn WDrawRectangleRoundedLines(rec: [*c]const Rectangle, roundness: f32, segments: c_int, lineThick: c_int, color: [*c]const Color) void; +pub extern fn WDrawTriangle(v1: [*c]const Vector2, v2: [*c]const Vector2, v3: [*c]const Vector2, color: [*c]const Color) void; +pub extern fn WDrawTriangleLines(v1: [*c]const Vector2, v2: [*c]const Vector2, v3: [*c]const Vector2, color: [*c]const Color) void; +pub extern fn WDrawTriangleFan(points: [*c]const Vector2, numPoints: c_int, color: [*c]const Color) void; +pub extern fn WDrawTriangleStrip(points: [*c]const Vector2, pointsCount: c_int, color: [*c]const Color) void; +pub extern fn WDrawPoly(center: [*c]const Vector2, sides: c_int, radius: f32, rotation: f32, color: [*c]const Color) void; +pub extern fn WDrawPolyLines(center: [*c]const Vector2, sides: c_int, radius: f32, rotation: f32, color: [*c]const Color) void; +pub extern fn WCheckCollisionRecs(rec1: [*c]const Rectangle, rec2: [*c]const Rectangle) bool; +pub extern fn WCheckCollisionCircles(center1: [*c]const Vector2, radius1: f32, center2: [*c]const Vector2, radius2: f32) bool; +pub extern fn WCheckCollisionCircleRec(center: [*c]const Vector2, radius: f32, rec: [*c]const Rectangle) bool; +pub extern fn WCheckCollisionPointRec(point: [*c]const Vector2, rec: [*c]const Rectangle) bool; +pub extern fn WCheckCollisionPointCircle(point: [*c]const Vector2, center: [*c]const Vector2, radius: f32) bool; +pub extern fn WCheckCollisionPointTriangle(point: [*c]const Vector2, p1: [*c]const Vector2, p2: [*c]const Vector2, p3: [*c]const Vector2) bool; +pub extern fn WDrawSphere(centerPos: [*c]const Vector3, radius: f32, color: [*c]const Color) void; +pub extern fn WDrawRay(ray: Ray, color: [*c]const Color) void; + +pub fn GetMouseRay(mousePosition: Vector2, camera: Camera) Ray +{ + return WGetMouseRay(&mousePosition, camera); +} + +pub fn DrawPixel(posX: c_int, posY: c_int, color: Color) void +{ + WDrawPixel(posX, posY, &color); +} + +pub fn DrawPixelV(position: Vector2, color: Color) void +{ + WDrawPixelV(&position, &color); +} + +pub fn DrawLine(startPosX: c_int, startPosY: c_int, endPosX: c_int, endPosY: c_int, color: Color) void +{ + WDrawLine(startPosX, startPosY, endPosX, endPosY, &color); +} + +pub fn DrawLineV(startPos: Vector2, endPos: Vector2, color: Color) void +{ + WDrawLineV(&startPos, &endPos, &color); +} + +pub fn DrawLineEx(startPos: Vector2, endPos: Vector2, thick: f32, color: Color) void +{ + WDrawLineEx(&startPos, &endPos, thick, &color); +} + +pub fn DrawLineBezier(startPos: Vector2, endPos: Vector2, thick: f32, color: Color) void +{ + WDrawLineBezier(&startPos, &endPos, thick, &color); +} + +pub fn DrawLineStrip(points: []const Vector2, numPoints: c_int, color: Color) void +{ + WDrawLineStrip(&points[0], numPoints, &color); +} + +pub fn DrawCircle(centerX: c_int, centerY: c_int, radius: f32, color: Color) void +{ + WDrawCircle(centerX, centerY, radius, &color); +} + +pub fn DrawCircleSector(center: Vector2, radius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: Color) void +{ + WDrawCircleSector(¢er, radius, startAngle, endAngle, segments, &color); +} + +pub fn DrawCircleSectorLines(center: Vector2, radius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: Color) void +{ + WDrawCircleSectorLines(¢er, radius, startAngle, endAngle, segments, &color); +} + +pub fn DrawCircleGradient(centerX: c_int, centerY: c_int, radius: f32, color1: Color, color2: Color) void +{ + WDrawCircleGradient(centerX, centerY, radius, &color1, &color2); +} + +pub fn DrawCircleV(center: Vector2, radius: f32, color: Color) void +{ + WDrawCircleV(¢er, radius, &color); +} + +pub fn DrawCircleLines(centerX: c_int, centerY: c_int, radius: f32, color: Color) void +{ + WDrawCircleLines(centerX, centerY, radius, &color); +} + +pub fn DrawEllipse(centerX: c_int, centerY: c_int, radiusH: f32, radiusV: f32, color: Color) void +{ + WDrawEllipse(centerX, centerY, radiusH, radiusV, &color); +} + +pub fn DrawEllipseLines(centerX: c_int, centerY: c_int, radiusH: f32, radiusV: f32, color: Color) void +{ + WDrawEllipseLines(centerX, centerY, radiusH, radiusV, &color); +} + +pub fn DrawRing(center: Vector2, innerRadius: f32, outerRadius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: Color) void +{ + WDrawRing(¢er, innerRadius, outerRadius, startAngle, endAngle, segments, &color); +} + +pub fn DrawRingLines(center: Vector2, innerRadius: f32, outerRadius: f32, startAngle: c_int, endAngle: c_int, segments: c_int, color: Color) void +{ + WDrawRingLines(¢er, innerRadius, outerRadius, startAngle, endAngle, segments, &color); +} + +pub fn DrawRectangle(posX: c_int, posY: c_int, width: c_int, height: c_int, color: Color) void +{ + WDrawRectangle(posX, posY, width, height, &color); +} + +pub fn DrawRectangleV(position: Vector2, size: Vector2, color: Color) void +{ + WDrawRectangleV(&position, &size, &color); +} + +pub fn DrawRectangleRec(rec: Rectangle, color: Color) void +{ + WDrawRectangleRec(&rec, &color); +} + +pub fn DrawRectanglePro(rec: Rectangle, origin: Vector2, rotation: f32, color: Color) void +{ + WDrawRectanglePro(&rec, &origin, rotation, &color); +} + +pub fn DrawRectangleGradientV(posX: c_int, posY: c_int, width: c_int, height: c_int, color1: Color, color2: Color) void +{ + WDrawRectangleGradientV(posX, posY, width, height, &color1, &color2); +} + +pub fn DrawRectangleGradientH(posX: c_int, posY: c_int, width: c_int, height: c_int, color1: Color, color2: Color) void +{ + WDrawRectangleGradientH(posX, posY, width, height, &color1, &color2); +} + +pub fn DrawRectangleGradientEx(rec: Rectangle, col1: Color, col2: Color, col3: Color, col4: Color) void +{ + WDrawRectangleGradientEx(&rec, &col1, &col2, &col3, &col4); +} + +pub fn DrawRectangleLines(posX: c_int, posY: c_int, width: c_int, height: c_int, color: Color) void +{ + WDrawRectangleLines(posX, posY, width, height, &color); +} + +pub fn DrawRectangleLinesEx(rec: Rectangle, lineThick: c_int, color: Color) void +{ + WDrawRectangleLinesEx(&rec, lineThick, &color); +} + +pub fn DrawRectangleRounded(rec: Rectangle, roundness: f32, segments: c_int, color: Color) void +{ + WDrawRectangleRounded(&rec, roundness, segments, &color); +} + +pub fn DrawRectangleRoundedLines(rec: Rectangle, roundness: f32, segments: c_int, lineThick: c_int, color: Color) void +{ + WDrawRectangleRoundedLines(&rec, roundness, segments, lineThick, &color); +} + +pub fn DrawTriangle(v1: Vector2, v2: Vector2, v3: Vector2, color: Color) void +{ + WDrawTriangle(&v1, &v2, &v3, &color); +} + +pub fn DrawTriangleLines(v1: Vector2, v2: Vector2, v3: Vector2, color: Color) void +{ + WDrawTriangleLines(&v1, &v2, &v3, &color); +} + +pub fn DrawTriangleFan(points: []const Vector2, numPoints: c_int, color: Color) void +{ + WDrawTriangleFan(&points[0], numPoints, &color); +} + +pub fn DrawTriangleStrip(points: []const Vector2, pointsCount: c_int, color: Color) void +{ + WDrawTriangleStrip(&points[0], pointsCount, &color); +} + +pub fn DrawPoly(center: Vector2, sides: c_int, radius: f32, rotation: f32, color: Color) void +{ + WDrawPoly(¢er, sides, radius, rotation, &color); +} + +pub fn DrawPolyLines(center: Vector2, sides: c_int, radius: f32, rotation: f32, color: Color) void +{ + WDrawPolyLines(¢er, sides, radius, rotation, &color); +} + +pub fn CheckCollisionRecs(rec1: Rectangle, rec2: Rectangle) bool +{ + return WCheckCollisionRecs(&rec1, &rec2); +} + +pub fn CheckCollisionCircles(center1: Vector2, radius1: f32, center2: Vector2, radius2: f32) bool +{ + return WCheckCollisionCircles(¢er1, radius1, ¢er2, radius2); +} + +pub fn CheckCollisionCircleRec(center: Vector2, radius: f32, rec: Rectangle) bool +{ + return WCheckCollisionCircleRec(¢er, radius, &rec); +} + +pub fn CheckCollisionPointRec(point: Vector2, rec: Rectangle) bool +{ + return WCheckCollisionPointRec(&point, &rec); +} + +pub fn CheckCollisionPointCircle(point: Vector2, center: Vector2, radius: f32) bool +{ + return WCheckCollisionPointCircle(&point, ¢er, radius); +} + +pub fn CheckCollisionPointTriangle(point: Vector2, p1: Vector2, p2: Vector2, p3: Vector2) bool +{ + return WCheckCollisionPointTriangle(&point, &p1, &p2, &p3); +} + +pub fn DrawSphere(centerPos: Vector3, radius: f32, color: Color) void +{ + WDrawSphere(¢erPos, radius, &color); +} + +pub fn DrawRay(ray: Ray, color: Color) void +{ + WDrawRay(ray, &color); +} + diff --git a/raylib-headers/math-funcs.py b/raylib-headers/math-funcs.py index ee91ce0..ff08854 100644 --- a/raylib-headers/math-funcs.py +++ b/raylib-headers/math-funcs.py @@ -11,15 +11,22 @@ raymath = open("raymath.h") prefix = "RMDEF " + # Some c types have a different size on different systems # and zig knows that so we tell it to get the system specific size for us -def c_to_zig_type(type: str) -> str: - if type == "float": type = "f32" - if type == "int": type = "c_int" - if type == "unsigned int": type = "c_uint" - if type == "long": type = "c_long" - if type == "void": type = "c_void" - return type +def c_to_zig_type(t: str) -> str: + if t == "float": + t = "f32" + if t == "int": + t = "c_int" + if t == "unsigned int": + t = "c_uint" + if t == "long": + t = "c_long" + if t == "void": + t = "c_void" + return t + def fix_pointer(name: str, type: str): if name.startswith("*"): @@ -27,6 +34,7 @@ def fix_pointer(name: str, type: str): type = "[*c]" + type return name, type + for line in raymath.readlines(): if line.startswith(prefix): # each (.*) is some variable value @@ -43,12 +51,11 @@ for line in raymath.readlines(): zig_arguments = [] for arg in arguments.split(", "): if arg == "void": break - arg_type = " ".join(arg.split(" ")[0:-1]) # everything but the last element (for stuff like "const Vector3") - arg_type = arg_type.replace("const ", "") # zig doesn't like const in function arguments that aren't pointer and really we don't need const + arg_type = " ".join(arg.split(" ")[0:-1]) # everything but the last element (for stuff like "const Vector3") + arg_type = arg_type.replace("const ", "") # zig doesn't like const in function arguments that aren't pointer and really we don't need const arg_type = c_to_zig_type(arg_type) - arg_name = arg.split(" ")[-1] # last element should be the name + arg_name = arg.split(" ")[-1] # last element should be the name arg_name, arg_type = fix_pointer(arg_name, arg_type) - zig_arguments.append(arg_name + ": " + arg_type) # put everything together + zig_arguments.append(arg_name + ": " + arg_type) # put everything together zig_arguments = ", ".join(zig_arguments) print("pub extern fn " + func_name + "(" + zig_arguments + ") " + return_type + ";") -