Added rectangle functions

This commit is contained in:
Casey 2024-04-09 20:45:35 +03:00
parent 0d7dd39b77
commit 9528effd6e
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
3 changed files with 54 additions and 11 deletions

View File

@ -11,10 +11,15 @@ function tick(t)
(math.sin(t) * 0.5 + 0.5) * 1920, (math.sin(t) * 0.5 + 0.5) * 1920,
(math.cos(t) * 0.5 + 0.5) * 1080) (math.cos(t) * 0.5 + 0.5) * 1080)
-- Color ARGB, thickness, x1, y1, x2, y2, xn, yn, ...
Draw.line(0xFFFF00FF, 8, 100, 100, 200, 400, 300, 600) --> { Vec2, ... }
-- Color ARGB, x1, y1, x2, y2, xn, yn, ... -- Color ARGB, x1, y1, x2, y2, xn, yn, ...
Draw.poly(0xFFFF00FF, 100, 100, 200, 400, 150, 500) --> Rect Draw.poly(0xFF00FFFF, 100, 100, 200, 100, 300, 600) --> Rect
-- Color ARGB, thickness, x1, y1, x2, y2, xn, yn, ...
Draw.line(0xFFFF00FF, 8, 100, 100, 200, 100, 300, 600, 100, 100) --> { Vec2, ... }
-- Color ARGB, thickness, x, y, w, h
Draw.rect(0xFFFF000F, 1, 100, 700, 320, 240) --> Rect
-- Color ARGB, x, y, w, h
Draw.rect_fill(0xFF00FFFF, 100, 700, 320, 240) --> Rect
--[[ --[[
-- Color ARGB, radius, x, y, start, end -- Color ARGB, radius, x, y, start, end
@ -22,11 +27,6 @@ function tick(t)
-- 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(0xFF00FF00, 30, 20, 300, 300) --> Rect
-- Color ARGB, x, y, w, h
Draw.rect_fill(0xFF00FFFF, 100, 700, 320, 240) --> Rect
-- Color ARGB, thickness, x, y, w, h
Draw.rect(0xFFFF00FF, 8, 100, 700, 320, 240) --> 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

@ -8,6 +8,8 @@ int api_draw_clear(lua_State *L);
int api_draw_pixel(lua_State *L); int api_draw_pixel(lua_State *L);
int api_draw_line(lua_State *L); 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_fill(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[] = {
@ -15,6 +17,8 @@ bool loadapi_draw(lua_State *lua) {
{ "pixel", api_draw_pixel }, { "pixel", api_draw_pixel },
{ "line", api_draw_line }, { "line", api_draw_line },
{ "poly", api_draw_poly }, { "poly", api_draw_poly },
{ "rect", api_draw_rect },
{ "rect_fill", api_draw_rect_fill },
}; };
lua_newtable(lua); lua_newtable(lua);
luaL_setfuncs(lua, api_draw, 0); luaL_setfuncs(lua, api_draw, 0);
@ -67,12 +71,12 @@ int api_draw_line(lua_State *L) {
double a, r, g, b; double a, r, g, b;
argb_to_cairo(color, &a, &r, &g, &b); argb_to_cairo(color, &a, &r, &g, &b);
lua_Number width = luaL_checknumber(L, 2); lua_Number stroke_width = luaL_checknumber(L, 2);
lua_Number x = luaL_checknumber(L, 3); lua_Number x = luaL_checknumber(L, 3);
lua_Number y = luaL_checknumber(L, 4); lua_Number y = luaL_checknumber(L, 4);
cairo_set_source_rgba(cr, r, g, b, a); cairo_set_source_rgba(cr, r, g, b, a);
cairo_set_line_width(cr, width); cairo_set_line_width(cr, stroke_width);
cairo_move_to(cr, x, y); cairo_move_to(cr, x, y);
for (int i = 5; i <= lua_gettop(L); i += 2) { for (int i = 5; i <= lua_gettop(L); i += 2) {
x = luaL_checknumber(L, i); x = luaL_checknumber(L, i);
@ -107,3 +111,42 @@ int api_draw_poly(lua_State *L) {
cairo_fill(cr); cairo_fill(cr);
return 0; return 0;
} }
int api_draw_rect(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 stroke_width = luaL_checknumber(L, 2);
lua_Number x = luaL_checknumber(L, 3);
lua_Number y = luaL_checknumber(L, 4);
lua_Number w = luaL_checknumber(L, 5);
lua_Number h = luaL_checknumber(L, 6);
cairo_set_line_width(cr, stroke_width);
cairo_set_source_rgba(cr, r, g, b, a);
cairo_rectangle(cr, x, y, w, h);
cairo_stroke(cr);
return 0;
}
int api_draw_rect_fill(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 x = luaL_checknumber(L, 2);
lua_Number y = luaL_checknumber(L, 3);
lua_Number w = luaL_checknumber(L, 4);
lua_Number h = luaL_checknumber(L, 5);
cairo_set_source_rgba(cr, r, g, b, a);
cairo_rectangle(cr, x, y, w, h);
cairo_fill(cr);
return 0;
}

View File

@ -1,6 +1,6 @@
#include <lua5.2/lualib.h>
#include <lua5.3/lua.h> #include <lua5.3/lua.h>
#include <lua5.3/luaconf.h> #include <lua5.3/luaconf.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h> #include <lua5.3/lauxlib.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>