source: golgotha/src/golg/objs/sfx_obj.cc

Last change on this file was 80, checked in by Sam Hocevar, 12 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: 7.6 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 "objs/sfx_obj.hh"
10#include "music/stream.hh"
11#include "file/get_filename.hh"
12#include "editor/editor.hh"
13#include "gui/create_dialog.hh"
14#include "window/colorwin.hh"
15#include "gui/text_input.hh"
16#include "object_definer.hh"
17#include "objs/model_draw.hh"
18#include "sound_man.hh"
19#include "g1_speed.hh"
20#include "map_man.hh"
21#include "app/app.hh"
22#include "editor/editor.hh"
23#include "editor/e_res.hh"
24#include "lisp/li_dialog.hh"
25#include "saver.hh"
26
27g1_object_type g1_sfx_obj_type;
28
29class g1_sfx_obj_edit_class : public i4_color_window_class
30{
31public:
32  w32 selected_objects[G1_MAX_OBJECTS];
33  int t_sel;
34
35  i4_str *filename;
36  i4_text_input_class *ti_filename,
37    *ti_max_volume,
38    *ti_max_hearable,
39    *ti_restart,
40    *ti_random_restart,
41    *ti_current_delay;
42
43  enum {
44    SET_SONG_CANCEL,
45    SET_SONG_OK,
46    OK, CANCEL,
47    BROWSE };
48
49
50  g1_sfx_obj_edit_class(i4_graphical_style_class *style)
51
52    : i4_color_window_class(400, 200, style->color_hint->neutral(), style)
53  {
54    t_sel=g1_get_map()->make_selected_objects_list(selected_objects, G1_MAX_OBJECTS);
55
56    g1_sfx_obj_class *sfx;
57    if (t_sel && (sfx=g1_sfx_obj_class::cast(g1_global_id.get(selected_objects[0]))))
58    {
59      if (sfx->filename)
60        filename=new i4_str(*sfx->filename);
61      else
62        filename=new i4_str(i4_string_man.get(0));
63
64      i4_create_dialog(g1_ges("sfx_obj_dialog"), this, style,
65                       &ti_filename, filename,  this, BROWSE,
66                       I4_SOUND_VOLUME_LEVELS-1, &ti_max_volume, sfx->max_volume,
67                       &ti_max_hearable, sfx->max_hearable_distance,
68                       G1_HZ, &ti_restart, sfx->restart_delay,
69                       &ti_random_restart, sfx->random_restart_delay,
70                       &ti_current_delay, sfx->current_delay,
71                       this, OK,
72                       this, CANCEL);
73    }
74  }
75
76  void receive_event(i4_event *ev)
77  {
78    i4_color_window_class::receive_event(ev);
79
80    if (ev->type()==i4_event::USER_MESSAGE)
81    {
82      CAST_PTR(uev, i4_user_message_event_class, ev);
83      switch (uev->sub_type)
84      {
85        case BROWSE :
86        {
87          i4_create_file_open_dialog(style,
88                                     g1_ges("get_song_title"),
89                                     g1_ges("get_song_start_dir"),
90                                     g1_ges("get_song_file_mask"),
91                                     g1_ges("get_song_mask_name"),
92                                     this,
93                                     SET_SONG_OK,
94                                     SET_SONG_CANCEL);
95        } break;
96
97        case SET_SONG_OK :
98        {
99          CAST_PTR(fev, i4_file_open_message_class, ev);
100          ti_filename->change_text(*fev->filename);
101          if (filename)
102            delete filename;
103
104          filename=i4_relative_path(*fev->filename);
105        } break;
106
107        case OK :
108        {
109          int v=ti_max_volume->get_number();
110          if (v<0 || v>=I4_SOUND_VOLUME_LEVELS)
111          {
112            g1_editor_instance.create_modal(500, 100, "bad_volume_title");
113            i4_create_dialog(g1_ges("bad_volume_dialog"),
114                             g1_editor_instance.modal_window.get(),
115                             style, v, I4_SOUND_VOLUME_LEVELS,
116                             &g1_editor_instance, G1_EDITOR_MODAL_BOX_CANCEL);
117          }
118          else
119          {
120            int max_hear=ti_max_hearable->get_number();
121            int restart=ti_restart->get_number();
122            int random_restart=ti_random_restart->get_number();
123            int cur_delay=ti_current_delay->get_number();
124            g1_sfx_obj_class *sfx;
125
126            for (int i=0; i<t_sel; i++)
127              if (g1_global_id.check_id(selected_objects[i]))
128              {
129                sfx=g1_sfx_obj_class::cast(g1_global_id.get(selected_objects[i]));
130                sfx->set_filename(*ti_filename->get_edit_string());             
131                sfx->max_volume=v;
132                sfx->max_hearable_distance=max_hear;
133                sfx->restart_delay=restart;
134                sfx->random_restart_delay=random_restart;
135                sfx->current_delay=cur_delay;
136              }
137           
138          }
139
140          li_call("object_changed");
141
142        } break;
143
144        case CANCEL :
145          li_call("object_changed");
146          break;
147
148      }
149    }
150
151  }
152
153};
154
155class g1_sfx_obj_def_class : public g1_object_definer<g1_sfx_obj_class>
156{
157public:
158  g1_sfx_obj_def_class(char *_name)
159    : g1_object_definer<g1_sfx_obj_class>(_name,
160                                          g1_object_definition_class::EDITOR_SELECTABLE
161                                          ) {}
162
163  virtual void init()
164  {
165    g1_sfx_obj_type = type;
166    g1_object_definition_class::init();
167  }
168
169  i4_window_class *create_edit_dialog()
170                                     
171  {   
172    return new g1_sfx_obj_edit_class(i4_current_app->get_style());
173  }
174
175} g1_sfx_obj_def("sfx_obj");
176
177
178g1_sfx_obj_class::g1_sfx_obj_class(g1_object_type id,
179                                   g1_loader_class *fp)
180  : g1_object_class(id, fp)
181{
182  draw_params.setup("speaker");
183
184  if (fp && fp->check_version(DATA_VERSION))
185  {
186    filename=fp->read_counted_str();
187    max_volume=fp->read_16();
188    max_hearable_distance=fp->read_32();
189    restart_delay=fp->read_32();
190    random_restart_delay=fp->read_32();
191    current_delay=fp->read_32();
192    fp->end_version(I4_LF);
193  }
194  else
195  {
196    filename=0;
197    max_volume=I4_SOUND_VOLUME_LEVELS-1;
198    max_hearable_distance=10000;
199    restart_delay=0;
200    random_restart_delay=0;
201    current_delay=0;
202  }
203
204  stream=0;
205  dist_from_camera_sqrd=100000;
206}
207
208g1_sfx_obj_class::~g1_sfx_obj_class()
209{
210  if (stream)
211    delete stream;
212
213  if (filename)
214    delete filename;
215}
216
217
218void g1_sfx_obj_class::save(g1_saver_class *fp)
219{
220  g1_object_class::save(fp);
221
222  fp->start_version(DATA_VERSION);
223 
224  if (filename)
225    fp->write_counted_str(*filename);
226  else fp->write_16(0);
227
228  fp->write_16(max_volume);
229  fp->write_32(max_hearable_distance);
230  fp->write_32(restart_delay);
231  fp->write_32(random_restart_delay);
232  fp->write_32(current_delay);
233
234  fp->end_version();
235}
236
237
238void g1_sfx_obj_class::draw(g1_draw_context_class *context)
239{
240  g1_editor_model_draw(this, draw_params, context);
241}
242
243void g1_sfx_obj_class::think()
244{
245}
246
247// adds self to sound_manager
248i4_bool g1_sfx_obj_class::occupy_location()
249{
250  g1_sound_man.add_sfx_to_list(this);
251  return g1_object_class::occupy_location();
252}
253
254// remove self from sound_manager
255void g1_sfx_obj_class::unoccupy_location()
256{
257  g1_object_class::unoccupy_location();
258  g1_sound_man.remove_sfx_from_list(this);
259}
260
261
262void g1_sfx_obj_class::set_filename(const i4_const_str &fname)
263{
264  if (filename)
265    delete filename;
266
267  if (fname.null())
268    filename=0;
269  else filename=new i4_str(fname);
270
271}
272
273i4_str *g1_sfx_obj_class::get_context_string()
274{
275  return i4gets("sfx_obj_fmt").sprintf(120,filename, max_volume,
276                                   max_hearable_distance,
277                                   current_delay);
278}
Note: See TracBrowser for help on using the repository browser.