home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / ms-0.07 / lib / mspawn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-27  |  4.1 KB  |  138 lines

  1. /* mspawn.c -- Mandelbrot-specific, X-independent MandelSpawn code */
  2.    
  3. /*  
  4.     This file is part of MandelSpawn, a network Mandelbrot program.
  5.  
  6.     Copyright (C) 1990-1993 Andreas Gustafsson
  7.  
  8.     MandelSpawn is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License, version 1,
  10.     as published by the Free Software Foundation.
  11.  
  12.     MandelSpawn is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License,
  18.     version 1, along with this program; if not, write to the Free 
  19.     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "datarep.h"
  23. #include "ms_ipc.h" /* for byteorder conversion functions */
  24. #include "mspawn.h"
  25. #include "work.h"
  26.  
  27. void ms_init(ms, client, wf)
  28.      ms_state *ms;
  29.      char *client;
  30.      struct wf_state *wf;
  31. { ms->client = client;
  32.   ms->workforce = wf;
  33.   ms->mi_count = 0L; /* done no iterations yet */
  34.   ms->chunks_out = 0;
  35.   ms->configuration = 0;
  36. }
  37.  
  38.  
  39. /*
  40.   Calculate the various parameters that go into ms.xi.job.  This includes
  41.   host to network real number format conversion. 
  42. */
  43.  
  44. void 
  45. ms_calculate_job_parameters(ms, j)
  46.      ms_state *ms; struct static_job_info *j;
  47. { ms->yrange=ms->xrange *
  48.     ((double) ms->height / (double) ms->width);
  49.   j->julia=ms->julia;
  50.   if(j->julia) /* Julia mode */
  51.   { j->z0.re=double_to_net(ms->center_x - ms->xrange / 2.0);
  52.     j->z0.im=double_to_net(ms->center_y - ms->yrange / 2.0);
  53.     j->corner.re=double_to_net(ms->c_x);
  54.     j->corner.im=double_to_net(ms->c_y);
  55.   }
  56.   else /* Mandelbrot */
  57.   { j->corner.re=double_to_net(ms->center_x - ms->xrange / 2.0);
  58.     j->corner.im=double_to_net(ms->center_y - ms->yrange / 2.0);
  59.     j->z0.re=double_to_net(0.0);
  60.     j->z0.im=double_to_net(0.0);
  61.   }
  62.   j->delta.re=double_to_net(ms->xrange/ms->width);
  63.   j->delta.im=double_to_net(ms->yrange/ms->height);
  64. }
  65.  
  66.  
  67. void ms_dispatch_chunk(ms, client, rect)
  68.      ms_state *ms; char *client; ms_rectangle rect;
  69. { ms_job j;
  70.   ms_client_info client_info;
  71.   client_info.configuration = ms->configuration;
  72.   client_info.s = rect; /* structure assignment */
  73.   /* build a job structure in network byte order */
  74.   j.j.flags = htons(ms->show_interior ? MS_OPT_INTERIOR : 0);
  75.   j.j.julia = htons(ms->job.julia);
  76.   j.j.corner.re = htonl(ms->job.corner.re);
  77.   j.j.corner.im = htonl(ms->job.corner.im);
  78.   j.j.z0.re = htonl(ms->job.z0.re);
  79.   j.j.z0.im = htonl(ms->job.z0.im);
  80.   j.j.delta.re = htonl(ms->job.delta.re);
  81.   j.j.delta.im = htonl(ms->job.delta.im);
  82.   j.j.iteration_limit = htonl(ms->job.iteration_limit);
  83.   j.s.x = htons(rect.x);
  84.   j.s.width = htons(rect.width);
  85.   j.s.y = htons(rect.y);
  86.   j.s.height = htons(rect.height);
  87.   /* ..and put it on the work queue */
  88.   wf_dispatch_chunk(ms->workforce, ms,
  89.         (char *) &client_info, sizeof(ms_client_info),
  90.         (char *) &j, sizeof(j));
  91.   ms->chunks_out++; /* one more to wait for */
  92. }
  93.  
  94.  
  95. /*
  96.   Take a rectangular area, split it into pieces and send the pieces
  97.   out to be calculated.
  98. */
  99.  
  100. void ms_dispatch_rect(ms, client, rx, ry, rwidth, rheight)
  101.      ms_state *ms;
  102.      char *client;
  103.      unsigned rx, ry, rwidth, rheight; 
  104. { ms_rectangle r;
  105.   unsigned int right_edge = rx+rwidth;
  106.   unsigned int bottom_edge = ry+rheight;
  107.   unsigned int x, y;
  108.  
  109.   wf_begin_dispatch(ms->workforce);
  110.   
  111.   for(y=ry; y < bottom_edge; y+=ms->chunk_height) 
  112.     for(x=rx; x < right_edge; x+=ms->chunk_width) 
  113.     { r.x = x;
  114.       r.y = y;
  115.       r.width = MIN(ms->chunk_width, right_edge-x);
  116.       r.height = MIN(ms->chunk_height, bottom_edge-y);
  117.       ms_dispatch_chunk(ms, client, r);
  118.     }
  119.   wf_restart(ms->workforce);
  120. }
  121.  
  122.  
  123. void wf_draw(client, client_data, data)
  124.      char *client;
  125.      char *client_data;
  126.      char *data; 
  127. { ms_state *ms = (ms_state *) client;
  128.   ms_draw(ms->client, client_data, data);
  129.   ms->chunks_out--;         /* one less to go */
  130.   if(ms->chunks_out == 0)
  131.     wf_done(ms->workforce);     /* tell the lower layers that we are done */
  132. }
  133.  
  134. ms_main(ms)
  135.   ms_state *ms;
  136. { wf_main(ms->workforce);
  137. }
  138.