]> gitweb.factorcode.org Git - factor.git/commitdiff
cocoa backend support for ui.pixel-formats; world-pixel-format-attributes generic
authorJoe Groff <arcata@gmail.com>
Fri, 1 May 2009 14:09:38 +0000 (09:09 -0500)
committerJoe Groff <arcata@gmail.com>
Fri, 1 May 2009 14:09:38 +0000 (09:09 -0500)
basis/cocoa/views/views.factor
basis/ui/backend/cocoa/cocoa.factor
basis/ui/backend/cocoa/views/views.factor
basis/ui/gadgets/worlds/worlds.factor
basis/ui/pixel-formats/pixel-formats.factor

index 69d0f9861808df309ae9a5c7fceaa04f5079e9cc..f65fddac58edcb2726b7128deb789f0c334872cd 100644 (file)
@@ -42,22 +42,10 @@ CONSTANT: NSOpenGLPFAAllowOfflineRenderers 96
 CONSTANT: NSOpenGLPFAVirtualScreenCount 128
 CONSTANT: NSOpenGLCPSwapInterval 222
 
-: <PixelFormat> ( attributes -- pixelfmt )
-    NSOpenGLPixelFormat -> alloc swap [
-        %
-        NSOpenGLPFADepthSize , 16 ,
-        ! NSOpenGLPFARendererID , kCGLRendererGenericFloatID ,
-        ! NSOpenGLPFASupersample ,
-        ! NSOpenGLPFASampleBuffers , 1 ,
-        ! NSOpenGLPFASamples , 8 ,
-        0 ,
-    ] int-array{ } make
-    -> initWithAttributes:
-    -> autorelease ;
-
-: <GLView> ( class dim -- view )
-    [ -> alloc 0 0 ] dip first2 <CGRect>
-    NSOpenGLPFAWindow NSOpenGLPFADoubleBuffer 2array <PixelFormat>
+: <GLView> ( class dim pixel-format -- view )
+    [ -> alloc ]
+    [ [ 0 0 ] dip first2 <CGRect> ]
+    [ handle>> ] tri*
     -> initWithFrame:pixelFormat:
     dup 1 -> setPostsBoundsChangedNotifications:
     dup 1 -> setPostsFrameChangedNotifications: ;
index 362305c8f70a4a32cdcf793babfaadd934bfe883..fbc713ac9a435659f9260788d6915b970ab6ac36 100755 (executable)
@@ -1,14 +1,15 @@
 ! Copyright (C) 2006, 2009 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors math arrays assocs cocoa cocoa.application
-command-line kernel memory namespaces cocoa.messages
+command-line kernel memory namespaces cocoa.messages classes
 cocoa.runtime cocoa.subclassing cocoa.pasteboard cocoa.types
 cocoa.windows cocoa.classes cocoa.nibs sequences ui ui.private
 ui.backend ui.clipboards ui.gadgets ui.gadgets.worlds
 ui.backend.cocoa.views core-foundation core-foundation.run-loop
 core-graphics.types threads math.rectangles fry libc
 generalizations alien.c-types cocoa.views
-combinators io.thread locals ;
+combinators io.thread locals ui.pixel-formats
+specialized-arrays.int literals core-graphics ;
 IN: ui.backend.cocoa
 
 TUPLE: handle ;
@@ -20,6 +21,57 @@ C: <offscreen-handle> offscreen-handle
 
 SINGLETON: cocoa-ui-backend
 
+<PRIVATE
+
+GENERIC: >NSOpenGLPFA ( attribute -- NSOpenGLPFAs )
+
+CONSTANT: attribute>NSOpenGLPFA-map H{
+    { double-buffered { $ NSOpenGLPFADoubleBuffer } }
+    { stereo { $ NSOpenGLPFAStereo } }
+    { offscreen { $ NSOpenGLPFAOffScreen } }
+    { fullscreen { $ NSOpenGLPFAFullScreen } }
+    { windowed { $ NSOpenGLPFAWindow } }
+    { accelerated { $ NSOpenGLPFAAccelerated } }
+    { software-rendered { $ NSOpenGLPFASingleRenderer $ kCGLRendererGenericFloatID } }
+    { robust { $ NSOpenGLPFARobust } }
+    { backing-store { $ NSOpenGLPFABackingStore } }
+    { multisampled { $ NSOpenGLPFAMultisample } }
+    { supersampled { $ NSOpenGLPFASupersample } }
+    { sample-alpha { $ NSOpenGLPFASampleAlpha } }
+    { color-float { $ NSOpenGLPFAColorFloat } }
+    { color-bits { $ NSOpenGLPFAColorSize } }
+    { alpha-bits { $ NSOpenGLPFAAlphaSize } }
+    { accum-bits { $ NSOpenGLPFAAccumSize } }
+    { depth-bits { $ NSOpenGLPFADepthSize } }
+    { stencil-bits { $ NSOpenGLPFAStencilSize } }
+    { aux-buffers { $ NSOpenGLPFAAuxBuffers } }
+    { sample-buffers { $ NSOpenGLPFASampleBuffers } }
+    { samples { $ NSOpenGLPFASamples } }
+}
+
+M: object >NSOpenGLPFA
+    drop { } ;
+M: symbol >NSOpenGLPFA
+    attribute>NSOpenGLPFA-map at [ { } ] unless* ;
+M: pixel-format-attribute >NSOpenGLPFA
+    dup class attribute>NSOpenGLPFA-map at
+    [ swap value>> suffix ]
+    [ drop { } ] if ;
+
+PRIVATE>
+
+M: cocoa-ui-backend (make-pixel-format)
+    [ >NSOpenGLPFA ] map concat >int-array
+    NSOpenGLPixelFormat -> alloc swap -> initWithAttributes: ;
+
+M: cocoa-ui-backend (free-pixel-format)
+    -> release ;
+
+M: cocoa-ui-backend (pixel-format-attribute)
+    attribute>NSOpenGLPFA-map at
+    [ first 0 <int> [ swap 0 -> getValues:forAttribute:forVirtualScreen: ] keep *int ]
+    [ f ] if* ;
+
 TUPLE: pasteboard handle ;
 
 C: <pasteboard> pasteboard
