2018-01-16 07:13:26 +00:00
|
|
|
#include "api.h"
|
2018-09-27 00:54:12 +00:00
|
|
|
#include "api/data.h"
|
2018-01-21 21:26:00 +00:00
|
|
|
#include "data/audioStream.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"
|
2018-07-06 03:23:46 +00:00
|
|
|
#include "data/soundData.h"
|
2018-02-11 23:22:04 +00:00
|
|
|
#include "data/textureData.h"
|
2018-01-16 07:13:26 +00:00
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrDataNewBlob(lua_State* L) {
|
2018-02-20 05:04:53 +00:00
|
|
|
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;
|
2018-02-20 05:04:53 +00:00
|
|
|
data = calloc(1, size);
|
2019-01-29 10:49:09 +00:00
|
|
|
lovrAssert(data, "Out of memory");
|
2018-03-11 05:37:05 +00:00
|
|
|
} else if (type == LUA_TSTRING) {
|
2018-02-20 05:04:53 +00:00
|
|
|
const char* str = luaL_checklstring(L, 1, &size);
|
|
|
|
data = malloc(size + 1);
|
2019-01-29 10:49:09 +00:00
|
|
|
lovrAssert(data, "Out of memory");
|
2018-02-20 05:04:53 +00:00
|
|
|
memcpy(data, str, size);
|
|
|
|
data[size] = '\0';
|
2018-03-11 05:37:05 +00:00
|
|
|
} else {
|
2018-07-25 04:15:07 +00:00
|
|
|
Blob* blob = luax_checktype(L, 1, Blob);
|
2018-03-11 05:37:05 +00:00
|
|
|
size = blob->size;
|
|
|
|
data = malloc(size);
|
2019-01-29 10:49:09 +00:00
|
|
|
lovrAssert(data, "Out of memory");
|
2018-02-20 05:04:53 +00:00
|
|
|
}
|
|
|
|
const char* name = luaL_optstring(L, 2, "");
|
|
|
|
Blob* blob = lovrBlobCreate(data, size, name);
|
2018-07-25 03:10:30 +00:00
|
|
|
luax_pushobject(L, blob);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Blob, blob);
|
2018-02-20 05:04:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrDataNewAudioStream(lua_State* L) {
|
2018-07-06 03:23:46 +00:00
|
|
|
Blob* blob = luax_readblob(L, 1, "AudioStream");
|
2019-03-17 07:58:01 +00:00
|
|
|
int bufferSize = luaL_optinteger(L, 2, 4096);
|
2018-01-22 16:40:47 +00:00
|
|
|
AudioStream* stream = lovrAudioStreamCreate(blob, bufferSize);
|
2018-07-25 03:10:30 +00:00
|
|
|
luax_pushobject(L, stream);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Blob, blob);
|
|
|
|
lovrRelease(AudioStream, stream);
|
2018-01-21 05:33:09 +00:00
|
|
|
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-01-27 22:29:06 +00:00
|
|
|
ModelData* modelData = lovrModelDataCreate(blob);
|
2018-07-25 03:10:30 +00:00
|
|
|
luax_pushobject(L, modelData);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Blob, blob);
|
|
|
|
lovrRelease(ModelData, modelData);
|
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)) {
|
2019-01-29 10:28:55 +00:00
|
|
|
size = luax_optfloat(L, 1, 32.f);
|
2018-01-22 16:40:47 +00:00
|
|
|
} else {
|
|
|
|
blob = luax_readblob(L, 1, "Font");
|
2019-01-29 10:28:55 +00:00
|
|
|
size = luax_optfloat(L, 2, 32.f);
|
2018-01-22 16:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rasterizer* rasterizer = lovrRasterizerCreate(blob, size);
|
2018-07-25 03:10:30 +00:00
|
|
|
luax_pushobject(L, rasterizer);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Blob, blob);
|
|
|
|
lovrRelease(Rasterizer, rasterizer);
|
2018-01-21 21:26:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrDataNewSoundData(lua_State* L) {
|
2018-07-06 03:23:46 +00:00
|
|
|
if (lua_type(L, 1) == LUA_TNUMBER) {
|
|
|
|
int samples = luaL_checkinteger(L, 1);
|
|
|
|
int sampleRate = luaL_optinteger(L, 2, 44100);
|
|
|
|
int bitDepth = luaL_optinteger(L, 3, 16);
|
|
|
|
int channelCount = luaL_optinteger(L, 4, 2);
|
|
|
|
SoundData* soundData = lovrSoundDataCreate(samples, sampleRate, bitDepth, channelCount);
|
2018-07-25 03:10:30 +00:00
|
|
|
luax_pushobject(L, soundData);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(SoundData, soundData);
|
2018-07-06 03:23:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-07-25 04:15:07 +00:00
|
|
|
AudioStream* audioStream = luax_totype(L, 1, AudioStream);
|
|
|
|
if (audioStream) {
|
|
|
|
SoundData* soundData = lovrSoundDataCreateFromAudioStream(audioStream);
|
2018-07-25 03:10:30 +00:00
|
|
|
luax_pushobject(L, soundData);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(SoundData, soundData);
|
2018-07-06 03:23:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Blob* blob = luax_readblob(L, 1, "SoundData");
|
|
|
|
SoundData* soundData = lovrSoundDataCreateFromBlob(blob);
|
2018-07-25 03:10:30 +00:00
|
|
|
luax_pushobject(L, soundData);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Blob, blob);
|
|
|
|
lovrRelease(SoundData, soundData);
|
2018-07-06 03:23:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static int l_lovrDataNewTextureData(lua_State* L) {
|
2018-02-11 01:27:29 +00:00
|
|
|
TextureData* textureData = NULL;
|
|
|
|
if (lua_type(L, 1) == LUA_TNUMBER) {
|
2019-01-12 06:37:54 +00:00
|
|
|
int width = luaL_checkinteger(L, 1);
|
|
|
|
int height = luaL_checkinteger(L, 2);
|
2018-08-11 07:02:17 +00:00
|
|
|
TextureFormat format = luaL_checkoption(L, 3, "rgba", TextureFormats);
|
2018-08-21 01:14:33 +00:00
|
|
|
textureData = lovrTextureDataCreate(width, height, 0x0, format);
|
2018-02-11 01:27:29 +00:00
|
|
|
} else {
|
|
|
|
Blob* blob = luax_readblob(L, 1, "Texture");
|
2019-01-05 05:51:20 +00:00
|
|
|
bool flip = lua_isnoneornil(L, 2) ? true : lua_toboolean(L, 2);
|
|
|
|
textureData = lovrTextureDataCreateFromBlob(blob, flip);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(Blob, blob);
|
2018-02-11 01:27:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 03:10:30 +00:00
|
|
|
luax_pushobject(L, textureData);
|
2019-04-05 10:41:03 +00:00
|
|
|
lovrRelease(TextureData, textureData);
|
2018-02-11 01:27:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-09-27 01:27:38 +00:00
|
|
|
static const luaL_Reg lovrData[] = {
|
2018-02-20 05:04:53 +00:00
|
|
|
{ "newBlob", l_lovrDataNewBlob },
|
2018-01-21 21:26:00 +00:00
|
|
|
{ "newAudioStream", l_lovrDataNewAudioStream },
|
2018-01-23 02:24:39 +00:00
|
|
|
{ "newModelData", l_lovrDataNewModelData },
|
2018-01-22 16:40:47 +00:00
|
|
|
{ "newRasterizer", l_lovrDataNewRasterizer },
|
2018-07-06 03:23:46 +00:00
|
|
|
{ "newSoundData", l_lovrDataNewSoundData },
|
2018-01-21 05:33:09 +00:00
|
|
|
{ "newTextureData", l_lovrDataNewTextureData },
|
2018-01-16 07:13:26 +00:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2018-09-27 01:27:38 +00:00
|
|
|
|
|
|
|
int luaopen_lovr_data(lua_State* L) {
|
|
|
|
lua_newtable(L);
|
|
|
|
luaL_register(L, NULL, lovrData);
|
2019-04-05 10:48:36 +00:00
|
|
|
luax_registertype(L, Blob);
|
|
|
|
luax_registertype(L, AudioStream);
|
|
|
|
luax_registertype(L, ModelData);
|
|
|
|
luax_registertype(L, Rasterizer);
|
|
|
|
luax_extendtype(L, Blob, SoundData);
|
|
|
|
luax_extendtype(L, Blob, TextureData);
|
2018-09-27 01:27:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|