]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/images/images.factor
core: subseq-index? -> subseq-of?
[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 images.loader
4 kernel math 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" subseq-of? >>2x? ] bi ;
16
17 PRIVATE>
18
19 GENERIC: cached-image ( image -- image )
20
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 ;
25
26 M: image cached-image ;
27
28 <PRIVATE
29
30 : image-texture-cache ( world -- texture-cache )
31     [ [ <cache-assoc> ] unless* ] change-images images>> ;
32
33 PRIVATE>
34
35 : rendered-image ( image -- texture )
36     world get image-texture-cache
37     [ cached-image { 0 0 } <texture> ] cache ;
38
39 : draw-image ( image -- )
40     rendered-image draw-texture ;
41
42 : draw-scaled-image ( dim image -- )
43     rendered-image draw-scaled-texture ;
44
45 : image-dim ( image -- dim )
46     cached-image [ dim>> ] [ 2x?>> [ [ 2 / ] map ] when ] bi ;
47
48 {
49     { [ os macosx? ] [ "images.loader.cocoa" require ] }
50     { [ os windows?  ] [ "images.loader.gdiplus" require ] }
51     { [ os { freebsd } member? ] [
52         "images.png" require
53         "images.tiff" require
54     ] }
55     [ "images.loader.gtk" require ]
56 } cond