Skip to content

Commit

Permalink
Normalized enum cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Guigui220D committed Jun 30, 2024
1 parent 2a22d03 commit 2c8f2cc
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 133 deletions.
12 changes: 6 additions & 6 deletions src/examples/heat_haze.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ pub fn main() !void {
while (window.pollEvent()) |event| {
switch (event) {
.closed => return,
.keyPressed => |kp| {
.key_pressed => |kp| {
switch (kp.code) {
.Escape => return,
.Up => distortion_factor *= 2,
.Down => distortion_factor /= 2,
.Right => rise_factor *= 2,
.Left => rise_factor /= 2,
.escape => return,
.up => distortion_factor *= 2,
.down => distortion_factor /= 2,
.right => rise_factor *= 2,
.left => rise_factor /= 2,
else => {},
}
},
Expand Down
10 changes: 5 additions & 5 deletions src/graphics/Text.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const sf = struct {
const Text = @This();

pub const TextStyle = enum(c_uint) {
Regular = 0,
Bold = 1 << 0,
Italic = 1 << 1,
Underlined = 1 << 2,
StrikeThrough = 1 << 3,
regular = 0,
bold = 1 << 0,
italic = 1 << 1,
underlined = 1 << 2,
strike_through = 1 << 3,
};

// Constructor/destructor
Expand Down
8 changes: 4 additions & 4 deletions src/graphics/VertexArray.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ test "VertexArray: sane getters and setters" {
.{ .position = .{ .x = 1, .y = 0 }, .color = sf.graphics.Color.Green },
.{ .position = .{ .x = -1, .y = 1 }, .color = sf.graphics.Color.Blue },
};
var va = try createFromSlice(va_slice[0..], sf.graphics.PrimitiveType.Triangles);
var va = try createFromSlice(va_slice[0..], sf.graphics.PrimitiveType.triangles);
defer va.destroy();

va.append(.{ .position = .{ .x = 1, .y = 1 }, .color = sf.graphics.Color.Yellow });
va.setPrimitiveType(sf.graphics.PrimitiveType.Quads);
va.setPrimitiveType(sf.graphics.PrimitiveType.quads);

try tst.expectEqual(@as(usize, 4), va.getVertexCount());
try tst.expectEqual(sf.graphics.PrimitiveType.Quads, va.getPrimitiveType());
try tst.expectEqual(sf.graphics.PrimitiveType.quads, va.getPrimitiveType());
try tst.expectEqual(sf.graphics.FloatRect{ .left = -1, .top = 0, .width = 2, .height = 1 }, va.getBounds());

va.resize(3);
va.setPrimitiveType(sf.graphics.PrimitiveType.TriangleFan);
va.setPrimitiveType(sf.graphics.PrimitiveType.triangle_fan);
try tst.expectEqual(@as(usize, 3), va.getVertexCount());

const vert = va.getVertex(0).*;
Expand Down
8 changes: 4 additions & 4 deletions src/graphics/VertexBuffer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sf = @import("../root.zig");

const VertexBuffer = @This();

pub const Usage = enum(c_uint) { Static = 0, Dynamic = 1, Stream = 2 };
pub const Usage = enum(c_uint) { static = 0, dynamic = 1, stream = 2 };

// Constructors/destructors

Expand Down Expand Up @@ -63,10 +63,10 @@ test "VertexBuffer: sane getters and setters" {
.{ .position = .{ .x = 1, .y = 0 }, .color = sf.graphics.Color.Green },
.{ .position = .{ .x = -1, .y = 1 }, .color = sf.graphics.Color.Blue },
};
var va = try createFromSlice(va_slice[0..], sf.graphics.PrimitiveType.Triangles, Usage.Static);
var va = try createFromSlice(va_slice[0..], sf.graphics.PrimitiveType.triangles, Usage.static);
defer va.destroy();

try tst.expectEqual(@as(usize, 3), va.getVertexCount());
try tst.expectEqual(sf.graphics.PrimitiveType.Triangles, va.getPrimitiveType());
try tst.expectEqual(Usage.Static, va.getUsage());
try tst.expectEqual(sf.graphics.PrimitiveType.triangles, va.getPrimitiveType());
try tst.expectEqual(Usage.static, va.getUsage());
}
28 changes: 14 additions & 14 deletions src/graphics/blend_mode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ pub const BlendMode = extern struct {
pub const Factor = enum(c_int) {
zero,
one,
srcColor,
oneMinusSrcColor,
dstColor,
oneMinusDstColor,
srcAlpha,
oneMinusSrcAlpha,
dstAlpha,
oneMinusDstAlpha,
src_color,
one_minus_src_color,
dst_color,
one_minus_dst_color,
src_alpha,
one_minus_src_alpha,
dst_alpha,
one_minus_dst_alpha,
};

pub const Equation = enum(c_int) {
Expand All @@ -25,16 +25,16 @@ pub const BlendMode = extern struct {

// Preset blend modes
pub const BlendAlpha = BlendMode{
.color_src_factor = .srcAlpha,
.color_dst_factor = .oneMinusSrcAlpha,
.color_src_factor = .src_alpha,
.color_dst_factor = .one_minus_src_alpha,
.color_equation = .add,
.alpha_src_factor = .one,
.alpha_dst_factor = .oneMinusSrcAlpha,
.alpha_dst_factor = .one_minus_src_alpha,
.alpha_equation = .add,
};

pub const BlendAdd = BlendMode{
.color_src_factor = .srcAlpha,
.color_src_factor = .src_alpha,
.color_dst_factor = .one,
.color_equation = .add,
.alpha_src_factor = .one,
Expand All @@ -43,10 +43,10 @@ pub const BlendMode = extern struct {
};

pub const BlendMultiply = BlendMode{
.color_src_factor = .dstColor,
.color_src_factor = .dst_color,
.color_dst_factor = .zero,
.color_equation = .add,
.alpha_src_factor = .dstColor,
.alpha_src_factor = .dst_color,
.alpha_dst_factor = .zero,
.alpha_equation = .add,
};
Expand Down
32 changes: 16 additions & 16 deletions src/graphics/primitive_type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
const Vertex = @import("vertex.zig").Vertex;

pub const PrimitiveType = enum(c_uint) {
Points,
Lines,
LineStrip,
Triangles,
TriangleStrip,
TriangleFan,
Quads,
points,
lines,
line_strip,
triangles,
triangle_strip,
triangle_fan,
quads,

/// Gives the corresponding primitive type for primitive iteration
/// See Vertex.verticesAsPrimitives()
pub fn Type(comptime primitive_type: PrimitiveType) type {
return switch (primitive_type) {
.Points => PointPrimitive,
.Lines => LinePrimitive,
.Triangles => TrianglePrimitive,
.Quads => QuadPrimitive,
.points => PointPrimitive,
.lines => LinePrimitive,
.triangles => TrianglePrimitive,
.quads => QuadPrimitive,
else => @compileError("Primitive type not supported"),
};
}
/// Says how many vertices each primitive is composed of
pub fn packedBy(primitive_type: PrimitiveType) usize {
pub fn vertexCount(primitive_type: PrimitiveType) usize {
return switch (primitive_type) {
.Points => 1,
.Lines => 2,
.Triangles => 3,
.Quads => 4,
.points => 1,
.lines => 2,
.triangles => 3,
.quads => 4,
else => @panic("Primitive type not supported"),
};
}
Expand Down
28 changes: 14 additions & 14 deletions src/window/Cursor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ pub const Type = enum(c_uint) {
/// Pointing hand cursor
hand,
/// Horizontal double arrow cursor
sizeHorizontal,
size_horizontal,
/// Vertical double arrow cursor
sizeVertical,
size_vertical,
/// Double arrow cursor going from top-left to bottom-right
sizeTopLeftBottomRight,
size_top_left_bottom_right,
/// Double arrow cursor going from bottom-left to top-right
sizeBottomLeftTopRight,
size_bottom_left_top_right,
/// Left arrow cursor on Linux, same as sizeHorizontal on other platforms
sizeLeft,
size_left,
/// Right arrow cursor on Linux, same as sizeHorizontal on other platforms
sizeRight,
size_right,
/// Up arrow cursor on Linux, same as sizeVertical on other platforms
sizeTop,
size_top,
/// Down arrow cursor on Linux, same as sizeVertical on other platforms
sizeBottom,
size_bottom,
/// Top-left arrow cursor on Linux, same as sizeTopLeftBottomRight on other platforms
sizeTopLeft,
size_top_left,
/// Bottom-right arrow cursor on Linux, same as sizeTopLeftBottomRight on other platforms
sizeBottomRight,
size_bottom_right,
/// Bottom-left arrow cursor on Linux, same as sizeBottomLeftTopRight on other platforms
sizeBottomLeft,
size_bottom_left,
/// Top-right arrow cursor on Linux, same as sizeBottomLeftTopRight on other platforms
sizeTopRight,
size_top_right,
/// Combination of sizeHorizontal and sizeVertical
sizeAll,
size_all,
/// Crosshair cursor
cross,
/// Help cursor
help,
/// Action not allowed cursor
notAllowed,
not_allowed,
};

// Constructors and destructors
Expand Down
2 changes: 1 addition & 1 deletion src/window/Joystick.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const MaxButtonCount = 32;
pub const MaxAxisCount = 8;

/// Joystick axis
pub const Axis = enum(c_uint) { X, Y, Z, R, U, V, PovX, PovY };
pub const Axis = enum(c_uint) { x, y, z, r, u, v, pov_x, pov_y };

/// Gets a joystick if it is connected, null if it is not
/// Technically, you can use a joystick's getters if it's not connected or before it connects
Expand Down
107 changes: 41 additions & 66 deletions src/window/event.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,35 @@ const sf = struct {
pub usingnamespace sfml.system;
};

pub const Event = union(Event.Type) {
pub const Event = union(enum) {
const Self = @This();

pub const Type = enum(c_int) {
closed,
resized,
lostFocus,
gainedFocus,
textEntered,
keyPressed,
keyReleased,
mouseWheelScrolled,
mouseButtonPressed,
mouseButtonReleased,
mouseMoved,
mouseEntered,
mouseLeft,
joystickButtonPressed,
joystickButtonReleased,
joystickMoved,
joystickConnected,
joystickDisconnected,
touchBegan,
touchMoved,
touchEnded,
sensorChanged,
};

// Big oof
/// Creates this event from a csfml one
pub fn _fromCSFML(event: sf.c.sfEvent) Self {
return switch (event.type) {
sf.c.sfEvtClosed => .closed,
sf.c.sfEvtResized => .{ .resized = .{ .size = .{ .x = event.size.width, .y = event.size.height } } },
sf.c.sfEvtLostFocus => .lostFocus,
sf.c.sfEvtGainedFocus => .gainedFocus,
sf.c.sfEvtTextEntered => .{ .textEntered = .{ .unicode = event.text.unicode } },
sf.c.sfEvtKeyPressed => .{ .keyPressed = .{ .code = @as(sf.window.keyboard.KeyCode, @enumFromInt(event.key.code)), .alt = (event.key.alt != 0), .control = (event.key.control != 0), .shift = (event.key.shift != 0), .system = (event.key.system != 0) } },
sf.c.sfEvtKeyReleased => .{ .keyReleased = .{ .code = @as(sf.window.keyboard.KeyCode, @enumFromInt(event.key.code)), .alt = (event.key.alt != 0), .control = (event.key.control != 0), .shift = (event.key.shift != 0), .system = (event.key.system != 0) } },
sf.c.sfEvtMouseWheelScrolled => .{ .mouseWheelScrolled = .{ .wheel = @as(sf.window.mouse.Wheel, @enumFromInt(event.mouseWheelScroll.wheel)), .delta = event.mouseWheelScroll.delta, .pos = .{ .x = event.mouseWheelScroll.x, .y = event.mouseWheelScroll.y } } },
sf.c.sfEvtMouseButtonPressed => .{ .mouseButtonPressed = .{ .button = @as(sf.window.mouse.Button, @enumFromInt(event.mouseButton.button)), .pos = .{ .x = event.mouseButton.x, .y = event.mouseButton.y } } },
sf.c.sfEvtMouseButtonReleased => .{ .mouseButtonReleased = .{ .button = @as(sf.window.mouse.Button, @enumFromInt(event.mouseButton.button)), .pos = .{ .x = event.mouseButton.x, .y = event.mouseButton.y } } },
sf.c.sfEvtMouseMoved => .{ .mouseMoved = .{ .pos = .{ .x = event.mouseMove.x, .y = event.mouseMove.y } } },
sf.c.sfEvtMouseEntered => .mouseEntered,
sf.c.sfEvtMouseLeft => .mouseLeft,
sf.c.sfEvtJoystickButtonPressed => .{ .joystickButtonPressed = .{ .joystickId = event.joystickButton.joystickId, .button = event.joystickButton.button } },
sf.c.sfEvtJoystickButtonReleased => .{ .joystickButtonReleased = .{ .joystickId = event.joystickButton.joystickId, .button = event.joystickButton.button } },
sf.c.sfEvtJoystickMoved => .{ .joystickMoved = .{ .joystickId = event.joystickMove.joystickId, .axis = event.joystickMove.axis, .position = event.joystickMove.position } },
sf.c.sfEvtJoystickConnected => .{ .joystickConnected = .{ .joystickId = event.joystickConnect.joystickId } },
sf.c.sfEvtJoystickDisconnected => .{ .joystickDisconnected = .{ .joystickId = event.joystickConnect.joystickId } },
sf.c.sfEvtTouchBegan => .{ .touchBegan = .{ .finger = event.touch.finger, .pos = .{ .x = event.touch.x, .y = event.touch.y } } },
sf.c.sfEvtTouchMoved => .{ .touchMoved = .{ .finger = event.touch.finger, .pos = .{ .x = event.touch.x, .y = event.touch.y } } },
sf.c.sfEvtTouchEnded => .{ .touchEnded = .{ .finger = event.touch.finger, .pos = .{ .x = event.touch.x, .y = event.touch.y } } },
sf.c.sfEvtSensorChanged => .{ .sensorChanged = .{ .sensorType = event.sensor.sensorType, .vector = .{ .x = event.sensor.x, .y = event.sensor.y, .z = event.sensor.z } } },
sf.c.sfEvtLostFocus => .lost_focus,
sf.c.sfEvtGainedFocus => .gained_focus,
sf.c.sfEvtTextEntered => .{ .text_entered = .{ .unicode = event.text.unicode } },
sf.c.sfEvtKeyPressed => .{ .key_pressed = .{ .code = @as(sf.window.keyboard.KeyCode, @enumFromInt(event.key.code)), .alt = (event.key.alt != 0), .control = (event.key.control != 0), .shift = (event.key.shift != 0), .system = (event.key.system != 0) } },
sf.c.sfEvtKeyReleased => .{ .key_released = .{ .code = @as(sf.window.keyboard.KeyCode, @enumFromInt(event.key.code)), .alt = (event.key.alt != 0), .control = (event.key.control != 0), .shift = (event.key.shift != 0), .system = (event.key.system != 0) } },
sf.c.sfEvtMouseWheelScrolled => .{ .mouse_wheel_scrolled = .{ .wheel = @as(sf.window.mouse.Wheel, @enumFromInt(event.mouseWheelScroll.wheel)), .delta = event.mouseWheelScroll.delta, .pos = .{ .x = event.mouseWheelScroll.x, .y = event.mouseWheelScroll.y } } },
sf.c.sfEvtMouseButtonPressed => .{ .mouse_button_pressed = .{ .button = @as(sf.window.mouse.Button, @enumFromInt(event.mouseButton.button)), .pos = .{ .x = event.mouseButton.x, .y = event.mouseButton.y } } },
sf.c.sfEvtMouseButtonReleased => .{ .mouse_button_released = .{ .button = @as(sf.window.mouse.Button, @enumFromInt(event.mouseButton.button)), .pos = .{ .x = event.mouseButton.x, .y = event.mouseButton.y } } },
sf.c.sfEvtMouseMoved => .{ .mouse_moved = .{ .pos = .{ .x = event.mouseMove.x, .y = event.mouseMove.y } } },
sf.c.sfEvtMouseEntered => .mouse_entered,
sf.c.sfEvtMouseLeft => .mouse_left,
sf.c.sfEvtJoystickButtonPressed => .{ .joystick_button_pressed = .{ .joystickId = event.joystickButton.joystickId, .button = event.joystickButton.button } },
sf.c.sfEvtJoystickButtonReleased => .{ .joystick_button_released = .{ .joystickId = event.joystickButton.joystickId, .button = event.joystickButton.button } },
sf.c.sfEvtJoystickMoved => .{ .joystick_moved = .{ .joystickId = event.joystickMove.joystickId, .axis = event.joystickMove.axis, .position = event.joystickMove.position } },
sf.c.sfEvtJoystickConnected => .{ .joystick_connected = .{ .joystickId = event.joystickConnect.joystickId } },
sf.c.sfEvtJoystickDisconnected => .{ .joystick_disconnected = .{ .joystickId = event.joystickConnect.joystickId } },
sf.c.sfEvtTouchBegan => .{ .touch_began = .{ .finger = event.touch.finger, .pos = .{ .x = event.touch.x, .y = event.touch.y } } },
sf.c.sfEvtTouchMoved => .{ .touch_moved = .{ .finger = event.touch.finger, .pos = .{ .x = event.touch.x, .y = event.touch.y } } },
sf.c.sfEvtTouchEnded => .{ .touch_ended = .{ .finger = event.touch.finger, .pos = .{ .x = event.touch.x, .y = event.touch.y } } },
sf.c.sfEvtSensorChanged => .{ .sensor_changed = .{ .sensorType = event.sensor.sensorType, .vector = .{ .x = event.sensor.x, .y = event.sensor.y, .z = event.sensor.z } } },
sf.c.sfEvtCount => @panic("sfEvtCount should't exist as an event!"),
else => @panic("Unknown event!"),
};
Expand Down Expand Up @@ -140,24 +115,24 @@ pub const Event = union(Event.Type) {
// An event is one of those
closed,
resized: SizeEvent,
lostFocus,
gainedFocus,
textEntered: TextEvent,
keyPressed: KeyEvent,
keyReleased: KeyEvent,
mouseWheelScrolled: MouseWheelScrollEvent,
mouseButtonPressed: MouseButtonEvent,
mouseButtonReleased: MouseButtonEvent,
mouseMoved: MouseMoveEvent,
mouseEntered,
mouseLeft,
joystickButtonPressed: JoystickButtonEvent,
joystickButtonReleased: JoystickButtonEvent,
joystickMoved: JoystickMoveEvent,
joystickConnected: JoystickConnectEvent,
joystickDisconnected: JoystickConnectEvent,
touchBegan: TouchEvent,
touchMoved: TouchEvent,
touchEnded: TouchEvent,
sensorChanged: SensorEvent,
lost_focus,
gained_focus,
text_entered: TextEvent,
key_pressed: KeyEvent,
key_released: KeyEvent,
mouse_wheel_scrolled: MouseWheelScrollEvent,
mouse_button_pressed: MouseButtonEvent,
mouse_button_released: MouseButtonEvent,
mouse_moved: MouseMoveEvent,
mouse_entered,
mouse_left,
joystick_button_pressed: JoystickButtonEvent,
joystick_button_released: JoystickButtonEvent,
joystick_moved: JoystickMoveEvent,
joystick_connected: JoystickConnectEvent,
joystick_disconnected: JoystickConnectEvent,
touch_began: TouchEvent,
touch_moved: TouchEvent,
touch_ended: TouchEvent,
sensor_changed: SensorEvent,
};
Loading

0 comments on commit 2c8f2cc

Please sign in to comment.