]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/opengl/opengl.factor
basis: use lint.vocabs tool to trim using lists
[factor.git] / basis / opengl / opengl.factor
index 26a3410677c3ca9e4554c98da0ed720c41ddcb64..4f1910eee5a3c24fac278278e253cb7271010a86 100644 (file)
@@ -3,9 +3,9 @@
 ! Portions copyright (C) 2008 Joe Groff.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: alien alien.c-types alien.data assocs colors
-combinators.smart continuations fry init io kernel locals macros
-math math.parser namespaces opengl.gl sequences
-sequences.generalizations specialized-arrays words ;
+combinators.smart continuations io kernel math
+math.functions math.parser namespaces opengl.gl sequences
+sequences.generalizations specialized-arrays system words ;
 FROM: alien.c-types => float ;
 SPECIALIZED-ARRAY: float
 SPECIALIZED-ARRAY: uint
@@ -40,20 +40,32 @@ TUPLE: gl-error-tuple function code string ;
 : gl-error-code ( -- code/f )
     glGetError dup 0 = [ drop f ] when ; inline
 
+: throw-gl-error? ( -- ? )
+    os macosx? [
+        ! This is kind of terrible, but we are having
+        ! problems on Mac OS X 10.11 where the
+        ! default framebuffer seems to be initialized
+        ! asynchronously or something, so we should
+        ! just log these for now in (gl-error).
+        GL_FRAMEBUFFER glCheckFramebufferStatus
+        GL_FRAMEBUFFER_UNDEFINED = not
+    ] [ t ] if ;
+
 : (gl-error) ( function -- )
-    gl-error-code [ <gl-error> throw ] [ drop ] if* ;
+    gl-error-code [
+        throw-gl-error? [
+            <gl-error> throw
+        ] [
+            [
+                [ number>string ] [ error>string ] bi ": " glue
+                "OpenGL error: " prepend print flush drop
+            ] with-global
+        ] if
+    ] [ drop ] if* ;
 
 : gl-error ( -- )
     f (gl-error) ; inline
 
-: gl-error-nonfatal ( -- )
-    gl-error-code [
-        [
-            [ number>string ] [ error>string ] bi ": " glue
-            "OpenGL error: " prepend print flush
-        ] with-global
-    ] when* ;
-
 : do-enabled ( what quot -- )
     over glEnable dip glDisable ; inline
 
@@ -104,9 +116,9 @@ MACRO: all-enabled-client-state ( seq quot -- quot )
     line-vertices GL_LINES 0 2 glDrawArrays ;
 
 :: (rect-vertices) ( loc dim -- vertices )
-    #! We use GL_LINE_STRIP with a duplicated first vertex
-    #! instead of GL_LINE_LOOP to work around a bug in Apple's
-    #! X3100 driver.
+    ! We use GL_LINE_STRIP with a duplicated first vertex
+    ! instead of GL_LINE_LOOP to work around a bug in Apple's
+    ! X3100 driver.
     loc first2 [ 0.3 + ] bi@ :> ( x y )
     dim first2 [ 0.6 - ] bi@ :> ( w h )
     [
@@ -162,7 +174,7 @@ MACRO: all-enabled-client-state ( seq quot -- quot )
 
 :: with-gl-buffer ( binding id quot -- )
     binding id glBindBuffer
-    quot [ binding 0 glBindBuffer ] [ ] cleanup ; inline
+    quot [ binding 0 glBindBuffer ] finally ; inline
 
 : with-array-element-buffers ( array-buffer element-buffer quot -- )
     [ GL_ELEMENT_ARRAY_BUFFER ] 2dip '[
@@ -177,7 +189,7 @@ MACRO: all-enabled-client-state ( seq quot -- quot )
 
 :: with-vertex-array ( id quot -- )
     id glBindVertexArray
-    quot [ 0 glBindVertexArray ] [ ] cleanup ; inline
+    quot [ 0 glBindVertexArray ] finally ; inline
 
 : <gl-buffer> ( target data hint -- id )
     pick gen-gl-buffer [
@@ -216,6 +228,15 @@ MACRO: set-draw-buffers ( buffers -- quot )
 : gl-unscale ( m -- n )
     gl-scale-factor get-global [ / ] when* ; inline
 
+: gl-floor ( m -- n )
+    gl-scale floor gl-unscale ; inline
+
+: gl-ceiling ( m -- n )
+    gl-scale ceiling gl-unscale ; inline
+
+: gl-round ( m -- n )
+    gl-scale round gl-unscale ; inline
+
 : fix-coordinates ( point1 point2 -- x1 y1 x2 y2 )
     [ first2 [ gl-scale >fixnum ] bi@ ] bi@ ;
 
@@ -226,10 +247,10 @@ MACRO: set-draw-buffers ( buffers -- quot )
     fix-coordinates glViewport ;
 
 : init-matrices ( -- )
-    #! Leaves with matrix mode GL_MODELVIEW
+    ! Leaves with matrix mode GL_MODELVIEW
     GL_PROJECTION glMatrixMode
     glLoadIdentity
     GL_MODELVIEW glMatrixMode
     glLoadIdentity ;
 
-[ f gl-scale-factor set-global ] "opengl" add-startup-hook
+STARTUP-HOOK: [ f gl-scale-factor set-global ]