]> gitweb.factorcode.org Git - factor.git/blob - extra/papier/papier.factor
arm.64.factor: extra semicolon removed
[factor.git] / extra / papier / papier.factor
1 ! (c)2010 Joe Groff bsd license
2 USING: accessors alien.c-types alien.data.map arrays assocs
3 combinators fry game.input game.input.scancodes game.loop
4 game.worlds gpu gpu.buffers gpu.framebuffers gpu.render
5 gpu.shaders gpu.state gpu.textures hashtables images kernel
6 literals math math.matrices.simd math.order math.vectors
7 math.vectors.simd papier.map papier.render papier.sprites
8 sequences sorting typed ui ui.gadgets ui.gadgets.worlds
9 ui.gestures ui.pixel-formats math.functions ;
10 IN: papier
11
12 CONSTANT: fov 0.7
13 CONSTANT: near-plane 0.25
14 CONSTANT: far-plane 1024.0
15 CONSTANT: move-rate 0.05
16 CONSTANT: eye float-4{ 0.0 2.5 7.0 0.0 }
17
18 CONSTANT:  1/√2 $[ 0.5 sqrt     ]
19 CONSTANT: -1/√2 $[ 0.5 sqrt neg ]
20
21 TUPLE: papier-world < game-world
22     { slabs array }
23     { slabs-by-name hashtable }
24     { slab-images hashtable }
25     { atlas image }
26     { uniforms papier-uniforms }
27     { renderer papier-renderer } ;
28
29 : load-slabs ( -- slabs )
30     <sprite>
31         "backdrop" >>name
32         { "backdrop.png" } >>images
33         0 >>frame
34         float-4{ 0 9.0 -2.0 1 } >>center
35         float-4{ 10 10 1 1 } >>size
36         float-4{ 1 0 0 0 } >>orient
37         float-4{ 1 1 1 1 } >>color
38         { { T{ animation-frame f  0 1 } } } swap set-up-sprite
39         dup update-slab-matrix
40
41     <sprite>
42         "ground" >>name
43         { "ground.png" } >>images
44         0 >>frame
45         float-4{ 0 -1 0 1 } >>center
46         float-4{ 10 2 2 1 } >>size
47         float-4{ $ 1/√2 $ 1/√2 0 0 } >>orient
48         float-4{ 1 1 1 1 } >>color
49         { { T{ animation-frame f  0 1 } } } swap set-up-sprite
50         dup update-slab-matrix
51
52     <sprite>
53         "cat" >>name
54         {
55             "dancing-cat001.png"
56             "dancing-cat002.png"
57             "dancing-cat003.png"
58             "dancing-cat004.png"
59             "dancing-cat005.png"
60             "dancing-cat006.png"
61             "dancing-cat007.png"
62             "dancing-cat008.png"
63             "dancing-cat009.png"
64             "dancing-cat010.png"
65             "dancing-cat011.png"
66             "dancing-cat012.png"
67         } >>images
68         0 >>frame
69         float-4{ 3 -0.25 -0.1 1 } >>center
70         float-4{ 0.75 0.75 1.0 1.0 } >>size
71         float-4{ 1 0 0 0 } >>orient
72         float-4{ 1 1 1 1 } >>color
73         {
74             {
75                 T{ animation-frame f  0 2 }
76                 T{ animation-frame f  1 2 }
77                 T{ animation-frame f  2 2 }
78                 T{ animation-frame f  3 2 }
79                 T{ animation-frame f  4 2 }
80                 T{ animation-frame f  5 2 }
81                 T{ animation-frame f  6 2 }
82                 T{ animation-frame f  7 2 }
83                 T{ animation-frame f  8 2 }
84                 T{ animation-frame f  9 2 }
85                 T{ animation-frame f 10 2 }
86                 T{ animation-frame f 11 2 }
87             }
88         } swap set-up-sprite
89         dup update-slab-matrix
90
91     <sprite>
92         "marco" >>name
93         {
94             "marco-still001.png"
95             "marco-walk001.png"
96             "marco-walk002.png"
97             "marco-walk003.png"
98             "marco-walk004.png"
99             "marco-walk005.png"
100             "marco-walk006.png"
101             "marco-walk007.png"
102             "marco-walk008.png"
103             "marco-walk009.png"
104             "marco-walk010.png"
105             "marco-walk011.png"
106             "marco-walk012.png"
107             "marco-walk013.png"
108             "marco-walk014.png"
109         } >>images
110         0 >>frame
111         float-4{ -3 0 0 1 } >>center
112         float-4{ 0.75 1.0 1.0 1.0 } >>size
113         float-4{ 1 0 0 0 } >>orient
114         float-4{ 1 1 1 1 } >>color
115         {
116             {
117                 T{ animation-frame f  0 1 }
118             }
119             {
120                 T{ animation-frame f  3 2 }
121                 T{ animation-frame f  4 2 }
122                 T{ animation-frame f  5 2 }
123                 T{ animation-frame f  6 2 }
124                 T{ animation-frame f  7 2 }
125                 T{ animation-frame f  8 2 }
126                 T{ animation-frame f  9 2 }
127                 T{ animation-frame f 10 2 }
128                 T{ animation-frame f 11 2 }
129                 T{ animation-frame f 12 2 }
130                 T{ animation-frame f 13 2 }
131                 T{ animation-frame f 14 2 }
132                 T{ animation-frame f  1 2 }
133                 T{ animation-frame f  2 2 }
134             }
135         } swap set-up-sprite
136         dup update-slab-matrix
137
138     4array ;
139
140 : load-images ( -- images atlas )
141     "vocab:papier/_resources" load-papier-images ;
142
143 TYPED: prepare-world-slabs ( world: papier-world -- )
144     [ dup slabs>> slabs-by-name >>slabs-by-name drop ]
145     [ [ slabs>> ] [ slab-images>> ] bi update-slabs-for-atlas ]
146     [ [ uniforms>> atlas>> 0 ] [ atlas>> ] bi allocate-texture-image ] tri ;
147
148 : dim4 ( world -- dim ) dim>> first2 0 0 float-4-boa ; inline
149
150 M: papier-world begin-game-world
151     init-gpu
152     set-papier-state
153
154     <papier-renderer> >>renderer
155     load-slabs >>slabs
156     load-images [ >>slab-images ] [ >>atlas ] bi*
157
158     papier-uniforms new
159         over dim4 fov near-plane far-plane <p-matrix> >>p_matrix
160         eye >>eye
161         RGBA ubyte-components T{ texture-parameters
162             { min-mipmap-filter f }
163         } <texture-2d> >>atlas
164     >>uniforms
165     
166     prepare-world-slabs ;
167
168 : move-eye ( world amount -- )
169     [ uniforms>> ] dip '[ _ v+ ] change-eye drop ; inline
170
171 : keyboard-input ( papier-world -- movement/f face/f )
172     read-keyboard keys>> {
173         { [ key-left-arrow  over nth ] [ 2drop float-4{ $ move-rate 0 0 0 } vneg float-4{ 0 0 1 0 } ] }
174         { [ key-right-arrow over nth ] [ 2drop float-4{ $ move-rate 0 0 0 }      float-4{ 1 0 0 0 } ] }
175         { [ key-escape      over nth ] [ drop close-window f f ] }
176         [ 2drop f f ]
177     } cond ;
178
179 : update-slabs ( slabs -- )
180     [ inc-sprite drop ] each ;
181
182 : move-player ( world move face -- )
183     [ slabs-by-name>> "marco" swap at dup animations>> second switch-animation ] 2dip
184     [ '[ _ v+ ] change-center ] [ >>orient ] bi* update-slab-matrix ;
185
186 : stop-player ( world -- )
187     slabs-by-name>> "marco" swap at dup animations>> first switch-animation drop ;
188
189 M: papier-world tick-game-world
190     dup slabs>> update-slabs
191     dup focused?>> [
192         dup keyboard-input
193         [ move-player ]
194         [ drop stop-player ] if*
195     ] [ drop ] if ;
196
197 M: papier-world draw-world*
198     [ renderer>> ] [ uniforms>> ] [ slabs>> ] tri draw-slabs ;
199
200 M: papier-world resize-world
201     [ uniforms>> ]
202     [ dim4 fov near-plane far-plane <p-matrix> ] bi
203     >>p_matrix drop ;
204
205 GAME: papier-game {
206         { world-class papier-world }
207         { title "Papier" }
208         { pixel-format-attributes {
209             windowed
210             double-buffered
211             T{ depth-bits { value 24 } }
212         } }
213         { use-game-input? t }
214         { pref-dim { 1024 768 } }
215         { tick-interval-nanos $[ 24 fps ] }
216     } ;