Replaced use of lua_opt/checkint(eger) with lua_opt/checknumber
Using lua_check/opt/tonumber() assures a non-integer number is floored when cast to an int, in contrast to lua_tointeger() which is "truncated in some non-specified way."
This commit is contained in:
@@ -35,9 +35,9 @@ static int getColorFromArgs(lua_State *L, int *rgb, const int *def) {
|
|||||||
g = def[1];
|
g = def[1];
|
||||||
b = def[2];
|
b = def[2];
|
||||||
} else {
|
} else {
|
||||||
r = luaL_checkint(L, 1);
|
r = luaL_checknumber(L, 1);
|
||||||
g = luaL_checkint(L, 2);
|
g = luaL_checknumber(L, 2);
|
||||||
b = luaL_checkint(L, 3);
|
b = luaL_checknumber(L, 3);
|
||||||
}
|
}
|
||||||
int idx = palette_colorToIdx(r, g, b);
|
int idx = palette_colorToIdx(r, g, b);
|
||||||
if (idx < 0) {
|
if (idx < 0) {
|
||||||
@@ -258,11 +258,11 @@ int l_graphics_draw(lua_State *L) {
|
|||||||
int x, y;
|
int x, y;
|
||||||
if (!lua_isnone(L, 2) && lua_type(L, 2) != LUA_TNUMBER) {
|
if (!lua_isnone(L, 2) && lua_type(L, 2) != LUA_TNUMBER) {
|
||||||
quad = luaobj_checkudata(L, 2, LUAOBJ_TYPE_QUAD);
|
quad = luaobj_checkudata(L, 2, LUAOBJ_TYPE_QUAD);
|
||||||
x = luaL_optint(L, 3, 0);
|
x = luaL_optnumber(L, 3, 0);
|
||||||
y = luaL_optint(L, 4, 0);
|
y = luaL_optnumber(L, 4, 0);
|
||||||
} else {
|
} else {
|
||||||
x = luaL_optint(L, 2, 0);
|
x = luaL_optnumber(L, 2, 0);
|
||||||
y = luaL_optint(L, 3, 0);
|
y = luaL_optnumber(L, 3, 0);
|
||||||
}
|
}
|
||||||
pixel_t *buf = graphics_canvas->data;
|
pixel_t *buf = graphics_canvas->data;
|
||||||
int bufw = graphics_canvas->width;
|
int bufw = graphics_canvas->width;
|
||||||
@@ -279,8 +279,8 @@ int l_graphics_draw(lua_State *L) {
|
|||||||
|
|
||||||
|
|
||||||
int l_graphics_point(lua_State *L) {
|
int l_graphics_point(lua_State *L) {
|
||||||
int x = luaL_checkint(L, 1);
|
int x = luaL_checknumber(L, 1);
|
||||||
int y = luaL_checkint(L, 2);
|
int y = luaL_checknumber(L, 2);
|
||||||
image_setPixel(graphics_canvas, x, y, graphics_color);
|
image_setPixel(graphics_canvas, x, y, graphics_color);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -288,14 +288,14 @@ int l_graphics_point(lua_State *L) {
|
|||||||
|
|
||||||
int l_graphics_line(lua_State *L) {
|
int l_graphics_line(lua_State *L) {
|
||||||
int argc = lua_gettop(L);
|
int argc = lua_gettop(L);
|
||||||
int lastx = luaL_checkint(L, 1);
|
int lastx = luaL_checknumber(L, 1);
|
||||||
int lasty = luaL_checkint(L, 2);
|
int lasty = luaL_checknumber(L, 2);
|
||||||
int idx = 3;
|
int idx = 3;
|
||||||
while (idx < argc) {
|
while (idx < argc) {
|
||||||
int x0 = lastx;
|
int x0 = lastx;
|
||||||
int y0 = lasty;
|
int y0 = lasty;
|
||||||
int x1 = luaL_checkint(L, idx);
|
int x1 = luaL_checknumber(L, idx);
|
||||||
int y1 = luaL_checkint(L, idx + 1);
|
int y1 = luaL_checknumber(L, idx + 1);
|
||||||
lastx = x1;
|
lastx = x1;
|
||||||
lasty = y1;
|
lasty = y1;
|
||||||
/* Draw line */
|
/* Draw line */
|
||||||
@@ -335,10 +335,10 @@ int l_graphics_line(lua_State *L) {
|
|||||||
|
|
||||||
int l_graphics_rectangle(lua_State *L) {
|
int l_graphics_rectangle(lua_State *L) {
|
||||||
const char *mode = luaL_checkstring(L, 1);
|
const char *mode = luaL_checkstring(L, 1);
|
||||||
int x = luaL_checkint(L, 2);
|
int x = luaL_checknumber(L, 2);
|
||||||
int y = luaL_checkint(L, 3);
|
int y = luaL_checknumber(L, 3);
|
||||||
int x2 = luaL_checkint(L, 4) + x;
|
int x2 = luaL_checknumber(L, 4) + x;
|
||||||
int y2 = luaL_checkint(L, 5) + y;
|
int y2 = luaL_checknumber(L, 5) + y;
|
||||||
int fill = 0;
|
int fill = 0;
|
||||||
if (!strcmp(mode, "fill")) {
|
if (!strcmp(mode, "fill")) {
|
||||||
fill = 1;
|
fill = 1;
|
||||||
@@ -381,9 +381,9 @@ int l_graphics_rectangle(lua_State *L) {
|
|||||||
|
|
||||||
int l_graphics_circle(lua_State *L) {
|
int l_graphics_circle(lua_State *L) {
|
||||||
const char *mode = luaL_checkstring(L, 1);
|
const char *mode = luaL_checkstring(L, 1);
|
||||||
int x = luaL_checkint(L, 2);
|
int x = luaL_checknumber(L, 2);
|
||||||
int y = luaL_checkint(L, 3);
|
int y = luaL_checknumber(L, 3);
|
||||||
int radius = luaL_checkint(L, 4);
|
int radius = luaL_checknumber(L, 4);
|
||||||
int fill = 0;
|
int fill = 0;
|
||||||
if (!strcmp(mode, "fill")) {
|
if (!strcmp(mode, "fill")) {
|
||||||
fill = 1;
|
fill = 1;
|
||||||
@@ -454,8 +454,8 @@ int l_graphics_circle(lua_State *L) {
|
|||||||
int l_graphics_print(lua_State *L) {
|
int l_graphics_print(lua_State *L) {
|
||||||
luaL_checkany(L, 1);
|
luaL_checkany(L, 1);
|
||||||
const char *str = luaL_tolstring(L, 1, NULL);
|
const char *str = luaL_tolstring(L, 1, NULL);
|
||||||
int x = luaL_checkint(L, 2);
|
int x = luaL_checknumber(L, 2);
|
||||||
int y = luaL_checkint(L, 3);
|
int y = luaL_checknumber(L, 3);
|
||||||
font_blit(graphics_font, graphics_canvas->data, graphics_canvas->width,
|
font_blit(graphics_font, graphics_canvas->data, graphics_canvas->width,
|
||||||
graphics_canvas->height, str, x, y);
|
graphics_canvas->height, str, x, y);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
18
src/image.c
18
src/image.c
@@ -253,8 +253,8 @@ int l_image_newCanvas(lua_State *L) {
|
|||||||
int width = VGA_WIDTH;
|
int width = VGA_WIDTH;
|
||||||
int height = VGA_HEIGHT;
|
int height = VGA_HEIGHT;
|
||||||
if (lua_gettop(L) > 0) {
|
if (lua_gettop(L) > 0) {
|
||||||
width = luaL_checkint(L, 1);
|
width = luaL_checknumber(L, 1);
|
||||||
height = luaL_checkint(L, 2);
|
height = luaL_checknumber(L, 2);
|
||||||
if (width <= 0) luaL_argerror(L, 1, "width must be larger than 0");
|
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");
|
if (height <= 0) luaL_argerror(L, 2, "height must be larger than 0");
|
||||||
}
|
}
|
||||||
@@ -296,8 +296,8 @@ int l_image_getHeight(lua_State *L) {
|
|||||||
|
|
||||||
int l_image_getPixel(lua_State *L) {
|
int l_image_getPixel(lua_State *L) {
|
||||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||||
int x = luaL_checkint(L, 2);
|
int x = luaL_checknumber(L, 2);
|
||||||
int y = luaL_checkint(L, 3);
|
int y = luaL_checknumber(L, 3);
|
||||||
if (x < 0 || x >= self->width || y < 0 || y >= self->height) {
|
if (x < 0 || x >= self->width || y < 0 || y >= self->height) {
|
||||||
/* Return `nil` for out of bounds (same as transparent) */
|
/* Return `nil` for out of bounds (same as transparent) */
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
@@ -322,17 +322,17 @@ int l_image_getPixel(lua_State *L) {
|
|||||||
|
|
||||||
int l_image_setPixel(lua_State *L) {
|
int l_image_setPixel(lua_State *L) {
|
||||||
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
image_t *self = luaobj_checkudata(L, 1, CLASS_TYPE);
|
||||||
int x = luaL_checkint(L, 2);
|
int x = luaL_checknumber(L, 2);
|
||||||
int y = luaL_checkint(L, 3);
|
int y = luaL_checknumber(L, 3);
|
||||||
if (lua_isnoneornil(L, 4)) {
|
if (lua_isnoneornil(L, 4)) {
|
||||||
/* Set transparent */
|
/* Set transparent */
|
||||||
image_setPixel(self, x, y, 0);
|
image_setPixel(self, x, y, 0);
|
||||||
image_setMaskPixel(self, x, y, 0xff);
|
image_setMaskPixel(self, x, y, 0xff);
|
||||||
} else {
|
} else {
|
||||||
/* Get color, set pixel and mask */
|
/* Get color, set pixel and mask */
|
||||||
int r = luaL_checkint(L, 4);
|
int r = luaL_checknumber(L, 4);
|
||||||
int g = luaL_checkint(L, 5);
|
int g = luaL_checknumber(L, 5);
|
||||||
int b = luaL_checkint(L, 6);
|
int b = luaL_checknumber(L, 6);
|
||||||
int idx = palette_colorToIdx(r, g, b);
|
int idx = palette_colorToIdx(r, g, b);
|
||||||
if (idx < -1) {
|
if (idx < -1) {
|
||||||
luaL_error(L, "color palette exhausted: use fewer unique colors");
|
luaL_error(L, "color palette exhausted: use fewer unique colors");
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ int l_mouse_isDown(lua_State *L) {
|
|||||||
int res = 0;
|
int res = 0;
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i <= n; i++) {
|
for (i = 1; i <= n; i++) {
|
||||||
int idx = luaL_checkinteger(L, i) - 1;
|
int idx = luaL_checknumber(L, i) - 1;
|
||||||
if (idx >= 0 && idx < MOUSE_BUTTON_MAX) {
|
if (idx >= 0 && idx < MOUSE_BUTTON_MAX) {
|
||||||
res |= mouse_buttonsDown[idx];
|
res |= mouse_buttonsDown[idx];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user