function [display,win,X,Y] = initScreen(varargin);
% initscreen (formerly a script):
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize screen; This version specific to non-gamma-calibrated machines;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('screenNum','var')	screenNum = max(screen('Screens'));	end
if ~exist('stimSize','var')		stimSize = 240;						end
if ~exist('fixationflag','var')	fixationflag = 1;						end
if ~exist('res','var')			res = [640 480];						end
if ~exist('Hz','var')				Hz = 60;								end
if ~exist('pixelSize','var')		pixelSize = 8;							end
gray = 127;
%%%%% parse the option flags
for i = 1:length(varargin)
    switch lower(varargin{i})
        otherwise,
            if (i < length(varargin)) & (isnumeric(varargin{i+1}))
                cmd = sprintf('%s = %i;',varargin{i},varargin{i+1});
                eval(cmd);
            end
    end
end
display = loadDisplayParams; % comment out if not displayParams file
maxGunVal = 2^pixelSize - 1;			
display.oldclut = screen(screenNum,'GetClut');
display.oldRes = screen(screenNum,'Resolution');
newRes = NearestResolution(screenNum,res(1),res(2));
% screen(win,'Resolution',newRes);
display.oldPixelSize = screen(screenNum,'PixelSize',pixelSize);
% display = openScreen(loadDisplayParams('cmapDepth', 10, 'displayName', '3T Back Bore'));
[display.windowPtr,display.rect] = screen(screenNum,'OpenWindow',gray,[],newRes);
X = display.rect(3); Y = display.rect(4); % X and Y size of screen in pixels
display.Hz = screen(screenNum,'FrameRate');
display.imgRect = [X-stimSize Y-stimSize X+stimSize Y+stimSize]/2;
display.clut = round(linspace(0,maxGunVal,255))'*[1 1 1]; % create a clut of grayscales
display.clut(256,:) = [maxGunVal 0 0];   	% this last entry is for a red fixation
imgRect = display.imgRect;
win = display.windowPtr;
% display.clut(255,:) = [0 0 maxGunVal];   	% bright green color for dots
% display.clut(254,:) = [0 511 511];   		% dark green color for dots
% [display.clut,gamma] = tenBit2EightBit(display,display.clut);
% screen(win,'Gamma',gamma);
screen(win,'SetClut',display.clut);
screen(win,'TextSize',24);
screen(win,'DrawText','Loading next experiment...',X/2-150,Y/2,maxGunVal-30);
% set up standard blank-screen window
display.blank = screen(screenNum,'OpenOffScreenWindow',gray);
screen(display.blank,'FillRect',gray,[X-stimSize Y-stimSize X+stimSize Y+stimSize]/2);
if fixationflag == 1
	putFixation(display.blank,X,Y,255);
end
% set dimBlank for fixation dimming during null trials
display.dimBlank = screen(screenNum,'OpenOffScreenWindow',gray);
screen('CopyWindow',display.blank,display.dimBlank);
putFixation(display.dimBlank,X,Y,0);
