class CameraWindow:
    '''A simple class to set up a pygame window
    
    A general design principle is that ALL openCV code should be encapsulated in
    classes such as the above.`'''

    writer = None

    def __init__(self, fname=None):
        '''Straightforward init

        fname :
            I think this needs to be *.avi
        '''
        # Set camera up first, as this is more likley to fail (I guess)
        self.cv_cam = CVCam()
        im = self.cv_cam.get_image()
        arr = self.cv_cam.conv2array(im)
        self.size = self.cv_cam.width, self.cv_cam.height
        if fname:
            self.writer = CVWriter(fname, self.size)

        # Then, we set up our graphical environment

        # SimpleVisionEgg is a custom set-up class I wrote to avoid boilerplate
        # code duplication. It's a little clunky, sadly.
        self.vision_egg = SimpleVisionEgg()
        screen_center = [x/2.0 for x in self.vision_egg.screen.size]
        tex_stim = TextureStimulus(mipmaps_enabled=False,
                                   texture=Texture(arr),
                                   size=self.size,
                                   # We shouldn't invoke OpenGL texture
                                   # filtering, as we take our texture size
                                   # directly from the camera.
                                   # the other option would be
                                   # GL_NEAREST
                                   texture_min_filter=GL.GL_LINEAR,
                                   position=screen_center,
                                   anchor='center')
        # This gives us programmatic access to the actual video memory (if
        # possible), so it should make things nice and fast. We still seem to be
        # down around 14 fps, though, on the Mac. We're not going to get much
        # faster on this front.
        self.tex_object = tex_stim.parameters.texture.get_texture_object()
        self.vision_egg.set_stimuli([tex_stim])


    def update_image(self, t):
        '''Grab an image from the camera and convert to a pygame.image
        
        I'm using my SimpleVisionEgg system, so I need to discard the time
        parameter `t` that gets passed in'''
        im = self.cv_cam.get_image()
        if self.writer:
            self.writer.write_im(im)
        arr = self.cv_cam.conv2array(im)
        # Note - we use .put_sub_image because we aren't using dimensions that
        # are a power of 2. There's a little more OpenGL magic going on somwhere
        # that I don't know about...
        self.tex_object.put_sub_image(arr)

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                # For some reason, using VisionEgg we don't exit cleanly and
                # finalize our writer (thus, the movie is corrupted)
                del self.writer
                del self.cv_cam
                self.vision_egg.quit()

    def run(self):
        '''Run forever (until we get a QUIT event)'''
        self.vision_egg.set_functions(update=self.update_image)
        self.vision_egg.go()