From 4798e70dcbbedb55de8e9f8e321e8fad7b9783f6 Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 8 Sep 2018 13:09:06 +0800 Subject: *update --- src/lua/modules/graphics/graphics.cpp | 38 ++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'src/lua/modules/graphics/graphics.cpp') 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, JIN_GRAPHICS_BITMAP)); return 1; + fail: + luax_pushnil(L); + return 1; } /* jin.graphics.newTexture(bitmap) */ @@ -126,16 +138,22 @@ namespace lua Ref& refBitmap = p->getRef(); Bitmap* bitmap = refBitmap.getObject(); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy)); - Texture* img = Texture::createTexture(bitmap); - proxy->bind(new Ref(img, JIN_GRAPHICS_TEXTURE)); + Texture* tex = Texture::createTexture(bitmap); + proxy->bind(new Ref(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(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); -- cgit v1.1-26-g67d0