/
texture.c
337 lines (283 loc) · 8.2 KB
/
texture.c
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/***
The `gfx.texture` module.
@module ctr.gfx.texture
@usage local texture = require("ctr.gfx.texture")
*/
#include <sf2d.h>
#include <sfil.h>
#include <lapi.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <string.h>
#include "texture.h"
int getType(const char *name) {
const char *dot = strrchr(name, '.');
if(!dot || dot == name) dot = "";
const char *ext = dot + 1;
if (strncmp(ext, "png", 3) == 0) {
return 0;
} else if (strncmp(ext, "jpeg", 4) == 0 || strncmp(ext, "jpg", 3) == 0) {
return 1;
} else if (strncmp(ext, "bmp", 3) == 0) {
return 2;
} else {
return 4;
}
}
// module functions
/***
Load a texture from a file. Supported formats: PNG, JPEG, BMP.
@function load
@tparam string path path to the image file
@tparam[opt=PLACE_RAM] number place where to put the loaded texture
@tparam[opt=auto] number type type of the image
@treturn texture the loaded texture object
*/
static int texture_load(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
u8 place = luaL_optinteger(L, 2, SF2D_PLACE_RAM); //place in vram by default
u8 type = luaL_optinteger(L, 3, 3); //type 3 is "search at the end of the filename"
texture_userdata *texture;
texture = (texture_userdata *)lua_newuserdata(L, sizeof(*texture));
luaL_getmetatable(L, "LTexture");
lua_setmetatable(L, -2);
if (type==3) type = getType(path);
if (type==0) { //PNG
texture->texture = sfil_load_PNG_file(path, place);
} else if (type==1) { //JPEG
texture->texture = sfil_load_JPEG_file(path, place);
} else if (type==2) { //BMP
texture->texture = sfil_load_BMP_file(path, place); //appears to be broken right now.
} else {
lua_pushnil(L);
lua_pushstring(L, "Bad type");
return 2;
}
if (texture->texture == NULL) {
lua_pushnil(L);
lua_pushstring(L, "No such file");
return 2;
}
texture->scaleX = 1.0f;
texture->scaleY = 1.0f;
texture->blendColor = 0xffffffff;
return 1;
}
/***
Create an empty texture.
@function new
@tparam number width Texture width
@tparam number height Texture height
@tparam[opt=PLACE_RAM] number place where to put the loaded texture
@treturn texture the loaded texture object
*/
static int texture_new(lua_State *L) {
int w = luaL_checkinteger(L, 1);
int h = luaL_checkinteger(L, 2);
u8 place = luaL_checkinteger(L, 3);
texture_userdata *texture;
texture = (texture_userdata *)lua_newuserdata(L, sizeof(*texture));
luaL_getmetatable(L, "LTexture");
lua_setmetatable(L, -2);
texture->texture = sf2d_create_texture(w, h, TEXFMT_RGBA8, place);
sf2d_texture_tile32(texture->texture);
texture->scaleX = 1.0f;
texture->scaleY = 1.0f;
texture->blendColor = 0xffffffff;
return 1;
}
/***
Texture object
@section Methods
*/
/***
Draw a texture.
@function :draw
@tparam number x X position
@tparam number y Y position
@tparam[opt=0.0] number rad rotation of the texture (in radians)
*/
static int texture_draw(lua_State *L) {
texture_userdata *texture = luaL_checkudata(L, 1, "LTexture");
int x = luaL_checkinteger(L, 2);
int y = luaL_checkinteger(L, 3);
float rad = luaL_optnumber(L, 4, 0.0f);
if (rad == 0.0f && texture->scaleX == 1.0f && texture->scaleY == 1.0f && texture->blendColor == 0xffffffff) {
sf2d_draw_texture(texture->texture, x, y);
} else {
sf2d_draw_texture_part_rotate_scale_blend(texture->texture, x, y, rad, 0, 0, texture->texture->width, texture->texture->height, texture->scaleX, texture->scaleY, texture->blendColor);
}
return 0;
}
/***
Draw a part of the texture
@function :drawPart
@tparam number x X position
@tparam number y Y position
@tparam number sx X position of the beginning of the part
@tparam number sy Y position of the beginning of the part
@tparam number w width of the part
@tparam number h height of the part
@tparam[opt=0.0] number rad rotation of the part (in radians)
*/
static int texture_drawPart(lua_State *L) {
texture_userdata *texture = luaL_checkudata(L, 1, "LTexture");
int x = luaL_checkinteger(L, 2);
int y = luaL_checkinteger(L, 3);
int sx = luaL_checkinteger(L, 4);
int sy = luaL_checkinteger(L, 5);
int w = luaL_checkinteger(L, 6);
int h = luaL_checkinteger(L, 7);
int rad = luaL_optnumber(L, 8, 0.0f);
sf2d_draw_texture_part_rotate_scale_blend(texture->texture, x, y, rad, sx, sy, w, h, texture->scaleX, texture->scaleY, texture->blendColor);
return 0;
}
/***
Return the size of the texture.
@function :getSize
@treturn number width of the texture
@treturn number height of the texture
*/
static int texture_getSize(lua_State *L) {
texture_userdata *texture = luaL_checkudata(L, 1, "LTexture");
lua_pushinteger(L, texture->texture->width);
lua_pushinteger(L, texture->texture->height);
return 2;
}
/***
Unload a texture.
@function :unload
*/
static int texture_unload(lua_State *L) {
texture_userdata *texture = luaL_checkudata(L, 1, "LTexture");
if (texture->texture == NULL) return 0;
sf2d_free_texture(texture->texture);
texture->texture = NULL;
return 0;
}
/***
Rescale the texture. The default scale is `1.0`.
@function :scale
@tparam number scaleX new scale of the width
@tparam number scaleY new scale of the height
*/
static int texture_scale(lua_State *L) {
texture_userdata *texture = luaL_checkudata(L, 1, "LTexture");
float sx = luaL_checknumber(L, 2);
float sy = luaL_checknumber(L, 3);
texture->scaleX = sx;
texture->scaleY = sy;
return 0;
}
/***
Return the color of a pixel.
@function :getPixel
@tparam number x X position of the pixel
@tparam number y Y position of the pixel
@treturn number color of the pixel.
*/
static int texture_getPixel(lua_State *L) {
texture_userdata *texture = luaL_checkudata(L, 1, "LTexture");
int x = luaL_checkinteger(L, 2);
int y = luaL_checkinteger(L, 3);
lua_pushinteger(L, sf2d_get_pixel(texture->texture, x, y));
return 1;
}
/***
Set the color of a pixel.
@function :setPixel
@tparam number x X position of the pixel
@tparam number y Y position of the pixel
@tparam number color New color of the pixel
*/
static int texture_setPixel(lua_State *L) {
texture_userdata *texture = luaL_checkudata(L, 1, "LTexture");
int x = luaL_checkinteger(L, 2);
int y = luaL_checkinteger(L, 3);
u32 color = luaL_checkinteger(L, 4);
sf2d_set_pixel(texture->texture, x, y, color);
return 0;
}
/***
Set the blend color of the texture.
@function :setBlendColor
@tparam number color new blend color
*/
static int texture_setBlendColor(lua_State *L) {
texture_userdata *texture = luaL_checkudata(L, 1, "LTexture");
u32 color = luaL_checkinteger(L, 2);
texture->blendColor = color;
return 0;
}
// object
static const struct luaL_Reg texture_methods[] = {
{ "draw", texture_draw },
{ "drawPart", texture_drawPart },
{ "scale", texture_scale },
{ "getSize", texture_getSize },
{ "unload", texture_unload },
{ "getPixel", texture_getPixel },
{ "setPixel", texture_setPixel },
{ "setBlendColor", texture_setBlendColor },
{ "__gc", texture_unload },
{NULL, NULL}
};
// module
static const struct luaL_Reg texture_functions[] = {
{"load", texture_load},
{"new", texture_new },
{NULL, NULL}
};
/***
Fields
@section Fields
*/
// constants
struct { char *name; int value; } texture_constants[] = {
/***
Constant used to select the RAM.
@field PLACE_RAM
*/
{"PLACE_RAM", SF2D_PLACE_RAM },
/***
Constant used to select the VRAM.
@field PLACE_VRAM
*/
{"PLACE_VRAM", SF2D_PLACE_VRAM},
/***
Constant used to select a temporary RAM pool. Don't use it.
@field PLACE_TEMP
*/
{"PLACE_TEMP", SF2D_PLACE_TEMP},
/***
Constant used to select the PNG type.
@field TYPE_PNG
*/
{"TYPE_PNG", 0 },
/***
Constant used to select the JPEG type.
@field TYPE_JPEG
*/
{"TYPE_JPEG", 1 },
/***
Constant used to select the BMP type.
@field TYPE_BMP
*/
{"TYPE_BMP", 2 },
{NULL, 0}
};
int luaopen_texture_lib(lua_State *L) {
luaL_newmetatable(L, "LTexture");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, texture_methods, 0);
luaL_newlib(L, texture_functions);
for (int i = 0; texture_constants[i].name; i++) {
lua_pushinteger(L, texture_constants[i].value);
lua_setfield(L, -2, texture_constants[i].name);
}
return 1;
}
void load_texture_lib(lua_State *L) {
luaL_requiref(L, "ctr.gfx.texture", luaopen_texture_lib, false);
}