source: golgotha/src/golg/m_flow.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: 6.3 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 "m_flow.hh"
10#include "saver.hh"
11#include "error/error.hh"
12#include "g1_speed.hh"
13#include "device/device.hh"
14#include "device/event.hh"
15#include "device/kernel.hh"
16#include "music/stream.hh"
17
18
19i4_event_reaction_class *g1_frame_change_notify=0,   
20  *g1_scene_change_notify=0, *g1_movie_stop_notify=0;
21
22
23g1_cut_scene_class *g1_movie_flow_class::add_cut_scene(const i4_const_str &name)
24{
25  w32 i=t_cut_scenes;
26  t_cut_scenes++;
27  set=(g1_cut_scene_class **)i4_realloc(set,
28                                        sizeof(g1_cut_scene_class *)*t_cut_scenes,
29                                        "cut_scene list");
30
31
32  set[i]=new g1_cut_scene_class(name);
33
34
35  return set[i];
36}
37
38
39g1_movie_flow_class::~g1_movie_flow_class()
40{
41  for (w32 i=0; i<t_cut_scenes; i++)
42    delete set[i];
43  if (set)
44    i4_free(set); 
45}
46
47
48g1_movie_flow_class::g1_movie_flow_class()
49{
50  t_cut_scenes=0;
51  set=0;
52  frame=0; 
53  scene=0;
54  memset(scores, 0, sizeof(scores));
55}
56
57
58void g1_cut_scene_class::save(g1_saver_class *fp)
59{
60  fp->start_version(DATA_VERSION_2);
61
62  fp->write_32(t_frames);
63
64  fp->write_16(T_PATHS);
65
66  for (int i=0; i<T_PATHS; i++)
67    paths[i].save(fp);
68
69
70  if (wave_file)
71    fp->write_counted_str(*wave_file);
72  else fp->write_16(0);
73
74  if (name)   
75    fp->write_counted_str(*name);
76  else fp->write_16(0);
77
78  fp->end_version();
79}
80
81// note : data version check has already been done by the time load is called
82g1_cut_scene_class::g1_cut_scene_class(g1_loader_class *fp, w16 ver)
83{
84  t_frames=fp->read_32();
85
86  if (ver==DATA_VERSION_1)
87  {
88    paths[0].load(fp);
89    paths[1].load(fp);
90  }
91  else if (ver==DATA_VERSION_2)
92  {
93    int tp=fp->read_16();
94    I4_ASSERT(tp<=T_PATHS, "bad total paths in file");
95
96    for (int i=0; i<tp; i++)
97      paths[i].load(fp);
98  }
99
100
101  wave_file=fp->read_counted_str();
102  name=fp->read_counted_str();
103}
104
105static g1_cut_scene_class *g1_load_cut_scene(g1_loader_class *fp)
106
107  w16 ver,size;
108  fp->get_version(ver,size);
109
110  if (ver==g1_cut_scene_class::DATA_VERSION_1 ||
111      ver==g1_cut_scene_class::DATA_VERSION_2)
112    return new g1_cut_scene_class(fp,ver);
113  else
114  {
115    fp->seek(fp->tell()+size);
116    return 0;
117  }
118}
119
120
121
122void g1_movie_flow_class::save(g1_saver_class *fp)
123{
124  fp->start_version(DATA_VERSION);
125  fp->write_16(t_cut_scenes);
126  fp->write_32(frame);
127
128  w32 con=0;
129 
130  for (w32 i=0; i<t_cut_scenes; i++)
131    set[i]->save(fp);
132
133  fp->write_16(scene);
134  fp->end_version();
135}
136
137g1_movie_flow_class *g1_load_movie_flow(g1_loader_class *fp)
138{
139  w16 ver,size;
140
141  fp->get_version(ver,size);
142
143  if (ver==g1_movie_flow_class::DATA_VERSION)
144  {
145    i4_bool error=i4_F;
146
147    w32 t_cut=fp->read_16();
148    w32 cframe=fp->read_32();
149
150    g1_cut_scene_class **set;
151    if (t_cut)
152      set=(g1_cut_scene_class **)i4_malloc(sizeof(g1_cut_scene_class *)*t_cut,
153                                                                "cut_scenes");
154    else
155      set=0;
156 
157    for (w32 i=0; i<t_cut; i++)
158    {     
159      set[i]=g1_load_cut_scene(fp);
160      if (!set[i])
161        error=i4_T;
162    }
163
164    if (error)
165    {
166      for (w32 i=0; i<t_cut; i++)
167        if (set[i])
168          delete set[i];
169      i4_free(set);
170
171      return 0;
172    }
173
174    g1_movie_flow_class *ret=new g1_movie_flow_class;
175    ret->t_cut_scenes=t_cut;
176    ret->set=set;
177    ret->set_scene(fp->read_16());
178    ret->set_frame(cframe);
179    return ret;
180
181  } else
182  {
183    fp->seek(fp->tell() + size);
184    return 0;
185  }
186}
187
188void g1_movie_flow_class::poll_scores()
189{
190  for (w32 i=0; i<MAX_SCORES; i++)
191    if (scores[i])
192    {
193      if (!scores[i]->poll())
194      {
195        delete scores[i];
196        scores[i]=0;
197      }
198    }
199}
200
201void g1_movie_flow_class::stop()
202{
203  i4_kernel.send(g1_movie_stop_notify);
204
205  for (w32 i=0; i<MAX_SCORES; i++)
206    if (scores[i])
207    {
208      delete scores[i];
209      scores[i]=0;
210    }
211}
212
213void g1_movie_flow_class::start()
214{
215  if (current()->wave_file)
216  {
217    i4_stream_wav_player **slot=0;
218    for (w32 i=0; i<MAX_SCORES && !slot; i++)
219      if (!scores[i])
220        slot=&scores[i];
221
222    if (slot)
223    {
224      i4_file_class *fp=i4_open(*current()->wave_file, I4_READ | I4_SUPPORT_ASYNC);
225      if (fp)
226      {
227        *slot=new i4_stream_wav_player(fp,
228                                       256*1024,
229                                       i4_F,
230                                       i4_F);
231      }
232    }
233
234  } 
235
236  frame_time.get();
237}
238
239
240g1_movie_flow_class::advance_status g1_movie_flow_class::advance_movie_with_time()
241{
242  poll_scores();
243
244  advance_status stat=PLAYING;
245
246  if (t_cut_scenes)
247  {
248    i4_time_class now;
249    sw32 md;
250
251    do
252    {
253      md=now.milli_diff(frame_time);
254      if (md>(1000/G1_MOVIE_HZ))
255      {
256        frame_time.add_milli((1000/G1_MOVIE_HZ));
257
258        if (frame+1<current()->total_frames())
259          set_frame(frame+1);             
260        else if (scene+1<t_cut_scenes)
261        {
262          do
263          {
264            set_scene(scene+1);
265            start();
266            set_frame(0);
267            stat=NEXT_SCENE;
268
269            if (scene+1==t_cut_scenes && current()->total_frames()==0)
270            {
271              stop();
272              return DONE;
273            }
274
275          } while (current()->total_frames()==0);
276        }
277        else
278        {
279          stop();
280          return DONE;
281        }
282      }
283    } while (md>(1000/G1_MOVIE_HZ));
284  }
285  return stat;
286}
287
288void g1_movie_flow_class::set_frame(w32 num)
289{
290  frame=num;
291  i4_kernel.send(g1_frame_change_notify);
292
293}
294
295void g1_movie_flow_class::set_scene(w32 num)
296{
297  scene=num;
298  i4_kernel.send(g1_scene_change_notify);
299}
300
301
302void g1_cut_scene_class::move(i4_float x_add, i4_float y_add, i4_float z_add)
303{
304  for (int i=0; i<T_PATHS; i++)
305    paths[i].move(x_add, y_add, z_add);
306}
Note: See TracBrowser for help on using the repository browser.