Add texture aging
parent
72f63d3e4c
commit
b7c4f548f9
|
@ -114,7 +114,7 @@ TUPLE: typographic-bounds width ascent descent leading ;
|
||||||
[ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
|
[ CTLineGetTypographicBounds ] 3keep [ *CGFloat ] tri@
|
||||||
typographic-bounds boa ;
|
typographic-bounds boa ;
|
||||||
|
|
||||||
TUPLE: line string font line bounds dim bitmap age disposed ;
|
TUPLE: line string font line bounds dim bitmap age refs disposed ;
|
||||||
|
|
||||||
: bounds>dim ( bounds -- dim )
|
: bounds>dim ( bounds -- dim )
|
||||||
[ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
|
[ width>> ] [ [ ascent>> ] [ descent>> ] bi + ] bi
|
||||||
|
@ -131,31 +131,36 @@ TUPLE: line string font line bounds dim bitmap age disposed ;
|
||||||
2dup white <CTLine> |CFRelease
|
2dup white <CTLine> |CFRelease
|
||||||
dup line-typographic-bounds
|
dup line-typographic-bounds
|
||||||
dup bounds>dim 3dup [ draw-line ] with-bitmap-context
|
dup bounds>dim 3dup [ draw-line ] with-bitmap-context
|
||||||
0 f line boa
|
0 0 f line boa
|
||||||
] with-destructors ;
|
] with-destructors ;
|
||||||
|
|
||||||
M: line dispose*
|
M: line dispose* line>> CFRelease ;
|
||||||
[ font>> ] [ line>> ] bi 2array dispose-each ;
|
|
||||||
|
|
||||||
<PRIVATE
|
: ref/unref-line ( line n -- )
|
||||||
|
'[ _ + ] change-refs 0 >>age drop ;
|
||||||
|
|
||||||
MEMO: (cached-line) ( string font -- line ) <line> ;
|
: ref-line ( line -- ) 1 ref/unref-line ;
|
||||||
|
: unref-line ( line -- ) -1 ref/unref-line ;
|
||||||
|
|
||||||
: cached-lines ( -- assoc )
|
SYMBOL: cached-lines
|
||||||
\ (cached-line) "memoize" word-prop ;
|
|
||||||
|
|
||||||
: set-cached-lines ( assoc -- )
|
: cached-line ( string font -- line )
|
||||||
\ (cached-line) "memoize" set-word-prop ;
|
cached-lines get [ <line> ] 2cache ;
|
||||||
|
|
||||||
CONSTANT: max-line-age 5
|
CONSTANT: max-line-age 5
|
||||||
|
|
||||||
PRIVATE>
|
: age ( obj -- ? )
|
||||||
|
[ 1+ ] change-age age>> max-line-age >= ;
|
||||||
|
|
||||||
|
: age-line ( line -- ? )
|
||||||
|
#! Outputs t whether the line is dead.
|
||||||
|
dup refs>> 0 = [ age ] [ drop f ] if ;
|
||||||
|
|
||||||
|
: age-assoc ( assoc quot -- assoc' )
|
||||||
|
'[ nip @ ] assoc-partition
|
||||||
|
[ values dispose-each ] dip ;
|
||||||
|
|
||||||
: age-lines ( -- )
|
: age-lines ( -- )
|
||||||
cached-lines
|
cached-lines global [ [ age-line ] age-assoc ] change-at ;
|
||||||
[ nip [ 1+ ] change-age age>> max-line-age <= ] assoc-filter
|
|
||||||
set-cached-lines ;
|
|
||||||
|
|
||||||
: cached-line ( string font -- line ) (cached-line) 0 >>age ;
|
[ H{ } clone cached-lines set-global ] "core-text" add-init-hook
|
||||||
|
|
||||||
[ \ (cached-line) reset-memoized ] "core-text" add-init-hook
|
|
|
@ -1,9 +1,9 @@
|
||||||
! Copyright (C) 2005, 2008 Slava Pestov.
|
! Copyright (C) 2005, 2009 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors arrays assocs continuations kernel math models
|
USING: accessors arrays assocs continuations kernel math models
|
||||||
namespaces opengl sequences io combinators fry math.vectors
|
namespaces opengl sequences io combinators combinators.short-circuit
|
||||||
ui.gadgets ui.gestures ui.render ui.backend ui.gadgets.tracks
|
fry math.vectors ui.gadgets ui.gestures ui.render ui.text ui.text.private
|
||||||
math.geometry.rect ;
|
ui.backend ui.gadgets.tracks math.geometry.rect ;
|
||||||
IN: ui.gadgets.worlds
|
IN: ui.gadgets.worlds
|
||||||
|
|
||||||
TUPLE: world < track
|
TUPLE: world < track
|
||||||
|
@ -54,9 +54,7 @@ M: world request-focus-on ( child gadget -- )
|
||||||
|
|
||||||
M: world layout*
|
M: world layout*
|
||||||
dup call-next-method
|
dup call-next-method
|
||||||
dup glass>> [
|
dup glass>> dup [ swap dim>> >>dim drop ] [ 2drop ] if ;
|
||||||
[ dup rect-dim ] dip (>>dim)
|
|
||||||
] when* drop ;
|
|
||||||
|
|
||||||
M: world focusable-child* gadget-child ;
|
M: world focusable-child* gadget-child ;
|
||||||
|
|
||||||
|
@ -64,13 +62,13 @@ M: world children-on nip children>> ;
|
||||||
|
|
||||||
: (draw-world) ( world -- )
|
: (draw-world) ( world -- )
|
||||||
dup handle>> [
|
dup handle>> [
|
||||||
[ dup init-gl ] keep draw-gadget
|
[ init-gl ] [ draw-gadget ] [ finish-text-rendering ] tri
|
||||||
] with-gl-context ;
|
] with-gl-context ;
|
||||||
|
|
||||||
: draw-world? ( world -- ? )
|
: draw-world? ( world -- ? )
|
||||||
#! We don't draw deactivated worlds, or those with 0 size.
|
#! We don't draw deactivated worlds, or those with 0 size.
|
||||||
#! On Windows, the latter case results in GL errors.
|
#! On Windows, the latter case results in GL errors.
|
||||||
[ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] tri and and ;
|
{ [ active?>> ] [ handle>> ] [ dim>> [ 0 > ] all? ] } 1&& ;
|
||||||
|
|
||||||
TUPLE: world-error error world ;
|
TUPLE: world-error error world ;
|
||||||
|
|
||||||
|
@ -86,16 +84,12 @@ ui-error-hook global [ [ rethrow ] or ] change-at
|
||||||
: draw-world ( world -- )
|
: draw-world ( world -- )
|
||||||
dup draw-world? [
|
dup draw-world? [
|
||||||
dup world [
|
dup world [
|
||||||
[
|
[ (draw-world) ] [
|
||||||
(draw-world)
|
|
||||||
] [
|
|
||||||
over <world-error> ui-error
|
over <world-error> ui-error
|
||||||
f >>active? drop
|
f >>active? drop
|
||||||
] recover
|
] recover
|
||||||
] with-variable
|
] with-variable
|
||||||
] [
|
] [ drop ] if ;
|
||||||
drop
|
|
||||||
] if ;
|
|
||||||
|
|
||||||
world H{
|
world H{
|
||||||
{ T{ key-down f { C+ } "x" } [ T{ cut-action } send-action ] }
|
{ T{ key-down f { C+ } "x" } [ T{ cut-action } send-action ] }
|
||||||
|
|
|
@ -18,17 +18,19 @@ SYMBOL: viewport-translation
|
||||||
|
|
||||||
: do-clip ( -- ) clip get flip-rect gl-set-clip ;
|
: do-clip ( -- ) clip get flip-rect gl-set-clip ;
|
||||||
|
|
||||||
: init-clip ( clip-rect rect -- )
|
: init-clip ( clip-rect -- )
|
||||||
GL_SCISSOR_TEST glEnable
|
[
|
||||||
[ rect-intersect ] keep
|
dim>>
|
||||||
dim>> dup { 0 1 } v* viewport-translation set
|
[ { 0 1 } v* viewport-translation set ]
|
||||||
{ 0 0 } over gl-viewport
|
[ [ { 0 0 } ] dip gl-viewport ]
|
||||||
0 swap first2 0 gluOrtho2D
|
[ [ 0 ] dip first2 0 gluOrtho2D ] tri
|
||||||
clip set
|
]
|
||||||
|
[ clip set ] bi
|
||||||
do-clip ;
|
do-clip ;
|
||||||
|
|
||||||
: init-gl ( clip-rect rect -- )
|
: init-gl ( clip-rect -- )
|
||||||
GL_SMOOTH glShadeModel
|
GL_SMOOTH glShadeModel
|
||||||
|
GL_SCISSOR_TEST glEnable
|
||||||
GL_BLEND glEnable
|
GL_BLEND glEnable
|
||||||
GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
|
GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
|
||||||
GL_VERTEX_ARRAY glEnableClientState
|
GL_VERTEX_ARRAY glEnableClientState
|
||||||
|
|
|
@ -51,13 +51,24 @@ M: core-text-renderer string-dim
|
||||||
TUPLE: line-texture line texture age disposed ;
|
TUPLE: line-texture line texture age disposed ;
|
||||||
|
|
||||||
: <line-texture> ( line -- texture )
|
: <line-texture> ( line -- texture )
|
||||||
dup [ dim>> ] [ bitmap>> ] bi GL_RGBA make-texture
|
#! Note: we only ref-line if make-texture succeeds
|
||||||
0 f \ line-texture boa ;
|
[
|
||||||
|
dup [ dim>> ] [ bitmap>> ] bi GL_RGBA make-texture
|
||||||
|
0 f \ line-texture boa
|
||||||
|
] keep ref-line ;
|
||||||
|
|
||||||
M: line-texture dispose* texture>> delete-texture ;
|
M: line-texture dispose*
|
||||||
|
[ line>> unref-line ]
|
||||||
|
[ texture>> delete-texture ] bi ;
|
||||||
|
|
||||||
: line-texture ( string open-font -- texture )
|
: line-texture ( string open-font -- texture )
|
||||||
world get fonts>> [ cached-line <line-texture> ] 2cache ;
|
world get fonts>> [ cached-line <line-texture> ] 2cache 0 >>age ;
|
||||||
|
|
||||||
|
: age-line-textures ( world -- )
|
||||||
|
[ [ age ] age-assoc ] change-fonts drop ;
|
||||||
|
|
||||||
|
M: core-text-renderer finish-text-rendering
|
||||||
|
age-line-textures age-lines ;
|
||||||
|
|
||||||
: draw-line-texture ( line-texture -- )
|
: draw-line-texture ( line-texture -- )
|
||||||
GL_TEXTURE_2D [
|
GL_TEXTURE_2D [
|
||||||
|
|
|
@ -11,6 +11,10 @@ TUPLE: font name size bold? italic? ;
|
||||||
|
|
||||||
SYMBOL: font-renderer
|
SYMBOL: font-renderer
|
||||||
|
|
||||||
|
HOOK: finish-text-rendering font-renderer ( world -- )
|
||||||
|
|
||||||
|
M: object finish-text-rendering drop ;
|
||||||
|
|
||||||
HOOK: open-font font-renderer ( font -- open-font )
|
HOOK: open-font font-renderer ( font -- open-font )
|
||||||
|
|
||||||
HOOK: string-dim font-renderer ( open-font string -- dim )
|
HOOK: string-dim font-renderer ( open-font string -- dim )
|
||||||
|
|
Loading…
Reference in New Issue