2017-12-10 20:40:37 +00:00
|
|
|
#include "api.h"
|
2017-12-07 07:50:52 +00:00
|
|
|
#include "graphics/canvas.h"
|
2018-08-24 21:47:35 +00:00
|
|
|
#include "graphics/graphics.h"
|
2021-02-08 18:16:00 +00:00
|
|
|
#include "graphics/texture.h"
|
2021-03-16 00:54:27 +00:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lauxlib.h>
|
2019-06-02 07:20:10 +00:00
|
|
|
#include <stdlib.h>
|
2017-12-07 07:50:52 +00:00
|
|
|
|
2018-08-22 04:08:40 +00:00
|
|
|
static int luax_checkattachment(lua_State* L, int index, Attachment* attachment) {
|
2018-08-31 23:37:30 +00:00
|
|
|
if (lua_istable(L, index)) {
|
|
|
|
lua_rawgeti(L, index, 1);
|
|
|
|
attachment->texture = luax_checktype(L, -1, Texture);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
lua_rawgeti(L, index, 2);
|
2022-02-08 20:40:10 +00:00
|
|
|
attachment->slice = luax_optu32(L, -1, 1) - 1;
|
2018-08-31 23:37:30 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
lua_rawgeti(L, index, 3);
|
|
|
|
attachment->level = luax_optmipmap(L, -1, attachment->texture);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
index++;
|
|
|
|
} else {
|
|
|
|
attachment->texture = luax_checktype(L, index++, Texture);
|
|
|
|
attachment->slice = lua_type(L, index) == LUA_TNUMBER ? lua_tointeger(L, index++) - 1 : 0;
|
|
|
|
attachment->level = lua_type(L, index) == LUA_TNUMBER ? luax_optmipmap(L, index++, attachment->texture) : 0;
|
|
|
|
}
|
2018-08-22 04:08:40 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2018-08-31 23:37:30 +00:00
|
|
|
void luax_readattachments(lua_State* L, int index, Attachment* attachments, int* count) {
|
|
|
|
bool table = lua_istable(L, index);
|
|
|
|
int top = table ? -1 : lua_gettop(L);
|
|
|
|
int n;
|
|
|
|
|
2019-04-21 01:42:25 +00:00
|
|
|
if (table) {
|
2019-03-17 07:58:01 +00:00
|
|
|
n = luax_len(L, index);
|
2018-08-31 23:37:30 +00:00
|
|
|
n = MIN(n, 3 * MAX_CANVAS_ATTACHMENTS);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
lua_rawgeti(L, index, i + 1);
|
|
|
|
}
|
|
|
|
index = -n;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (*count = 0; *count < MAX_CANVAS_ATTACHMENTS && index <= top; (*count)++) {
|
|
|
|
index = luax_checkattachment(L, index, attachments + *count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (table) {
|
|
|
|
lua_pop(L, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 03:17:47 +00:00
|
|
|
static int l_lovrCanvasNewImage(lua_State* L) {
|
2018-08-30 00:06:20 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
2019-05-20 21:34:03 +00:00
|
|
|
uint32_t index = luaL_optinteger(L, 2, 1) - 1;
|
|
|
|
uint32_t count;
|
2018-08-30 00:06:20 +00:00
|
|
|
lovrCanvasGetAttachments(canvas, &count);
|
2021-02-09 03:17:47 +00:00
|
|
|
lovrAssert(index < count, "Can not create an Image from Texture #%d of Canvas (it only has %d textures)", index, count);
|
|
|
|
Image* image = lovrCanvasNewImage(canvas, index);
|
|
|
|
luax_pushtype(L, Image, image);
|
|
|
|
lovrRelease(image, lovrImageDestroy);
|
2018-08-30 00:06:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasRenderTo(lua_State* L) {
|
2018-08-30 00:06:20 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
|
|
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
|
|
|
int argumentCount = lua_gettop(L) - 2;
|
|
|
|
Canvas* old = lovrGraphicsGetCanvas();
|
|
|
|
lovrGraphicsSetCanvas(canvas);
|
|
|
|
lua_call(L, argumentCount, 0);
|
|
|
|
lovrGraphicsSetCanvas(old);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasGetTexture(lua_State* L) {
|
2018-08-24 01:28:37 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
2019-05-20 21:34:03 +00:00
|
|
|
uint32_t count;
|
2018-08-24 01:28:37 +00:00
|
|
|
const Attachment* attachments = lovrCanvasGetAttachments(canvas, &count);
|
2019-05-20 21:34:03 +00:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, Texture, attachments[i].texture);
|
2018-08-24 01:28:37 +00:00
|
|
|
}
|
|
|
|
return count;
|
2018-08-22 04:08:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasSetTexture(lua_State* L) {
|
2018-08-22 04:08:40 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
2018-08-24 01:28:37 +00:00
|
|
|
Attachment attachments[MAX_CANVAS_ATTACHMENTS];
|
|
|
|
int count;
|
2018-08-31 23:37:30 +00:00
|
|
|
luax_readattachments(L, 2, attachments, &count);
|
2018-08-24 01:28:37 +00:00
|
|
|
lovrCanvasSetAttachments(canvas, attachments, count);
|
2018-08-22 04:08:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasGetWidth(lua_State* L) {
|
2018-08-29 21:03:14 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
2018-08-30 00:06:20 +00:00
|
|
|
lua_pushinteger(L, lovrCanvasGetWidth(canvas));
|
2018-08-29 21:03:14 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasGetHeight(lua_State* L) {
|
2018-08-24 21:47:35 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
2018-08-30 00:06:20 +00:00
|
|
|
lua_pushinteger(L, lovrCanvasGetHeight(canvas));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasGetDimensions(lua_State* L) {
|
2018-08-30 00:06:20 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
|
|
|
lua_pushinteger(L, lovrCanvasGetWidth(canvas));
|
|
|
|
lua_pushinteger(L, lovrCanvasGetHeight(canvas));
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasGetDepthTexture(lua_State* L) {
|
2018-08-30 00:06:20 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
2018-09-28 01:34:43 +00:00
|
|
|
Texture* texture = lovrCanvasGetDepthTexture(canvas);
|
2019-06-02 07:20:10 +00:00
|
|
|
luax_pushtype(L, Texture, texture);
|
2018-08-30 00:06:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasGetMSAA(lua_State* L) {
|
2018-08-30 00:06:20 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
|
|
|
int msaa = lovrCanvasGetMSAA(canvas);
|
|
|
|
lua_pushinteger(L, msaa);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:52:22 +00:00
|
|
|
static int l_lovrCanvasIsStereo(lua_State* L) {
|
2018-08-30 00:06:20 +00:00
|
|
|
Canvas* canvas = luax_checktype(L, 1, Canvas);
|
|
|
|
bool stereo = lovrCanvasIsStereo(canvas);
|
|
|
|
lua_pushboolean(L, stereo);
|
|
|
|
return 1;
|
2018-08-24 21:47:35 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 07:50:52 +00:00
|
|
|
const luaL_Reg lovrCanvas[] = {
|
2021-02-09 03:17:47 +00:00
|
|
|
{ "newImage", l_lovrCanvasNewImage },
|
2018-08-24 21:47:35 +00:00
|
|
|
{ "renderTo", l_lovrCanvasRenderTo },
|
2018-08-30 00:06:20 +00:00
|
|
|
{ "getTexture", l_lovrCanvasGetTexture },
|
|
|
|
{ "setTexture", l_lovrCanvasSetTexture },
|
|
|
|
{ "getWidth", l_lovrCanvasGetWidth },
|
|
|
|
{ "getHeight", l_lovrCanvasGetHeight },
|
|
|
|
{ "getDimensions", l_lovrCanvasGetDimensions },
|
2018-09-28 01:34:43 +00:00
|
|
|
{ "getDepthTexture", l_lovrCanvasGetDepthTexture },
|
2018-08-30 00:06:20 +00:00
|
|
|
{ "getMSAA", l_lovrCanvasGetMSAA },
|
|
|
|
{ "isStereo", l_lovrCanvasIsStereo },
|
2017-12-07 07:50:52 +00:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|