From 0d8a6cfbfa474be123cedff6a4faddfe5443fcec Mon Sep 17 00:00:00 2001 From: Ray Date: Tue, 10 Oct 2023 08:48:55 +0200 Subject: [PATCH 1/2] Revert "Update zig build system to zig version 0.11.0 (#3393)" This reverts commit 540ad9944205235cd9ccbd716c5a667daf929616. --- build.zig | 7 +++++++ examples/build.zig | 36 ++++++++++++++++-------------------- src/build.zig | 10 +++++----- 3 files changed, 28 insertions(+), 25 deletions(-) create mode 100644 build.zig diff --git a/build.zig b/build.zig new file mode 100644 index 000000000..12c0513f6 --- /dev/null +++ b/build.zig @@ -0,0 +1,7 @@ +const std = @import("std"); +const raylib = @import("src/build.zig"); + +// This has been tested to work with zig master branch as of commit 87de821 or May 14 2023 +pub fn build(b: *std.Build) void { + raylib.build(b); +} diff --git a/examples/build.zig b/examples/build.zig index 383d9f651..6e13ab3da 100644 --- a/examples/build.zig +++ b/examples/build.zig @@ -1,7 +1,7 @@ const std = @import("std"); const builtin = @import("builtin"); -// This has been tested to work with zig 0.11.0 (67709b6, Aug 4 2023) +// This has been tested to work with zig master branch as of commit 87de821 or May 14 2023 fn add_module(comptime module: []const u8, b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) !*std.Build.Step { if (target.getOsTag() == .emscripten) { @panic("Emscripten building via Zig unsupported"); @@ -11,7 +11,7 @@ fn add_module(comptime module: []const u8, b: *std.Build, target: std.zig.CrossT const dir = try std.fs.cwd().openIterableDir(module, .{}); var iter = dir.iterate(); while (try iter.next()) |entry| { - if (entry.kind != .file) continue; + if (entry.kind != .File) continue; const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue; const name = entry.name[0..extension_idx]; const path = try std.fs.path.join(b.allocator, &.{ module, entry.name }); @@ -24,26 +24,26 @@ fn add_module(comptime module: []const u8, b: *std.Build, target: std.zig.CrossT .target = target, .optimize = optimize, }); - exe.addCSourceFile(.{ .file = .{ .path = path }, .flags = &.{} }); + exe.addCSourceFile(path, &[_][]const u8{}); exe.linkLibC(); exe.addObjectFile(switch (target.getOsTag()) { - .windows => .{ .path = "../src/zig-out/lib/raylib.lib" }, - .linux => .{ .path = "../src/zig-out/lib/libraylib.a" }, - .macos => .{ .path = "../src/zig-out/lib/libraylib.a" }, - .emscripten => .{ .path = "../src/zig-out/lib/libraylib.a" }, + .windows => "../src/zig-out/lib/raylib.lib", + .linux => "../src/zig-out/lib/libraylib.a", + .macos => "../src/zig-out/lib/libraylib.a", + .emscripten => "../src/zig-out/lib/libraylib.a", else => @panic("Unsupported OS"), }); - exe.addIncludePath(.{ .path = "../src" }); - exe.addIncludePath(.{ .path = "../src/external" }); - exe.addIncludePath(.{ .path = "../src/external/glfw/include" }); + exe.addIncludePath("../src"); + exe.addIncludePath("../src/external"); + exe.addIncludePath("../src/external/glfw/include"); switch (target.getOsTag()) { .windows => { exe.linkSystemLibrary("winmm"); exe.linkSystemLibrary("gdi32"); exe.linkSystemLibrary("opengl32"); - exe.addIncludePath(.{ .path = "external/glfw/deps/mingw" }); + exe.addIncludePath("external/glfw/deps/mingw"); exe.defineCMacro("PLATFORM_DESKTOP", null); }, @@ -71,15 +71,11 @@ fn add_module(comptime module: []const u8, b: *std.Build, target: std.zig.CrossT }, } - const install_cmd = b.addInstallArtifact(exe, .{}); - - const run_cmd = b.addRunArtifact(exe); - run_cmd.step.dependOn(&install_cmd.step); - - const run_step = b.step(name, name); - run_step.dependOn(&run_cmd.step); - - all.dependOn(&install_cmd.step); + b.installArtifact(exe); + var run = b.addRunArtifact(exe); + run.cwd = module; + b.step(name, name).dependOn(&run.step); + all.dependOn(&exe.step); } return all; } diff --git a/src/build.zig b/src/build.zig index 3bb4f4213..27250f5ff 100644 --- a/src/build.zig +++ b/src/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); -// This has been tested to work with zig 0.11.0 (67709b6, Aug 4 2023) +// This has been tested to work with zig master branch as of commit 87de821 or May 14 2023 pub fn addRaylib(b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode, options: Options) *std.Build.CompileStep { const raylib_flags = &[_][]const u8{ "-std=gnu99", @@ -192,12 +192,12 @@ pub fn build(b: *std.Build) void { const lib = addRaylib(b, target, optimize, options); - lib.installHeader("raylib.h", "raylib.h"); - lib.installHeader("raymath.h", "raymath.h"); - lib.installHeader("rlgl.h", "rlgl.h"); + lib.installHeader("src/raylib.h", "raylib.h"); + lib.installHeader("src/raymath.h", "raymath.h"); + lib.installHeader("src/rlgl.h", "rlgl.h"); if (options.raygui) { - lib.installHeader("../../raygui/src/raygui.h", "raygui.h"); + lib.installHeader("../raygui/src/raygui.h", "raygui.h"); } b.installArtifact(lib); From f0d949f931e286773aa0d0b87af4f58214ef611b Mon Sep 17 00:00:00 2001 From: Murlocohol Date: Tue, 10 Oct 2023 02:59:09 -0400 Subject: [PATCH 2/2] Hotfix for Vector2LineAngle(), should probably be reviewed along with the rest of raylib angle functions to determine what coordinate system we want. (#3394) --- examples/others/raymath_vector_angle.c | 56 +++++++++++++++++--------- src/raymath.h | 5 ++- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/examples/others/raymath_vector_angle.c b/examples/others/raymath_vector_angle.c index b193648fe..ab8ccf571 100644 --- a/examples/others/raymath_vector_angle.c +++ b/examples/others/raymath_vector_angle.c @@ -2,7 +2,7 @@ * * raylib [shapes] example - Vector Angle * -* Example originally created with raylib 1.0, last time updated with raylib 4.2 +* Example originally created with raylib 1.0, last time updated with raylib 4.6 * * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, * BSD-like license that allows static linking with closed source software @@ -10,7 +10,7 @@ * Copyright (c) 2023 Ramon Santamaria (@raysan5) * ********************************************************************************************/ - + #include "raylib.h" #include "raymath.h" @@ -28,7 +28,7 @@ int main(void) InitWindow(screenWidth, screenHeight, "raylib [math] example - vector angle"); Vector2 v0 = { screenWidth/2, screenHeight/2 }; - Vector2 v1 = { 100.0f, 80.0f }; + Vector2 v1 = Vector2Add(v0, (Vector2){ 100.0f, 80.0f }); Vector2 v2 = { 0 }; // Updated with mouse position float angle = 0.0f; // Angle in degrees @@ -42,21 +42,29 @@ int main(void) { // Update //---------------------------------------------------------------------------------- + float startangle; + + if (angleMode == 0) startangle = -Vector2LineAngle(v0, v1)*RAD2DEG; + if (angleMode == 1) startangle = 0.0f; + + v2 = GetMousePosition(); + if (IsKeyPressed(KEY_SPACE)) angleMode = !angleMode; + if(angleMode == 0 && IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) v1 = GetMousePosition(); + if (angleMode == 0) { // Calculate angle between two vectors, considering a common origin (v0) - v1 = Vector2Add(v0, (Vector2){ 100.0f, 80.0f }); - v2 = GetMousePosition(); - angle = Vector2Angle(Vector2Normalize(Vector2Subtract(v1, v0)), Vector2Normalize(Vector2Subtract(v2, v0)))*RAD2DEG; + Vector2 v1Normal = Vector2Normalize(Vector2Subtract(v1, v0)); + Vector2 v2Normal = Vector2Normalize(Vector2Subtract(v2, v0)); + + angle = Vector2Angle(v1Normal, v2Normal)*RAD2DEG; } else if (angleMode == 1) { // Calculate angle defined by a two vectors line, in reference to horizontal line - v1 = (Vector2){ screenWidth/2, screenHeight/2 }; - v2 = GetMousePosition(); - angle = Vector2LineAngle(v1, v2)*RAD2DEG; + angle = Vector2LineAngle(v0, v2)*RAD2DEG; } //---------------------------------------------------------------------------------- @@ -66,32 +74,40 @@ int main(void) ClearBackground(RAYWHITE); - if (angleMode == 0) DrawText("v0", v0.x, v0.y, 10, DARKGRAY); - DrawText("v1", v1.x, v1.y, 10, DARKGRAY); - DrawText("v2", v2.x, v2.y, 10, DARKGRAY); - if (angleMode == 0) { - DrawText("MODE: Angle between V1 and V2", 10, 10, 20, BLACK); + DrawText("MODE 0: Angle between V1 and V2", 10, 10, 20, BLACK); + DrawText("Right Click to Move V2", 10, 30, 20, DARKGRAY); DrawLineEx(v0, v1, 2.0f, BLACK); DrawLineEx(v0, v2, 2.0f, RED); - float startangle = 90 - Vector2LineAngle(v0, v1)*RAD2DEG; - DrawCircleSector(v0, 40.0f, startangle, startangle + angle - 360.0f*(angle > 180.0f), 32, Fade(GREEN, 0.6f)); + DrawCircleSector(v0, 40.0f, startangle, startangle - angle, 32, Fade(GREEN, 0.6f)); } else if (angleMode == 1) { - DrawText("MODE: Angle formed by line V1 to V2", 10, 10, 20, BLACK); + DrawText("MODE 1: Angle formed by line V1 to V2", 10, 10, 20, BLACK); DrawLine(0, screenHeight/2, screenWidth, screenHeight/2, LIGHTGRAY); - DrawLineEx(v1, v2, 2.0f, RED); + DrawLineEx(v0, v2, 2.0f, RED); - DrawCircleSector(v1, 40.0f, 90.0f, 180 - angle - 90, 32, Fade(GREEN, 0.6f)); + DrawCircleSector(v0, 40.0f, startangle, startangle - angle, 32, Fade(GREEN, 0.6f)); } + DrawText("v0", v0.x, v0.y, 10, DARKGRAY); + + // If the line from v0 to v1 would overlap the text, move it's position up 10 + if (angleMode == 0 && Vector2Subtract(v0, v1).y > 0.0f) DrawText("v1", v1.x, v1.y-10.0f, 10, DARKGRAY); + if (angleMode == 0 && Vector2Subtract(v0, v1).y < 0.0f) DrawText("v1", v1.x, v1.y, 10, DARKGRAY); + + // If angle mode 1, use v1 to emphasize the horizontal line + if (angleMode == 1) DrawText("v1", v0.x + 40.0f, v0.y, 10, DARKGRAY); + + // position adjusted by -10 so it isn't hidden by cursor + DrawText("v2", v2.x-10.0f, v2.y-10.0f, 10, DARKGRAY); + DrawText("Press SPACE to change MODE", 460, 10, 20, DARKGRAY); - DrawText(TextFormat("ANGLE: %2.2f", angle), 10, 40, 20, LIME); + DrawText(TextFormat("ANGLE: %2.2f", angle), 10, 70, 20, LIME); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/src/raymath.h b/src/raymath.h index db04c51ee..b01b0b22b 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -323,6 +323,8 @@ RMAPI float Vector2Angle(Vector2 v1, Vector2 v2) float dot = v1.x*v2.x + v1.y*v2.y; float det = v1.x*v2.y - v1.y*v2.x; + + // TODO(10/9/2023): Currently angles move clockwise, determine if this is wanted behavior result = -atan2f(det, dot); return result; @@ -335,7 +337,8 @@ RMAPI float Vector2LineAngle(Vector2 start, Vector2 end) { float result = 0.0f; - result = atan2f(end.y - start.y, end.x - start.x); + // TODO(10/9/2023): Currently angles move clockwise, determine if this is wanted behavior + result = -atan2f(end.y - start.y, end.x - start.x); return result; }