Hijacking NSGraphicsContext - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Hijacking NSGraphicsContext (/thread-5749.html)
Hijacking NSGraphicsContext - tigakub - Mar 18, 2005 09:38 PM
Hi sorry if this is noobish.
Is there a way to "redirect" the drawing of an NSView from the windows graphics context to my own bitmap context?
The reason I want to do this is I want to capture all drawing done by a standard NSTextView into a bitmap without letting any of it go to the screen.
I've tried subclassing NSGraphicsContext and using NSView's allocateGState and overriding its setupGState. I'm not entirely sure when I should make my custom NSGraphicsContext the current context. Should I be overriding [NSView lockFocus] and [NSView unlockFocus] to call [NSGraphicsContext setCurrentContext: myContext] and [NSGraphicsContext setCurrentContext: prevContext]?
Does anyone have any insight into how the AppKit works at this level? There doesn't seem to be much documentation out there on this. Or should I be looking at capturing the backstore while somehow preventing it from being flushed to the screen?
What is the best/correct way of doing this?
Hijacking NSGraphicsContext - arekkusu - Mar 18, 2005 10:04 PM
You can -lockFocus on an NSImage or NSBitmapImageRep directly. Try searching cocoabuilder for "bitmap lockFocus".
However, if you want to do any kind of animation this way (i.e. render to bitmap, manipulate bitmap, draw bitmap to screen, repeat) forget about using NSImage. This is one of the really dumb parts of Cocoa, NSBitmapImageReps are changed into NSCachedImageReps as soon as you draw them and you can no longer get your modified bytes back out. You can use CoreGraphics bitmap contexts instead, it just makes for uglier code.
Hijacking NSGraphicsContext - tigakub - Mar 18, 2005 11:19 PM
Thanks! This might just work! I'll give it a try.
I don't really intend to use NSImage to draw the image to the screen, but to use glTexSubImage2D to copy it to an OpenGL texture. So I don't think I'll be touching the AppKit blting routines at all.
I'd actually prefer to use Core Graphics because I get the feeling that the AppKit is just an extra layer over it anyway. But I don't want to reproduce the functionality of say NSTextView, or the other nifty widgets.
I'm already using CG bitmap contexts to draw Quartz graphics into OpenGL textures, but I can't figure out how to marry CG bitmap contexts with NSGraphicsContexts in the NSView heirarchy.
Hijacking NSGraphicsContext - arekkusu - Mar 22, 2005 11:55 AM
Since there is now a public blurb about it, I can tell you that Tiger has new support for redirecting NSView drawing like you originally asked about.
Hijacking NSGraphicsContext - tigakub - Mar 25, 2005 12:18 AM
arekkusu Wrote:Since there is now a public blurb about it, I can tell you that Tiger has new support for redirecting NSView drawing like you originally asked about.
Excellent. Looks like Apple thought of everything ... again.