shithub: lua9

Download patch

ref: f00064c3e10452efe86d9a646c54b074d6da54b0
parent: 9cd8f1f0e8a962efd05a1bfc7f8b4068bcb8d526
author: telephil9 <[email protected]>
date: Thu Oct 22 00:36:11 EDT 2020

Added allocimage() function

It is now possible to use colors. Sample modified to illustrate this.

--- a/lua9.c
+++ b/lua9.c
@@ -32,6 +32,16 @@
 static lua_State *state;
 static int ridx, tidx;
 
+static ImagePtr* to_image(lua_State *L, Image *img) {
+	ImagePtr *i;
+
+	i = (ImagePtr*)lua_newuserdata(L, sizeof(ImagePtr));
+	luaL_getmetatable(L, IMAGE_META);
+	lua_setmetatable(L, -2);
+	i->p = img;
+	return i;
+}
+
 void eresized(int new) {
 	if(new && getwindow(display, Refnone) < 0){
 		fprintf(stderr, "cannot reattach to window: %r");
@@ -196,6 +206,15 @@
 	return p->p;
 }
 
+static Display* l_checkdisplay(lua_State *L, int index)
+{
+	DisplayPtr *p;
+
+	p = (DisplayPtr*)luaL_checkudata(L, index, DISPLAY_META);
+	luaL_argcheck(L, p!=NULL, index, "draw: Display expected");
+	return p->p;
+}
+
 static int l_draw(lua_State *L) {
 	Image *dst, *src, *mask;
 	Point p;
@@ -276,6 +295,24 @@
 	return 0;
 }
 
+static int l_allocimage(lua_State *L)
+{
+	Display *d;
+	Rectangle r;
+	ulong chan, col;
+	int repl;
+	Image *i;
+
+	d    = l_checkdisplay(L, 1);
+	r    = l_checkrect(L, 2);
+	chan = (ulong)luaL_checkinteger(L, 3);
+	repl = luaL_checkinteger(L, 4);
+	col  = (ulong)luaL_checkinteger(L, 5);
+	i    = allocimage(d, r, chan, repl, col);
+	to_image(L, i);
+	return 1;
+}
+
 /* Image metatable */
 static int l_image_gc(lua_State *L) {
 	ImagePtr *i;
@@ -363,16 +400,6 @@
 	return 1;
 }
 
-static ImagePtr* to_image(lua_State *L, Image *img) {
-	ImagePtr *i;
-
-	i = (ImagePtr*)lua_newuserdata(L, sizeof(ImagePtr));
-	luaL_getmetatable(L, IMAGE_META);
-	lua_setmetatable(L, -2);
-	i->p = img;
-	return i;
-}
-
 static int l_display_index(lua_State *L) {
 	DisplayPtr *d;
 	const char *s;
@@ -435,6 +462,7 @@
 	{ "ellipse",     l_ellipse },
 	{ "fillellipse", l_fillellipse },
 	{ "string",      l_string },
+	{ "allocimage",  l_allocimage },
 	{ NULL, NULL }
 };
 
--- a/sample.lua
+++ b/sample.lua
@@ -11,6 +11,7 @@
 end
 
 local ZP = pt(0, 0)
+local red = nil
 
 function eresized()
 	draw.draw(screen, screen.r, display.white, nil, ZP)
@@ -18,11 +19,12 @@
 	draw.string(screen, pt(110, 160), display.black, nil, font, 'Hello LUA')
 	draw.line(screen, pt(50, 210), pt(150, 210), draw.Endsquare, draw.Endarrow, 1, display.black, ZP)
 	draw.ellipse(screen, pt(300, 300), 200, 100, 2, display.black, ZP)
-	draw.fillellipse(screen, pt(300, 300), 20, 50, display.black, ZP)
+	draw.fillellipse(screen, pt(300, 300), 20, 50, red, ZP)
 end
 
 draw.initdraw('lua sample')
 draw.einit(draw.Emouse|draw.Ekeyboard)
+red = draw.allocimage(display, rect(0,0,1,1), screen.chan, 1, 0xFF0000FF)
 eresized()
 while true do
 	local e, ev = draw.event()