1 ! Copyright (C) 2015 Sankaranarayanan Viswanathan.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs byte-vectors formatting
4 images images.loader kernel 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-snake-image ( filename -- image )
32 "vocab:snake-game/_resources/%s" sprintf load-image ;
34 : load-snake-texture ( file-name -- texture )
35 load-snake-image { 0 0 } <texture> ;
37 : load-sprite-textures ( filename cols rows -- seq )
38 [ load-snake-image ] 2dip generate-sprite-sheet
39 [ { 0 0 } <texture> ] map ;
41 : snake-head-textures ( -- assoc )
42 { "head-up" "head-right" "head-down" "head-left" }
43 "head.png" 1 4 load-sprite-textures zip ;
45 : snake-body-textures ( -- assoc )
47 "body-right-up" "body-down-right" "body-right-right"
48 "body-up-up" "body-up-right" "body-right-down"
51 "body-down-left" "body-left-up" "body-left-left"
52 "body-down-down" "body-left-down" "body-up-left"
54 "body.png" 3 2 load-sprite-textures '[ _ zip ] bi@ append ;
56 : snake-tail-textures ( -- assoc )
57 { "tail-down" "tail-left" "tail-up" "tail-right" }
58 "tail.png" 2 2 load-sprite-textures zip ;
60 : food-texture ( -- assoc )
61 "food" "food.png" load-snake-texture 2array 1array ;
63 : background-texture ( -- assoc )
64 "background" "background.png" load-snake-texture 2array 1array ;
66 : snake-textures ( -- assoc )
68 snake-head-textures %%
69 snake-body-textures %%
70 snake-tail-textures %%