1 ! (c)2010 Joe Groff bsd license
2 USING: accessors alien assocs classes.struct combinators
3 combinators.short-circuit fry gpu.shaders images images.atlas
4 images.loader io.directories io.encodings.utf8 io.files
5 io.pathnames json json.reader kernel locals math math.matrices.simd
6 math.vectors.simd sequences sets specialized-arrays
8 FROM: alien.c-types => float ;
9 SPECIALIZED-ARRAYS: float float-4 ;
12 ERROR: bad-papier-version version ;
14 CONSTANT: papier-map-version 3
16 : check-papier-version ( hash -- hash )
17 "papier" over at dup papier-map-version = [ drop ] [ bad-papier-version ] if ;
19 UNION: ?string string POSTPONE: f ;
31 { texcoords float-4-array } ;
33 VERTEX-FORMAT: papier-vertex
34 { "vertex" float-components 3 f }
35 { f float-components 1 f }
36 { "texcoord" float-components 2 f }
37 { f float-components 2 f }
38 { "color" float-components 4 f } ;
39 STRUCT: papier-vertex-struct
43 SPECIALIZED-ARRAY: papier-vertex-struct
45 ERROR: bad-matrix-dim matrix ;
47 : parse-slab ( hash -- name images frame center size orient color )
49 [ "name" swap at [ f ] when-json-null ]
51 [ "frame" swap at >fixnum ]
52 [ "center" swap at 3 0.0 pad-tail 4 1.0 pad-tail >float-4 ]
53 [ "size" swap at 4 1.0 pad-tail >float-4 ]
54 [ "orient" swap at >float-4 ]
55 [ "color" swap at >float-4 ]
58 TYPED: slab-matrix ( slab: slab -- matrix: matrix4 )
59 [ center>> translation-matrix4 ]
60 [ size>> scale-matrix4 m4. ]
61 [ orient>> q>matrix4 m4. ] tri ;
63 TYPED: update-slab-matrix ( slab: slab -- )
64 dup slab-matrix >>matrix drop ;
66 TYPED: cycle-slab-frame ( slab: slab -- )
67 dup images>> length '[ 1 + dup _ < [ drop 0 ] unless ] change-frame drop ;
69 : <slab> ( -- slab ) slab new ; inline
71 : set-up-slab ( name images frame center size orient color slab -- slab )
79 dup update-slab-matrix ; inline
81 TYPED: update-slab-for-atlas ( slab: slab images -- )
82 [ dup images>> ] dip '[ _ at >float-4 ] float-4-array{ } map-as >>texcoords drop ;
84 : update-slabs-for-atlas ( slabs images -- )
85 '[ _ update-slab-for-atlas ] each ; inline
87 : parse-papier-map ( hash -- slabs )
89 "slabs" swap at [ parse-slab <slab> set-up-slab ] map ;
91 : load-papier-map ( path name -- slabs )
92 append-path utf8 file-contents json> parse-papier-map ;
94 : load-papier-images ( path -- images atlas )
96 [ file-extension { "tiff" "png" } member? ] filter [ dup load-image ] H{ } map>assoc
97 ] with-directory-files make-atlas-assoc ;
99 : slabs-by-name ( slabs -- assoc )
100 [ name>> ] filter [ [ name>> ] keep ] H{ } map>assoc ; inline