factor/extra/tetris/gl/gl.factor

49 lines
1.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2006, 2007, 2008 Alex Chapman
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
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 ;
2007-09-20 18:09:08 -04:00
IN: tetris.gl
#! OpenGL rendering for tetris
: draw-block ( block -- )
[ { 1 1 } gl-fill-rect ] with-translation ;
2007-09-20 18:09:08 -04:00
: draw-piece-blocks ( piece -- )
piece-blocks [ draw-block ] each ;
: draw-piece ( piece -- )
dup tetromino>> colour>> gl-color draw-piece-blocks ;
2007-09-20 18:09:08 -04:00
: draw-next-piece ( piece -- )
dup tetromino>> colour>>
clone 0.2 >>alpha gl-color draw-piece-blocks ;
2007-09-20 18:09:08 -04:00
! TODO: move implementation specific stuff into tetris-board
: (draw-row) ( x y row -- )
>r over r> nth dup
[ gl-color 2array draw-block ] [ 3drop ] if ;
2007-09-20 18:09:08 -04:00
: draw-row ( y row -- )
dup length -rot [ (draw-row) ] 2curry each ;
: draw-board ( board -- )
rows>> dup length swap
2007-09-20 18:09:08 -04:00
[ dupd nth draw-row ] curry each ;
: scale-board ( width height board -- )
[ width>> ] [ height>> ] bi swapd [ / ] dup 2bi* 1 glScalef ;
2007-09-20 18:09:08 -04:00
: (draw-tetris) ( width height tetris -- )
#! width and height are in pixels
GL_MODELVIEW [
{
[ board>> scale-board ]
[ board>> draw-board ]
[ next-piece draw-next-piece ]
[ current-piece draw-piece ]
} cleave
2007-09-20 18:09:08 -04:00
] do-matrix ;
: draw-tetris ( width height tetris -- )
origin get [ (draw-tetris) ] with-translation ;