lovr/src/api/l_graphics_canvas.c

150 lines
4.4 KiB
C
Raw Normal View History

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>
#include <stdlib.h>
2017-12-07 07:50:52 +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);
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;
}
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);
}
}
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);
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++) {
luax_pushtype(L, Texture, attachments[i].texture);
2018-08-24 01:28:37 +00:00
}
return count;
}
2019-02-17 22:52:22 +00:00
static int l_lovrCanvasSetTexture(lua_State* L) {
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);
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);
Texture* texture = lovrCanvasGetDepthTexture(canvas);
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[] = {
{ "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 },
{ "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 }
};