source: golgotha/src/golg/path.cc

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 3.5 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#include "path.hh"
10#include "memory/malloc.hh"
11#include "g1_limits.hh"
12#include "saver_id.hh"
13#include "saver.hh"
14#include <string.h>
15
16g1_path_manager_class g1_path_manager;
17
18enum {VERSION=1};
19
20class g1_path_class
21{
22public:
23  class point
24  {
25  public:
26    i4_float x,y;
27  } *node;
28  w8 nodes;
29  w8 current;
30
31  i4_bool get_pos(int n, i4_float &x, i4_float &y) const
32  {
33    int i=current+n;
34
35    if (i<0 || i>=nodes)
36      return i4_F;
37
38    x = node[i].x;
39    y = node[i].y;
40    return i4_T;
41  }
42  i4_float x() const { return node[current].x; }
43  i4_float y() const { return node[current].y; }
44
45  i4_bool last() const { return current == nodes-1; }
46
47  void alloc()
48  {
49    if (nodes>0)
50      node = (point *)i4_malloc(sizeof(point)*(nodes), "alloc_path");
51    else
52      node = 0;
53  }
54
55  g1_path_class(w32 t_positions, i4_float *positions)
56  {
57    nodes = t_positions;
58    current = 0;
59    alloc();
60    for (w32 i=0; i<nodes; i++)
61    {
62      //find_path returns paths in reverse order. this will fix that
63      node[nodes-1-i].x = *positions++;
64      node[nodes-1-i].y = *positions++;
65    }
66  }
67
68  g1_path_class(g1_loader_class *fp)
69  {
70    w16 ver,data_size;
71
72    if (fp && fp->check_version(VERSION))
73    {
74      nodes = fp->read_8();
75      current = fp->read_8();
76      alloc();
77      for (w8 i=0; i<nodes; i++)
78      {
79        node[i].x = fp->read_float();
80        node[i].y = fp->read_float();
81      }
82
83      fp->end_version(I4_LF);
84    }
85    else
86    {
87      nodes = 0;
88      current = 0;
89    }
90  }
91
92  void save(g1_saver_class *fp)
93  {
94    fp->start_version(VERSION);
95    fp->write_8(nodes);
96    fp->write_8(current);
97    for (w8 i=0; i<nodes; i++)
98    {
99      fp->write_float(node[i].x);
100      fp->write_float(node[i].y);
101    }
102    fp->end_version();
103  }
104
105  ~g1_path_class()
106  {
107    if (node)
108      delete node;
109  }
110
111  void advance_path()
112  {
113    current++;
114  }
115};
116
117g1_path_handle g1_path_manager_class::alloc_path(w32 t_positions,
118                                                 i4_float *positions)
119{
120  return new g1_path_class(t_positions,positions);
121}
122
123void g1_path_manager_class::get_position(g1_path_handle path, i4_float &x, i4_float &y)
124{
125  x=path->x();
126  y=path->y();
127}
128
129i4_bool g1_path_manager_class::get_nth_position(g1_path_handle path, int n,
130                                                i4_float &x, i4_float &y)
131{
132  if (!path)
133    return i4_F;
134  return path->get_pos(n,x,y);
135}
136
137g1_path_handle g1_path_manager_class::advance_path(g1_path_handle path)
138{
139  if (path->last())
140  {
141    delete path;
142    return 0;
143  }
144
145  path->advance_path();
146  return path;
147}
148
149
150i4_bool g1_path_manager_class::is_last_path_point(g1_path_handle path)
151{
152  return path->last();
153}
154
155void g1_path_manager_class::free_path(g1_path_handle &path)
156{
157  delete path;
158}
159
160void g1_path_manager_class::save(g1_saver_class *fp, g1_path_handle path)
161{
162  path->save(fp);
163}
164
165g1_path_handle g1_path_manager_class::load(g1_loader_class *fp)
166{
167  return new g1_path_class(fp);
168}
Note: See TracBrowser for help on using the repository browser.