aboutsummaryrefslogtreecommitdiff
path: root/src/sfml/graphics/RectangleShape.zig
blob: 34bb1148b35f36e7b53ae773a442f25cd1ae1f41 (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
151
152
//! Specialized shape representing a rectangle.

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

const RectangleShape = @This();

// Constructor/destructor

/// Creates a rectangle shape with a size. The rectangle will be white
pub fn create(size: sf.Vector2f) !RectangleShape {
    var rect = sf.c.sfRectangleShape_create();
    if (rect == null)
        return sf.Error.nullptrUnknownReason;

    sf.c.sfRectangleShape_setFillColor(rect, sf.c.sfWhite);
    sf.c.sfRectangleShape_setSize(rect, size.toCSFML());

    return RectangleShape{ .ptr = rect.? };
}

/// Destroys a rectangle shape
pub fn destroy(self: RectangleShape) void {
    sf.c.sfRectangleShape_destroy(self.ptr);
}

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

// Getters/setters

/// Gets the fill color of this rectangle shape
pub fn getFillColor(self: RectangleShape) sf.Color {
    return sf.Color.fromCSFML(sf.c.sfRectangleShape_getFillColor(self.ptr));
}
/// Sets the fill color of this rectangle shape
pub fn setFillColor(self: RectangleShape, color: sf.Color) void {
    sf.c.sfRectangleShape_setFillColor(self.ptr, color.toCSFML());
}

/// Gets the size of this rectangle shape
pub fn getSize(self: RectangleShape) sf.Vector2f {
    return sf.Vector2f.fromCSFML(sf.c.sfRectangleShape_getSize(self.ptr));
}
/// Sets the size of this rectangle shape
pub fn setSize(self: RectangleShape, size: sf.Vector2f) void {
    sf.c.sfRectangleShape_setSize(self.ptr, size.toCSFML());
}

/// Gets the position of this rectangle shape
pub fn getPosition(self: RectangleShape) sf.Vector2f {
    return sf.Vector2f.fromCSFML(sf.c.sfRectangleShape_getPosition(self.ptr));
}
/// Sets the position of this rectangle shape
pub fn setPosition(self: RectangleShape, pos: sf.Vector2f) void {
    sf.c.sfRectangleShape_setPosition(self.ptr, pos.toCSFML());
}
/// Adds the offset to this shape's position
pub fn move(self: RectangleShape, offset: sf.Vector2f) void {
    sf.c.sfRectangleShape_move(self.ptr, offset.toCSFML());
}

/// Gets the origin of this rectangle shape
pub fn getOrigin(self: RectangleShape) sf.Vector2f {
    return sf.Vector2f.fromCSFML(sf.c.sfRectangleShape_getOrigin(self.ptr));
}
/// Sets the origin of this rectangle shape
pub fn setOrigin(self: RectangleShape, origin: sf.Vector2f) void {
    sf.c.sfRectangleShape_setOrigin(self.ptr, origin.toCSFML());
}

/// Gets the rotation of this rectangle shape
pub fn getRotation(self: RectangleShape) f32 {
    return sf.c.sfRectangleShape_getRotation(self.ptr);
}
/// Sets the rotation of this rectangle shape
pub fn setRotation(self: RectangleShape, angle: f32) void {
    sf.c.sfRectangleShape_setRotation(self.ptr, angle);
}
/// Rotates this shape by a given amount
pub fn rotate(self: RectangleShape, angle: f32) void {
    sf.c.sfRectangleShape_rotate(self.ptr, angle);
}

/// Gets the texture of this shape
pub fn getTexture(self: RectangleShape) ?sf.Texture {
    const t = sf.c.sfRectangleShape_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: RectangleShape, texture: ?sf.Texture) void {
    var tex = if (texture) |t| t.get() else null;
    sf.c.sfRectangleShape_setTexture(self.ptr, tex, 0);
}
/// Gets the sub-rectangle of the texture that the shape will display
pub fn getTextureRect(self: RectangleShape) sf.FloatRect {
    return sf.FloatRect.fromCSFML(sf.c.sfRectangleShape_getTextureRect(self.ptr));
}
/// Sets the sub-rectangle of the texture that the shape will display
pub fn setTextureRect(self: RectangleShape, rect: sf.FloatRect) void {
    sf.c.sfRectangleShape_getTextureRect(self.ptr, rect.toCSFML());
}

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

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

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

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

    var rect = try RectangleShape.create(sf.Vector2f{ .x = 30, .y = 50 });
    defer rect.destroy();

    try tst.expectEqual(sf.Vector2f{ .x = 30, .y = 50 }, rect.getSize());

    rect.setFillColor(sf.Color.Yellow);
    rect.setSize(.{ .x = 15, .y = 510 });
    rect.setRotation(15);
    rect.setPosition(.{ .x = 1, .y = 2 });
    rect.setOrigin(.{ .x = 20, .y = 25 });
    rect.setTexture(null);  //Weirdly, getTexture if texture wasn't set gives a wrong pointer

    try tst.expectEqual(sf.Color.Yellow, rect.getFillColor());
    try tst.expectEqual(sf.Vector2f{ .x = 15, .y = 510 }, rect.getSize());
    try tst.expectEqual(@as(f32, 15), rect.getRotation());
    try tst.expectEqual(sf.Vector2f{ .x = 1, .y = 2 }, rect.getPosition());
    try tst.expectEqual(sf.Vector2f{ .x = 20, .y = 25 }, rect.getOrigin());
    try tst.expectEqual(@as(?sf.Texture, null), rect.getTexture());

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

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