aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/modules/graphics')
-rw-r--r--src/lua/modules/graphics/graphics.cpp38
1 files changed, 28 insertions, 10 deletions
diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp
index 8a58ff0..ada4754 100644
--- a/src/lua/modules/graphics/graphics.cpp
+++ b/src/lua/modules/graphics/graphics.cpp
@@ -107,16 +107,28 @@ namespace lua
Filesystem* fs = Filesystem::get();
if (!fs->exists(f))
{
- printf("Error: no such texture %s\n", f);
- exit(1);
+ error(L, "No such image file %s", f);
+ goto fail;
}
Buffer b;
- fs->read(f, &b);
+ if (!fs->read(f, &b))
+ {
+ error(L, "Failed to read image %s", f);
+ goto fail;
+ }
bitmap = Bitmap::createBitmap(b.data, b.size);
+ if (bitmap == nullptr)
+ {
+ error(L, "Failed to decode image file %s", f);
+ goto fail;
+ }
}
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy));
proxy->bind(new Ref<Bitmap>(bitmap, JIN_GRAPHICS_BITMAP));
return 1;
+ fail:
+ luax_pushnil(L);
+ return 1;
}
/* jin.graphics.newTexture(bitmap) */
@@ -126,16 +138,22 @@ namespace lua
Ref<Bitmap>& refBitmap = p->getRef<Bitmap>();
Bitmap* bitmap = refBitmap.getObject();
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy));
- Texture* img = Texture::createTexture(bitmap);
- proxy->bind(new Ref<Texture>(img, JIN_GRAPHICS_TEXTURE));
+ Texture* tex = Texture::createTexture(bitmap);
+ proxy->bind(new Ref<Texture>(tex, JIN_GRAPHICS_TEXTURE));
return 1;
}
static int l_newShader(lua_State* L)
{
- Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy));
const char* program = luax_checkstring(L, 1);
JSLProgram* jsl = JSLProgram::createJSLProgram(program);
+ if (jsl == nullptr)
+ {
+ error(L, "Failed to compile shader");
+ luax_pushnil(L);
+ return 1;
+ }
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy));
proxy->bind(new Ref<JSLProgram>(jsl, JIN_GRAPHICS_SHADER));
return 1;
}
@@ -420,11 +438,10 @@ namespace lua
luax_error(L, emsg, n * 2, tn);
return 1;
}
- float* p = new float[2 * n];
+ float* p = (float*)alloca(2 * n * sizeof(float));
for (int i = 1; i <= 2 * n; ++i)
p[i - 1] = luax_rawgetnumber(L, 3, i);
polygon(mode, p, n);
- delete[] p;
}
else
{
@@ -445,8 +462,9 @@ namespace lua
Buffer b = {};
if (!fs->exists(path))
{
- printf("Error: no such font %s\n", path);
- exit(1);
+ error(L, "No such font %s\n", path);
+ luax_pushnil(L);
+ return 1;
}
fs->read(path, &b);
font->loadMemory((const unsigned char*)b.data);