planetis-m / naylib

Yet another raylib Nim wrapper
MIT License
227 stars 10 forks source link

Use enums and sets #53

Closed planetis-m closed 1 year ago

planetis-m commented 1 year ago

There are workarounds for each issue, it's time to try this.

planetis-m commented 1 year ago

Works fine for ConfigFlags

type
  ConfigFlags* = enum
    Invalid,
    FullscreenMode, ## Set to run program in fullscreen
    WindowResizable, ## Set to allow resizable window
    WindowUndecorated, ## Set to disable window decoration (frame and buttons)
    WindowTransparent, ## Set to allow transparent framebuffer
    Msaa4xHint, ## Set to try enabling MSAA 4X
    VsyncHint, ## Set to try enabling V-Sync on GPU
    WindowHidden, ## Set to hide window
    WindowAlwaysRun, ## Set to allow windows running while minimized
    WindowMinimized, ## Set to minimize window (iconify)
    WindowMaximized, ## Set to maximize window (expanded to monitor)
    WindowUnfocused, ## Set to window non focused
    WindowTopmost, ## Set to window always on top
    WindowHighdpi, ## Set to support HighDPI
    WindowMousePassthrough, ## Set to support mouse passthrough, only supported when FLAG_WINDOW_UNDECORATED
    InterlacedHint, ## Set to try enabling interlaced video format (for V3D)

proc isWindowState*(flag: ConfigFlags): bool =
  ## Check if one specific window flag is enabled
  echo cast[int32]({flag})

proc setWindowState*(flags: set[ConfigFlags]) =
  ## Set window configuration state using flags (only PLATFORM_DESKTOP)
  echo cast[int32](flags)

proc clearWindowState*(flags: set[ConfigFlags]) =
  ## Clear window configuration state flags
  echo cast[int32](flags)

proc setConfigFlags*(flags: set[ConfigFlags]) =
  ## Setup init configuration flags (view FLAGS)
  echo cast[int32](flags)

discard isWindowState(FullscreenMode)
setWindowState({FullscreenMode, VsyncHint})
planetis-m commented 1 year ago

Duplicate enum values are covered:

type
  MaterialMap = enum
    Diffuse, Metalness

  ShaderLocationIndex = enum
    MapDiffuse, MapMetalness

template Albedo(_: typedesc[MaterialMap]): untyped = Diffuse
template Specular(_: typedesc[MaterialMap]): untyped = Metalness

template MapAlbedo(_: typedesc[ShaderLocationIndex]): untyped = MapDiffuse
template MapSpecular(_: typedesc[ShaderLocationIndex]): untyped = MapMetalness
planetis-m commented 1 year ago

This is totally wrong and can cause issues:

type
  Gesture* {.size: sizeof(int32).} = enum
    # None, ## No gesture
    Tap, ## Tap gesture
    Doubletap, ## Double tap gesture
    Hold, ## Hold gesture
    Drag, ## Drag gesture
    SwipeRight, ## Swipe right gesture
    SwipeLeft, ## Swipe left gesture
    SwipeUp, ## Swipe up gesture
    SwipeDown, ## Swipe down gesture
    PinchIn, ## Pinch in gesture
    PinchOut, ## Pinch out gesture

echo sizeof(set[Gesture]) # 2!
planetis-m commented 1 year ago

Seems to work

type
  Gesture* {.size: sizeof(int32).} = enum
    None, ## No gesture
    Tap, ## Tap gesture
    Doubletap, ## Double tap gesture
    Hold, ## Hold gesture
    Drag, ## Drag gesture
    SwipeRight, ## Swipe right gesture
    SwipeLeft, ## Swipe left gesture
    SwipeUp, ## Swipe up gesture
    SwipeDown, ## Swipe down gesture
    PinchIn, ## Pinch in gesture
    PinchOut, ## Pinch out gesture

proc setGesturesEnabled*(flags: set[Gesture]) =
  ## Enable a set of gestures using flags
  echo (cast[uint32](flags) shr 1)

proc isGestureDetected*(gesture: Gesture): bool =
  ## Check if a gesture have been detected
  echo (cast[uint32]({gesture}) shr 1) # <- wrong type!

proc getGestureDetected*(): Gesture =
  ## Get latest detected gesture
  result = cast[Gesture](8 shr 1)
  echo result

setGesturesEnabled({Doubletap, Drag}) # 10
setGesturesEnabled({PinchIn}) # 256
discard getGestureDetected() # Drag
planetis-m commented 1 year ago
    T3_ |=((NU16)(1)<<(((gesture__mscA6huwlzfOgXKBuE5ncg))%(sizeof(NU16)*8)));
    colontmpD_ = dollar___systemZdollars_14(((NU64) ((NU32)((NU32)(((NU32) (T3_))) >> (NU64)(((NI)1))))));

So many ops to support sets, it is not worth it imo. Better use enums and flags[enum]

planetis-m commented 1 year ago

Done