]> gitweb.factorcode.org Git - factor.git/blob - extra/tetris/gl/gl.factor
Fix comments to be ! not #!.
[factor.git] / extra / tetris / gl / gl.factor
1 ! Copyright (C) 2006, 2007, 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators kernel math math.vectors
4 namespaces opengl opengl.gl sequences tetris.board tetris.game
5 tetris.piece ui.render tetris.tetromino ui.gadgets colors ;
6 IN: tetris.gl
7
8 #! OpenGL rendering for tetris
9
10 : draw-block ( block -- )
11     { 1 1 } gl-fill-rect ;
12
13 : draw-piece-blocks ( piece -- )
14     piece-blocks [ draw-block ] each ;
15
16 : draw-piece ( piece -- )
17     dup tetromino>> colour>> gl-color draw-piece-blocks ;
18
19 : draw-next-piece ( piece -- )
20     dup tetromino>> colour>>
21     >rgba-components drop 0.2 <rgba> gl-color draw-piece-blocks ;
22
23 ! TODO: move implementation specific stuff into tetris-board
24 : (draw-row) ( x y row -- )
25     [ over ] dip nth dup
26     [ gl-color 2array draw-block ] [ 3drop ] if ;
27
28 : draw-row ( y row -- )
29     [ length iota swap ] keep [ (draw-row) ] 2curry each ;
30
31 : draw-board ( board -- )
32     rows>> [ length iota ] keep
33     [ dupd nth draw-row ] curry each ;
34
35 : scale-board ( width height board -- )
36     [ width>> ] [ height>> ] bi swapd [ / ] dup 2bi* 1 glScalef ;
37
38 : draw-tetris ( width height tetris -- )
39     ! width and height are in pixels
40     [
41         {
42             [ board>> scale-board ]
43             [ board>> draw-board ]
44             [ next-piece draw-next-piece ]
45             [ current-piece draw-piece ]
46         } cleave
47     ] do-matrix ;