% mousingAround.m
%
% A program that draws a circle on the screen, the position of which is
% controlled by the mouse cursor
%
% by Hristiyan Kourtev - 7/15/2010

clear all;

try
    %create screen
	which_screen=0;
	bg_color = [0, 0, 0];
	[window_ptr, screen_dimensions]= Screen(which_screen,'OpenWindow', bg_color);

	dot_r = 20; % radius of cursor
	HideCursor; % hide cursor during simulation so it doesn't distract us
	green = [0, 255, 0];    % define green color
	red = [255,0, 0];       % define red color

	tic;    % start counter
    while(toc<5)    % loop for 5 seconds
        % get mouse data
        [mouse_x, mouse_y, buttons] = GetMouse;
        if(sum(buttons)>0)
            dot_color = red;    % if mouse clicked - red
        else
            dot_color = green;  % if not, circle is green
        end
        
        % generate rectangle for the circle to be drawn inside
        cursor = [ mouse_x-dot_r,  mouse_y-dot_r, ...
            mouse_x+dot_r,  mouse_y+dot_r];
        
        % draw circle and flip
        Screen(window_ptr, 'FillOval', dot_color, cursor);
        Screen('Flip', window_ptr);
    end
    
    clear Screen;
    ShowCursor;
    
catch err
    clear Screen;
    ShowCursor;
    disp(err);
end
