Hey all. I'm new to Apple's ecosystem and need some help with a few basics.
Specifically, I've started with Xcode 14's metal game code and am struggling to read user inputs. I've tried variations of the below without luck:
#import "GameViewController.h"
#import "Renderer.h"
@interface InputView : NSView
@end
@implementation InputView
- (BOOL)acceptsFirstResponder{ return YES; }
- (void)mouseDown:(NSEvent*)event
{
printf("mouse down");
}
- (void)keyDown:(NSEvent *)event
{
printf("key down");
}
@end
@implementation GameViewController
{
MTKView *_view;
InputView *_input_view;
Renderer *_renderer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_view = (MTKView *)self.view;
_view.device = MTLCreateSystemDefaultDevice();
if(!_view.device)
{
NSLog(@"Metal is not supported on this device");
self.view = [[NSView alloc] initWithFrame:self.view.frame];
return;
}
_renderer = [[Renderer alloc] initWithMetalKitView:_view];
[_renderer mtkView:_view drawableSizeWillChange:_view.bounds.size];
_view.delegate = _renderer;
_input_view = [[InputView alloc] initWithFrame:_view.frame];
BOOL fr = [_input_view becomeFirstResponder];
printf("fr %d\n", fr);
}
@end
But I also don't understand the basic structure of this boilerplate application. I see my main file is this:
#import <Cocoa/Cocoa.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
}
return NSApplicationMain(argc, argv);
}
It doesn't seem to touch any of the rest of the code (GameViewController, AppDelegate, etc.), and yet it obviously does, since it runs and renders things. I assume some build magic is happening, but where can I find information on that? What official resources can I read to understand how this app is being built? I've been having a lot of trouble navigating Apple's official documentation and could use some direction.
I think I've found a way through this after discovering this bit of text in the docs for NSViewController: In addition, in macOS 10.10 and later, a view controller participates in the responder chain. You can implement action methods directly in the view controller. Corresponding actions that originate in the view controller’s view proceed up the responder chain and are handled by those methods.
So I can just implement mouseDown etc. directly in the view controller and it just works! Great, but I'd still like to know why the other options didn't work and overall how the app is working. Nib files, storyboards, etc. -- is there a good overview somewhere?