]> gitweb.factorcode.org Git - factor.git/commitdiff
raylib.ffi.util: Provide convenience for defining array accesses
authortimor <timor.dd@googlemail.com>
Sun, 23 May 2021 10:39:41 +0000 (12:39 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 23 May 2021 22:54:17 +0000 (15:54 -0700)
This abstracts the pattern that raylib structures store arrays as pointers with
an associated length slot

extra/raylib/ffi/ffi.factor
extra/raylib/ffi/util/util.factor [new file with mode: 0644]

index 68fb229bf2e73fe72fcd58d31408b44a6e9602ff..b7537b5fb631740cd53bc4f560b24d020a8629cf 100644 (file)
@@ -4,7 +4,7 @@
 ! Most of the comments are included from the original header
 ! for your convenience.
 USING: accessors alien alien.c-types alien.destructors alien.libraries
-alien.syntax classes.struct combinators kernel math sequences
+alien.syntax classes.struct combinators kernel raylib.ffi.util sequences
 sequences.private system vocabs ;
 IN: raylib.ffi
 FROM: alien.c-types => float ;
@@ -185,10 +185,10 @@ STRUCT: BoundingBox
 STRUCT: Mesh
     { vertexCount int }   ! Number of vertices stored in arrays
     { triangleCount int } ! Number of triangles stored (indexed or not )
-    { texcoords float* }  ! Vertex texture coordinates (UV - 2 components per vertex )
-    { vertices float* }  ! Vertex position (XYZ - 3 components per vertex)
+    { _vertices float* }  ! Vertex position (XYZ - 3 components per vertex)
+    { _texcoords float* }  ! Vertex texture coordinates (UV - 2 components per vertex )
     { texcoords2 float* } ! Vertex second texture coordinates (useful for lightmaps)
-    { normals float* }    ! Vertex normals (XYZ - 3 components per vertex)
+    { _normals float* }    ! Vertex normals (XYZ - 3 components per vertex)
     { tangents float* }   ! Vertex tangents (XYZW - 4 components per vertex )
     { colors uchar* }     ! Vertex colors (RGBA - 4 components per vertex)
     { indices ushort* }   ! Vertex indices (in case vertex data comes indexed)
@@ -199,6 +199,9 @@ STRUCT: Mesh
     { vaoId uint }        ! OpenGL Vertex Array Object id
     { vboId uint* } ;     ! OpenGL Vertex Buffer Objects id (7  types of vertex data)
 
+ARRAY-SLOT: Mesh Vector3 _vertices [ vertexCount>> ] vertices
+ARRAY-SLOT: Mesh Vector2 _texcoords [ vertexCount>> ] texcoords
+ARRAY-SLOT: Mesh Vector3 _normals [ vertexCount>> ] normals
 
 STRUCT: Shader
     { id uint }              ! Shader program id
@@ -210,10 +213,12 @@ STRUCT: MaterialMap
     { color Color }          ! Material map color
     { value float } ;        ! Material map value
 
+CONSTANT: MAX_MATERIAL_MAPS 12 ! NOTE: This seems to be a compile-time constant!
 STRUCT: Material
     { shader Shader }        ! Material shader
-    { maps MaterialMap* } ! Material maps.  Uses MAX_MATERIAL_MAPS.
+    { _maps MaterialMap* }    ! Material maps.  Uses MAX_MATERIAL_MAPS.
     { params float* } ;      ! Material generic parameters (if required)
+ARRAY-SLOT: Material MaterialMap _maps [ drop 12 ] maps
 
 STRUCT: Transform
     { translation Vector3 }
@@ -235,18 +240,23 @@ STRUCT: Model
     { transform Matrix }
     { meshCount int }
     { materialCount int }
-    { meshes Mesh* }
-    { materials Material* }
+    { _meshes Mesh* }
+    { _materials Material* }
     { meshMaterial int* }
     { boneCount int }
-    { bones BoneInfo* }
+    { _bones BoneInfo* }
     { bindPose Transform* } ;
 
+ARRAY-SLOT: Model Material _materials [ materialCount>> ] materials
+ARRAY-SLOT: Model Mesh _meshes [ meshCount>> ] meshes
+ARRAY-SLOT: Model BoneInfo _bones [ boneCount>> ] bones
+
 STRUCT: ModelAnimation
     { boneCount int }
-    { bones BoneInfo* }
+    { _bones BoneInfo* }
     { frameCount int }
     { framePoses Transform** } ;
+ARRAY-SLOT: ModelAnimation BoneInfo _bones [ boneCount>> ] bones
 
 STRUCT: Ray
     { position Vector3 }    ! Ray position (origin)
diff --git a/extra/raylib/ffi/util/util.factor b/extra/raylib/ffi/util/util.factor
new file mode 100644 (file)
index 0000000..51ec86d
--- /dev/null
@@ -0,0 +1,30 @@
+USING: accessors alien alien.parser classes generic kernel lexer parser
+quotations sequences slots specialized-arrays specialized-arrays.private
+vocabs.parser words ;
+
+IN: raylib.ffi.util
+
+: array-word ( c-type -- str )
+    name>> "-array" append parse-word ;
+
+: array-direct-like ( c-type -- quot: ( alien len -- array ) )
+    array-word "prototype" word-prop '[ _ direct-like ] ;
+
+: ?use-vocab ( vocab -- )
+    dup using-vocab? [ drop ] [ use-vocab ] if ;
+
+: use-specialized-array ( c-type -- direct-constructor )
+    [ define-array-vocab ?use-vocab ]
+    [ array-direct-like ] bi ;
+
+! TODO: setter?
+
+:: define-array-slot ( struct-class element-type pointer-slot length-quot accessor -- )
+    element-type use-specialized-array :> cons-quot
+    accessor [ define-protocol-slot ] [ reader-word ] bi :> reader
+    struct-class reader create-method :> reader-method
+    pointer-slot reader-word 1quotation length-quot cons-quot
+    '[ [ @ >c-ptr ] _ bi @ ] reader-method swap define ;
+
+! ARRAY-SLOT: struct-class element-type-class pointer-slot length-quotation new-accessor
+SYNTAX: ARRAY-SLOT: scan-class scan-c-type scan-token scan-object quotation check-instance scan-token define-array-slot ;