function exitFlag = showMovie(win,offscreenPointers,rate);
% showMovie(win,OffScreenPointers,[rate]);
% shows a movie contained in a series of offscreen pointers. Written for Rory's expts.
%
% win: window in which to display movie
%
% offscreenPointers: array of pointers to offscreen windows containing
% frames of the movie.
%
% rate: frames per second at which to show movie. Default = 10.
%
% written 1/13/03 by ras
if ~exist('rate','var')		rate = 10;		end

nFrames = length(offscreenPointers);
waitTime = 1/rate;
onsets = (0:nFrames) * waitTime;

% rect = screen(win,'Rect');

exitFlag = 0;
i = 1;
elapsedTime = 0;
movieStart = getSecs;
while elapsedTime < onsets(end)
	if elapsedTime > onsets(i)
		screen('CopyWindow',offscreenPointers(i),win,[],[],'srcCopyQuickly');
		i = i + 1;
	end

	[keyisdown,secs,keycode] = kbcheck;
	
	if keyisdown & keycode(54)==1
		screen(win,'FillRect',127);
		exitFlag = 1;
		break;
	end
	
	elapsedTime = getSecs(elapsedTime) - movieStart;
end

return
