full auto-generation of wrappers
This commit is contained in:
parent
3f6b44603b
commit
028f6a9dd7
@ -13,7 +13,6 @@ keywords = ["raylib", "graphics"]
|
|||||||
categories = ["external-ffi-bindings", "graphics", "multimedia", "rendering"]
|
categories = ["external-ffi-bindings", "graphics", "multimedia", "rendering"]
|
||||||
exclude = [
|
exclude = [
|
||||||
"third_party/raylib/examples/*",
|
"third_party/raylib/examples/*",
|
||||||
"third_party/raylib/parser/*",
|
|
||||||
"third_party/raylib/logo/*",
|
"third_party/raylib/logo/*",
|
||||||
"third_party/raylib/projects/*"
|
"third_party/raylib/projects/*"
|
||||||
]
|
]
|
||||||
@ -24,9 +23,8 @@ bindgen = "^0.65.0"
|
|||||||
cmake = "^0.1.49"
|
cmake = "^0.1.49"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
regex = "1"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["enums", "macros", "colors"]
|
default = ["macros"]
|
||||||
enums = []
|
macros = []
|
||||||
macros = []
|
|
||||||
colors = []
|
|
@ -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 bind;
|
||||||
mod wrap;
|
mod wrap;
|
||||||
@ -20,5 +20,6 @@ pub fn main() {
|
|||||||
let api_defs = RayLibApiDefinition::load("third_party/raylib/parser/output/raylib_api.json").unwrap();
|
let api_defs = RayLibApiDefinition::load("third_party/raylib/parser/output/raylib_api.json").unwrap();
|
||||||
|
|
||||||
// Generate safe wrappers
|
// Generate safe wrappers
|
||||||
wrap_exposed_enums(api_defs);
|
wrap_exposed_enums(api_defs.clone());
|
||||||
|
wrap_default_colors(api_defs);
|
||||||
}
|
}
|
||||||
|
44
build/wrap/colors.rs
Normal file
44
build/wrap/colors.rs
Normal file
@ -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();
|
||||||
|
}
|
@ -1,2 +1,3 @@
|
|||||||
pub mod raylib_api;
|
pub mod raylib_api;
|
||||||
pub mod enums;
|
pub mod enums;
|
||||||
|
pub mod colors;
|
@ -1,4 +1,4 @@
|
|||||||
use std::{path::Path, error::Error};
|
use std::error::Error;
|
||||||
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
@ -38,4 +38,4 @@ impl RayLibApiDefinition {
|
|||||||
let api = serde_json::from_reader(reader)?;
|
let api = serde_json::from_reader(reader)?;
|
||||||
Ok(api)
|
Ok(api)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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"
|
|
||||||
])
|
|
209
src/colors.rs
209
src/colors.rs
@ -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,
|
|
||||||
};
|
|
@ -11,7 +11,10 @@ pub mod enums {
|
|||||||
include!(concat!(env!("OUT_DIR"), "/enums.rs"));
|
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")]
|
#[cfg(feature = "macros")]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user