#!/usr/bin/env python
"""mpeg.py - play MPEG movies in the Vision Egg

This demo uses pygame.movie to draw movies.  See also the quicktime.py
demo."""

import os
import VisionEgg
VisionEgg.start_default_logging(); VisionEgg.watch_exceptions()

from VisionEgg.Core import *
from VisionEgg.FlowControl import Presentation, Controller, FunctionController
from VisionEgg.Text import *
from VisionEgg.Textures import *
import sys
import pygame
import pygame.surface, pygame.locals
import pygame.movie
import OpenGL.GL as gl

if len(sys.argv) > 1:
    filename = sys.argv[1]
else:
    filename = os.path.join(VisionEgg.config.VISIONEGG_SYSTEM_DIR,"data","water.mpg")
    
movie = pygame.movie.Movie(filename)

screen = get_default_screen()

width, height = movie.get_size()
scale_x = screen.size[0]/float(width)
scale_y = screen.size[1]/float(height)
scale = min(scale_x,scale_y) # maintain aspect ratio

# create pygame surface (buffer to draw uncompressed movie data into)
pygame_surface = pygame.surface.Surface((width,height))

# tell the movie to render onto the surface
movie.set_display( pygame_surface )

# create a texture using this surface as the source of texel data
texture = Texture(pygame_surface)

text = Text( text = "Vision Egg MPEG demo - Press Esc to quit",
             position = (screen.size[0]/2,2),
             anchor = 'bottom',
             color = (1.0,1.0,1.0,1.0))

# Create the instance of TextureStimulus

stimulus = TextureStimulus(texture  = texture,
                           position = (screen.size[0]/2,screen.size[1]/2),
                           anchor = 'center',
                           size = (width*scale,height*scale),
                           mipmaps_enabled = 0,
                           texture_min_filter=gl.GL_LINEAR)

texture_object = stimulus.parameters.texture.get_texture_object()
def update_movie():
    # While movie.play() decompresses the movie to pygame surface
    # in a separate thread, this sends the data to OpenGL.
    texture_object.put_sub_image( pygame_surface )
    
viewport = Viewport(screen=screen,
                    stimuli=[stimulus,text])

p = Presentation(go_duration=('forever',),viewports=[viewport])

def quit(dummy_arg=None):
    p.parameters.go_duration = (0,'frames')
    
def keydown(event):
    if event.key == pygame.locals.K_ESCAPE:
        quit()
        
p.parameters.handle_event_callbacks=[(pygame.locals.QUIT, quit),
                                     (pygame.locals.KEYDOWN, keydown)]
p.add_controller(None,None,FunctionController(during_go_func=update_movie,
                                              temporal_variables=Controller.TIME_INDEPENDENT))

movie.play()
p.go()

@@ -587,150 +587,1159 @@
 e = 
-os.path.join(VisionEgg.config.VISIONEGG_SYSTEM_DIR,%22data%22,%22water.mpg%22)%0A    %0Amovie = pygame.movie.Movie(filename)%0A%0Ascreen = get_default_screen(
+None%0A%0Ascreen = get_default_screen()%0A%0Adef quit(dummy_arg=None):%0A    p.parameters.go_duration = (0,'frames')%0A    %0Adef keydown(event):%0A    if event.key == pygame.locals.K_ESCAPE:%0A        quit()%0A%0Aif not filename:%0A    text = Text( text = %22Error: Use MPEG file as command line argument - Press Esc to quit%22,%0A                 position = (screen.size%5B0%5D/2,screen.size%5B1%5D),%0A                 anchor = 'top',%0A                 font_size=24,%0A                 color = (1.0,0.0,0.0))%0A    text2 = Text( text = %22(If you have a free MPEG to contribute, it could go here.)%22,%0A                  position = (screen.size%5B0%5D/2,screen.size%5B1%5D/2),%0A                  anchor = 'center',%0A                  font_size=20,%0A                  color = (1.0,1.0,1.0))%0A    viewport = Viewport(screen=screen,%0A                        stimuli=%5Btext,text2%5D)%0A    %0A    p = Presentation(go_duration=('forever',),%0A                     viewports=%5Bviewport%5D,%0A                     handle_event_callbacks=%5B(pygame.locals.QUIT, quit),%0A                                             (pygame.locals.KEYDOWN, keydown)%5D,%0A                     )%0A    p.go()%0A    sys.exit(1)%0A%0Amovie = pygame.movie.Movie(filename
 )%0A%0Aw
@@ -3168,169 +3168,47 @@
 ',),
-viewports=%5Bviewport%5D)%0A%0Adef quit(dummy_arg=None):%0A    p.parameters.go_duration = (0,'frames')%0A    %0Adef keydown(event):%0A    if event.key == pygame.locals.K_ESCAPE:
+%0A                 viewports=%5Bviewport%5D,
 %0A   
@@ -3216,23 +3216,16 @@
     
-quit()%0A
         
 %0Ap.p
@@ -3224,22 +3224,9 @@
     
-%0Ap.parameters.
+ 
 hand
@@ -3302,32 +3302,36 @@
                 
+    
 (pygame.locals.K
@@ -3347,16 +3347,37 @@
 eydown)%5D
+,%0A                 )%0A
 %0Ap.add_c

print warning when no MPEG specified