Added more functions

This commit is contained in:
Casey 2024-04-09 19:53:35 +03:00
parent 12c80a3eab
commit 0d7dd39b77
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
2 changed files with 93 additions and 12 deletions

View File

@ -4,17 +4,19 @@
function tick(t) function tick(t)
-- Background -- Background
Draw.clear(math.floor(t * 255)) Draw.clear(0xFF131300 | math.floor(128 + math.sin(t) * 127))
--[[
-- Color ARGB, x, y -- Color ARGB, x, y
Draw.pixel(0xFFFF0000, 30, 30) Draw.pixel(0xFFFF0000,
(math.sin(t) * 0.5 + 0.5) * 1920,
(math.cos(t) * 0.5 + 0.5) * 1080)
-- Color ARGB, thickness, x1, y1, x2, y2, xn, yn, ... -- Color ARGB, thickness, x1, y1, x2, y2, xn, yn, ...
Draw.line(0xFFFF00FF, 8, 100, 100, 200, 400, 300, 600) --> { Vec2, ... } Draw.line(0xFFFF00FF, 8, 100, 100, 200, 400, 300, 600) --> { Vec2, ... }
-- Color ARGB, thickness, x1, y1, x2, y2, xn, yn, ... -- Color ARGB, x1, y1, x2, y2, xn, yn, ...
Draw.poly(0xFFFF00FF, 8, 100, 100, 200, 400, 300, 600) --> Rect Draw.poly(0xFFFF00FF, 100, 100, 200, 400, 150, 500) --> Rect
--[[
-- Color ARGB, radius, x, y, start, end -- Color ARGB, radius, x, y, start, end
Draw.circle(0xFFFF0000, 20, 300, 300) --> Rect Draw.circle(0xFFFF0000, 20, 300, 300) --> Rect
-- Color ARGB, outer, inner, x, y, start, end -- Color ARGB, outer, inner, x, y, start, end

View File

@ -5,10 +5,16 @@
#include <lua5.3/lauxlib.h> #include <lua5.3/lauxlib.h>
int api_draw_clear(lua_State *L); int api_draw_clear(lua_State *L);
int api_draw_pixel(lua_State *L);
int api_draw_line(lua_State *L);
int api_draw_poly(lua_State *L);
bool loadapi_draw(lua_State *lua) { bool loadapi_draw(lua_State *lua) {
const struct luaL_Reg api_draw[] = { const struct luaL_Reg api_draw[] = {
{ "clear", api_draw_clear }, { "clear", api_draw_clear },
{ "pixel", api_draw_pixel },
{ "line", api_draw_line },
{ "poly", api_draw_poly },
}; };
lua_newtable(lua); lua_newtable(lua);
luaL_setfuncs(lua, api_draw, 0); luaL_setfuncs(lua, api_draw, 0);
@ -16,15 +22,88 @@ bool loadapi_draw(lua_State *lua) {
return true; return true;
} }
void argb_to_cairo(int color, double *a, double *r, double *g, double *b) {
int ia = (color >> 24) & 0xFF,
ir = (color >> 16) & 0xFF,
ig = (color >> 8) & 0xFF,
ib = color & 0xFF;
*r = ir / 255.0;
*g = ig / 255.0;
*b = ib / 255.0;
*a = ia / 255.0;
}
int api_draw_clear(lua_State *L) { int api_draw_clear(lua_State *L) {
printf("Draw.clear()\n");
int color = luaL_checkinteger(L, 1);
printf("Draw.clear(0x%08x)\n", color);
cairo_t *cr = global_context.cairo_context.cairo; cairo_t *cr = global_context.cairo_context.cairo;
int r = (color >> 16) & 0xFF, int color = luaL_checkinteger(L, 1);
g = (color >> 8) & 0xFF, double a, r, g, b;
b = color & 0xFF; argb_to_cairo(color, &a, &r, &g, &b);
cairo_set_source_rgb(cr, r / 255.0, g / 255.0, b / 255.0); cairo_set_source_rgba(cr, r, g, b, a);
cairo_paint(cr); cairo_paint(cr);
return 0; return 0;
} }
int api_draw_pixel(lua_State *L) {
cairo_t *cr = global_context.cairo_context.cairo;
int color = luaL_checkinteger(L, 1);
lua_Number x = luaL_checknumber(L, 2);
lua_Number y = luaL_checknumber(L, 3);
double a, r, g, b;
argb_to_cairo(color, &a, &r, &g, &b);
cairo_set_source_rgba(cr, r, g, b, a);
cairo_rectangle(cr, x, y, 1, 1);
cairo_fill(cr);
return 0;
}
int api_draw_line(lua_State *L) {
cairo_t *cr = global_context.cairo_context.cairo;
if (lua_gettop(L) < 6) {
return luaL_error(L, "Not enough arguments: should have at least 2 points");
}
int color = luaL_checkinteger(L, 1);
double a, r, g, b;
argb_to_cairo(color, &a, &r, &g, &b);
lua_Number width = luaL_checknumber(L, 2);
lua_Number x = luaL_checknumber(L, 3);
lua_Number y = luaL_checknumber(L, 4);
cairo_set_source_rgba(cr, r, g, b, a);
cairo_set_line_width(cr, width);
cairo_move_to(cr, x, y);
for (int i = 5; i <= lua_gettop(L); i += 2) {
x = luaL_checknumber(L, i);
y = luaL_checknumber(L, i + 1);
cairo_line_to(cr, x, y);
}
cairo_stroke(cr);
return 0;
}
int api_draw_poly(lua_State *L) {
cairo_t *cr = global_context.cairo_context.cairo;
if (lua_gettop(L) < 7) {
return luaL_error(L, "Not enough arguments: should have at least 3 points");
}
int color = luaL_checkinteger(L, 1);
double a, r, g, b;
argb_to_cairo(color, &a, &r, &g, &b);
lua_Number x = luaL_checknumber(L, 2);
lua_Number y = luaL_checknumber(L, 3);
cairo_set_source_rgba(cr, r, g, b, a);
cairo_move_to(cr, x, y);
for (int i = 4; i <= lua_gettop(L); i += 2) {
x = luaL_checknumber(L, i);
y = luaL_checknumber(L, i + 1);
cairo_line_to(cr, x, y);
}
cairo_fill(cr);
return 0;
}