diff --git a/Cargo.toml b/Cargo.toml index 7d92a78..76895f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ keywords = ["raylib", "graphics"] categories = ["external-ffi-bindings", "graphics", "multimedia", "rendering"] exclude = [ "third_party/raylib/examples/*", - "third_party/raylib/parser/*", "third_party/raylib/logo/*", "third_party/raylib/projects/*" ] @@ -24,9 +23,8 @@ bindgen = "^0.65.0" cmake = "^0.1.49" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +regex = "1" [features] -default = ["enums", "macros", "colors"] -enums = [] -macros = [] -colors = [] \ No newline at end of file +default = ["macros"] +macros = [] \ No newline at end of file diff --git a/build/main.rs b/build/main.rs index 2a97319..facf5b1 100644 --- a/build/main.rs +++ b/build/main.rs @@ -1,4 +1,4 @@ -use crate::wrap::{raylib_api::RayLibApiDefinition, enums::wrap_exposed_enums}; +use crate::wrap::{raylib_api::RayLibApiDefinition, enums::wrap_exposed_enums, colors::wrap_default_colors}; mod bind; mod wrap; @@ -20,5 +20,6 @@ pub fn main() { let api_defs = RayLibApiDefinition::load("third_party/raylib/parser/output/raylib_api.json").unwrap(); // Generate safe wrappers - wrap_exposed_enums(api_defs); + wrap_exposed_enums(api_defs.clone()); + wrap_default_colors(api_defs); } diff --git a/build/wrap/colors.rs b/build/wrap/colors.rs new file mode 100644 index 0000000..7ea9c45 --- /dev/null +++ b/build/wrap/colors.rs @@ -0,0 +1,44 @@ +use std::path::PathBuf; + +use regex::Regex; +use serde_json::Value; + +use super::raylib_api::RayLibApiDefinition; + +pub fn wrap_default_colors(api_defs: RayLibApiDefinition) { + // Allocate an output buffer for lines + let mut lines = Vec::new(); + + // Compile the regex statement that fines color definitions + let color_re = Regex::new(r"CLITERAL\(Color\)\{ (\d+), (\d+), (\d+), (\d+) \}").unwrap(); + + // Handle each enum + for def in api_defs.defines { + // Only operate on color types + if def.kind == "COLOR" { + if let Value::String(value) = def.value { + // Write a doc comment describing the color + lines.push("".to_string()); + lines.push(format!("/// {}", def.description)); + + // Extract the color components from the value + let caps = color_re.captures(&value).unwrap(); + let r = caps.get(1).unwrap().as_str(); + let g = caps.get(2).unwrap().as_str(); + let b = caps.get(3).unwrap().as_str(); + let a = caps.get(4).unwrap().as_str(); + + // Write the color declaration + lines.push(format!( + "pub const {}: crate::Color = crate::Color {{\n\tr: {},\n\tg: {},\n\tb: {},\n\ta: {}\n}};", + def.name, r, g, b, a + )); + } + } + } + + // Write the output file + let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap()); + let output = lines.join("\n"); + std::fs::write(out_path.join("colors.rs"), output).unwrap(); +} diff --git a/build/wrap/mod.rs b/build/wrap/mod.rs index 7116f4b..d333496 100644 --- a/build/wrap/mod.rs +++ b/build/wrap/mod.rs @@ -1,2 +1,3 @@ pub mod raylib_api; -pub mod enums; \ No newline at end of file +pub mod enums; +pub mod colors; \ No newline at end of file diff --git a/build/wrap/raylib_api.rs b/build/wrap/raylib_api.rs index 5603efc..646e1bc 100644 --- a/build/wrap/raylib_api.rs +++ b/build/wrap/raylib_api.rs @@ -1,4 +1,4 @@ -use std::{path::Path, error::Error}; +use std::error::Error; use serde_json::Value; @@ -38,4 +38,4 @@ impl RayLibApiDefinition { let api = serde_json::from_reader(reader)?; Ok(api) } -} \ No newline at end of file +} diff --git a/scripts/regenerate_colors.py b/scripts/regenerate_colors.py deleted file mode 100644 index 5318859..0000000 --- a/scripts/regenerate_colors.py +++ /dev/null @@ -1,44 +0,0 @@ -import json -from pathlib import Path -import re - -REPO_ROOT = Path(__file__).parent.parent -COLOR_DEF_RE = re.compile(r"CLITERAL\(Color\){ (\d+), (\d+), (\d+), (\d+) }") - -print("Searching for raylib API definitions") -with open(REPO_ROOT / "third_party" / "raylib" / "parser" / "output" / - "raylib_api.json") as f: - raylib_api = json.load(f) - -# Find the raylib defines -raylib_defines = raylib_api["defines"] - -# Delete the old colors file if it exists -if (REPO_ROOT / "src" / "colors.rs").exists(): - print("Deleting old colors file") - (REPO_ROOT / "src" / "colors.rs").unlink() - -# Open the Rust colors file -with open(REPO_ROOT / "src" / "colors.rs", "w") as f: - - # Write a file header - f.writelines([ - "//! This module contains auto-generated Rust representations of raylib's colors.\n" - ]) - - # Search for color definitions - for definition in raylib_defines: - if definition["type"] == "COLOR": - print(f"Writing color: {definition['name']}") - # Parse the RGBA values - match = COLOR_DEF_RE.match(definition["value"]) - if match is None: - raise RuntimeError( - f"Failed to parse color definition {definition}") - r, g, b, a = match.groups() - - # Write the color definition - f.writelines([ - "\n", f"/// {definition['description']}\n", - f"pub const {definition['name']}: crate::Color = crate::Color {{\n\tr: {r},\n\tg: {g},\n\tb: {b},\n\ta: {a},\n}};\n" - ]) diff --git a/src/colors.rs b/src/colors.rs deleted file mode 100644 index c261e82..0000000 --- a/src/colors.rs +++ /dev/null @@ -1,209 +0,0 @@ -//! This module contains auto-generated Rust representations of raylib's colors. - -/// Light Gray -pub const LIGHTGRAY: crate::Color = crate::Color { - r: 200, - g: 200, - b: 200, - a: 255, -}; - -/// Gray -pub const GRAY: crate::Color = crate::Color { - r: 130, - g: 130, - b: 130, - a: 255, -}; - -/// Dark Gray -pub const DARKGRAY: crate::Color = crate::Color { - r: 80, - g: 80, - b: 80, - a: 255, -}; - -/// Yellow -pub const YELLOW: crate::Color = crate::Color { - r: 253, - g: 249, - b: 0, - a: 255, -}; - -/// Gold -pub const GOLD: crate::Color = crate::Color { - r: 255, - g: 203, - b: 0, - a: 255, -}; - -/// Orange -pub const ORANGE: crate::Color = crate::Color { - r: 255, - g: 161, - b: 0, - a: 255, -}; - -/// Pink -pub const PINK: crate::Color = crate::Color { - r: 255, - g: 109, - b: 194, - a: 255, -}; - -/// Red -pub const RED: crate::Color = crate::Color { - r: 230, - g: 41, - b: 55, - a: 255, -}; - -/// Maroon -pub const MAROON: crate::Color = crate::Color { - r: 190, - g: 33, - b: 55, - a: 255, -}; - -/// Green -pub const GREEN: crate::Color = crate::Color { - r: 0, - g: 228, - b: 48, - a: 255, -}; - -/// Lime -pub const LIME: crate::Color = crate::Color { - r: 0, - g: 158, - b: 47, - a: 255, -}; - -/// Dark Green -pub const DARKGREEN: crate::Color = crate::Color { - r: 0, - g: 117, - b: 44, - a: 255, -}; - -/// Sky Blue -pub const SKYBLUE: crate::Color = crate::Color { - r: 102, - g: 191, - b: 255, - a: 255, -}; - -/// Blue -pub const BLUE: crate::Color = crate::Color { - r: 0, - g: 121, - b: 241, - a: 255, -}; - -/// Dark Blue -pub const DARKBLUE: crate::Color = crate::Color { - r: 0, - g: 82, - b: 172, - a: 255, -}; - -/// Purple -pub const PURPLE: crate::Color = crate::Color { - r: 200, - g: 122, - b: 255, - a: 255, -}; - -/// Violet -pub const VIOLET: crate::Color = crate::Color { - r: 135, - g: 60, - b: 190, - a: 255, -}; - -/// Dark Purple -pub const DARKPURPLE: crate::Color = crate::Color { - r: 112, - g: 31, - b: 126, - a: 255, -}; - -/// Beige -pub const BEIGE: crate::Color = crate::Color { - r: 211, - g: 176, - b: 131, - a: 255, -}; - -/// Brown -pub const BROWN: crate::Color = crate::Color { - r: 127, - g: 106, - b: 79, - a: 255, -}; - -/// Dark Brown -pub const DARKBROWN: crate::Color = crate::Color { - r: 76, - g: 63, - b: 47, - a: 255, -}; - -/// White -pub const WHITE: crate::Color = crate::Color { - r: 255, - g: 255, - b: 255, - a: 255, -}; - -/// Black -pub const BLACK: crate::Color = crate::Color { - r: 0, - g: 0, - b: 0, - a: 255, -}; - -/// Blank (Transparent) -pub const BLANK: crate::Color = crate::Color { - r: 0, - g: 0, - b: 0, - a: 0, -}; - -/// Magenta -pub const MAGENTA: crate::Color = crate::Color { - r: 255, - g: 0, - b: 255, - a: 255, -}; - -/// My own White (raylib logo) -pub const RAYWHITE: crate::Color = crate::Color { - r: 245, - g: 245, - b: 245, - a: 255, -}; diff --git a/src/lib.rs b/src/lib.rs index 913cc80..3d7cc21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,10 @@ pub mod enums { include!(concat!(env!("OUT_DIR"), "/enums.rs")); } -pub mod colors; +/// This module contains auto-generated Rust representations of raylib's colors. +pub mod colors { + include!(concat!(env!("OUT_DIR"), "/colors.rs")); +} #[cfg(feature = "macros")] #[macro_use]