1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs cache combinators images images.loader
4 kernel math namespaces opengl opengl.textures sequences
5 splitting system ui.gadgets.worlds vocabs ;
8 TUPLE: image-name path ;
10 C: <image-name> image-name
14 MEMO: cached-image-path ( path -- image )
15 [ load-image ] [ "@2x" find-subseq? >>2x? ] bi ;
19 GENERIC: cached-image ( image -- image )
21 M: image-name cached-image
22 path>> gl-scale-factor get-global [ 1.0 > ] [ f ] if* [
23 "." split1-last "@2x." glue
24 ] when cached-image-path ;
26 M: image cached-image ;
30 : image-texture-cache ( world -- texture-cache )
31 [ [ <cache-assoc> ] unless* ] change-images images>> ;
35 : rendered-image ( image -- texture )
36 world get image-texture-cache
37 [ cached-image { 0 0 } <texture> ] cache ;
39 : draw-image ( image -- )
40 rendered-image draw-texture ;
42 : draw-scaled-image ( dim image -- )
43 rendered-image draw-scaled-texture ;
45 : image-dim ( image -- dim )
46 cached-image [ dim>> ] [ 2x?>> [ [ 2 / ] map ] when ] bi ;
49 { [ os macosx? ] [ "images.loader.cocoa" require ] }
50 { [ os windows? ] [ "images.loader.gdiplus" require ] }
51 { [ os { freebsd } member? ] [
55 [ "images.loader.gtk" require ]