home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / src / point.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-10  |  3.3 KB  |  191 lines  |  [TEXT/KAHL]

  1. /*
  2.  *  Peter's Final Project -- A texture mapping demonstration
  3.  *  © 1995, Peter Mattis
  4.  *
  5.  *  E-mail:
  6.  *  petm@soda.csua.berkeley.edu
  7.  *
  8.  *  Snail-mail:
  9.  *   Peter Mattis
  10.  *   557 Fort Laramie Dr.
  11.  *   Sunnyvale, CA 94087
  12.  *
  13.  *  Avaible from:
  14.  *  http://www.csua.berkeley.edu/~petm/final.html
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  */
  30.  
  31. #include <assert.h>
  32. #include "point.h"
  33. #include "sys.stuff.h"
  34.  
  35. /*
  36.  * Keep a list of free points for fast allocation.
  37.  * Keep track of the memory used by points.
  38.  */
  39.  
  40. static POINTS free_points_list = NULL;
  41. static long point_mem = 0;
  42.  
  43. /*
  44.  * Make a point and initialize its values.
  45.  */
  46.  
  47. POINT
  48. make_point ()
  49. {
  50.     POINTS points;
  51.     POINT point;
  52.  
  53.     if (free_points_list)
  54.     {
  55.         points = free_points_list;
  56.         point = points_first (points);
  57.         free_points_list = points_rest (free_points_list);
  58.         free_list (points);
  59.     }
  60.     else
  61.     {
  62.         point_mem += sizeof (_POINT);
  63.  
  64.         point = (POINT) ALLOC (sizeof (_POINT));
  65.     }
  66.  
  67.     set_point_intensity (point, NUM_ZERO);
  68.  
  69.     set_point_x (point, NUM_ZERO);
  70.     set_point_y (point, NUM_ZERO);
  71.     set_point_z (point, NUM_ZERO);
  72.     set_point_w (point, NUM_ONE);
  73.  
  74.     return point;
  75. }
  76.  
  77. /*
  78.  * Free a point by placing it on the free list.
  79.  */
  80.  
  81. void
  82. free_point (p)
  83.     POINT p;
  84. {
  85.     LIST temp;
  86.  
  87.     temp = make_list ();
  88.     set_list_datum (temp, p);
  89.     set_list_next (temp, free_points_list);
  90.     free_points_list = temp;
  91. }
  92.  
  93. /*
  94.  * Clone a point. That is, create a new point and
  95.  *  copy all the values from the old point into the
  96.  *  new point. (Used in clipping).
  97.  */
  98.  
  99. POINT
  100. point_clone (p)
  101.     POINT p;
  102. {
  103.     POINT new_point = make_point ();
  104.  
  105.     vector_copy (point_coord (p), point_coord (new_point));
  106.     set_point_intensity (new_point, point_intensity (p));
  107.  
  108.     return new_point;
  109. }
  110.  
  111. /*
  112.  * Return the last point in a list of points.
  113.  * (Actually, the list containing the last point).
  114.  */
  115.  
  116. POINTS
  117. points_last (points)
  118.     POINTS points;
  119. {
  120.     if (points)
  121.     {
  122.         while (points_rest (points))
  123.             points = points_rest (points);
  124.  
  125.         return points;
  126.     }
  127.  
  128.     return NULL;
  129. }
  130.  
  131. /*
  132.  * Append a point to a list of points.
  133.  */
  134.  
  135. POINTS
  136. points_append_point (set, p)
  137.     POINTS set;
  138.     POINT p;
  139. {
  140.     LIST n;
  141.  
  142.     n = make_list ();
  143.     set_list_datum (n, p);
  144.  
  145.     return ((POINTS) list_append_list ((LIST) set, n));
  146. }
  147.  
  148. /*
  149.  * Prepend a point to a list of points.
  150.  */
  151.  
  152. POINTS
  153. points_prepend_point (set, p)
  154.     POINTS set;
  155.     POINT p;
  156. {
  157.     LIST n;
  158.  
  159.     n = make_list ();
  160.     set_list_datum (n, p);
  161.  
  162.     return ((POINTS) list_prepend_list ((LIST) set, n));
  163. }
  164.  
  165. /*
  166.  * Free a list of points.
  167.  */
  168.  
  169. void
  170. free_points (set)
  171.     POINTS set;
  172. {
  173.     if (set == NULL)
  174.         return;
  175.  
  176.     free_points (points_rest (set));
  177.  
  178.     set_points_rest (set, free_points_list);
  179.     free_points_list = set;
  180. }
  181.  
  182. /*
  183.  * Return the point memory usage.
  184.  */
  185.  
  186. long
  187. point_mem_usage ()
  188. {
  189.     return point_mem;
  190. }
  191.