Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m committed Oct 12, 2024
1 parent d424e39 commit b1f510c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/raylib.nim
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ type
boneMatrices: ptr UncheckedArray[Matrix] ## Bones animated transformation matrices
boneCount: int32 ## Number of bones
vaoId*: uint32 ## OpenGL Vertex Array Object id
vboId: ptr array[MaxMeshVertexBuffers, uint32] ## OpenGL Vertex Buffer Objects id (default vertex data)
vboId: ptr UncheckedArray[uint32] ## OpenGL Vertex Buffer Objects id (default vertex data)

Shader* {.importc, header: "raylib.h", bycopy.} = object ## Shader
id*: uint32 ## Shader program id
Expand All @@ -610,7 +610,7 @@ type

Material* {.importc, header: "raylib.h", bycopy.} = object ## Material, includes shader and maps
shader: Shader ## Material shader
maps: ptr array[MaxMaterialMaps, MaterialMap] ## Material maps array (MAX_MATERIAL_MAPS)
maps: ptr UncheckedArray[MaterialMap] ## Material maps array (MAX_MATERIAL_MAPS)
params*: array[4, float32] ## Material generic parameters (if required)

Transform* {.importc, header: "raylib.h", bycopy.} = object ## Transform, vertex transformation data
Expand Down
69 changes: 40 additions & 29 deletions wrapper/raylib_gen.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[algorithm, sets, tables, sequtils, strutils, strformat]
import std/[algorithm, sets, tables, sequtils, strutils]
when defined(nimPreviewSlimSystem):
from std/syncio import readFile
import schema, builder, ctypes, utils
Expand Down Expand Up @@ -81,8 +81,6 @@ const
"""
typeReplacements = toTable({
"Shader/locs": "ptr UncheckedArray[ShaderLocation]",
"Mesh/vboId": "ptr array[MaxMeshVertexBuffers, uint32]",
"Material/maps": "ptr array[MaxMaterialMaps, MaterialMap]",
"GenImageFontAtlas/glyphRecs": "ptr ptr UncheckedArray[Rectangle]",
"CheckCollisionLines/collisionPoint": "out Vector2",
"LoadImageAnim/frames": "out int32",
Expand Down Expand Up @@ -159,6 +157,8 @@ const
# Arrays in struct fields
"Font/recs",
"Font/glyphs",
"Material/maps",
"Mesh/vboId",
"Mesh/vertices",
"Mesh/texcoords",
"Mesh/texcoords2",
Expand Down Expand Up @@ -299,7 +299,29 @@ const
"UnloadModelAnimations",
"UnloadWaveSamples",
])
privateFuncs = toHashSet([
readOnlyFields = toHashSet([
"Font/glyphCount",
"Mesh/vertexCount",
"Mesh/triangleCount",
"Mesh/boneCount",
"Model/meshCount",
"Model/materialCount",
"Model/boneCount",
"ModelAnimation/boneCount",
"ModelAnimation/frameCount",
])
privateSymbols = toHashSet([
# Struct fields
"MaterialMap/texture",
"Material/shader",
"AudioStream/buffer",
"AudioStream/processor",
"AutomationEventList/events",
"AutomationEventList/count",
"AutomationEventList/capacity",
# Structs
"FilePathList",
# Functions
"MemAlloc",
"MemRealloc",
"MemFree",
Expand Down Expand Up @@ -515,46 +537,35 @@ proc getReplacement*(x, y: string): string =
proc isArray(x, y: string): bool =
(if y != "": x & "/" & y else: x) in arrayTypes

proc preprocessStructs(ctx: var ApiContext) =
proc isPrivateField(obj: StructInfo, fld: FieldInfo): bool =
(obj.name, fld.name) notin
{"Wave": "frameCount", "Sound": "frameCount", "Music": "frameCount"} and
fld.name.endsWith("Count")

proc shouldBePrivate(obj: StructInfo, fld: FieldInfo, isArray, isPrivate: bool): bool =
isPrivate or isArray or
obj.name == "AutomationEventList" or
(obj.name, fld.name) in {
"MaterialMap": "texture",
"Material": "shader",
"AudioStream": "buffer",
"AudioStream": "processor",
"Mesh": "vboId",
"Material": "maps",
}
proc isPrivateSymbol(x, y: string): bool =
(if y != "": x & "/" & y else: x) in privateSymbols

proc isReadOnlyField(x, y: string): bool =
(if y != "": x & "/" & y else: x) in readOnlyFields

proc preprocessStructs(ctx: var ApiContext) =
for obj in mitems(ctx.api.structs):
if obj.name in mangledSymbols:
obj.flags.incl isMangled
if obj.name in ["Color", "Vector2", "Vector3", "Vector4"]:
obj.flags.incl isCompleteStruct
if obj.name == "FilePathList":
if obj.name in privateSymbols:
obj.flags.incl isPrivate

for fld in mitems(obj.fields):
if isPrivateField(obj, fld):
if isPrivateSymbol(obj.name, fld.name) or isPrivate in obj.flags:
fld.flags.incl isPrivate
var fieldType = getReplacement(obj.name, fld.name)
let isArray = isArray(obj.name, fld.name)
var fieldType = getReplacement(obj.name, fld.name)
if fieldType == "":
fieldType = convertType(fld.`type`, if isArray: ptArray else: ptPtr)
if isPrivate in fld.flags:
if isReadOnlyField(obj.name, fld.name):
fld.flags.incl isPrivate
ctx.readOnlyFieldAccessors.add (struct: obj.name, field: fld.name, `type`: fieldType)
if (isArray or (obj.name, fld.name) in {"Mesh": "vboId", "Material": "maps"}) and
(obj.name, fld.name) != ("AutomationEventList", "events"):
if isArray and isPrivate notin fld.flags:
let tmp = capitalizeAscii(fld.name)
ctx.boundCheckedArrayAccessors.add (struct: obj.name & tmp, field: obj.name, `type`: "")
if shouldBePrivate(obj, fld, isArray, isPrivate in fld.flags + obj.flags):
if isArray:
fld.flags.incl isPrivate
fld.`type` = fieldType

Expand Down Expand Up @@ -622,7 +633,7 @@ proc preprocessFunctions(ctx: var ApiContext) =
if fnc.name in wrappedFuncs:
fnc.flags.incl isWrappedFunc
fnc.flags.incl isPrivate
if fnc.name in privateFuncs:
if fnc.name in privateSymbols:
fnc.flags.incl isPrivate
if fnc.name in noSideEffectsFuncs:
fnc.flags.incl isFunc
Expand Down

0 comments on commit b1f510c

Please sign in to comment.