shithub: slug

Download patch

ref: f0e667390f5f9e9e1cf366e8db032f1f9718b9d2
parent: e76d34a16eec236618daed706029093ce2a91440
author: phil9 <[email protected]>
date: Thu Dec 1 01:13:35 EST 2022

expose mouseX and mouseY variables

	these variables are exposed to lua and can be used to draw based on the mouse position:
		line(0, 0, mouseX, mouseY)

--- a/a.h
+++ b/a.h
@@ -8,10 +8,11 @@
 #include <lauxlib.h>
 #include <lualib.h>
 
+void	lset(lua_State*, const char*, int);
 void	resize(lua_State*, int, int);
 
 void	drawcanvas(void);
-void	initstate(void);
+void	initstate(lua_State*);
 void	registerfuncs(lua_State*);
 
 Image*	color(int, int, int);
@@ -21,3 +22,4 @@
 extern Image* canvas;
 extern int width;
 extern int height;
+extern Point origin;
--- a/api.c
+++ b/api.c
@@ -11,7 +11,7 @@
 Point origin;
 
 void
-initstate(void)
+initstate(lua_State *L)
 {
 	Rectangle r;
 
@@ -25,6 +25,8 @@
 	nofill = 0;
 	fill = display->white;
 	origin = ZP;
+	lset(L, "mouseX", 0);
+	lset(L, "mouseY", 0);
 }
 
 void
--- a/slug.c
+++ b/slug.c
@@ -83,6 +83,17 @@
 }
 
 void
+emouse(lua_State *L, Mouse m)
+{
+	Point xy;
+
+	xy = subpt(m.xy, screen->r.min);
+	xy = subpt(xy, origin);
+	lset(L, "mouseX", xy.x);
+	lset(L, "mouseY", xy.y);
+}
+
+void
 threadmain(int argc, char *argv[])
 {
 	lua_State *L;
@@ -107,7 +118,7 @@
 	alts[2].c = kc->c;
 	L = linit(argc, argv);
 	registerfuncs(L);
-	initstate();
+	initstate(L);
 	resize(L, width, height);
 	drawing = 0;
 	lcall(L, "setup");
@@ -116,6 +127,7 @@
 		lcall(L, "draw");
 		switch(alt(alts)){
 		case 0:
+			emouse(L, m);
 			break;
 		case 1:
 			if(getwindow(display, Refnone)<0)