However there is one thing I don't quite understand. In my view delegate I got this function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | - (void)drawRect:(NSRect)dirtyRect { CGContextRef gctx = [[NSGraphicsContext currentContext] graphicsPort]; CGRect myBoundingBox; myBoundingBox = CGRectMake(0, 0, GlobalBackbuffer.BitmapWidth, GlobalBackbuffer.BitmapHeight); CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); int bitmapBytesPerRow = GlobalBackbuffer.BitmapWidth * 4; _backBuffer = CGBitmapContextCreate(GlobalBackbuffer.BitmapMemory, GlobalBackbuffer.BitmapWidth, GlobalBackbuffer.BitmapHeight, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); CGImageRef backImage = CGBitmapContextCreateImage(_backBuffer); CGContextDrawImage(gctx, myBoundingBox, backImage); CGContextRelease(_backBuffer); CGImageRelease(backImage); } |
This takes a pointer to the bitmap buffer creates a bitmap context, creates a image out of that context and draws it to the screen.
The code which actually controls the bitmap is invoked in the main run loop. I'm right know animating the buffer and it works without problems which leads me to the question "why"? I'm invoking the animation function as often as the run loop gets surpassed but I'm not calling the drawRect method every time after I altered the bitmap. I'm never ever calling it. Apple says that drawRect is called when the window thinks it should repaint the view. But how does it know when the bitmap has changed?
Is it because [view setNeedsDisplay:YES]; is called right after the animation function? All righty, YES it does!
Is there another place where I can put the drawing code? Like doing it in a more Windows way without sending a message to view that then calls draw rect? Like an update function that gets called after animation is done without the use of delegates?
However I read that drawRect ist the only place where you can actually draw stuff. Sounds stupid to me but its the same on windows with the paint message right?
Another thing, would it make sense to use Metal for the bitmap blit? It seems to be faster then OpenGL and since the rendering is done by software until much later in the project, Metal could increase performance a bit I guess?
Next problem, how is input handled by cocoa? The IOKit seems extremely complicated.
Maybe I will just implement the keyboard stuff first. Controllers seem extremely complicated.
Could you recommend resources for implementin keyboard/gamepad input?
I tried to understand this stuff from Jeff Buck's osx layer but daaaamn there is a lot of stuff going on.
EDIT: I implemented input through casual event handling. However I've seen people using IOKit for the Keyboard as well. Are there any advantages for using the IOKit?