|
|
@ -154,6 +154,10 @@ |
|
|
|
(SDL_DestroyTexture svg-image-texture))) |
|
|
|
(var packed-rectangles (addr stbrp_rect) null) |
|
|
|
(defer (when packed-rectangles (free packed-rectangles))) |
|
|
|
(var puppet-atlas-texture (addr SDL_Texture) null) |
|
|
|
(defer (when puppet-atlas-texture (SDL_DestroyTexture puppet-atlas-texture))) |
|
|
|
(var puppet-atlas-width int 0) |
|
|
|
(var puppet-atlas-height int 0) |
|
|
|
(scope |
|
|
|
(var filename (addr (const char)) "data/TestPuppet.svg") |
|
|
|
(set puppet-image (nsvgParseFromFile filename "px" 96.0f)) |
|
|
@ -179,27 +183,39 @@ |
|
|
|
(at 2 (path shape > bounds)) |
|
|
|
(at 3 (path shape > bounds)))) |
|
|
|
|
|
|
|
(var puppet-atlas-buffer (addr (unsigned char)) null) |
|
|
|
(defer (when puppet-atlas-buffer (free puppet-atlas-buffer))) |
|
|
|
(var max-texture-size int 4096) |
|
|
|
(var attempt-pack-width int 256) |
|
|
|
(var attempt-pack-height int 256) |
|
|
|
(var packed-successfully bool true) |
|
|
|
(while (= 0 (nanosvg-pack-into-atlas |
|
|
|
puppet-image |
|
|
|
attempt-pack-width |
|
|
|
attempt-pack-height |
|
|
|
(addr packed-rectangles))) |
|
|
|
(free packed-rectangles) |
|
|
|
(set attempt-pack-width (* 2 attempt-pack-width)) |
|
|
|
(set attempt-pack-height (* 2 attempt-pack-height)) |
|
|
|
(when (> attempt-pack-width max-texture-size) |
|
|
|
(vpslog "Failed to pack image into atlas\n") |
|
|
|
(set packed-successfully false) |
|
|
|
(break))) |
|
|
|
(var packed-successfully bool |
|
|
|
(nanosvg-rasterize-atlas rasterizer |
|
|
|
puppet-image max-texture-size |
|
|
|
(addr packed-rectangles) |
|
|
|
(addr puppet-atlas-buffer) |
|
|
|
(addr puppet-atlas-width) |
|
|
|
(addr puppet-atlas-height))) |
|
|
|
(when packed-successfully |
|
|
|
(vpslog "Packed SVG shapes into %dx%d texture\n" attempt-pack-width |
|
|
|
attempt-pack-height)) |
|
|
|
|
|
|
|
(nsvgRasterize rasterizer puppet-image 0 0 1 image-buffer |
|
|
|
(vpslog "Packed SVG shapes into %dx%d texture\n" puppet-atlas-width |
|
|
|
puppet-atlas-height) |
|
|
|
(var image-surface (addr SDL_Surface) |
|
|
|
(SDL_CreateRGBSurfaceWithFormatFrom |
|
|
|
puppet-atlas-buffer |
|
|
|
puppet-atlas-width |
|
|
|
puppet-atlas-height |
|
|
|
32 ;; bit depth of each pixel (RGBA) |
|
|
|
(* puppet-atlas-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)) |
|
|
|
(unless image-surface |
|
|
|
(sdl-print-error) |
|
|
|
(return 1)) |
|
|
|
(defer (SDL_FreeSurface image-surface)) ;; We don't need this after making the texture |
|
|
|
(set puppet-atlas-texture (SDL_CreateTextureFromSurface renderer image-surface)) |
|
|
|
(unless puppet-atlas-texture |
|
|
|
(sdl-print-error) |
|
|
|
(return 1))) |
|
|
|
|
|
|
|
(nsvgRasterize rasterizer puppet-image 0.f 0.f 1.f image-buffer |
|
|
|
(path puppet-image > width) (path puppet-image > height) |
|
|
|
(* 4 (path puppet-image > width))) |
|
|
|
(var image-surface (addr SDL_Surface) |
|
|
@ -274,25 +290,35 @@ |
|
|
|
(SDL_SetRenderDrawColor renderer 11 11 11 255) |
|
|
|
(SDL_RenderClear renderer) |
|
|
|
|
|
|
|
(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))) |
|
|
|
;; (scope ;; Draw entire SVG |
|
|
|
;; (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))) |
|
|
|
|
|
|
|
(var mouse-x int 0) |
|
|
|
(var mouse-y int 0) |
|
|
|
(SDL_GetMouseState (addr mouse-x) (addr mouse-y)) |
|
|
|
|
|
|
|
(when puppet-atlas-texture |
|
|
|
(var atlas-rectangle SDL_Rect |
|
|
|
(array 0 |
|
|
|
0 |
|
|
|
puppet-atlas-width |
|
|
|
puppet-atlas-height)) |
|
|
|
(unless (= 0 (SDL_RenderCopy renderer puppet-atlas-texture (addr atlas-rectangle) (addr atlas-rectangle))) |
|
|
|
(sdl-print-error) |
|
|
|
(set exit-reason "Failed to render atlas texture"))) |
|
|
|
|
|
|
|
(scope ;; Shape selection and debug viewing |
|
|
|
(var hovered-shape-id-buffer (array 1024 char) (array 0)) |
|
|
|
(var hovered-write-head (addr char) hovered-shape-id-buffer) |
|
|
|