Added circle and ring drawing functions

This commit is contained in:
Casey 2024-04-09 22:56:28 +03:00
parent 9528effd6e
commit dcfc12d0fc
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
2 changed files with 62 additions and 4 deletions

View File

@ -17,16 +17,16 @@ function tick(t)
Draw.line(0xFFFF00FF, 8, 100, 100, 200, 100, 300, 600, 100, 100) --> { Vec2, ... } Draw.line(0xFFFF00FF, 8, 100, 100, 200, 100, 300, 600, 100, 100) --> { Vec2, ... }
-- Color ARGB, thickness, x, y, w, h -- Color ARGB, thickness, x, y, w, h
Draw.rect(0xFFFF000F, 1, 100, 700, 320, 240) --> Rect Draw.rect(0xFFFF000F, 4, 100, 700, 320, 240) --> Rect
-- Color ARGB, x, y, w, h -- Color ARGB, x, y, w, h
Draw.rect_fill(0xFF00FFFF, 100, 700, 320, 240) --> Rect Draw.rect_fill(0xFF00FFFF, 100, 700, 320, 240) --> 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, 40, 300, 300, t, t + math.pi / 2) --> Rect
-- Color ARGB, outer, inner, x, y, start, end -- Color ARGB, outer, inner, x, y, start, end
Draw.ring(0xFF00FF00, 30, 20, 300, 300) --> Rect Draw.ring(0xFFFFFFFF, 30, 20, 300, 300, t, t + math.pi) --> Rect
--[[
-- font, text, size, x, y, Color ARGB -- font, text, size, x, y, Color ARGB
Draw.text(font, "Hello, world!", 32, 400, 400, 0xFFFF00FF) --> Rect Draw.text(font, "Hello, world!", 32, 400, 400, 0xFFFF00FF) --> Rect

View File

@ -3,6 +3,7 @@
#include <cairo/cairo.h> #include <cairo/cairo.h>
#include <lua5.3/lua.h> #include <lua5.3/lua.h>
#include <lua5.3/lauxlib.h> #include <lua5.3/lauxlib.h>
#include <math.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_pixel(lua_State *L);
@ -10,6 +11,8 @@ int api_draw_line(lua_State *L);
int api_draw_poly(lua_State *L); int api_draw_poly(lua_State *L);
int api_draw_rect(lua_State *L); int api_draw_rect(lua_State *L);
int api_draw_rect_fill(lua_State *L); int api_draw_rect_fill(lua_State *L);
int api_draw_circle(lua_State *L);
int api_draw_ring(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[] = {
@ -19,6 +22,8 @@ bool loadapi_draw(lua_State *lua) {
{ "poly", api_draw_poly }, { "poly", api_draw_poly },
{ "rect", api_draw_rect }, { "rect", api_draw_rect },
{ "rect_fill", api_draw_rect_fill }, { "rect_fill", api_draw_rect_fill },
{ "circle", api_draw_circle },
{ "ring", api_draw_ring },
}; };
lua_newtable(lua); lua_newtable(lua);
luaL_setfuncs(lua, api_draw, 0); luaL_setfuncs(lua, api_draw, 0);
@ -150,3 +155,56 @@ int api_draw_rect_fill(lua_State *L) {
return 0; return 0;
} }
int api_draw_circle(lua_State *L) {
cairo_t *cr = global_context.cairo_context.cairo;
int color = luaL_checkinteger(L, 1);
double a, r, g, b;
argb_to_cairo(color, &a, &r, &g, &b);
lua_Number radius = luaL_checknumber(L, 2);
lua_Number x = luaL_checknumber(L, 3);
lua_Number y = luaL_checknumber(L, 4);
lua_Number start = 0.0, end = M_PI * 2.0;
if (lua_gettop(L) > 4) {
start = luaL_checknumber(L, 5);
end = luaL_checknumber(L, 6);
}
cairo_set_source_rgba(cr, r, g, b, a);
cairo_move_to(cr, x, y);
cairo_arc(cr, x, y, radius, start, end);
cairo_fill(cr);
return 0;
}
int api_draw_ring(lua_State *L) {
cairo_t *cr = global_context.cairo_context.cairo;
int color = luaL_checkinteger(L, 1);
double a, r, g, b;
argb_to_cairo(color, &a, &r, &g, &b);
lua_Number outer = luaL_checknumber(L, 2);
lua_Number inner = luaL_checknumber(L, 3);
if (outer < inner) {
lua_Number tmp = outer;
outer = inner;
inner = tmp;
}
lua_Number x = luaL_checknumber(L, 4);
lua_Number y = luaL_checknumber(L, 5);
lua_Number start = 0.0, end = M_PI * 2.0;
if (lua_gettop(L) > 5) {
start = luaL_checknumber(L, 6);
end = luaL_checknumber(L, 7);
}
cairo_set_line_width(cr, outer - inner);
cairo_set_source_rgba(cr, r, g, b, a);
cairo_arc(cr, x, y, (outer + inner) / 2.0, start, end);
cairo_stroke(cr);
return 0;
}