]> gitweb.factorcode.org Git - factor.git/blob - extra/tetris/gl/gl.factor
Initial import
[factor.git] / extra / tetris / gl / gl.factor
1 ! Copyright (C) 2006, 2007 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences arrays math math.vectors namespaces
4 opengl opengl.gl ui.render ui.gadgets tetris.game tetris.board
5 tetris.piece tetris.tetromino ;
6 IN: tetris.gl
7
8 #! OpenGL rendering for tetris
9
10 : draw-block ( block -- )
11     dup { 1 1 } v+ 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 clone 0.2 3 pick set-nth gl-color draw-piece-blocks ;
21
22 ! TODO: move implementation specific stuff into tetris-board
23 : (draw-row) ( x y row -- )
24     >r over r> nth dup
25     [ gl-color 2array draw-block ] [ 3drop ] if ;
26
27 : draw-row ( y row -- )
28     dup length -rot [ (draw-row) ] 2curry each ;
29
30 : draw-board ( board -- )
31     board-rows dup length swap
32     [ dupd nth draw-row ] curry each ;
33
34 : scale-tetris ( width height tetris -- )
35     [ board-width swap ] keep board-height / -rot / swap 1 glScalef ;
36
37 : (draw-tetris) ( width height tetris -- )
38     #! width and height are in pixels
39     GL_MODELVIEW [
40         [ scale-tetris ] keep
41         dup tetris-board draw-board
42         dup tetris-next-piece draw-next-piece
43         tetris-current-piece draw-piece
44     ] do-matrix ;
45
46 : draw-tetris ( width height tetris -- )
47     origin get [ (draw-tetris) ] with-translation ;