#[SOLVED] 3D Cameras in Raylib - why do enums and IsKeyPressed chars do this in Odin?

1 messages · Page 1 of 1 (latest)

charred mauve
#

I'm trying to learn the basics of Odin by rewriting Raylib's 3D examples. You can see the 3D camera example I was trying to recreate HERE.

I wrote THIS CODE, but it doesn't work. My questions are

  1. It turns out I can get this to work by changing rl.CAMERA_PERSPECTIVE to .PERSPECTIVE. Reading the raylib source code says this parameter takes an "int", but passing 0 doesn't work either, so I guess it's an enum? Why do we write this as .PERSPECTIVE instead of something that starts with rl.CAMERA_?

2, The same happens for IsKeyPressed, where I have to write 'Z' as .Z instead. I assume it's related to the above. What is going on here?

  1. Optional but let me know if I handled Vector3s correctly. Not really sure if I should be writing rl.Vector3 before or after the equals sign in different scenarios

  2. Optional as well but can I specify a bit depth for float literals that I'm passing into raylib's functions? (For example, can I specify the 0.0s in some function calls to be 64-bit or 128-bit? Or is this always automatically handled)

Thanks!

tawdry mantle
#

(2) The Odin bindings probably accept an enum, and .Z is a short way to write for example MyKeyEnum.Z. If you want to use a character with that function, then maybe try casting it first to that enum type. Check the docs for the raylib package for what types are available.
(4) Probably f64(0.0) if you want something specific. The function will always accept a specific type though and you will have to cast to that type.

raw magnet
#

(3) Both are correct, : X = {} lets the compiler infer the struct type in the initializer {}, := X{} uses explicitly X instead of letting the compiler infer it

raw magnet
#

Enum.Z explicitly says to the compiler use Enum, .Z lets the compiler infer it from parameter types

hollow halo
raw magnet
wraith egret
#

To answer the first question "why". Because bindings want to behave like native/idiomatic Odin as much as possible, preferring bit_set over enums for flags, stripping unnecessary prefixes (due to enums in Odin are already scoped to the name of the type - unlike C), etc.

The code that you should look up for reference is the vendor/raylib/raylib.odin, not the C header or source code Raylib.

Example:

  • rl.CAMERA_FREE => rl.CameraMode.FREE or .FREE for short when passed as a parameter of type rl.CameraMode.
  • rl.CAMERA_PERSPECTIVE => rl.CameraProjection.PERSPECTIVE or .PERSPECTIVE for short when passed as a parameter of type rl.CameraProjection.
hollow halo
#

hmm ols go to definition doesn't work for me for libraries out of the box

raw magnet
clear knot
# charred mauve I'm trying to learn the basics of Odin by rewriting Raylib's 3D examples. You ca...
  1. in C, enums don't have a namespace, which means every enum value is put into global namespace. i.e.
enum Foo {
  One,
  Two,
  Three
}

puts One, Two, and Three in the global namespace. For this reason, it's common to prefix the values with the name of the enum, like Foo_One,Foo_Two, Foo_Three. In odin, enums do have a namespace, which means you'd have Foo.Foo_One, which is redundant, so the names are usually changed. Additionally, it seems like raylib doesn't use an enum for this at all for some reason. In C that doesn't make a big difference, but it was probably turned into an enum for the odin bindings, because it makes the code writing nicer. As for why you can type .PERSPECTIVE instead of rl.CameraProjection.PERSPECTIVE, that's basic type inference. Odin knows that the type is an rl.CameraProjection, so it can infer that .PERSPECTIVE is a member of that enum.

  1. In Odin, enums are more "first class" than in C, so you can't implicitly convert from a character to an enum value. Still, that enum has the same value, so you should be able to cast it, like rl.KeyboardKey('Z')

  2. where you use Name : Type = Value or Name := Typed_Initializer doesn't matter in most cases, though if you're not declaring a new variable, the type can be inferred, i.e. camera.position = {10,10,10}. Note that using 10 is fine, because of odin's "Untyped Types" system

  3. Odin doesn't support 128-bit floats. The size of the floats is defined by raylib, that's baked into the bindings. Changing the size of a value would require recompiling the code. Usually, a C float is an odin f32, and a C double is an odin f64. Raylib uses f32 for almost everything, and it's fine to use something like 0 for that because of untyped types

charred mauve
#

Understood, thank you everyone!

#

[SOLVED] 3D Cameras in Raylib - why do enums and IsKeyPressed chars do this in Odin?