Skip to content
This repository has been archived by the owner on Apr 15, 2023. It is now read-only.

Generate bindings from the JSON files #60

Closed
wants to merge 13 commits into from
Closed

Conversation

planetis-m
Copy link
Collaborator

No description provided.

@planetis-m
Copy link
Collaborator Author

planetis-m commented Jan 3, 2022

I don't know if you agree with the last change but I decided to produce distinct cint instead of enums. Reasons are the usual: duplicate values, enums with holes used as bitsets. The last straw for me was having to support MaterialMapDiffuse and ShaderLocDiffuse, without the prefixes there are name collisions...

@greenfork
Copy link
Owner

The last straw for me was having to support MaterialMapDiffuse and ShaderLocDiffuse, without the prefixes there are name collisions...

If I'm not mistaken, there are a bunch of #define statements in C file for a bunch of things. But they all serve only a backwards-compatibility purpose because of renames. I think we should strive for better Nim and put backwards-compatibility about such renames in the Changelog notes or just special case them. At least this is a strategy I was going for so far. Looks like it works fine, I don't get a lot of complaints when the changes are reasonable.

If we don't try to be fully backwards-compatible, are we okay with overloadable enums as discussed in #55?

@greenfork
Copy link
Owner

Saw latest changes, looks really great

@greenfork
Copy link
Owner

greenfork commented Feb 4, 2022

@planetis-m I found these functions which are not in the set of destructors in #60, do we include them as well or there are some problems?

unloadVrStereoConfig*(config: VrStereoConfig)
unloadFileData*(data: ptr uint8)
unloadFileText*(text: cstring)
unloadImageColors*(colors: ptr Color)
unloadImagePalette*(colors: ptr Color)
unloadFontData*(chars: ptr GlyphInfo; glyphCount: cint)
unloadCodepoints*(codepoints: ptr cint)
unloadWaveSamples*(samples: ptr cfloat)
  • unloadVrStereoConfig seems alright to add
  • unloadFileData and unloadFileText seem too general
  • unloadImageColors and unloadImagePalette can use distinct ptr Color for load... functions
  • unloadFontData can partially use raylib_fields.nim accessors like []?
  • unloadCodepoints could use distinct ptr cint and related to Can codepoints map to unicode.Rune #69
  • unloadWaveSamples could use distinct ptr cfloat

Distinct types will have to also convert other function signatures which use these types though. And it is less maintainable regarding our automation feature of c2nim. Maybe there are some tricks we could use to avoid a lot of text replacement and somehow do it more smartly.

Comment on lines +92 to +94
proc drawTexturePoly*(texture: Texture2D, center: Vector2, points: openarray[Vector2], texcoords: openarray[Vector2], tint: Color) =
## Draw a textured polygon
drawTexturePolyPriv(texture, center, cast[ptr UncheckedArray[Vector2]](points), cast[ptr UncheckedArray[Vector2]](texcoords), points.len.int32, tint)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really need to rename the original function to drawTexturePolyPriv, it can have same name

Comment on lines +56 to +62
proc loadFontData*(fileData: openarray[uint8], fontSize: int32, fontChars: openarray[int32], `type`: FontType): seq[GlyphInfo] =
## Load font data for further use
let data = loadFontDataPriv(cast[ptr UncheckedArray[uint8]](fileData), fileData.len.int32,
fontSize, cast[ptr UncheckedArray[int32]](fontChars), fontChars.len.int32, `type`)
result = newSeq[GlyphInfo](fontChars.len)
copyMem(result[0].addr, data, fontChars.len * sizeof(GlyphInfo))
memFree(data)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we can allow ourself doing newSeq because it does extra allocation. Allocations are generally considered slow and this here will allocate twice the amount of memory and do twice the number of calls to allocate.

As I understand it is also impossible to make a sequence without addition allocations because of GC. Also, what would be a use case for sequences which we want to modify? There are length checks already in raylib_fields.nim. Here I assume we need sequences to dynamically modify them, but why? We should also take into considerations that operations on sequences will allocate memory uncontrollably which is sometimes undesirable for games.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also a View type. I'm not sure about the progress on this. But it looks like a good option here. We can create a custom container which implements toOpenArray for each type as well as an option.

type GlyphInfoArray = object
  len: int,
  p: ptr UncheckedArray[GlyphInfo]

proc toOpenArray(obj: GlyphInfoArray) ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do that.

Copy link
Owner

@greenfork greenfork Feb 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, great! Do you want to discuss these options on the specific examples? It could make sense to try several ways of doing this conversion before rewriting the whole file to conform to the one specific variant. There's at least View and custom container options. But we want to minimize the amount of work we are going to do. And possibly automate it.

Regarding automation, it really seems that your version with generation from JSON is going to be better in terms of maintainability. But it's not entirely clear if this is the case.

@planetis-m
Copy link
Collaborator Author

  • unloadVrStereoConfig I removed it, its a noop for now.
  • unloadWaveSamples just a for loop that calls unloadWaveSample, since we used seq[Wave] it was not need.
  • unloadImageColors, unloadFontData, unloadImagePalette I think same as above
  • unloadFileData, unloadFileText it's hard to tell what to do with these, could we instead use std equivalents instead of LoadFile*
  • unloadCodepoints haven't decided what to do with codepoints yet.

@greenfork
Copy link
Owner

@planetis-m I made an attempt at using destructors #72

@greenfork
Copy link
Owner

Does raylib_fields.nim work now? There's a comment about .copy pragma and I'm not sure what's going to be the best fix for this file. Just adding .copy to each proc with var parameter?

@planetis-m
Copy link
Collaborator Author

They work, I used templates as this comment suggests https://forum.nim-lang.org/t/8871#57987 but for the [] proc the problem is still there, but it won't cause any issue. So it's good to go.

Just adding .copy to each proc with var parameter?

Not yet implemented but it's a good idea imo.

@greenfork
Copy link
Owner

On each type c2nim automatically adds bycopy pragma. I would assume that this would fix the same issue?

@planetis-m
Copy link
Collaborator Author

Yes but now .bycopy would only be added in the .importc procs and that would allow to write a native Nim procedure using .importc types without any issue.

@planetis-m planetis-m closed this Feb 11, 2023
@planetis-m planetis-m deleted the planetis-m-patch-1 branch February 11, 2023 10:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants