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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
//! Image living on the graphics card that can be used for drawing.
const sf = struct {
pub usingnamespace @import("../sfml.zig");
pub usingnamespace sf.system;
pub usingnamespace sf.graphics;
};
const std = @import("std");
const assert = std.debug.assert;
const TextureType = enum { _ptr, _const_ptr };
pub const Texture = union(TextureType) {
// Constructor/destructor
/// Creates a texture from nothing
pub fn create(size: sf.Vector2u) !Texture {
const tex = sf.c.sfTexture_create(@intCast(c_uint, size.x), @intCast(c_uint, size.y));
if (tex == null)
return sf.Error.nullptrUnknownReason;
return Texture{ ._ptr = tex.? };
}
/// Loads a texture from a file
pub fn createFromFile(path: [:0]const u8) !Texture {
const tex = sf.c.sfTexture_createFromFile(path, null);
if (tex == null)
return sf.Error.resourceLoadingError;
return Texture{ ._ptr = tex.? };
}
/// Creates an texture from an image
pub fn createFromImage(image: sf.Image, area: ?sf.IntRect) !Texture {
const tex = if (area) |a|
sf.c.sfTexture_createFromImage(image._ptr, &a._toCSFML())
else
sf.c.sfTexture_createFromImage(image._ptr, null);
if (tex == null)
return sf.Error.nullptrUnknownReason;
return Texture{ ._ptr = tex.? };
}
/// Destroys a texture
/// Be careful, you can only destroy non const textures
pub fn destroy(self: *Texture) void {
// TODO : is it possible to detect that comptime?
// Should this panic?
if (self.* == ._const_ptr)
@panic("Can't destroy a const texture pointer");
sf.c.sfTexture_destroy(self._ptr);
}
// Getters/Setters
/// Gets a const pointer to this texture
/// For inner workings
pub fn _get(self: Texture) *const sf.c.sfTexture {
return switch (self) {
._ptr => self._ptr,
._const_ptr => self._const_ptr,
};
}
/// Clones this texture (the clone won't be const)
pub fn copy(self: Texture) !Texture {
const cpy = sf.c.sfTexture_copy(self._get());
if (cpy == null)
return sf.Error.nullptrUnknownReason;
return Texture{ ._ptr = cpy.? };
}
/// Copy this texture to an image in ram
pub fn copyToImage(self: Texture) sf.Image {
return .{ ._ptr = sf.c.sfTexture_copyToImage(self._get()).? };
}
/// Makes this texture constant (I don't know why you would do that)
pub fn makeConst(self: *Texture) void {
self.* = Texture{ ._const_ptr = self._get() };
}
/// Gets the size of this image
pub fn getSize(self: Texture) sf.Vector2u {
const size = sf.c.sfTexture_getSize(self._get());
return sf.Vector2u{ .x = size.x, .y = size.y };
}
/// Gets the pixel count of this image
pub fn getPixelCount(self: Texture) usize {
const dim = self.getSize();
return dim.x * dim.y;
}
/// Updates the pixels of the image from an array of pixels (colors)
pub fn updateFromPixels(self: *Texture, pixels: []const sf.Color, zone: ?sf.Rect(c_uint)) !void {
if (self.* == ._const_ptr)
@panic("Can't set pixels on a const texture");
if (self.isSrgb())
@panic("Updating an srgb from a pixel array isn't implemented");
var real_zone: sf.Rect(c_uint) = undefined;
var size = self.getSize();
if (zone) |z| {
// Check if the given zone is fully inside the image
var intersection = z.intersects(sf.Rect(c_uint).init(0, 0, size.x, size.y));
if (intersection) |i| {
if (!i.equals(z))
return sf.Error.areaDoesNotFit;
} else return sf.Error.areaDoesNotFit;
real_zone = z;
} else {
real_zone.left = 0;
real_zone.top = 0;
real_zone.width = size.x;
real_zone.height = size.y;
}
// Check if there is enough data
if (pixels.len < real_zone.width * real_zone.height)
return sf.Error.notEnoughData;
sf.c.sfTexture_updateFromPixels(self._ptr, @ptrCast([*]const u8, pixels.ptr), real_zone.width, real_zone.height, real_zone.left, real_zone.top);
}
/// Updates the pixels of the image from an other texture
pub fn updateFromTexture(self: *Texture, other: Texture, copy_pos: ?sf.Vector2u) void {
if (self == ._const_ptr)
@panic("Can't set pixels on a const texture");
var pos = if (copy_pos) |a| a else sf.Vector2u{ .x = 0, .y = 0 };
var max = other.getSize().add(pos);
var size = self.getSize();
assert(max.x <= size.x and max.y <= size.y);
sf.c.sfTexture_updateFromTexture(self._ptr, other._get(), pos.x, pos.y);
}
/// Updates the pixels of the image from an image
pub fn updateFromImage(self: *Texture, image: sf.Image, copy_pos: ?sf.Vector2u) void {
if (self.* == ._const_ptr)
@panic("Can't set pixels on a const texture");
var pos = if (copy_pos) |a| a else sf.Vector2u{ .x = 0, .y = 0 };
var max = image.getSize().add(pos);
var size = self.getSize();
assert(max.x <= size.x and max.y <= size.y);
sf.c.sfTexture_updateFromImage(self._ptr, image._ptr, pos.x, pos.y);
}
/// Tells whether or not this texture is to be smoothed
pub fn isSmooth(self: Texture) bool {
return sf.c.sfTexture_isSmooth(self._ptr) != 0;
}
/// Enables or disables texture smoothing
pub fn setSmooth(self: *Texture, smooth: bool) void {
if (self.* == ._const_ptr)
@panic("Can't set properties on a const texture");
sf.c.sfTexture_setSmooth(self._ptr, @boolToInt(smooth));
}
/// Tells whether or not this texture should repeat when rendering outside its bounds
pub fn isRepeated(self: Texture) bool {
return sf.c.sfTexture_isRepeated(self._ptr) != 0;
}
/// Enables or disables texture repeating
pub fn setRepeated(self: *Texture, repeated: bool) void {
if (self.* == ._const_ptr)
@panic("Can't set properties on a const texture");
sf.c.sfTexture_setRepeated(self._ptr, @boolToInt(repeated));
}
/// Tells whether or not this texture has colors in the SRGB format
/// SRGB functions arent implemented yet
pub fn isSrgb(self: Texture) bool {
return sf.c.sfTexture_isSrgb(self._ptr) != 0;
}
/// Enables or disables SRGB
pub fn setSrgb(self: *Texture, srgb: bool) void {
if (self.* == ._const_ptr)
@panic("Can't set properties on a const texture");
sf.c.sfTexture_setSrgb(self._ptr, @boolToInt(srgb));
}
/// Swaps this texture's contents with an other texture
pub fn swap(self: *Texture, other: *Texture) void {
if (self.* == ._const_ptr or other.* == ._const_ptr)
@panic("Texture swapping must be done between two non const textures");
sf.c.sfTexture_swap(self._ptr, other._ptr);
}
// Others
/// Generates a mipmap for the current texture data, returns true if the operation succeeded
pub fn generateMipmap(self: *Texture) bool {
if (self == ._const_ptr)
@panic("Can't act on a const texture");
return sf.c.sfTexture_generateMipmap(self._ptr) != 0;
}
/// Pointer to the csfml texture
_ptr: *sf.c.sfTexture,
/// Const pointer to the csfml texture
_const_ptr: *const sf.c.sfTexture
};
test "texture: sane getters and setters" {
const tst = std.testing;
const allocator = std.heap.page_allocator;
var tex = try Texture.create(.{ .x = 12, .y = 10 });
defer tex.destroy();
var size = tex.getSize();
tex.setSrgb(false);
tex.setSmooth(true);
tex.setRepeated(true);
try tst.expectEqual(@as(u32, 12), size.x);
try tst.expectEqual(@as(u32, 10), size.y);
try tst.expectEqual(@as(usize, 120), tex.getPixelCount());
var pixel_data = try allocator.alloc(sf.Color, 120);
defer allocator.free(pixel_data);
for (pixel_data) |*c, i| {
c.* = sf.graphics.Color.fromHSVA(@intToFloat(f32, i) / 144 * 360, 100, 100, 1);
}
pixel_data[0] = sf.Color.Green;
try tex.updateFromPixels(pixel_data, null);
try tst.expect(!tex.isSrgb());
try tst.expect(tex.isSmooth());
try tst.expect(tex.isRepeated());
var img = tex.copyToImage();
defer img.destroy();
try tst.expectEqual(sf.Color.Green, img.getPixel(.{ .x = 0, .y = 0 }));
tex.updateFromImage(img, null);
var t = tex;
t.makeConst();
var copy = try t.copy();
try tst.expectEqual(@as(usize, 120), copy.getPixelCount());
var tex2 = try Texture.create(.{ .x = 100, .y = 100 });
defer tex2.destroy();
copy.swap(&tex2);
try tst.expectEqual(@as(usize, 100 * 100), copy.getPixelCount());
try tst.expectEqual(@as(usize, 120), tex2.getPixelCount());
}
|