Began separating lua bindings, moved to src/modules dir
This commit is contained in:
108
src/filesystem.c
108
src/filesystem.c
@@ -11,7 +11,6 @@
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "luaobj.h"
|
||||
#include "lib/microtar/microtar.h"
|
||||
#include "lib/dmt/dmt.h"
|
||||
|
||||
@@ -573,110 +572,3 @@ int filesystem_write(const char *filename, const void *data, int size) {
|
||||
fclose(fp);
|
||||
return n == size ? FILESYSTEM_ESUCCESS : FILESYSTEM_EWRITEFAIL;
|
||||
}
|
||||
|
||||
|
||||
/*==================*/
|
||||
/* Lua Binds */
|
||||
/*==================*/
|
||||
|
||||
int l_filesystem_mount(lua_State *L) {
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
int err = filesystem_mount(path);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, filesystem_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_unmount(lua_State *L) {
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
int err = filesystem_unmount(path);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, filesystem_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_exists(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
lua_pushboolean( L, filesystem_exists(filename) );
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_isFile(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
lua_pushboolean( L, filesystem_isFile(filename) );
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_isDirectory(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
lua_pushboolean( L, filesystem_isDirectory(filename) );
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_read(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
int size;
|
||||
void *data = filesystem_read(filename, &size);
|
||||
if (!data) {
|
||||
luaL_error(L, "could not read file");
|
||||
}
|
||||
lua_pushlstring(L, data, size);
|
||||
filesystem_free(data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_setWriteDir(lua_State *L) {
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
int err = filesystem_setWriteDir(path);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, filesystem_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_write(lua_State *L) {
|
||||
size_t sz;
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
const char *data = luaL_tolstring(L, 2, &sz);
|
||||
int err = filesystem_write(filename, data, sz);
|
||||
if (err) {
|
||||
luaL_error(L, "%s", filesystem_strerror(err));
|
||||
}
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int luaopen_filesystem(lua_State *L) {
|
||||
luaL_Reg reg[] = {
|
||||
{ "mount", l_filesystem_mount },
|
||||
{ "unmount", l_filesystem_unmount },
|
||||
{ "exists", l_filesystem_exists },
|
||||
{ "isFile", l_filesystem_isFile },
|
||||
{ "isDirectory", l_filesystem_isDirectory },
|
||||
{ "read", l_filesystem_read },
|
||||
{ "setWriteDir", l_filesystem_setWriteDir },
|
||||
{ "write", l_filesystem_write },
|
||||
{ 0, 0 },
|
||||
};
|
||||
luaL_newlib(L, reg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
68
src/font.c
68
src/font.c
@@ -13,7 +13,6 @@
|
||||
#include "lib/stb/stb_truetype.h"
|
||||
#include "filesystem.h"
|
||||
#include "font.h"
|
||||
#include "luaobj.h"
|
||||
|
||||
|
||||
static const char *initFont(font_t *self, const void *data, int ptsize) {
|
||||
@@ -144,70 +143,3 @@ void font_blit(font_t *self, pixel_t *buf, int bufw, int bufh,
|
||||
image_blendMode = oldBlendMode;
|
||||
image_flip = oldFlip;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define CLASS_TYPE LUAOBJ_TYPE_FONT
|
||||
#define CLASS_NAME "Font"
|
||||
|
||||
|
||||
int l_font_new(lua_State *L) {
|
||||
const char *filename;
|
||||
int ptsize = 12;
|
||||
if ( lua_isnoneornil(L, 2) ) {
|
||||
filename = NULL;
|
||||
ptsize = luaL_optnumber(L, 1, ptsize);
|
||||
} else {
|
||||
filename = luaL_checkstring(L, 1);
|
||||
ptsize = luaL_optnumber(L, 2, ptsize);
|
||||
}
|
||||
font_t *self = luaobj_newudata(L, sizeof(*self));
|
||||
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
|
||||
if (filename) {
|
||||
const char *err = font_init(self, filename, ptsize);
|
||||
if (err) luaL_error(L, err);
|
||||
} else {
|
||||
font_initEmbedded(self, ptsize);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_font_gc(lua_State *L) {
|
||||
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
font_deinit(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int l_font_getWidth(lua_State *L) {
|
||||
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
const char *p = luaL_checkstring(L, 2);
|
||||
int width = 0;
|
||||
while (*p) {
|
||||
width += self->glyphs[(int) (*p++ & 127)].xadvance;
|
||||
}
|
||||
lua_pushinteger(L, width);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_font_getHeight(lua_State *L) {
|
||||
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
lua_pushinteger(L, self->height);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int luaopen_font(lua_State *L) {
|
||||
luaL_Reg reg[] = {
|
||||
{ "new", l_font_new },
|
||||
{ "__gc", l_font_gc },
|
||||
{ "getWidth", l_font_getWidth },
|
||||
{ "getHeight", l_font_getHeight },
|
||||
{ 0, 0 },
|
||||
};
|
||||
luaobj_newclass(L, CLASS_NAME, NULL, l_font_new, reg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
127
src/image.c
127
src/image.c
@@ -14,7 +14,6 @@
|
||||
#include "filesystem.h"
|
||||
#include "image.h"
|
||||
#include "palette.h"
|
||||
#include "luaobj.h"
|
||||
|
||||
int image_blendMode = IMAGE_NORMAL;
|
||||
int image_flip = 0;
|
||||
@@ -232,129 +231,3 @@ void image_deinit(image_t *self) {
|
||||
dmt_free(self->data);
|
||||
dmt_free(self->mask);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define CLASS_TYPE LUAOBJ_TYPE_IMAGE
|
||||
#define CLASS_NAME "Image"
|
||||
|
||||
|
||||
int l_image_new(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
image_t *self = luaobj_newudata(L, sizeof(*self));
|
||||
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
|
||||
const char *err = image_init(self, filename);
|
||||
if (err) luaL_error(L, err);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_image_newCanvas(lua_State *L) {
|
||||
int width = VGA_WIDTH;
|
||||
int height = VGA_HEIGHT;
|
||||
if (lua_gettop(L) > 0) {
|
||||
width = luaL_checknumber(L, 1);
|
||||
height = luaL_checknumber(L, 2);
|
||||
if (width <= 0) luaL_argerror(L, 1, "width must be larger than 0");
|
||||
if (height <= 0) luaL_argerror(L, 2, "height must be larger than 0");
|
||||
}
|
||||
image_t *self = luaobj_newudata(L, sizeof(*self));
|
||||
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
|
||||
image_initBlank(self, width, height);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_image_gc(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
image_deinit(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int l_image_getDimensions(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
lua_pushinteger(L, self->width);
|
||||
lua_pushinteger(L, self->height);
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
int l_image_getWidth(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
lua_pushinteger(L, self->width);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_image_getHeight(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
lua_pushinteger(L, self->height);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_image_getPixel(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
int x = luaL_checknumber(L, 2);
|
||||
int y = luaL_checknumber(L, 3);
|
||||
if (x < 0 || x >= self->width || y < 0 || y >= self->height) {
|
||||
/* Return `nil` for out of bounds (same as transparent) */
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
} else {
|
||||
/* Return `nil` if color is transparent, else return 3 channel values */
|
||||
int idx = self->data[x + y * self->width];
|
||||
if (idx == 0) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
} else {
|
||||
int rgb[3];
|
||||
palette_idxToColor(idx, rgb);
|
||||
lua_pushinteger(L, rgb[0]);
|
||||
lua_pushinteger(L, rgb[1]);
|
||||
lua_pushinteger(L, rgb[2]);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int l_image_setPixel(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
int x = luaL_checknumber(L, 2);
|
||||
int y = luaL_checknumber(L, 3);
|
||||
if (lua_isnoneornil(L, 4)) {
|
||||
/* Set transparent */
|
||||
image_setPixel(self, x, y, 0);
|
||||
image_setMaskPixel(self, x, y, 0xff);
|
||||
} else {
|
||||
/* Get color, set pixel and mask */
|
||||
int r = luaL_checknumber(L, 4);
|
||||
int g = luaL_checknumber(L, 5);
|
||||
int b = luaL_checknumber(L, 6);
|
||||
int idx = palette_colorToIdx(r, g, b);
|
||||
if (idx < -1) {
|
||||
luaL_error(L, "color palette exhausted: use fewer unique colors");
|
||||
}
|
||||
image_setPixel(self, x, y, idx);
|
||||
image_setMaskPixel(self, x, y, 0x0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int luaopen_image(lua_State *L) {
|
||||
luaL_Reg reg[] = {
|
||||
{ "new", l_image_new },
|
||||
{ "__gc", l_image_gc },
|
||||
{ "getDimensions", l_image_getDimensions },
|
||||
{ "getWidth", l_image_getWidth },
|
||||
{ "getHeight", l_image_getHeight },
|
||||
{ "getPixel", l_image_getPixel },
|
||||
{ "setPixel", l_image_setPixel },
|
||||
{ 0, 0 },
|
||||
};
|
||||
luaobj_newclass(L, CLASS_NAME, NULL, l_image_new, reg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
112
src/modules/l_filesystem.c
Normal file
112
src/modules/l_filesystem.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* Copyright (c) 2016 rxi
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the MIT license. See LICENSE for details.
|
||||
*/
|
||||
|
||||
#include "filesystem.h"
|
||||
#include "luaobj.h"
|
||||
|
||||
|
||||
int l_filesystem_mount(lua_State *L) {
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
int err = filesystem_mount(path);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, filesystem_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_unmount(lua_State *L) {
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
int err = filesystem_unmount(path);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, filesystem_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_exists(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
lua_pushboolean( L, filesystem_exists(filename) );
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_isFile(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
lua_pushboolean( L, filesystem_isFile(filename) );
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_isDirectory(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
lua_pushboolean( L, filesystem_isDirectory(filename) );
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_read(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
int size;
|
||||
void *data = filesystem_read(filename, &size);
|
||||
if (!data) {
|
||||
luaL_error(L, "could not read file");
|
||||
}
|
||||
lua_pushlstring(L, data, size);
|
||||
filesystem_free(data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_setWriteDir(lua_State *L) {
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
int err = filesystem_setWriteDir(path);
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, filesystem_strerror(err));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_filesystem_write(lua_State *L) {
|
||||
size_t sz;
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
const char *data = luaL_tolstring(L, 2, &sz);
|
||||
int err = filesystem_write(filename, data, sz);
|
||||
if (err) {
|
||||
luaL_error(L, "%s", filesystem_strerror(err));
|
||||
}
|
||||
lua_pushboolean(L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int luaopen_filesystem(lua_State *L) {
|
||||
luaL_Reg reg[] = {
|
||||
{ "mount", l_filesystem_mount },
|
||||
{ "unmount", l_filesystem_unmount },
|
||||
{ "exists", l_filesystem_exists },
|
||||
{ "isFile", l_filesystem_isFile },
|
||||
{ "isDirectory", l_filesystem_isDirectory },
|
||||
{ "read", l_filesystem_read },
|
||||
{ "setWriteDir", l_filesystem_setWriteDir },
|
||||
{ "write", l_filesystem_write },
|
||||
{ 0, 0 },
|
||||
};
|
||||
luaL_newlib(L, reg);
|
||||
return 1;
|
||||
}
|
||||
74
src/modules/l_font.c
Normal file
74
src/modules/l_font.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Copyright (c) 2016 rxi
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the MIT license. See LICENSE for details.
|
||||
*/
|
||||
|
||||
#include "font.h"
|
||||
#include "luaobj.h"
|
||||
|
||||
#define CLASS_TYPE LUAOBJ_TYPE_FONT
|
||||
#define CLASS_NAME "Font"
|
||||
|
||||
|
||||
int l_font_new(lua_State *L) {
|
||||
const char *filename;
|
||||
int ptsize = 12;
|
||||
if ( lua_isnoneornil(L, 2) ) {
|
||||
filename = NULL;
|
||||
ptsize = luaL_optnumber(L, 1, ptsize);
|
||||
} else {
|
||||
filename = luaL_checkstring(L, 1);
|
||||
ptsize = luaL_optnumber(L, 2, ptsize);
|
||||
}
|
||||
font_t *self = luaobj_newudata(L, sizeof(*self));
|
||||
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
|
||||
if (filename) {
|
||||
const char *err = font_init(self, filename, ptsize);
|
||||
if (err) luaL_error(L, err);
|
||||
} else {
|
||||
font_initEmbedded(self, ptsize);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_font_gc(lua_State *L) {
|
||||
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
font_deinit(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int l_font_getWidth(lua_State *L) {
|
||||
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
const char *p = luaL_checkstring(L, 2);
|
||||
int width = 0;
|
||||
while (*p) {
|
||||
width += self->glyphs[(int) (*p++ & 127)].xadvance;
|
||||
}
|
||||
lua_pushinteger(L, width);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_font_getHeight(lua_State *L) {
|
||||
font_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
lua_pushinteger(L, self->height);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int luaopen_font(lua_State *L) {
|
||||
luaL_Reg reg[] = {
|
||||
{ "new", l_font_new },
|
||||
{ "__gc", l_font_gc },
|
||||
{ "getWidth", l_font_getWidth },
|
||||
{ "getHeight", l_font_getHeight },
|
||||
{ 0, 0 },
|
||||
};
|
||||
luaobj_newclass(L, CLASS_NAME, NULL, l_font_new, reg);
|
||||
return 1;
|
||||
}
|
||||
135
src/modules/l_image.c
Normal file
135
src/modules/l_image.c
Normal file
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* Copyright (c) 2016 rxi
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the MIT license. See LICENSE for details.
|
||||
*/
|
||||
|
||||
#include "luaobj.h"
|
||||
#include "palette.h"
|
||||
#include "image.h"
|
||||
|
||||
|
||||
#define CLASS_TYPE LUAOBJ_TYPE_IMAGE
|
||||
#define CLASS_NAME "Image"
|
||||
|
||||
|
||||
int l_image_new(lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
image_t *self = luaobj_newudata(L, sizeof(*self));
|
||||
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
|
||||
const char *err = image_init(self, filename);
|
||||
if (err) luaL_error(L, err);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_image_newCanvas(lua_State *L) {
|
||||
int width = VGA_WIDTH;
|
||||
int height = VGA_HEIGHT;
|
||||
if (lua_gettop(L) > 0) {
|
||||
width = luaL_checknumber(L, 1);
|
||||
height = luaL_checknumber(L, 2);
|
||||
if (width <= 0) luaL_argerror(L, 1, "width must be larger than 0");
|
||||
if (height <= 0) luaL_argerror(L, 2, "height must be larger than 0");
|
||||
}
|
||||
image_t *self = luaobj_newudata(L, sizeof(*self));
|
||||
luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
|
||||
image_initBlank(self, width, height);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_image_gc(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
image_deinit(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int l_image_getDimensions(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
lua_pushinteger(L, self->width);
|
||||
lua_pushinteger(L, self->height);
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
int l_image_getWidth(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
lua_pushinteger(L, self->width);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_image_getHeight(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
lua_pushinteger(L, self->height);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int l_image_getPixel(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
int x = luaL_checknumber(L, 2);
|
||||
int y = luaL_checknumber(L, 3);
|
||||
if (x < 0 || x >= self->width || y < 0 || y >= self->height) {
|
||||
/* Return `nil` for out of bounds (same as transparent) */
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
} else {
|
||||
/* Return `nil` if color is transparent, else return 3 channel values */
|
||||
int idx = self->data[x + y * self->width];
|
||||
if (idx == 0) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
} else {
|
||||
int rgb[3];
|
||||
palette_idxToColor(idx, rgb);
|
||||
lua_pushinteger(L, rgb[0]);
|
||||
lua_pushinteger(L, rgb[1]);
|
||||
lua_pushinteger(L, rgb[2]);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int l_image_setPixel(lua_State *L) {
|
||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||
int x = luaL_checknumber(L, 2);
|
||||
int y = luaL_checknumber(L, 3);
|
||||
if (lua_isnoneornil(L, 4)) {
|
||||
/* Set transparent */
|
||||
image_setPixel(self, x, y, 0);
|
||||
image_setMaskPixel(self, x, y, 0xff);
|
||||
} else {
|
||||
/* Get color, set pixel and mask */
|
||||
int r = luaL_checknumber(L, 4);
|
||||
int g = luaL_checknumber(L, 5);
|
||||
int b = luaL_checknumber(L, 6);
|
||||
int idx = palette_colorToIdx(r, g, b);
|
||||
if (idx < -1) {
|
||||
luaL_error(L, "color palette exhausted: use fewer unique colors");
|
||||
}
|
||||
image_setPixel(self, x, y, idx);
|
||||
image_setMaskPixel(self, x, y, 0x0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int luaopen_image(lua_State *L) {
|
||||
luaL_Reg reg[] = {
|
||||
{ "new", l_image_new },
|
||||
{ "__gc", l_image_gc },
|
||||
{ "getDimensions", l_image_getDimensions },
|
||||
{ "getWidth", l_image_getWidth },
|
||||
{ "getHeight", l_image_getHeight },
|
||||
{ "getPixel", l_image_getPixel },
|
||||
{ "setPixel", l_image_setPixel },
|
||||
{ 0, 0 },
|
||||
};
|
||||
luaobj_newclass(L, CLASS_NAME, NULL, l_image_new, reg);
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user