lovr/src/api/l_data.c

162 lines
4.9 KiB
C
Raw Normal View History

2018-01-16 07:13:26 +00:00
#include "api.h"
2019-05-18 00:11:22 +00:00
#include "data/blob.h"
2018-02-11 23:22:04 +00:00
#include "data/modelData.h"
2018-01-22 16:40:47 +00:00
#include "data/rasterizer.h"
2021-02-09 02:52:56 +00:00
#include "data/sound.h"
#include "data/image.h"
2021-03-16 00:54:27 +00:00
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <string.h>
2018-01-16 07:13:26 +00:00
2021-10-08 21:25:03 +00:00
StringEntry lovrAnimationProperty[] = {
[PROP_TRANSLATION] = ENTRY("translation"),
[PROP_ROTATION] = ENTRY("rotation"),
[PROP_SCALE] = ENTRY("scale"),
{ 0 }
};
StringEntry lovrSmoothMode[] = {
[SMOOTH_STEP] = ENTRY("step"),
[SMOOTH_LINEAR] = ENTRY("linear"),
[SMOOTH_CUBIC] = ENTRY("cubic"),
{ 0 }
};
2018-09-27 01:27:38 +00:00
static int l_lovrDataNewBlob(lua_State* L) {
size_t size;
uint8_t* data = NULL;
int type = lua_type(L, 1);
if (type == LUA_TNUMBER) {
2019-02-18 02:43:52 +00:00
int isize = lua_tonumber(L, 1);
lovrAssert(isize > 0, "Blob size must be positive");
size = (size_t) isize;
data = calloc(1, size);
lovrAssert(data, "Out of memory");
} else if (type == LUA_TSTRING) {
const char* str = luaL_checklstring(L, 1, &size);
data = malloc(size + 1);
lovrAssert(data, "Out of memory");
memcpy(data, str, size);
data[size] = '\0';
} else {
Blob* blob = luax_checktype(L, 1, Blob);
size = blob->size;
data = malloc(size);
lovrAssert(data, "Out of memory");
2020-01-14 03:36:12 +00:00
memcpy(data, blob->data, size);
}
const char* name = luaL_optstring(L, 2, "");
Blob* blob = lovrBlobCreate(data, size, name);
luax_pushtype(L, Blob, blob);
2021-02-09 00:52:26 +00:00
lovrRelease(blob, lovrBlobDestroy);
return 1;
}
static int l_lovrDataNewImage(lua_State* L) {
Image* image = NULL;
if (lua_type(L, 1) == LUA_TNUMBER) {
2022-03-14 20:19:59 +00:00
uint32_t width = luax_checku32(L, 1);
uint32_t height = luax_checku32(L, 2);
TextureFormat format = luax_checkenum(L, 3, TextureFormat, "rgba");
Blob* blob = lua_isnoneornil(L, 4) ? NULL : luax_checktype(L, 4, Blob);
image = lovrImageCreate(width, height, blob, 0x0, format);
} else {
Image* source = luax_totype(L, 1, Image);
if (source) {
image = lovrImageCreate(source->width, source->height, source->blob, 0x0, source->format);
} else {
Blob* blob = luax_readblob(L, 1, "Texture");
bool flip = lua_isnoneornil(L, 2) ? true : lua_toboolean(L, 2);
image = lovrImageCreateFromBlob(blob, flip);
lovrRelease(blob, lovrBlobDestroy);
}
}
luax_pushtype(L, Image, image);
lovrRelease(image, lovrImageDestroy);
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrDataNewModelData(lua_State* L) {
2018-01-23 02:24:39 +00:00
Blob* blob = luax_readblob(L, 1, "Model");
2019-12-08 09:46:54 +00:00
ModelData* modelData = lovrModelDataCreate(blob, luax_readfile);
luax_pushtype(L, ModelData, modelData);
2021-02-09 00:52:26 +00:00
lovrRelease(blob, lovrBlobDestroy);
lovrRelease(modelData, lovrModelDataDestroy);
2018-01-23 02:24:39 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static int l_lovrDataNewRasterizer(lua_State* L) {
2018-01-22 16:40:47 +00:00
Blob* blob = NULL;
float size;
if (lua_type(L, 1) == LUA_TNUMBER || lua_isnoneornil(L, 1)) {
size = luax_optfloat(L, 1, 32.f);
2018-01-22 16:40:47 +00:00
} else {
blob = luax_readblob(L, 1, "Font");
size = luax_optfloat(L, 2, 32.f);
2018-01-22 16:40:47 +00:00
}
Rasterizer* rasterizer = lovrRasterizerCreate(blob, size);
luax_pushtype(L, Rasterizer, rasterizer);
2021-02-09 00:52:26 +00:00
lovrRelease(blob, lovrBlobDestroy);
lovrRelease(rasterizer, lovrRasterizerDestroy);
2018-01-21 21:26:00 +00:00
return 1;
}
2021-02-09 02:52:56 +00:00
static int l_lovrDataNewSound(lua_State* L) {
2021-04-03 04:28:16 +00:00
int type = lua_type(L, 1);
if (type == LUA_TNUMBER) {
uint32_t frames = luax_checku32(L, 1);
2021-02-07 13:07:50 +00:00
SampleFormat format = luax_checkenum(L, 2, SampleFormat, "f32");
2021-02-24 21:11:16 +00:00
ChannelLayout layout = luax_checkenum(L, 3, ChannelLayout, "stereo");
uint32_t sampleRate = luax_optu32(L, 4, 48000);
2020-05-10 08:48:01 +00:00
Blob* blob = luax_totype(L, 5, Blob);
2021-02-07 13:07:50 +00:00
const char* other = lua_tostring(L, 5);
bool stream = other && !strcmp(other, "stream");
2021-02-09 02:52:56 +00:00
Sound* sound = stream ?
2021-02-24 21:11:16 +00:00
lovrSoundCreateStream(frames, format, layout, sampleRate) :
lovrSoundCreateRaw(frames, format, layout, sampleRate, blob);
2021-02-09 02:52:56 +00:00
luax_pushtype(L, Sound, sound);
lovrRelease(sound, lovrSoundDestroy);
2018-07-06 03:23:46 +00:00
return 1;
2021-04-03 04:28:16 +00:00
} else if (type != LUA_TSTRING && type != LUA_TUSERDATA) {
return luax_typeerror(L, 1, "number, string, or Blob");
2018-07-06 03:23:46 +00:00
}
2021-02-09 02:52:56 +00:00
Blob* blob = luax_readblob(L, 1, "Sound");
2020-05-10 08:48:01 +00:00
bool decode = lua_toboolean(L, 2);
2021-02-09 02:52:56 +00:00
Sound* sound = lovrSoundCreateFromFile(blob, decode);
luax_pushtype(L, Sound, sound);
2021-02-09 00:52:26 +00:00
lovrRelease(blob, lovrBlobDestroy);
2021-02-09 02:52:56 +00:00
lovrRelease(sound, lovrSoundDestroy);
2018-07-06 03:23:46 +00:00
return 1;
}
2018-09-27 01:27:38 +00:00
static const luaL_Reg lovrData[] = {
{ "newBlob", l_lovrDataNewBlob },
{ "newImage", l_lovrDataNewImage },
2018-01-23 02:24:39 +00:00
{ "newModelData", l_lovrDataNewModelData },
2018-01-22 16:40:47 +00:00
{ "newRasterizer", l_lovrDataNewRasterizer },
2021-02-09 02:52:56 +00:00
{ "newSound", l_lovrDataNewSound },
2018-01-16 07:13:26 +00:00
{ NULL, NULL }
};
2018-09-27 01:27:38 +00:00
2021-03-16 00:54:27 +00:00
extern const luaL_Reg lovrBlob[];
extern const luaL_Reg lovrImage[];
extern const luaL_Reg lovrModelData[];
extern const luaL_Reg lovrRasterizer[];
extern const luaL_Reg lovrSound[];
int luaopen_lovr_data(lua_State* L) {
2018-09-27 01:27:38 +00:00
lua_newtable(L);
2020-08-19 19:12:06 +00:00
luax_register(L, lovrData);
luax_registertype(L, Blob);
luax_registertype(L, Image);
luax_registertype(L, ModelData);
luax_registertype(L, Rasterizer);
2021-02-09 02:52:56 +00:00
luax_registertype(L, Sound);
2018-09-27 01:27:38 +00:00
return 1;
}