login

<     >

2024-01-14 12:54:34 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

sdl: add SDL2_gfx

diff --git a/lib/arco/sdl2.lua b/lib/arco/sdl2.lua
index 99785ee..16f619f 100644
--- a/lib/arco/sdl2.lua
+++ b/lib/arco/sdl2.lua
@@ -1,11 +1,12 @@
 local ffi = require "ffi"
+local sdl_gfx = require "arco.sdl2_gfx"
 
 local here = debug.getinfo(1).source:match("@?(.*/)")
 local cdefs = io.open(here.."cdef_sdl2.h", "r"):read("*a")
 ffi.cdef(cdefs)
 local sdl = ffi.load("SDL2")
 
-return setmetatable({}, {
+return setmetatable({gfx=sdl_gfx}, {
     __index = function (t, k)
         local ok, obj = pcall(function () return sdl["SDL_"..k] end)
         if not ok then

diff --git a/lib/arco/sdl2_gfx.lua b/lib/arco/sdl2_gfx.lua
new file mode 100644
index 0000000..cfb7784
--- /dev/null
+++ b/lib/arco/sdl2_gfx.lua
@@ -0,0 +1,60 @@
+local ffi = require "ffi"
+
+ffi.cdef[[
+typedef int8_t Sint8;
+typedef uint8_t Uint8;
+typedef int16_t Sint16;
+typedef uint16_t Uint16;
+typedef int32_t Sint32;
+typedef uint32_t Uint32;
+typedef int64_t Sint64;
+typedef uint64_t Uint64;
+
+typedef struct SDL_Renderer SDL_Renderer;
+typedef struct SDL_Surface SDL_Surface;
+
+/* https://www.ferzkopp.net/Software/SDL2_gfx/Docs/html/_s_d_l2__gfx_primitives_8h.html */
+int pixelColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Uint32 color);
+int hlineColor(SDL_Renderer *renderer, Sint16 x1, Sint16 x2, Sint16 y, Uint32 color);
+int vlineColor(SDL_Renderer *renderer, Sint16 x, Sint16 y1, Sint16 y2, Uint32 color);
+int rectangleColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
+int roundedRectangleColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 rad, Uint32 color);
+int boxColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
+int roundedBoxColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 rad, Uint32 color);
+int lineColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
+int aalineColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color);
+int thickLineColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 width, Uint32 color);
+int circleColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Uint32 color);
+int arcColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color);
+int aacircleColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Uint32 color);
+int filledCircleColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 r, Uint32 color);
+int ellipseColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color);
+int aaellipseColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color);
+int filledEllipseColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color);
+int pieColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color);
+int filledPieColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color);
+int trigonColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color);
+int aatrigonColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color);
+int filledTrigonColor(SDL_Renderer *renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Sint16 x3, Sint16 y3, Uint32 color);
+int polygonColor(SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, Uint32 color);
+int aapolygonColor(SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, Uint32 color);
+int filledPolygonColor(SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, Uint32 color);
+int texturedPolygon(SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, SDL_Surface *texture, int texture_dx, int texture_dy);
+int bezierColor(SDL_Renderer *renderer, const Sint16 *vx, const Sint16 *vy, int n, int s, Uint32 color);
+void gfxPrimitivesSetFont(const void *fontdata, Uint32 cw, Uint32 ch);
+void gfxPrimitivesSetFontRotation(Uint32 rotation);
+int characterColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, char c, Uint32 color);
+int stringColor(SDL_Renderer *renderer, Sint16 x, Sint16 y, const char *s, Uint32 color);
+]]
+
+local gfx = ffi.load("SDL2_gfx")
+
+return setmetatable({}, {
+    __index = function (t, k)
+        local ok, obj = pcall(function () return gfx["gfx"..k] end)
+        if not ok then
+            obj = gfx[k]
+        end
+        return obj
+    end
+})