aboutsummaryrefslogtreecommitdiff
path: root/src/sfml/graphics/CircleShape.zig
blob: b2759e511179376f1b7f5afe7eb976c23df6c65e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! Specialized shape representing a circle.

const sf = struct {
    pub usingnamespace @import("../sfml.zig");
    pub usingnamespace sf.system;
    pub usingnamespace sf.graphics;
};

const CircleShape = @This();

// Constructor/destructor

/// Inits a circle shape with a radius. The circle will be white and have 30 points
pub fn create(radius: f32) !CircleShape {
    var circle = sf.c.sfCircleShape_create();
    if (circle == null)
        return sf.Error.nullptrUnknownReason;

    sf.c.sfCircleShape_setFillColor(circle, sf.c.sfWhite);
    sf.c.sfCircleShape_setRadius(circle, radius);

    return CircleShape{ .ptr = circle.? };
}

/// Destroys a circle shape
pub fn destroy(self: CircleShape) void {
    sf.c.sfCircleShape_destroy(self.ptr);
}

// Draw function
pub fn sfDraw(self: CircleShape, window: sf.RenderWindow, states: ?*sf.c.sfRenderStates) void {
    sf.c.sfRenderWindow_drawCircleShape(window.ptr, self.ptr, states);
}

// Getters/setters

/// Gets the fill color of this circle shape
pub fn getFillColor(self: CircleShape) sf.Color {
    return sf.Color.fromCSFML(sf.c.sfCircleShape_getFillColor(self.ptr));
}
/// Sets the fill color of this circle shape
pub fn setFillColor(self: CircleShape, color: sf.Color) void {
    sf.c.sfCircleShape_setFillColor(self.ptr, color.toCSFML());
}

/// Gets the radius of this circle shape
pub fn getRadius(self: CircleShape) f32 {
    return sf.c.sfCircleShape_getRadius(self.ptr);
}
/// Sets the radius of this circle shape
pub fn setRadius(self: CircleShape, radius: f32) void {
    sf.c.sfCircleShape_setRadius(self.ptr, radius);
}

/// Gets the position of this circle shape
pub fn getPosition(self: CircleShape) sf.Vector2f {
    return sf.Vector2f.fromCSFML(sf.c.sfCircleShape_getPosition(self.ptr));
}
/// Sets the position of this circle shape
pub fn setPosition(self: CircleShape, pos: sf.Vector2f) void {
    sf.c.sfCircleShape_setPosition(self.ptr, pos.toCSFML());
}
/// Adds the offset to this shape's position
pub fn move(self: CircleShape, offset: sf.Vector2f) void {
    sf.c.sfCircleShape_move(self.ptr, offset.toCSFML());
}

/// Gets the origin of this circle shape
pub fn getOrigin(self: CircleShape) sf.Vector2f {
    return sf.Vector2f.fromCSFML(sf.c.sfCircleShape_getOrigin(self.ptr));
}
/// Sets the origin of this circle shape
pub fn setOrigin(self: CircleShape, origin: sf.Vector2f) void {
    sf.c.sfCircleShape_setOrigin(self.ptr, origin.toCSFML());
}

/// Gets the rotation of this circle shape
pub fn getRotation(self: CircleShape) f32 {
    return sf.c.sfCircleShape_getRotation(self.ptr);
}
/// Sets the rotation of this circle shape
pub fn setRotation(self: CircleShape, angle: f32) void {
    sf.c.sfCircleShape_setRotation(self.ptr, angle);
}
/// Rotates this shape by a given amount
pub fn rotate(self: CircleShape, angle: f32) void {
    sf.c.sfCircleShape_rotate(self.ptr, angle);
}

/// Gets the texture of this shape
pub fn getTexture(self: CircleShape) ?sf.Texture {
    const t = sf.c.sfCircleShape_getTexture(self.ptr);
    if (t) |tex| {
        return sf.Texture{ .const_ptr = tex };
    } else return null;
}
/// Sets the texture of this shape
pub fn setTexture(self: CircleShape, texture: ?sf.Texture) void {
    var tex = if (texture) |t| t.get() else null;
    sf.c.sfCircleShape_setTexture(self.ptr, tex, 0);
}
/// Gets the sub-rectangle of the texture that the shape will display
pub fn getTextureRect(self: CircleShape) sf.FloatRect {
    return sf.FloatRect.fromCSFML(sf.c.sfCircleShape_getTextureRect(self.ptr));
}
/// Sets the sub-rectangle of the texture that the shape will display
pub fn setTextureRect(self: CircleShape, rect: sf.FloatRect) void {
    sf.c.sfCircleShape_getCircleRect(self.ptr, rect.toCSFML());
}

/// Gets the bounds in the local coordinates system
pub fn getLocalBounds(self: CircleShape) sf.FloatRect {
    return sf.FloatRect.fromCSFML(sf.c.sfCircleShape_getLocalBounds(self.ptr));
}

/// Gets the bounds in the global coordinates
pub fn getGlobalBounds(self: CircleShape) sf.FloatRect {
    return sf.FloatRect.fromCSFML(sf.c.sfCircleShape_getGlobalBounds(self.ptr));
}

/// Pointer to the csfml structure
ptr: *sf.c.sfCircleShape,

test "circle shape: sane getters and setters" {
    const tst = @import("std").testing;

    var circle = try CircleShape.create(30);
    defer circle.destroy();

    circle.setFillColor(sf.Color.Yellow);
    circle.setRadius(50);
    circle.setRotation(15);
    circle.setPosition(.{ .x = 1, .y = 2 });
    circle.setOrigin(.{ .x = 20, .y = 25 });
    circle.setTexture(null);

    // TODO : issue #2
    try tst.expectEqual(sf.Color.Yellow, circle.getFillColor());
    try tst.expectEqual(@as(f32, 50), circle.getRadius());
    try tst.expectEqual(@as(f32, 15), circle.getRotation());
    try tst.expectEqual(sf.Vector2f{ .x = 1, .y = 2 }, circle.getPosition());
    try tst.expectEqual(sf.Vector2f{ .x = 20, .y = 25 }, circle.getOrigin());
    try tst.expectEqual(@as(?sf.Texture, null), circle.getTexture());

    circle.rotate(5);
    circle.move(.{ .x = -5, .y = 5 });

    try tst.expectEqual(@as(f32, 20), circle.getRotation());
    try tst.expectEqual(sf.Vector2f{ .x = -4, .y = 7 }, circle.getPosition());
}