Browse Source

Render puppet with nanosvg

master
Macoy Madson 4 months ago
parent
commit
56bc8ed3d3
  1. 3
      .gitmodules
  2. 1
      Dependencies/nanosvg
  3. 221
      data/TestPuppet.svg
  4. 7
      src/NanoSVG.cake
  5. 74
      src/VectorPuppetShow.cake

3
.gitmodules

@ -13,3 +13,6 @@
[submodule "Dependencies/FreeType"]
path = Dependencies/FreeType
url = https://gitlab.freedesktop.org/freetype/freetype.git
[submodule "Dependencies/nanosvg"]
path = Dependencies/nanosvg
url = gitea@macoy.me:macoy/nanosvg.git

1
Dependencies/nanosvg

@ -0,0 +1 @@
Subproject commit 5d01ca80ec32efaecee50c0d71461993ede55c16

221
data/TestPuppet.svg

@ -0,0 +1,221 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="1024"
height="1024"
viewBox="0 0 270.93333 270.93333"
version="1.1"
id="svg8"
sodipodi:docname="TestPuppet.svg"
inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#2f2f2f"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:zoom="1.1775363"
inkscape:cx="446.18745"
inkscape:cy="649.97768"
inkscape:document-units="px"
inkscape:current-layer="g1453"
showgrid="false"
units="px"
inkscape:pagecheckerboard="false"
inkscape:window-width="3840"
inkscape:window-height="2082"
inkscape:window-x="0"
inkscape:window-y="41"
inkscape:window-maximized="1" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-26.066667)">
<g
id="g1429"
inkscape:label="Arm Right"
transform="matrix(0.92814178,0,0,0.92814178,12.903386,-31.027851)"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.42533898;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<ellipse
inkscape:label="Arm Upper Right"
ry="7.6395378"
rx="29.659384"
cy="134.09808"
cx="83.36084"
id="path1405"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.42533898;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<ellipse
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.42533898;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="ellipse1409"
cx="38.759418"
cy="134.09808"
rx="25.954048"
ry="6.1939092"
inkscape:label="Arm Lower Right" />
<ellipse
inkscape:label="Hand Right"
ry="5.842"
rx="6.7407689"
cy="134.09808"
cx="8.0889225"
id="path1411"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.42533898;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
</g>
<g
inkscape:label="Leg Left"
id="g1453"
transform="matrix(0,-1.3183544,1.3183544,0,-60.6927,295.51511)"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.00346053;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<ellipse
inkscape:label="Arm Lower Right"
ry="6.1939092"
rx="25.954048"
cy="134.09808"
cx="38.759418"
id="ellipse1449"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.00346053;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<ellipse
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.00346053;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="ellipse1447"
cx="83.36084"
cy="134.09808"
rx="29.659384"
ry="7.6395378"
inkscape:label="Arm Upper Right" />
<ellipse
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.00346053;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="ellipse1451"
cx="8.0889225"
cy="134.09808"
rx="6.7407689"
ry="5.842"
inkscape:label="Hand Right" />
</g>
<ellipse
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.32291663;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="path1455"
cx="134.55244"
cy="81.183044"
rx="6.2913847"
ry="6.6284227"
inkscape:label="Neck" />
<circle
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.32291663;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="path1397"
cx="134.55244"
cy="53.551071"
inkscape:label="Head"
r="24.557083" />
<g
id="g1424"
inkscape:label="Arm Left"
transform="matrix(0.92814178,0,0,0.92814178,12.903386,-31.027851)"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.42533898;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<ellipse
transform="scale(-1,1)"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.42533898;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="ellipse1415"
cx="-179.30446"
cy="134.09808"
rx="29.659384"
ry="7.6395378"
inkscape:label="Arm Upper Left" />
<ellipse
transform="scale(-1,1)"
inkscape:label="Arm Lower Left"
ry="6.1939092"
rx="25.954048"
cy="134.09808"
cx="-223.90587"
id="ellipse1417"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.42533898;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<ellipse
transform="scale(-1,1)"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.42533898;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="ellipse1419"
cx="-254.57637"
cy="134.09808"
rx="6.7407689"
ry="5.842"
inkscape:label="Hand Left" />
</g>
<ellipse
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.15914345;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="path1399"
cx="134.55244"
cy="106.91435"
rx="29.553249"
ry="22.116808"
inkscape:label="UpperBody" />
<ellipse
ry="21.382437"
rx="24.259771"
cy="135.45627"
cx="134.55244"
id="ellipse1401"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.32291663;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
inkscape:label="Waist" />
<g
transform="matrix(0,-1.3183544,1.3183544,0,-23.780004,295.51511)"
id="g1445"
inkscape:label="Leg Left"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.00346053;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<ellipse
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.00346053;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="ellipse1441"
cx="38.759418"
cy="134.09808"
rx="25.954048"
ry="6.1939092"
inkscape:label="Arm Lower Right" />
<ellipse
inkscape:label="Arm Upper Right"
ry="7.6395378"
rx="29.659384"
cy="134.09808"
cx="83.36084"
id="ellipse1439"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.00346053;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<ellipse
inkscape:label="Hand Right"
ry="5.842"
rx="6.7407689"
cy="134.09808"
cx="8.0889225"
id="ellipse1443"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.00346053;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
</g>
<ellipse
inkscape:label="Hip"
style="fill:#c37033;fill-opacity:1;stroke:#275d59;stroke-width:1.32291663;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="ellipse1403"
cx="134.55244"
cy="148.49042"
rx="26.762945"
ry="16.273668" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 8.1 KiB

