lovr/src/api/data.c

172 lines
4.9 KiB
C
Raw Normal View History

2018-01-16 07:13:26 +00:00
#include "api.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-07-06 03:23:46 +00:00
#include "data/vertexData.h"
2018-01-16 07:13:26 +00:00
int l_lovrDataInit(lua_State* L) {
lua_newtable(L);
luaL_register(L, NULL, lovrData);
luax_registertype(L, "Blob", lovrBlob);
2018-01-21 21:26:00 +00:00
luax_registertype(L, "AudioStream", lovrAudioStream);
2018-01-23 02:24:39 +00:00
luax_registertype(L, "ModelData", lovrModelData);
2018-01-22 16:40:47 +00:00
luax_registertype(L, "Rasterizer", lovrRasterizer);
2018-07-06 03:23:46 +00:00
luax_extendtype(L, "Blob", "SoundData", lovrBlob, lovrSoundData);
luax_extendtype(L, "Blob", "TextureData", lovrBlob, lovrTextureData);
luax_extendtype(L, "Blob", "VertexData", lovrBlob, lovrVertexData);
2018-01-22 16:40:47 +00:00
return 1;
}
int l_lovrDataNewBlob(lua_State* L) {
size_t size;
uint8_t* data = NULL;
int type = lua_type(L, 1);
if (type == LUA_TNUMBER) {
size = lua_tonumber(L, 1);
data = calloc(1, size);
} else if (type == LUA_TSTRING) {
const char* str = luaL_checklstring(L, 1, &size);
data = malloc(size + 1);
memcpy(data, str, size);
data[size] = '\0';
} else {
Blob* blob = luax_checktypeof(L, 1, Blob);
size = blob->size;
data = malloc(size);
}
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);
2018-02-26 08:59:03 +00:00
lovrRelease(blob);
return 1;
}
2018-01-22 16:40:47 +00:00
int l_lovrDataNewAudioStream(lua_State* L) {
2018-07-06 03:23:46 +00:00
Blob* blob = luax_readblob(L, 1, "AudioStream");
2018-01-22 16:40:47 +00:00
size_t bufferSize = luaL_optinteger(L, 2, 4096);
AudioStream* stream = lovrAudioStreamCreate(blob, bufferSize);
2018-07-25 03:10:30 +00:00
luax_pushobject(L, stream);
2018-02-26 08:59:03 +00:00
lovrRelease(blob);
lovrRelease(stream);
2018-01-21 05:33:09 +00:00
return 1;
}
2018-01-23 02:24:39 +00:00
int l_lovrDataNewModelData(lua_State* L) {
Blob* blob = luax_readblob(L, 1, "Model");
ModelData* modelData = lovrModelDataCreate(blob);
2018-07-25 03:10:30 +00:00
luax_pushobject(L, modelData);
2018-02-26 08:59:03 +00:00
lovrRelease(blob);
lovrRelease(modelData);
2018-01-23 02:24:39 +00:00
return 1;
}
2018-01-22 16:40:47 +00:00
int l_lovrDataNewRasterizer(lua_State* L) {
Blob* blob = NULL;
float size;
if (lua_type(L, 1) == LUA_TNUMBER || lua_isnoneornil(L, 1)) {
size = luaL_optnumber(L, 1, 32);
} else {
blob = luax_readblob(L, 1, "Font");
size = luaL_optnumber(L, 2, 32);
}
Rasterizer* rasterizer = lovrRasterizerCreate(blob, size);
2018-07-25 03:10:30 +00:00
luax_pushobject(L, rasterizer);
2018-02-26 08:59:03 +00:00
lovrRelease(blob);
lovrRelease(rasterizer);
2018-01-21 21:26:00 +00:00
return 1;
}
2018-07-06 03:23:46 +00:00
int l_lovrDataNewSoundData(lua_State* L) {
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);
2018-07-06 03:23:46 +00:00
return 1;
}
AudioStream** audioStream;
if ((audioStream = luax_totype(L, 1, AudioStream)) != NULL) {
SoundData* soundData = lovrSoundDataCreateFromAudioStream(*audioStream);
2018-07-25 03:10:30 +00:00
luax_pushobject(L, 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);
2018-07-06 03:23:46 +00:00
lovrRelease(blob);
lovrRelease(soundData);
return 1;
}
2018-02-11 01:27:29 +00:00
int l_lovrDataNewTextureData(lua_State* L) {
TextureData* textureData = NULL;
if (lua_type(L, 1) == LUA_TNUMBER) {
int width = luaL_checknumber(L, 1);
int height = luaL_checknumber(L, 2);
textureData = lovrTextureDataGetBlank(width, height, 0x0, FORMAT_RGBA);
} else {
Blob* blob = luax_readblob(L, 1, "Texture");
textureData = lovrTextureDataFromBlob(blob);
2018-02-26 08:59:03 +00:00
lovrRelease(blob);
2018-02-11 01:27:29 +00:00
}
2018-07-25 03:10:30 +00:00
luax_pushobject(L, textureData);
2018-02-26 08:59:03 +00:00
lovrRelease(textureData);
2018-02-11 01:27:29 +00:00
return 1;
}
int l_lovrDataNewVertexData(lua_State* L) {
uint32_t count;
int dataIndex = 0;
bool hasFormat = false;
2018-02-11 01:27:29 +00:00
VertexFormat format;
vertexFormatInit(&format);
if (lua_isnumber(L, 1)) {
count = lua_tointeger(L, 1);
} else if (lua_istable(L, 1)) {
if (lua_isnumber(L, 2)) {
hasFormat = luax_checkvertexformat(L, 1, &format);
count = lua_tointeger(L, 2);
dataIndex = 0;
} else if (lua_istable(L, 2)) {
hasFormat = luax_checkvertexformat(L, 1, &format);
count = lua_objlen(L, 2);
dataIndex = 2;
} else {
count = lua_objlen(L, 1);
dataIndex = 1;
}
} else {
return luaL_argerror(L, 1, "table or number expected");
}
2018-03-21 21:48:46 +00:00
VertexData* vertexData = lovrVertexDataCreate(count, hasFormat ? &format : NULL);
if (dataIndex) {
luax_loadvertices(L, dataIndex, &vertexData->format, (VertexPointer) { .raw = vertexData->blob.data });
}
2018-07-25 03:10:30 +00:00
luax_pushobject(L, vertexData);
2018-02-26 08:59:03 +00:00
lovrRelease(vertexData);
2018-02-11 01:27:29 +00:00
return 1;
}
2018-01-16 07:13:26 +00:00
const luaL_Reg lovrData[] = {
{ "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-02-11 01:27:29 +00:00
{ "newVertexData", l_lovrDataNewVertexData },
2018-01-16 07:13:26 +00:00
{ NULL, NULL }
};