More convenience functions in structs

This commit is contained in:
Not-Nik 2023-07-09 19:31:27 +02:00
parent 42671d0195
commit 7a9cdd3af0
No known key found for this signature in database
GPG Key ID: 08BB71E672DB3BFD
9 changed files with 167 additions and 71 deletions

View File

@ -27,12 +27,12 @@ pub fn main() anyerror!void {
spacing += @floatToInt(i32, buildings[i].width);
buildColors[i] = rl.Color{ .r = @intCast(u8, rl.GetRandomValue(200, 240)), .g = @intCast(u8, rl.GetRandomValue(200, 240)), .b = @intCast(u8, rl.GetRandomValue(200, 250)), .a = 255 };
buildColors[i] = rl.Color.init(@intCast(u8, rl.GetRandomValue(200, 240)), @intCast(u8, rl.GetRandomValue(200, 240)), @intCast(u8, rl.GetRandomValue(200, 250)), 255);
}
var camera = rl.Camera2D{
.target = rl.Vector2{ .x = player.x + 20, .y = player.y + 20 },
.offset = rl.Vector2{ .x = screenWidth / 2, .y = screenHeight / 2 },
.target = rl.Vector2.init(player.x + 20, player.y + 20),
.offset = rl.Vector2.init(screenWidth / 2, screenHeight / 2),
.rotation = 0,
.zoom = 1,
};
@ -53,7 +53,7 @@ pub fn main() anyerror!void {
}
// Camera target follows player
camera.target = rl.Vector2{ .x = player.x + 20, .y = player.y + 20 };
camera.target = rl.Vector2.init(player.x + 20, player.y + 20);
// Camera rotation controls
if (rl.IsKeyDown(rl.KeyboardKey.KEY_A)) {

View File

@ -14,9 +14,9 @@ pub fn main() anyerror!void {
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - 3d camera first person");
var camera = rl.Camera3D{
.position = rl.Vector3{ .x = 4, .y = 2, .z = 4 },
.target = rl.Vector3{ .x = 0, .y = 1.8, .z = 0 },
.up = rl.Vector3{ .x = 0, .y = 1, .z = 0 },
.position = rl.Vector3.init(4, 2, 4),
.target = rl.Vector3.init(0, 1.8, 0),
.up = rl.Vector3.init(0, 1, 0),
.fovy = 60,
.projection = rl.CameraProjection.CAMERA_PERSPECTIVE,
};
@ -27,8 +27,8 @@ pub fn main() anyerror!void {
for (heights) |_, i| {
heights[i] = @intToFloat(f32, rl.GetRandomValue(1, 12));
positions[i] = rl.Vector3{ .x = @intToFloat(f32, rl.GetRandomValue(-15, 15)), .y = heights[i] / 2.0, .z = @intToFloat(f32, rl.GetRandomValue(-15, 15)) };
colors[i] = rl.Color{ .r = @intCast(u8, rl.GetRandomValue(20, 255)), .g = @intCast(u8, rl.GetRandomValue(10, 55)), .b = 30, .a = 255 };
positions[i] = rl.Vector3.init(@intToFloat(f32, rl.GetRandomValue(-15, 15)), heights[i] / 2.0, @intToFloat(f32, rl.GetRandomValue(-15, 15)));
colors[i] = rl.Color.init(@intCast(u8, rl.GetRandomValue(20, 255)), @intCast(u8, rl.GetRandomValue(10, 55)), 30, 255);
}
camera.setMode(rl.CameraMode.CAMERA_FIRST_PERSON);
@ -52,10 +52,10 @@ pub fn main() anyerror!void {
camera.begin();
// Draw ground
rl.DrawPlane(rl.Vector3{ .x = 0.0, .y = 0.0, .z = 0.0 }, rl.Vector2{ .x = 32.0, .y = 32.0 }, rl.LIGHTGRAY);
rl.DrawCube(rl.Vector3{ .x = -16.0, .y = 2.5, .z = 0.0 }, 1.0, 5.0, 32.0, rl.BLUE); // Draw a blue wall
rl.DrawCube(rl.Vector3{ .x = 16.0, .y = 2.5, .z = 0.0 }, 1.0, 5.0, 32.0, rl.LIME); // Draw a green wall
rl.DrawCube(rl.Vector3{ .x = 0.0, .y = 2.5, .z = 16.0 }, 32.0, 5.0, 1.0, rl.GOLD); // Draw a yellow wall
rl.DrawPlane(rl.Vector3.init(0, 0, 0), rl.Vector2.init(32, 32), rl.LIGHTGRAY);
rl.DrawCube(rl.Vector3.init(-16.0, 2.5, 0.0), 1.0, 5.0, 32.0, rl.BLUE); // Draw a blue wall
rl.DrawCube(rl.Vector3.init(16.0, 2.5, 0.0), 1.0, 5.0, 32.0, rl.LIME); // Draw a green wall
rl.DrawCube(rl.Vector3.init(0.0, 2.5, 16.0), 32.0, 5.0, 1.0, rl.GOLD); // Draw a yellow wall
// Draw some cubes around
for (heights) |height, i| {

View File

@ -10,7 +10,7 @@ pub fn main() anyerror!void {
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - keyboard input");
var ballPosition = rl.Vector2{ .x = screenWidth / 2, .y = screenHeight / 2 };
var ballPosition = rl.Vector2.init(screenWidth / 2, screenHeight / 2);
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

View File

@ -10,7 +10,7 @@ pub fn main() anyerror!void {
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - mouse input");
var ballPosition = rl.Vector2{ .x = -100.0, .y = -100.0 };
var ballPosition = rl.Vector2.init(-100, -100);
var ballColor = rl.DARKBLUE;
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second

View File

@ -10,11 +10,11 @@ pub fn main() anyerror!void {
rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
var ballPosition = rl.Vector2{ .x = -100.0, .y = -100.0 };
var ballPosition = rl.Vector2.init(-100, -100);
var ballColor = rl.BEIGE;
var touchCounter: f32 = 0;
var touchPosition = rl.Vector2{ .x = 0.0, .y = 0.0 };
var touchPosition = rl.Vector2.init(0, 0);
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------

View File

@ -20,7 +20,7 @@ pub fn main() anyerror!void {
var outlineSize: f32 = 2.0;
const outlineColor = [4]f32{ 1.0, 0.0, 0.0, 1.0 }; // Normalized RED color
const textureSize = rl.Vector2{ .x = @intToFloat(f32, texture.width), .y = @intToFloat(f32, texture.height) };
const textureSize = rl.Vector2.init(@intToFloat(f32, texture.width), @intToFloat(f32, texture.height));
// Get shader locations
const outlineSizeLoc = rl.GetShaderLocation(shdrOutline, "outlineSize");

View File

@ -18,7 +18,7 @@ pub fn main() anyerror!void {
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
const scarfy: rl.Texture2D = rl.LoadTexture("resources/textures/scarfy.png"); // Texture loading
const position = rl.Vector2{ .x = 350.0, .y = 280.0 };
const position = rl.Vector2.init(350.0, 280.0);
var frameRec = rl.Rectangle{ .x = 0.0, .y = 0.0, .width = @intToFloat(f32, @divFloor(scarfy.width, 6)), .height = @intToFloat(f32, scarfy.height) };
var currentFrame: u8 = 0;

View File

@ -5,12 +5,20 @@ const rl = @This();
pub const Vector2 = extern struct {
x: f32,
y: f32,
pub fn init(x: f32, y: f32) Vector2 {
return Vector2{ .x = x, .y = y };
}
};
pub const Vector3 = extern struct {
x: f32,
y: f32,
z: f32,
pub fn init(x: f32, y: f32, z: f32) Vector3 {
return Vector3{ .x = x, .y = y, .z = z };
}
};
pub const Vector4 = extern struct {
@ -18,6 +26,10 @@ pub const Vector4 = extern struct {
y: f32,
z: f32,
w: f32,
pub fn init(x: f32, y: f32, z: f32, w: f32) Vector4 {
return Vector4{ .x = x, .y = y, .z = z, .w = w };
}
};
pub const Quaternion = Vector4;
@ -45,35 +57,39 @@ pub const Color = extern struct {
g: u8,
b: u8,
a: u8,
pub fn init(r: u8, g: u8, b: u8, a: u8) Color {
return Color{ .r = r, .g = g, .b = b, .a = a };
}
};
pub const LIGHTGRAY = Color{ .r = 200, .g = 200, .b = 200, .a = 255 };
pub const GRAY = Color{ .r = 130, .g = 130, .b = 130, .a = 255 };
pub const DARKGRAY = Color{ .r = 80, .g = 80, .b = 80, .a = 255 };
pub const YELLOW = Color{ .r = 253, .g = 249, .b = 0, .a = 255 };
pub const GOLD = Color{ .r = 255, .g = 203, .b = 0, .a = 255 };
pub const ORANGE = Color{ .r = 255, .g = 161, .b = 0, .a = 255 };
pub const PINK = Color{ .r = 255, .g = 109, .b = 194, .a = 255 };
pub const RED = Color{ .r = 230, .g = 41, .b = 55, .a = 255 };
pub const MAROON = Color{ .r = 190, .g = 33, .b = 55, .a = 255 };
pub const GREEN = Color{ .r = 0, .g = 228, .b = 48, .a = 255 };
pub const LIME = Color{ .r = 0, .g = 158, .b = 47, .a = 255 };
pub const DARKGREEN = Color{ .r = 0, .g = 117, .b = 44, .a = 255 };
pub const SKYBLUE = Color{ .r = 102, .g = 191, .b = 255, .a = 255 };
pub const BLUE = Color{ .r = 0, .g = 121, .b = 241, .a = 255 };
pub const DARKBLUE = Color{ .r = 0, .g = 82, .b = 172, .a = 255 };
pub const PURPLE = Color{ .r = 200, .g = 122, .b = 255, .a = 255 };
pub const VIOLET = Color{ .r = 135, .g = 60, .b = 190, .a = 255 };
pub const DARKPURPLE = Color{ .r = 112, .g = 31, .b = 126, .a = 255 };
pub const BEIGE = Color{ .r = 211, .g = 176, .b = 131, .a = 255 };
pub const BROWN = Color{ .r = 127, .g = 106, .b = 79, .a = 255 };
pub const DARKBROWN = Color{ .r = 76, .g = 63, .b = 47, .a = 255 };
pub const LIGHTGRAY = Color.init(200, 200, 200, 255);
pub const GRAY = Color.init(130, 130, 130, 255);
pub const DARKGRAY = Color.init(80, 80, 80, 255);
pub const YELLOW = Color.init(253, 249, 0, 255);
pub const GOLD = Color.init(255, 203, 0, 255);
pub const ORANGE = Color.init(255, 161, 0, 255);
pub const PINK = Color.init(255, 161, 0, 255);
pub const RED = Color.init(230, 41, 55, 255);
pub const MAROON = Color.init(190, 33, 55, 255);
pub const GREEN = Color.init(0, 228, 48, 255);
pub const LIME = Color.init(0, 158, 47, 255);
pub const DARKGREEN = Color.init(0, 117, 44, 255);
pub const SKYBLUE = Color.init(102, 191, 255, 255);
pub const BLUE = Color.init(0, 121, 241, 255);
pub const DARKBLUE = Color.init(0, 82, 172, 255);
pub const PURPLE = Color.init(200, 122, 255, 255);
pub const VIOLET = Color.init(135, 60, 190, 255);
pub const DARKPURPLE = Color.init(112, 31, 126, 255);
pub const BEIGE = Color.init(211, 176, 131, 255);
pub const BROWN = Color.init(127, 106, 79, 255);
pub const DARKBROWN = Color.init(76, 63, 47, 255);
pub const WHITE = Color{ .r = 255, .g = 255, .b = 255, .a = 255 };
pub const BLACK = Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
pub const BLANK = Color{ .r = 0, .g = 0, .b = 0, .a = 0 };
pub const MAGENTA = Color{ .r = 255, .g = 0, .b = 255, .a = 255 };
pub const RAYWHITE = Color{ .r = 245, .g = 245, .b = 245, .a = 255 };
pub const WHITE = Color.init(255, 255, 255, 255);
pub const BLACK = Color.init(0, 0, 0, 255);
pub const BLANK = Color.init(0, 0, 0, 0);
pub const MAGENTA = Color.init(255, 0, 255, 255);
pub const RAYWHITE = Color.init(245, 245, 245, 255);
pub const Rectangle = extern struct {
x: f32,
@ -274,6 +290,14 @@ pub const Mesh = extern struct {
pub const Shader = extern struct {
id: c_uint,
locs: [*c]c_int,
pub fn activate(self: Shader) void {
rl.BeginShaderMode(self);
}
pub fn deactivate(_: Shader) void {
rl.EndShaderMode();
}
};
pub const MaterialMap = extern struct {
@ -309,6 +333,30 @@ pub const Model = extern struct {
boneCount: c_int,
bones: [*c]BoneInfo,
bindPose: [*c]Transform,
pub fn init(fileName: [*c]const u8) Model {
return rl.LoadModel(fileName);
}
pub fn initFromMesh(mesh: Mesh) Model {
return rl.LoadModelFromMesh(mesh);
}
pub fn draw(self: Mesh, position: Vector3, scale: f32, tint: Color) void {
return rl.DrawMesh(self, position, scale, tint);
}
pub fn drawEx(self: Mesh, position: Vector3, rotationAxis: Vector3, rotationAngle: f32, scale: Vector3, tint: Color) void {
return rl.DrawMeshEx(self, position, rotationAxis, rotationAngle, scale, tint);
}
pub fn drawWires(self: Mesh, position: Vector3, scale: f32, tint: Color) void {
return rl.DrawMeshWires(self, position, scale, tint);
}
pub fn drawWiresEx(self: Mesh, position: Vector3, rotationAxis: Vector3, rotationAngle: f32, scale: Vector3, tint: Color) void {
return rl.DrawMeshWiresEx(self, position, rotationAxis, rotationAngle, scale, tint);
}
};
pub const ModelAnimation = extern struct {

View File

@ -5,12 +5,20 @@ const rl = @This();
pub const Vector2 = extern struct {
x: f32,
y: f32,
pub fn init(x: f32, y: f32) Vector2 {
return Vector2{ .x = x, .y = y };
}
};
pub const Vector3 = extern struct {
x: f32,
y: f32,
z: f32,
pub fn init(x: f32, y: f32, z: f32) Vector3 {
return Vector3{ .x = x, .y = y, .z = z };
}
};
pub const Vector4 = extern struct {
@ -18,6 +26,10 @@ pub const Vector4 = extern struct {
y: f32,
z: f32,
w: f32,
pub fn init(x: f32, y: f32, z: f32, w: f32) Vector4 {
return Vector4{ .x = x, .y = y, .z = z, .w = w };
}
};
pub const Quaternion = Vector4;
@ -45,35 +57,39 @@ pub const Color = extern struct {
g: u8,
b: u8,
a: u8,
pub fn init(r: u8, g: u8, b: u8, a: u8) Color {
return Color{ .r = r, .g = g, .b = b, .a = a };
}
};
pub const LIGHTGRAY = Color{ .r = 200, .g = 200, .b = 200, .a = 255 };
pub const GRAY = Color{ .r = 130, .g = 130, .b = 130, .a = 255 };
pub const DARKGRAY = Color{ .r = 80, .g = 80, .b = 80, .a = 255 };
pub const YELLOW = Color{ .r = 253, .g = 249, .b = 0, .a = 255 };
pub const GOLD = Color{ .r = 255, .g = 203, .b = 0, .a = 255 };
pub const ORANGE = Color{ .r = 255, .g = 161, .b = 0, .a = 255 };
pub const PINK = Color{ .r = 255, .g = 109, .b = 194, .a = 255 };
pub const RED = Color{ .r = 230, .g = 41, .b = 55, .a = 255 };
pub const MAROON = Color{ .r = 190, .g = 33, .b = 55, .a = 255 };
pub const GREEN = Color{ .r = 0, .g = 228, .b = 48, .a = 255 };
pub const LIME = Color{ .r = 0, .g = 158, .b = 47, .a = 255 };
pub const DARKGREEN = Color{ .r = 0, .g = 117, .b = 44, .a = 255 };
pub const SKYBLUE = Color{ .r = 102, .g = 191, .b = 255, .a = 255 };
pub const BLUE = Color{ .r = 0, .g = 121, .b = 241, .a = 255 };
pub const DARKBLUE = Color{ .r = 0, .g = 82, .b = 172, .a = 255 };
pub const PURPLE = Color{ .r = 200, .g = 122, .b = 255, .a = 255 };
pub const VIOLET = Color{ .r = 135, .g = 60, .b = 190, .a = 255 };
pub const DARKPURPLE = Color{ .r = 112, .g = 31, .b = 126, .a = 255 };
pub const BEIGE = Color{ .r = 211, .g = 176, .b = 131, .a = 255 };
pub const BROWN = Color{ .r = 127, .g = 106, .b = 79, .a = 255 };
pub const DARKBROWN = Color{ .r = 76, .g = 63, .b = 47, .a = 255 };
pub const LIGHTGRAY = Color.init(200, 200, 200, 255);
pub const GRAY = Color.init(130, 130, 130, 255);
pub const DARKGRAY = Color.init(80, 80, 80, 255);
pub const YELLOW = Color.init(253, 249, 0, 255);
pub const GOLD = Color.init(255, 203, 0, 255);
pub const ORANGE = Color.init(255, 161, 0, 255);
pub const PINK = Color.init(255, 161, 0, 255);
pub const RED = Color.init(230, 41, 55, 255);
pub const MAROON = Color.init(190, 33, 55, 255);
pub const GREEN = Color.init(0, 228, 48, 255);
pub const LIME = Color.init(0, 158, 47, 255);
pub const DARKGREEN = Color.init(0, 117, 44, 255);
pub const SKYBLUE = Color.init(102, 191, 255, 255);
pub const BLUE = Color.init(0, 121, 241, 255);
pub const DARKBLUE = Color.init(0, 82, 172, 255);
pub const PURPLE = Color.init(200, 122, 255, 255);
pub const VIOLET = Color.init(135, 60, 190, 255);
pub const DARKPURPLE = Color.init(112, 31, 126, 255);
pub const BEIGE = Color.init(211, 176, 131, 255);
pub const BROWN = Color.init(127, 106, 79, 255);
pub const DARKBROWN = Color.init(76, 63, 47, 255);
pub const WHITE = Color{ .r = 255, .g = 255, .b = 255, .a = 255 };
pub const BLACK = Color{ .r = 0, .g = 0, .b = 0, .a = 255 };
pub const BLANK = Color{ .r = 0, .g = 0, .b = 0, .a = 0 };
pub const MAGENTA = Color{ .r = 255, .g = 0, .b = 255, .a = 255 };
pub const RAYWHITE = Color{ .r = 245, .g = 245, .b = 245, .a = 255 };
pub const WHITE = Color.init(255, 255, 255, 255);
pub const BLACK = Color.init(0, 0, 0, 255);
pub const BLANK = Color.init(0, 0, 0, 0);
pub const MAGENTA = Color.init(255, 0, 255, 255);
pub const RAYWHITE = Color.init(245, 245, 245, 255);
pub const Rectangle = extern struct {
x: f32,
@ -274,6 +290,14 @@ pub const Mesh = extern struct {
pub const Shader = extern struct {
id: c_uint,
locs: [*c]c_int,
pub fn activate(self: Shader) void {
rl.BeginShaderMode(self);
}
pub fn deactivate(_: Shader) void {
rl.EndShaderMode();
}
};
pub const MaterialMap = extern struct {
@ -309,6 +333,30 @@ pub const Model = extern struct {
boneCount: c_int,
bones: [*c]BoneInfo,
bindPose: [*c]Transform,
pub fn init(fileName: [*c]const u8) Model {
return rl.LoadModel(fileName);
}
pub fn initFromMesh(mesh: Mesh) Model {
return rl.LoadModelFromMesh(mesh);
}
pub fn draw(self: Mesh, position: Vector3, scale: f32, tint: Color) void {
return rl.DrawMesh(self, position, scale, tint);
}
pub fn drawEx(self: Mesh, position: Vector3, rotationAxis: Vector3, rotationAngle: f32, scale: Vector3, tint: Color) void {
return rl.DrawMeshEx(self, position, rotationAxis, rotationAngle, scale, tint);
}
pub fn drawWires(self: Mesh, position: Vector3, scale: f32, tint: Color) void {
return rl.DrawMeshWires(self, position, scale, tint);
}
pub fn drawWiresEx(self: Mesh, position: Vector3, rotationAxis: Vector3, rotationAngle: f32, scale: Vector3, tint: Color) void {
return rl.DrawMeshWiresEx(self, position, rotationAxis, rotationAngle, scale, tint);
}
};
pub const ModelAnimation = extern struct {