7
src/NanoSVG.cake

@ -0,0 +1,7 @@
;; NanoSVG.cake: Load and rasterize SVG files
(c-preprocessor-define-constant NANOSVG_IMPLEMENTATION 1)
(c-preprocessor-define-constant NANOSVGRAST_IMPLEMENTATION 1)
(export-and-evaluate
(add-c-search-directory-module "Dependencies/nanosvg/src")
(c-import "nanosvg.h" "nanosvgrast.h"))

74
src/VectorPuppetShow.cake

@ -5,6 +5,7 @@
(add-cakelisp-search-directory "src")
(import
"NanoSVG.cake"
;; Cakelisp
"CHelpers.cake"
;; GameLib
@ -138,8 +139,52 @@
(- (path font > end-file-data) (path font > start-file-data))
device-dpi
(path font > font-size-points))
(return 1)))
(return 1)))
;;
;; SVGs
;;
(var svg-image-texture (addr SDL_Texture) null)
(var svg-image-width int 0)
(var svg-image-height int 0)
(defer
(when svg-image-texture
(SDL_DestroyTexture svg-image-texture)))
(scope
(var filename (addr (const char)) "data/TestPuppet.svg")
(var image (addr NSVGimage) (nsvgParseFromFile filename "px" 96.0f))
(unless image
(vpslog "Failed to load SVG %s\n" filename)
(return 1))
(defer (nsvgDelete image))
(var rasterizer (addr NSVGrasterizer) (nsvgCreateRasterizer))
(unless rasterizer
(vpslog "Failed to create NanoSVG rasterizer\n")
(return 1))
(defer (nsvgDeleteRasterizer rasterizer))
(var-cast-to image-buffer (addr (unsigned char))
(malloc (* (path image > width) (path image > height) 4)))
(defer (free image-buffer))
(nsvgRasterize rasterizer image 0 0 1 image-buffer (path image > width) (path image > height)
(* 4 (path image > width)))
(var image-surface (addr SDL_Surface)
(SDL_CreateRGBSurfaceWithFormatFrom
image-buffer
(path image > width)
(path image > height)
32 ;; bit depth of each pixel (RGBA)
(* (path image > width) 4) ;; pitch (width of a row in bytes)
;; Because of how SDL reads pixels in, this does seem to be endian-dependent
;; I don't fully understand why
SDL_PIXELFORMAT_RGBA32))
(defer (SDL_FreeSurface image-surface)) ;; We don't need this after making the texture
(set svg-image-texture (SDL_CreateTextureFromSurface renderer image-surface))
(set svg-image-width (path image > width))
(set svg-image-height (path image > height))
(unless svg-image-texture
(sdl-print-error)
(return 1)))
;; current-key-states is owned by SDL, but we own last-frame-states
(defer (dynarray-free (field s-key-states last-frame-states)))
@ -192,14 +237,29 @@
;; (set virtual-window-width 1920)
;; (set virtual-window-height 1080))
(SDL_SetRenderDrawColor renderer 11 19 40 255)
(SDL_SetRenderDrawColor renderer 11 11 11 255)
(SDL_RenderClear renderer)
(render-string
renderer (addr (field (at heading-font-index font-atlases) atlas))
(field (at heading-font-index font-atlases) texture)
(/ true-window-width 4.5f) (/ true-window-height 8)
"This is a test string")
;; (render-string
;; renderer (addr (field (at heading-font-index font-atlases) atlas))
;; (field (at heading-font-index font-atlases) texture)
;; (/ true-window-width 4.5f) (/ true-window-height 8)
;; "This is a test string")
(scope
(var source-rectangle SDL_Rect
(array 0
0
svg-image-width
svg-image-height))
(var destination-rectangle SDL_Rect
(array
0
0
svg-image-width
svg-image-height))
(SDL_RenderCopy renderer svg-image-texture
(addr source-rectangle) (addr destination-rectangle)))
;; (when s-enable-debug-overlay

Loading…
Cancel
Save