2

I'm building a simple game in python using OpenGL.

I built my logic through classes, the classes generate the information and pass it to OpenGL.

Unfortunatly, OpenGL is using callback and I don't know how to use my class methods with this process.

class Drawer(object):
    level = 0
    def main_draw():
        ...
        glutInit() 
        glutTimerFunc(interval, update, 0)          
        glutDisplayFunc(self.draw) #<----- here's my trouble
        glutIdleFunc(self.draw) #<----- here's my trouble
        ...

    def draw():
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()
        refresh2d_custom(width, height, field_width, field_height)

        #TODO
        draw_walls()

        glutSwapBuffers()

I don't think a static method would work since my class would have multiple instances.

If I use functions directly, which seems the easiest solution so far, how would I pass an instance to a specific file?

#draw.py

#instance that has been pass to this file. It is not instanciated in draw.py
#level would be global in this file
level = MyGameInstance.level 
2
  • 1
    It is unclear what you're asking. You already seem to have answered your own question, just pass it self.draw and it'll call that. If you need any more information, just store it in the Drawer object. P.S. your main_draw and draw functions need to take self arguments (since they're not static/class methods). Commented Jul 6, 2015 at 20:56
  • thanks @CrazyCasta I had it right, just forgot the self in methods. Thanks Commented Jul 6, 2015 at 21:03

1 Answer 1

1

Add the self parameter to your object's functions and you can access your object when it gets called back. The self parameter will be required to get that to run anyway since that's a non-static method (and therefore requires at least one parameter).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.