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 namespaces opengl opengl.gl sequences tetris.board tetris.game tetris.piece ui.render tetris.tetromino ui.gadgets ;
6 #! OpenGL rendering for tetris
8 : draw-block ( block -- )
9 [ { 1 1 } gl-fill-rect ] with-translation ;
11 : draw-piece-blocks ( piece -- )
12 piece-blocks [ draw-block ] each ;
14 : draw-piece ( piece -- )
15 dup tetromino>> colour>> gl-color draw-piece-blocks ;
17 : draw-next-piece ( piece -- )
18 dup tetromino>> colour>>
19 clone 0.2 >>alpha gl-color draw-piece-blocks ;
21 ! TODO: move implementation specific stuff into tetris-board
22 : (draw-row) ( x y row -- )
24 [ gl-color 2array draw-block ] [ 3drop ] if ;
26 : draw-row ( y row -- )
27 dup length -rot [ (draw-row) ] 2curry each ;
29 : draw-board ( board -- )
30 rows>> dup length swap
31 [ dupd nth draw-row ] curry each ;
33 : scale-board ( width height board -- )
34 [ width>> ] [ height>> ] bi swapd [ / ] dup 2bi* 1 glScalef ;
36 : (draw-tetris) ( width height tetris -- )
37 #! width and height are in pixels
40 [ board>> scale-board ]
41 [ board>> draw-board ]
42 [ next-piece draw-next-piece ]
43 [ current-piece draw-piece ]
47 : draw-tetris ( width height tetris -- )
48 origin get [ (draw-tetris) ] with-translation ;