%
% bodview.m -- simple routine to display moving bodies
%
% bodview prompts the user for
%
%   - input filename
%   - image edge size, n
%   - display time step, t
%
% it then opens the input file and displays the data as a squence
% of n x n images, one pixel per byte and one image for each time
% step t.  The bytes are rendered with a simple greyscale colormap.
%

% get input filename
fin = input('source file [nbody.pix] > ', 's');
if isempty(fin)
  fin = 'nbody.pix';
end

% open the input file
cin = fopen(fin, 'r');
if cin == -1
  error('can''t open input file')
end

% get display parameters
bn = input('image edge size [100] > ');
if isempty(bn)
  bn = 100;
end
t  = input('display time step [0.1] > ');
if isempty(t)
  t = 0.1;
end

% inital image plot
figure(2);
pix = zeros(bn,bn);
h = image(pix, 'EraseMode', 'none');
title('n-body image')

% loop on local dt steps
i = 0;
while 1

  % read in one "frame" or image 
  [pix, count] = fread(cin, [bn, bn], 'uchar');
  if count ~= bn*bn
    break
  end

  % draw one frame
  set(h, 'CData', pix');
  colormap (gray);
  drawnow;
  pause(t)
  i = i + 1;
end 

fclose(cin);
fprintf('showed %d frames\n', i);