@@ -70,7 +122,7 @@ M: cocoa-ui-backend fullscreen* ( world -- ? )
     handle>> view>> -> isInFullScreenMode zero? not ;
 
 M:: cocoa-ui-backend (open-window) ( world -- )
-    world dim>> <FactorView> :> view
+    [ [ dim>> ] dip <FactorView> ] with-world-pixel-format :> view
     view world world>NSRect <ViewWindow> :> window
     view -> release
     world view register-window
@@ -97,18 +149,19 @@ M: cocoa-ui-backend raise-window* ( world -- )
     ] when* ;
 
 : pixel-size ( pixel-format -- size )
-    0 <int> [ NSOpenGLPFAColorSize 0 -> getValues:forAttribute:forVirtualScreen: ]
-    keep *int -3 shift ;
+    color-bits pixel-format-attribute -3 shift ;
 
 : offscreen-buffer ( world pixel-format -- alien w h pitch )
     [ dim>> first2 ] [ pixel-size ] bi*
     { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } 3cleave ;
 
-: gadget-offscreen-context ( world -- context buffer )
-    NSOpenGLPFAOffScreen 1array <PixelFormat>
-    [ nip NSOpenGLContext -> alloc swap f -> initWithFormat:shareContext: dup ]
-    [ offscreen-buffer ] 2bi
-    4 npick [ -> setOffScreen:width:height:rowbytes: ] dip ;
+:: gadget-offscreen-context ( world -- context buffer )
+    world world-pixel-format-attributes offscreen suffix
+    <pixel-format> [
+        :> pf
+        NSOpenGLContext -> alloc pf handle>> f -> initWithFormat:shareContext:
+        dup world pf offscreen-buffer -> setOffScreen:width:height:rowbytes:
+    ] with-disposal ;
 
 M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
     dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
index 602c9bec73c188e2a6d0656870dcd11c8534ac4c..4a16e3bd376b809679e1137ddbc35bcf17cf1838 100644 (file)
@@ -365,8 +365,8 @@ CLASS: {
     -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 <int>
     CGLSetParameter drop ;
 
-: <FactorView> ( dim -- view )
-    FactorView swap <GLView> [ sync-refresh-to-screen ] keep ;
+: <FactorView> ( dim pixel-format -- view )
+    [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
 
 : save-position ( world window -- )
     -> frame CGRect-top-left 2array >>window-loc drop ;
index a186de76709cbe4197514c2f26f243665de07083..d4162fa6306cbfcf7170ee1327f360446662a2f0 100644 (file)
@@ -4,7 +4,7 @@ USING: accessors arrays assocs continuations kernel math models
 namespaces opengl opengl.textures sequences io combinators
 combinators.short-circuit fry math.vectors math.rectangles cache
 ui.gadgets ui.gestures ui.render ui.backend ui.gadgets.tracks
-ui.commands ;
+ui.commands ui.pixel-formats destructors ;
 IN: ui.gadgets.worlds
 
 TUPLE: world < track
@@ -149,3 +149,12 @@ M: world handle-gesture ( gesture gadget -- ? )
 
 : close-global ( world global -- )
     [ get-global find-world eq? ] keep '[ f _ set-global ] when ;
+
+GENERIC: world-pixel-format-attributes ( world -- attributes )
+
+M: world world-pixel-format-attributes
+    { windowed double-buffered T{ depth-bits { value 16 } } } ;
+
+: with-world-pixel-format ( world quot -- )
+    [ dup world-pixel-format-attributes <pixel-format> ]
+    dip with-disposal ; inline
index 09450f2c7266807be0ff1271ba45d5d9cbd81d52..66de98ea1cb74bc30aecd3dce64a699ac88def0d 100644 (file)
@@ -1,7 +1,7 @@
 USING: destructors math ui.backend ;
 IN: ui.pixel-formats
 
-SINGLETONS:
+SYMBOLS:
     double-buffered
     stereo
     offscreen