Navigation chapter - InputController

Good evening.

trying the Navigation chapter example on Mac M1 Max, I cannot get any output in the keyChangedHandler. did anyone have any issues with this?

import GameController

class InputController {
  static let shared = InputController()
    
    var keysPressed: Set<GCKeyCode> = []
    
    private init() {
        print("in init")
      let center = NotificationCenter.default
      center.addObserver(
        forName: .GCKeyboardDidConnect,
        object: nil,
        queue: nil) { notification in
            print("in observer")
          let keyboard = notification.object as? GCKeyboard
            if keyboard == nil {
                print("keyb is nil")
            }
            print("after let keyb")
            keyboard?.keyboardInput?.keyChangedHandler
              = { _, _, keyCode, pressed in
                  print ("in handler")
            if pressed {
                print("key pressed %d" , keyCode.rawValue)
              self.keysPressed.insert(keyCode)
            } else {
              self.keysPressed.remove(keyCode)
            }
          }
      }
    }

}


Hi @marek-kuticka and welcome to the forum :slightly_smiling_face:

I don’t have an M1 Max, but it works on my M1 mini. I wonder if it’s the built in keyboard.

Does the final or challenge sample code for the chapter work?

Do you have an external keyboard you could temporarily try?

1 Like

Thx a lot. Today I updated macbook to 12.5.1 And now it works. Before update, not even official sample did work…

2 Likes

That’s very weird. I’m glad it works now

1 Like

thx a lot. sorry for using this post, @caroline , I couldn’t find it anywhere else…
is there any tutorial, do you plan any, can you point me… HOW am I able to include text ( HUD like ) into any of the scenes from the Metal book? e.g. write out current actor location, camera vector, and similar. I am quite struggling to find anything to this topic. thx a lot, will create new topic if needed.

Marek

I tend to print to the debug console for those, but you can use SwiftUI as that’s pretty easy. You’d have to set up a structure containing the properties you’re interested in to pass between SwiftUI and your rendering code.

Later in the book, there are examples of using SwiftUI to send different rendering options to the renderer (eg deferred vs forward rendering, and Chapter 20, Fragment Post-Processing turns on and off different options.

thx for the reply. is it somehow possible to render that text as bitmap/texture? my aim was rather have this within rendering using Metal, using shaders, etc. so use the power of Metal, instead of relying on SwiftUI. only articles I have found talk about rendering text to bitmap and then displaying bitmap on top of viewport/screen

Metal is a graphics API and doesn’t know anything about text rendering.

Some of your options might be:

  1. Warren Moore wrote an article a while ago on rendering text using signed distance fields: https://metalbyexample.com/rendering-text-in-metal-with-signed-distance-fields/. It’s quite complex and uses Objective-C rather than Swift though.

  2. There is a full library for text called Dear ImGui:
    GitHub - ocornut/imgui: Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies. I personally haven’t used it, and it’s in C++, but I believe other people have implemented it for Metal.

  3. SpriteKit’s SKRenderer - you can create text nodes in SpriteKit and render text to a texture. Caveat - the first edition of Metal by Tutorials (available if you have a subscription) showed briefly how to do this, but it was dropped because it didn’t work in later versions of Xcode. So I don’t know if it works now, and I’d personally rather pop a SwiftUI layer on top of the Metal window.

  4. Check how other graphics APIs do it. Here’s an example of using FreeType in OpenGL: LearnOpenGL - Text Rendering

  5. Use SceneKit to create the mesh and then you can render the mesh in Metal: Text rendering in metal - Stack Overflow

  6. You can render text using CoreImage too: swift - How should I go about rendering text in Metal? - Stack Overflow

Edit: If you run Apple’s Modern Rendering Sample: Apple Developer Documentation, they do their stats with an NSTextField on top of the Metal view. Which would be the equivalent of using SwiftUI.