Skip to content

the dynamic binding

Orfeo Da Vià edited this page Nov 2, 2021 · 1 revision

The dynamic binding requires no special configuration when using DUB to manage your project. There is no link-time dependency. At run time, the raylib shared library is required to be on the shared library search path of the user's system. On Windows, this is typically handled by distributing the raylib DLL with your program. On other systems, it usually means the user must install the raylib shared library through a package manager.

To load the shared library, you need to call the loadRaylib function. This returns a member of the RaylibSupport enumeration (see the README for bindbc.loader for the error handling API):

  • RaylibSupport.noLibrary indicating that the library failed to load (it couldn't be found)
  • RaylibSupport.badLibrary indicating that one or more symbols in the library failed to load
  • a member of RaylibSupport indicating a version number that matches the version of raylib that bindbc-raylib3 was configured at compile-time to load. j By default, that is RaylibSupport.raylib370, but can be configured via a version identifier (see below). This value will match the global manifest constant, raylibSupport.
import bindbc.raylib3;

void main(string[] args) {
   RaylibSupport retVal = loadRaylib();
   // raylibSupport is an enum with current raylib version
   if (retVal != raylibSupport) {
      // Handle error. For most use cases, its reasonable to use the the error handling API in
      // bindbc-loader to retrieve error messages for logging and then abort. If necessary, it's
      // possible to determine the root cause via the return value:
      if (ret == RaylibSupport.noLibrary) {
        // raylib shared library failed to load
       } else if (RaylibSupport.badLibrary) {
        // One or more symbols failed to load. The likely cause is that the
        // shared library is for a lower version than bindbc-raylib was configured
        // to load.
      }
   } else {
      // successful
   }
}
/*
This version attempts to load the raylib library using a user-supplied file name.
Usually, the name and/or path used will be platform specific, as in this example
which attempts to load `raylib.dll` from the `libs` subdirectory, relative
to the executable, only on Windows.
*/
// version(Windows) loadraylib("libs/raylib.dll")

By default, the bindbc-raylib binding is configured to load raylib 3.7.0. This behavior can be overridden via the -version compiler switch or the versions DUB directive with the desired raylib version number. In this next example, the raylib dynamic binding is compiled to support raylib 3.7.0:

dub.json

"dependencies": {
    "bindbc-raylib": "~>0.1.0"
},
"versions": ["RAYLIB_370"]

dub.sdl

dependency "bindbc-raylib" version="~>0.1.0"
versions "RAYLIB_370"

With this example configuration, raylibSupport == RaylibSupport.raylib370 on a successful load.

If raylib 3.7.0 or later is installed on the user's system, loadRaylib will return RaylibSupport.raylib370. If raylib 3.6.0 is found instead, loadRaylib will return RaylibSupport.badLibrary. In this scenario, calling loadedRaylibVersion() will return a RaylibSupport member indicating which version of raylib, if any, actually loaded. If a lower version was loaded, it's still possible to call functions from that version of raylib, but any calls to functions from higher versions will result in a null pointer access. For this reason, it's recommended to always specify your required version of the raylib library at compile time and abort when you receive an RaylibSupport.badLibrary return value from loadRaylib.

No matter which version was configured, the successfully loaded version can be obtained via a call to loadedRaylibVersion. It returns one of the following:

  • RaylibSupport.noLibrary if loadRaylib returned RaylibSupport.noLibrary
  • RaylibSupport.badLibrary if loadraylib returned RaylibSupport.badLibrary and no version of raylib successfully loaded
  • a member of RaylibSupport indicating the version of raylib that successfully loaded. When loadRaylib returns RaylibSupport.badLibrary, this will be a version number lower than that configured at compile time. Otherwise, it will be the same as the manifest constant raylibSupport.

The function isRaylibLoaded returns true if any version of raylib was successfully loaded and false otherwise.