Skip to content

Instantly share code, notes, and snippets.

@gokr
Last active July 2, 2020 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokr/3ee7a0d75d42a54a01c35ca023612a8b to your computer and use it in GitHub Desktop.
Save gokr/3ee7a0d75d42a54a01c35ca023612a8b to your computer and use it in GitHub Desktop.
Nim variant for ORX sample code
# A Nim port of https://wiki.orx-project.org/en/tutorials/cameras/fixing_camera_to_object_by_parenting
# Nim has templates (macros) defined for the canonical ORX init, run, exit, execute.
init:
# For Create, CreateFromConfig and a very select few other functions,
# Norx keeps the prefix, but shortened to for example "viewport".
# For other ORX functions the module prefix is stripped and the first letter
# in the function name is lower cased. So `orxViewport_GetCamera` turns into
# `getCamera`.
#
# Nim has a module system that handles potential collisions between modules, so there
# is no need to "worry" about this, the Nim compiler will complain if there is disambiguity.
#
# For `var viewport` Nim will use type inference to find the type of viewport,
# in VSCode etc one can hover over the variable to see the inferred type.
# One can also declare types, then it would be `var viewport: ptr orxVIEWPORT = ...`.
#
# For the ORX C types like `orxCAMERA *` the corresponding C type in Nim is
# `ptr orxCAMERA`. A `ptr` is a plain C pointer and is normally not used in Nim
# unless you interact with C. The normal corresponding type for "references"
# is called `ref` and such references are garbage collected by Nim.
#
# Note that "Viewport" here is a Nim `string` (which is a bit more than a char pointer)
# but it happens to be fairly compatible with C so we can pass it "as it is" to the ORX function.
# Nim also has a type representing a C string called `cstring`.
var viewport = viewportCreateFromConfig("Viewport")
# Nim has "unified syntax" so you can let the first argument act as a "receiver".
# In other words, we can write both `getCamera(viewport)` and `viewport.getCamera()`.
# In fact, in this case we could even write `viewport.getCamera`.
var camera = viewport.getCamera()
# Nothing new here.
var cameraHandle = objectCreateFromConfig("CameraHandle")
# Same here, we can write either `setParent(camera, cameraHandle)` or
# `camera.setParent(cameraHandle)`.
camera.setParent(cameraHandle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment