1 ! Copyright (C) 2015 Sankaranarayanan Viswanathan.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs byte-vectors formatting fry
4 images images.loader kernel locals make math math.vectors
5 opengl.textures sequences ;
9 : new-image-like ( image w h -- image )
11 [ 2array >>dim ] 2keep *
12 over bytes-per-pixel * <byte-vector> >>bitmap ;
14 :: image-part ( image x y w h -- image )
15 image w h new-image-like :> new-image
18 x y i + w image pixel-row-slice-at
22 :: generate-sprite-sheet ( image rows cols -- seq )
23 cols rows 2array :> split-dims
24 image dim>> split-dims [ / ] 2map first2 :> ( sw sh )
25 rows <iota> sh v*n :> ys
26 cols <iota> sh v*n :> xs
28 swap [ image ] 2dip sw sh image-part
29 ] cartesian-map f join ;
31 : load-sprite-image ( filename -- image )
32 "vocab:snake-game/_resources/%s" sprintf load-image ;
34 : make-texture ( image -- texture )
37 : make-sprites ( filename cols rows -- seq )
38 [ load-sprite-image ] 2dip generate-sprite-sheet
39 [ make-texture ] map ;
41 : snake-head-textures ( -- assoc )
42 "head.png" 1 4 make-sprites
43 { "head-up" "head-right" "head-down" "head-left" }
44 [ swap 2array ] 2map ;
46 :: assoc-with-value-like ( assoc key seq -- )
48 seq [ [ value ] dip assoc set-at ] each ;
50 : snake-body-textures ( -- assoc )
51 "body.png" 3 2 make-sprites
54 dup 1 { "body-right-up" "body-down-left" } assoc-with-value-like
55 dup 2 { "body-down-right" "body-left-up" } assoc-with-value-like
56 dup 3 { "body-right-right" "body-left-left" } assoc-with-value-like
57 dup 4 { "body-up-up" "body-down-down" } assoc-with-value-like
58 dup 5 { "body-up-right" "body-left-down" } assoc-with-value-like
59 dup 6 { "body-right-down" "body-up-left" } assoc-with-value-like
60 dup [ { 1 2 3 4 5 6 } ] dip [ delete-at ] curry each ;
62 : snake-tail-textures ( -- assoc )
63 "tail.png" 2 2 make-sprites
64 { "tail-down" "tail-left" "tail-up" "tail-right" }
65 [ swap 2array ] 2map ;
67 : food-texture ( -- assoc )
68 "food" "food.png" load-sprite-image make-texture
71 : background-texture ( -- assoc )
72 "background" "background.png" load-sprite-image make-texture