]> gitweb.factorcode.org Git - factor.git/blob - extra/tetris/gl/gl.factor
Merge branch 'master' of git://repo.or.cz/factor/jcg
[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 namespaces opengl opengl.gl sequences tetris.board tetris.game tetris.piece ui.render tetris.tetromino ui.gadgets ;
4 IN: tetris.gl
5
6 #! OpenGL rendering for tetris
7
8 : draw-block ( block -- )
9     dup { 1 1 } v+ gl-fill-rect ;
10
11 : draw-piece-blocks ( piece -- )
12     piece-blocks [ draw-block ] each ;
13
14 : draw-piece ( piece -- )
15     dup tetromino>> colour>> set-color draw-piece-blocks ;
16
17 : draw-next-piece ( piece -- )
18     dup tetromino>> colour>>
19     clone 0.2 >>alpha set-color draw-piece-blocks ;
20
21 ! TODO: move implementation specific stuff into tetris-board
22 : (draw-row) ( x y row -- )
23     >r over r> nth dup
24     [ set-color 2array draw-block ] [ 3drop ] if ;
25
26 : draw-row ( y row -- )
27     dup length -rot [ (draw-row) ] 2curry each ;
28
29 : draw-board ( board -- )
30     rows>> dup length swap
31     [ dupd nth draw-row ] curry each ;
32
33 : scale-board ( width height board -- )
34     [ width>> ] [ height>> ] bi swapd [ / ] dup 2bi* 1 glScalef ;
35
36 : (draw-tetris) ( width height tetris -- )
37     #! width and height are in pixels
38     GL_MODELVIEW [
39         {
40             [ board>> scale-board ]
41             [ board>> draw-board ]
42             [ next-piece draw-next-piece ]
43             [ current-piece draw-piece ]
44         } cleave
45     ] do-matrix ;
46
47 : draw-tetris ( width height tetris -- )
48     origin get [ (draw-tetris) ] with-translation ;