function sys_preloadStimuli(type, winObj, design, param)
% Preload stimuli before stimulation
% function exp_preloadStimuli(type, winObj, design, param)
%
% In:
%   TYPE    : Screen type
%       'stimuli'   : Stimulation screens
%       'blank'     : Blank screen
%
%   WINOBJ  : Window objects
%
%   DESIGN  : Stimulation design
%
%   PARAM   : Screen parameters
%
% Masaki Nakanishi, Swartz Center for Computational Neuroscience, Institute
% for Neural Computation, University of California, San Diego
% Date: Feb-11-2015, Update: Feb-11-2015.

Screen(winObj, 'TextColor', param.TextColor);
Screen(winObj, 'TextFont', param.TextFont);
Screen(winObj, 'TextSize', param.TextSize);

% Draw stimulation
for stim_i = 1:1:design.NumStim
    
    % Set code
    if strcmp(type, 'stimuli')
        fillColor = repmat(param.FillColor(stim_i),1,3)*255;
        frameColor = repmat(param.FrameColor(stim_i),1,3)*255;
    elseif strcmp(type, 'blank')
        fillColor = param.FillColor;
        frameColor = param.FrameColor;
    end
    
    % Present stimuli
    Screen('FillRect', winObj, fillColor, design.StimLoc{stim_i});
    Screen('FrameRect', winObj, frameColor, design.StimLoc{stim_i});   % Draw rectangle
    
end % targ_i
 
% Draw fixation point
for targ_i = 1:1:design.NumTarg
    
    Screen('FillOval', winObj, [255 255 255], design.PointLoc{targ_i});
    bounds = Screen(winObj, 'TextBounds', num2str(targ_i));
    mean(design.PointLoc{targ_i}([1,3]))
    mean(design.PointLoc{targ_i}([2,4]))
    textX = mean(design.PointLoc{targ_i}([1,3]))+bounds(RectRight)/2;
    textY = mean(design.PointLoc{targ_i}([2,4]))-bounds(RectBottom)/2;
    %textX = design.TargLoc{targ_i}(1)+bounds(RectRight)/2;
    %textY = design.TargLoc{targ_i}(2)-bounds(RectBottom)/2;
    Screen('DrawText', winObj, num2str(targ_i), textX, textY, [0 0 0]);
    
end % targ_i