]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/images/images.factor
ui.images: fix for gl-scale-factor not being set.
[factor.git] / basis / ui / images / images.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs cache combinators images.loader kernel
4 math memoize namespaces opengl opengl.textures sequences
5 splitting system ui.gadgets.worlds vocabs ;
6 IN: ui.images
7
8 TUPLE: image-name path ;
9
10 C: <image-name> image-name
11
12 <PRIVATE
13
14 MEMO: cached-image-path ( path -- image )
15     [ load-image ] [ "@2x" swap subseq? >>2x? ] bi ;
16
17 PRIVATE>
18
19 : cached-image ( image-name -- image )
20     path>> gl-scale-factor get-global [ 1.0 > ] [ f ] if* [
21         "." split1-last "@2x." glue
22     ] when cached-image-path ;
23
24 <PRIVATE
25
26 : image-texture-cache ( world -- texture-cache )
27     [ [ <cache-assoc> ] unless* ] change-images images>> ;
28
29 PRIVATE>
30
31 : rendered-image ( image-name -- texture )
32     world get image-texture-cache
33     [ cached-image { 0 0 } <texture> ] cache ;
34
35 : draw-image ( image-name -- )
36     rendered-image draw-texture ;
37
38 : draw-scaled-image ( dim image-name -- )
39     rendered-image draw-scaled-texture ;
40
41 : image-dim ( image-name -- dim )
42     cached-image [ dim>> ] [ 2x?>> [ [ 2 / ] map ] when ] bi ;
43
44 {
45     { [ os macosx? ] [ "images.loader.cocoa" require ] }
46     { [ os windows?  ] [ "images.loader.gdiplus" require ] }
47     { [ os { freebsd } member? ] [
48         "images.png" require
49         "images.tiff" require
50     ] }
51     [ "images.loader.gtk" require ]
52 } cond