1 ! Copyright (C) 2010 Erik Charlebois.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.data classes.struct destructors game.loop
4 game.worlds gpu gpu.effects.blur gpu.effects.step gpu.state
5 gpu.textures gpu.util images.loader kernel literals make math
6 math.vectors namespaces sequences specialized-arrays
7 ui.gadgets.worlds ui.gestures ui.pixel-formats ;
8 FROM: alien.c-types => float ;
9 SPECIALIZED-ARRAY: float
16 SPECIALIZED-ARRAY: particle_t
18 CONSTANT: gravity { 0.0 -0.1 }
20 :: verlet-integrate-particle ( particle dt -- particle' )
21 particle [ p>> ] [ v>> ] bi dt v*n v+
22 gravity dt dt * particle m>> 2 * / v*n v+ :> p'
23 p' particle p>> v- dt v/n :> v'
24 p' v' particle m>> particle_t boa ; inline
26 CONSTANT: initial-particles
28 S{ particle_t f float-array{ 0.5 0.6 } float-array{ 0 0.1 } 1.0 }
29 S{ particle_t f float-array{ 0.5 0.6 } float-array{ 0.1 0 } 3.0 }
31 S{ particle_t f float-array{ 0.5 0.5 } float-array{ 0.1 0.1 } 2.0 }
32 S{ particle_t f float-array{ 0.5 0.6 } float-array{ -0.1 0 } 1.0 }
33 S{ particle_t f float-array{ 0.6 0.5 } float-array{ 0 -0.1 } 3.0 }
34 S{ particle_t f float-array{ 0.7 0.5 } float-array{ 0.1 0.1 } 1.0 }
35 S{ particle_t f float-array{ 0.1 0.5 } float-array{ -0.1 -0.1 } 5.0 }
36 S{ particle_t f float-array{ 0.2 0.5 } float-array{ 0 0 } 1.0 }
37 S{ particle_t f float-array{ 0.3 0.3 } float-array{ 0 0 } 4.0 }
38 S{ particle_t f float-array{ 0.5 0.15 } float-array{ 0 0 } 1.0 }
39 S{ particle_t f float-array{ 0.5 0.1 } float-array{ 0 0 } 9.0 }
42 : integrate-particles! ( particles dt -- particles )
43 [ verlet-integrate-particle ] curry map! ;
45 TUPLE: fluids-world < game-world
46 particles texture ramp { paused boolean initial: f } ;
48 : make-texture ( pathname -- texture )
52 [ component-type>> ] bi
54 { wrap clamp-texcoord-to-edge }
55 { min-filter filter-nearest }
56 { mag-filter filter-nearest }
57 { min-mipmap-filter f } }
61 0 swap [ allocate-texture-image ] keepdd
66 : integrate ( world -- )
67 particles>> 1/60 integrate-particles! drop ;
70 fluid get [ not ] change-paused drop ;
73 fluid get paused>> [ fluid get integrate ] when ;
75 M: fluids-world begin-game-world
78 initial-particles clone >>particles
79 "vocab:fluids/particle2.pgm" make-texture >>texture
80 "vocab:fluids/colors.ppm" make-texture >>ramp
83 M: fluids-world end-game-world
86 M: fluids-world tick-game-world
87 dup paused>> [ drop ] [ integrate ] if ;
89 M:: fluids-world draw-world* ( world -- )
91 [ p>> [ first , ] [ second , ] bi ] each
92 ] curry float-array{ } make :> verts
95 verts world texture>> 30.0 world dim>> { 4 4 } v/
96 blended-point-sprite-batch &dispose
97 blend-state new set-gpu-state
98 gaussian-blur &dispose
99 world ramp>> world dim>> step-texture &dispose
100 world dim>> draw-texture
104 { world-class fluids-world }
105 { title "Fluids Test" }
106 { pixel-format-attributes {
107 windowed double-buffered T{ depth-bits { value 24 } } } }
108 { pref-dim { 1024 768 } }
109 { tick-interval-nanos $[ 60 fps ] }
113 { T{ button-down } [ [
114 hand-loc get float >c-array
115 world get dim>> float >c-array v/ 2 v*n 1 v-n { 1 -1 } v*
116 float-array{ 0 0.2 } 2.0 particle_t boa suffix
117 ] change-particles drop ] }