2008-05-10 17:22:12 -04:00
|
|
|
USING: cairo cairo.lib ui.render kernel opengl.gl opengl
|
2008-05-03 12:59:59 -04:00
|
|
|
math byte-arrays ui.gadgets accessors arrays
|
2008-05-03 17:30:41 -04:00
|
|
|
namespaces io.backend ;
|
2008-05-03 12:59:59 -04:00
|
|
|
|
2008-05-03 17:30:41 -04:00
|
|
|
IN: cairo.gadgets
|
|
|
|
|
|
|
|
! We need two kinds of gadgets:
|
|
|
|
! one performs the cairo ops once and caches the bytes, the other
|
|
|
|
! performs cairo ops every refresh
|
2008-05-03 12:59:59 -04:00
|
|
|
|
2008-05-10 17:22:12 -04:00
|
|
|
TUPLE: cairo-gadget width height quot cache? bytes ;
|
|
|
|
PREDICATE: cached-cairo < cairo-gadget cache?>> ;
|
2008-05-03 12:59:59 -04:00
|
|
|
: <cairo-gadget> ( width height quot -- cairo-gadget )
|
|
|
|
cairo-gadget construct-gadget
|
|
|
|
swap >>quot
|
|
|
|
swap >>height
|
|
|
|
swap >>width ;
|
|
|
|
|
2008-05-10 17:22:12 -04:00
|
|
|
: <cached-cairo> ( width height quot -- cairo-gadget )
|
|
|
|
<cairo-gadget> t >>cache? ;
|
2008-05-03 12:59:59 -04:00
|
|
|
|
2008-05-10 17:22:12 -04:00
|
|
|
: width>stride ( width -- stride ) 4 * ;
|
|
|
|
|
|
|
|
: copy-cairo ( width height quot -- byte-array )
|
|
|
|
>r over width>stride
|
2008-05-03 12:59:59 -04:00
|
|
|
[ * nip <byte-array> dup CAIRO_FORMAT_ARGB32 ]
|
|
|
|
[ cairo_image_surface_create_for_data ] 3bi
|
2008-05-10 17:22:12 -04:00
|
|
|
r> with-cairo-from-surface ;
|
|
|
|
|
|
|
|
: (cairo>bytes) ( gadget -- byte-array )
|
|
|
|
[ width>> ] [ height>> ] [ quot>> ] tri copy-cairo ;
|
|
|
|
|
|
|
|
GENERIC: cairo>bytes
|
|
|
|
M: cairo-gadget cairo>bytes ( gadget -- byte-array )
|
|
|
|
(cairo>bytes) ;
|
2008-05-03 12:59:59 -04:00
|
|
|
|
2008-05-10 17:22:12 -04:00
|
|
|
M: cached-cairo cairo>bytes ( gadget -- byte-array )
|
|
|
|
dup bytes>> [ ] [
|
|
|
|
dup (cairo>bytes) [ >>bytes drop ] keep
|
|
|
|
] ?if ;
|
|
|
|
|
|
|
|
: cairo>png ( gadget path -- )
|
|
|
|
>r [ cairo>bytes CAIRO_FORMAT_ARGB32 ] [ width>> ]
|
|
|
|
[ height>> ] tri over width>stride
|
|
|
|
cairo_image_surface_create_for_data
|
|
|
|
r> [ cairo_surface_write_to_png check-cairo ] curry with-surface ;
|
2008-05-03 16:44:39 -04:00
|
|
|
|
2008-05-03 12:59:59 -04:00
|
|
|
M: cairo-gadget draw-gadget* ( gadget -- )
|
|
|
|
origin get [
|
|
|
|
0 0 glRasterPos2i
|
|
|
|
1.0 -1.0 glPixelZoom
|
2008-05-10 17:22:12 -04:00
|
|
|
[ width>> ] [ height>> GL_BGRA GL_UNSIGNED_BYTE ]
|
|
|
|
[ cairo>bytes ] tri glDrawPixels
|
2008-05-03 12:59:59 -04:00
|
|
|
] with-translation ;
|
2008-05-03 17:30:41 -04:00
|
|
|
|
2008-05-03 12:59:59 -04:00
|
|
|
M: cairo-gadget pref-dim* ( gadget -- rect )
|
2008-05-03 17:30:41 -04:00
|
|
|
[ width>> ] [ height>> ] bi 2array ;
|
|
|
|
|
2008-05-10 17:22:12 -04:00
|
|
|
: copy-surface ( surface -- )
|
|
|
|
cr swap 0 0 cairo_set_source_surface
|
|
|
|
cr cairo_paint ;
|
2008-05-03 17:30:41 -04:00
|
|
|
|
2008-05-10 17:22:12 -04:00
|
|
|
: <bytes-gadget> ( width height bytes -- cairo-gadget )
|
|
|
|
>r [ ] <cached-cairo> r> >>bytes ;
|
2008-05-03 17:30:41 -04:00
|
|
|
|
|
|
|
: <png-gadget> ( path -- gadget )
|
|
|
|
normalize-path cairo_image_surface_create_from_png
|
2008-05-10 17:22:12 -04:00
|
|
|
[ cairo_image_surface_get_width ]
|
|
|
|
[ cairo_image_surface_get_height 2dup ]
|
|
|
|
[ [ copy-surface ] curry copy-cairo ] tri
|
|
|
|
<bytes-gadget> ;
|