[Pyobjc-dev] PyObjC and NSOpenGLView
Brought to you by:
ronaldoussoren
From: Tobias W. <t.w...@im...> - 2010-01-03 15:46:53
|
Hello, First off, sorry if this is the wrong place to post this - I couldn't find a PyObjC users list. If there's a better place to post please let me know and I'll send it there. I've been trying to rewrite a game of mine using PyObjC - it's currently a horrible mess of Objective-C and C++ and I think PyObjC will be a much better infrastructure. Unfortunately I'm failing at the first hurdle - implementing a subclass of NSOpenGLView working by reimplementing my Objective-C version line-by-line in PyObjC. I've got two problems with it: 1) The NSOpenGLView seems to interfere with the drawing of the Window it's in. When I don't have the NSOpenGLView instantiated in Interface Builder, I'll get the standard grey/silver background in the window. Once I add the NSOpenGLView, the Window is suddenly filled in white (The OpenGLView is drawn correctly on top). 2) If I try to use the GLU function gluPerspective I get the following unexpected error (My subclass is called WorldView, code is at the end of this message): ValueError: NSInvalidArgumentException - Unlocking Focus on wrong view (<NSThemeFrame:0x2c11580>), expected <WorldView: 0x4e7850>) Should I not be subclassing NSOpenGLView? Should I be subclassing something else? I've included my current sub-class below. On a different note, when I started this project I was intending to write the interface mainly in Objective-C and the game engine in Python. However the PyObjC examples all seem to be geared towards writing everything in Python. Is there a recommended way to call Python-implemented objects from Objective-C code? The only way I could find to do it is something along the lines of: id someObject; Class PythonClass = NSClassFromString(@"PythonName"); someObject = [PythonClass new]; but I was wondering if there was any other way? Thanks in advance for any help, Toby Wood WorldView.py: from objc import YES, NO, IBAction, IBOutlet from Foundation import * from AppKit import * from OpenGL.GL import * from OpenGL.GLU import * class WorldView(NSOpenGLView): def awakeFromNib(self): NSLog(u'Awoke') def initWithFrame_(self, frame): self = super(WorldView, self).initWithFrame_(frame) if self: # initialization code here pass NSLog(u'init') return self def prepareOpenGL(self): NSLog(u'Prep') glClearColor(1.0,0.0,0.0,1.0) def reshape(self): NSLog(u'Reshape') rect = self.bounds() glViewport(0, 0, int(rect.size.width), int(rect.size.height)) if (rect.size.height == 0.0): rect.size.height == 1.0 aspect = rect.size.width/rect.size.height glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45.0, fAspect, 1., 1000.) # Fails on this line glMatrixMode(GL_MODELVIEW) glLoadIdentity() def drawRect_(self, rect): # drawing code here NSLog(u'Drawing') glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glFlush() |