function HolidaySnap3();
% HolidaySnap3();
%
% Example of displaying an image.
%
% This script
% - reads an image from a file
% - shows the image
% - draws a fixation spot
% - collects a response (key and response time)
%
% Please refer to other demos in Psychtoolbox-3

%%%%%%%%%%%%%%%%%%%%%%%%%
% preliminary stuff
%%%%%%%%%%%%%%%%%%%%%%%%%

% Clear Matlab/Octave window:
clc;

% check for OpenGL compatibility, abort otherwise:
AssertOpenGL;

% Make sure keyboard mapping is the same on all supported operating systems
% Apple MacOS/X, MS-Windows and GNU/Linux:
KbName('UnifyKeyNames');

% assign response key
resp=KbName('b'); % respond with key 'b'


%%%%%%%%%%%%%%%%%%%%%%
% file handling
%%%%%%%%%%%%%%%%%%%%%%

% read stimulus image into matlab matrix 'imdata':
imdata=imread('HolidaySnap1.jpg');


%%%%%%%%%%%%%%%%%%%%%%
% image display
%%%%%%%%%%%%%%%%%%%%%%

% Embed core of code in try ... catch statement. If anything goes wrong
% inside the 'try' block (Matlab error), the 'catch' block is executed to
% clean up, save results, close the onscreen window etc.
try
    % Get screenNumber of stimulation display. We choose the display with
    % the maximum index, which is usually the right one, e.g., the external
    % display on a Laptop:
    screens=Screen('Screens');
    screenNumber=max(screens);
    
    % Hide the mouse cursor:
    HideCursor;
    
    % Returns as default the mean gray value of screen:
    gray=GrayIndex(screenNumber); 

    % Open a double buffered fullscreen window on the stimulation screen
    % 'screenNumber' and choose/draw a gray background. 'w' is the handle
    % used to direct all drawing commands to that window - the "Name" of
    % the window. 'wRect' is a rectangle defining the size of the window.
    % See "help PsychRects" for help on such rectangles and useful helper
    % functions:
    [w, wRect]=Screen('OpenWindow',screenNumber, gray);
    
    
    % Do dummy calls to GetSecs, WaitSecs, KbCheck to make sure
    % they are loaded and ready when we need them - without delays
    % in the wrong moment:
    KbCheck;
    WaitSecs(0.1);
    GetSecs;

    % Set priority for script execution to realtime priority:
    priorityLevel=MaxPriority(w);
    Priority(priorityLevel);
    
        
    % Wait for mouse click:
    GetClicks(w);
                
    % Clear screen to background color (our 'gray' as set at the
    % beginning):
    Screen('Flip', w);
        
    % Wait a second before starting trial
    WaitSecs(1.0);                    
            
    % initialize KbCheck and variables to make sure they're
    % properly initialized/allocted by Matlab - this to avoid time
    % delays in the critical reaction time measurement part of the
    % script:
    [KeyIsDown, endrt, KeyCode]=KbCheck;        
        
    % make texture image out of image matrix 'imdata'
    tex=Screen('MakeTexture', w, imdata);
            
    % Draw texture image to backbuffer. It will be automatically
    % centered in the middle of the display if you don't specify a
    % different destination:
    Screen('DrawTexture', w, tex);
            
    
    % Screen('DrawDots', windowPtr, xy [,size] [,color] [,center] [,dot_type]);
    Screen('DrawDots', w, [720 450], 10, [255 0 0], [0 0], 1)   
    
    % Show stimulus on screen at next possible display refresh cycle,
    % and record stimulus onset time in 'startrt':
    [VBLTimestamp startrt]=Screen('Flip', w);
            
    % while loop to show stimulus until subjects response or until
    % 10 seconds elapsed.
    while (GetSecs - startrt)<=10
          % poll for a resp subjects can respond
          % before stimulus terminates

          if ( KeyCode(resp)==1)
              break;
          end
          [KeyIsDown, endrt, KeyCode]=KbCheck;

          % Wait 1 ms before checking the keyboard again to prevent
          % overload of the machine at elevated Priority():
          WaitSecs(0.001);
    end

    % Clear screen to background color 
    Screen('Flip', w);
            
    % loop until valid key is pressed
    % if a response is made already, then this loop will be skipped
            
    while ( KeyCode(resp)==0)
          [KeyIsDown, endrt, KeyCode]=KbCheck;
           WaitSecs(0.001);
    end
            
    % compute response time in ms
    rt=round(1000*(endrt-startrt))
            
    response=KbName(KeyCode); % get key pressed by subject
   
    
    % Cleanup at end of experiment - Close window, show mouse cursor, close
    % result file, switch Matlab/Octave back to priority 0 -- normal
    % priority:
    Screen('CloseAll');
    ShowCursor;
    fclose('all');
    Priority(0);
    
    % End of experiment:
    return;
catch
    % catch error: This is executed in case something goes wrong in the
    % 'try' part due to programming error etc.:
    
    % Do same cleanup as at the end of a regular session...
    Screen('CloseAll');
    ShowCursor;
    fclose('all');
    Priority(0);
    
    % Output the error message that describes the error:
    psychrethrow(psychlasterror);
end % try ... cc atch %