[go: up one dir, main page]

Menu

[485645]: / world / Gl_World.h  Maximize  Restore  History

Download this file

364 lines (295 with data), 10.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Gl_World.h - handles interactions between a car and its environment.
//
// Vamos Automotive Simulator
// Copyright (C) 2001--2004 Sam Varner
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef _GL_WORLD_H_
#define _GL_WORLD_H_
#include "../body/Gl_Car.h"
#include "../geometry/Three_Vector.h"
#include "../geometry/Rectangle.h"
#include "../media/XML_Parser.h"
#include "World.h"
#include "Controls.h"
#include <SDL_events.h>
namespace Vamos_Media
{
class Two_D;
}
namespace Vamos_World
{
class Sounds;
class Gl_Window
{
int m_width;
int m_height;
double m_aspect;
int m_id;
int m_video_flags;
public:
Gl_Window (int width, int height, const char* name, bool full_screen);
~Gl_Window ();
double aspect () const { return m_aspect; }
int width () const { return m_width; }
int height () const { return m_height; }
void resize (int width, int height);
};
typedef unsigned int Tick;
/// \brief The timekeeper for the simulation.
///
/// Reports simulation time accounting for pauses and non-realtime
/// operation. Gives the size of the timestep when queried. Time
/// steps are averaged to smooth out variations.
class Timer
{
public:
/// Initialize the timer.
/// \param interval the time interval (ms) to average over when
/// determining the current time step.
/// \param fixed_time_step the time step (ms) for non-realtime
/// operation.
Timer (Tick interval, Tick fixed_time_step);
/// Set the timer to zero.
void reset ();
/// Recalculate the time step.
void update ();
/// Stop time.
void set_paused (bool is_paused);
/// Tell the time that a frame has been rendered.
void add_frame () { m_frames++; }
/// Set the time step for non-realtime operation.
void set_fixed_time_step (double step) { m_fixed_time_step = step; }
/// Start and stop non-realtime operation.
void use_fixed_time_step (bool use);
/// Return the time in seconds since the last reset.
double get_current_time () const
{ return ticks_to_seconds (m_current_ticks - m_pause_ticks + m_fixed_time); }
/// Return the current time step.
double get_time_step () const
{
return (m_use_fixed_time_step)
? ticks_to_seconds (m_fixed_time_step)
: m_frame_step / steps_per_frame ();
}
/// Return the number of times to propagate the simulation before
/// rendering.
int steps_per_frame () const { return 3; }
/// Return the current frame rate.
double get_frame_rate () const
{ return (m_use_fixed_time_step) ? 0.0 : 1.0 / m_frame_step; }
private:
/// Start a new averaging interval.
void start_averaging ();
/// Convert ticks (integer milliseconds) to seconds.
static double ticks_to_seconds (unsigned ticks)
{ return 0.001 * ticks; }
/// Convert seconds to ticks (integer milliseconds).
static double seconds_to_ticks (double seconds)
{ return unsigned (1000.0 * seconds); }
/// How long to average the time step.
Tick m_timeout;
/// The current interval between rendered frames.
double m_frame_step;
/// The number of machine ticks since program start
Tick m_current_ticks;
/// How many machine ticks we've been paused for.
Tick m_pause_ticks;
/// When the last averaging cycle wast started.
Tick m_start_ticks;
/// The total number of frames rendered.
int m_frames;
bool m_is_paused;
/// The time step for non-realtime operation.
Tick m_fixed_time_step;
bool m_use_fixed_time_step;
/// How many machine ticks we've been using a fixed time step.
Tick m_fixed_time;
};
struct Can_Not_Intialize_SDL
{
std::string error;
Can_Not_Intialize_SDL (std::string error_in) : error (error_in) {};
};
struct No_SDL_Screen
{
int width;
int height;
int depth;
int video_flags;
No_SDL_Screen (int w, int h, int d, int flags)
: width (w), height (h), depth (d), video_flags (flags) {};
};
class Map : public Control_Handler
{
public:
Map ();
void set_bounds (const Vamos_Track::Strip_Track& track,
const Gl_Window& window);
void set_view ();
virtual Control& joystick () { return m_joystick; }
virtual Control& keyboard () { return m_keyboard; }
virtual Control& mouse () { return m_mouse; }
Control m_joystick;
Control m_keyboard;
Control m_mouse;
bool pan (double, double direction);
bool zoom (double, double direction);
bool set_zoom (double, double factor);
private:
Vamos_Geometry::Rectangle m_initial_bounds;
Vamos_Geometry::Rectangle m_bounds;
};
class Gl_World : public World, public Control_Handler
{
public:
Gl_World (int argc, char** argv,
Vamos_Track::Strip_Track* track,
Atmosphere* atmosphere,
Sounds* sounds,
bool full_screen);
~Gl_World ();
virtual void add_car (Vamos_Body::Car* car,
Driver* driver,
const Vamos_Track::Road& road,
bool fcontrolled);
// Read the world definition file.
void read (std::string world_file = "", std::string controls_file = "");
virtual void start (size_t laps);
void set_paused (bool is_paused);
// Methods registered with the controls system.
// reset() and restart() defined in a base class are also
// registered.
bool pause (double, double); // Pause the program.
bool quit (double = false, double = false); // Quit the program.
bool read_car (double, double); // Read the car definition file.
bool read_track (double, double); // Read the track definition file.
bool read_world (double, double); // Read the world definition file.
bool reset_car (double, double); // Put the car back on the track.
bool restart_car (double, double); // Put the car at the starting line.
bool cycle_view (double, double); // Change the view.
bool toggle_graphics (double, double);
bool focus_previous_car (double, double);
bool focus_next_car (double, double);
void set_focused_car (size_t index);
bool replay (double, double);
virtual Control& joystick () { return m_joystick; }
virtual Control& keyboard () { return m_keyboard; }
virtual Control& mouse () { return m_mouse; }
private:
virtual void draw_timing_info ();
void draw_leaderboard (Vamos_Media::Two_D& screen);
void draw_lap_times (Vamos_Media::Two_D& screen);
friend class World_Reader;
friend class Controls_Reader;
std::string m_world_file;
std::string m_controls_file;
Timer m_timer;
Sounds* mp_sounds;
Control m_keyboard;
Control m_joystick;
Control m_mouse;
Gl_Window* mp_window;
GLuint m_font;
enum View
{
BODY_VIEW = 0,
MAP_VIEW,
WORLD_VIEW,
CHASE_VIEW
};
View m_view;
int m_frames;
bool m_paused;
bool m_update_graphics;
bool m_done;
Map m_map;
void check_for_events ();
void animate ();
void update_car_timing ();
/// Set the starting lights at the beginning of the race.
void set_starting_lights ();
void play_sounds ();
void display ();
void reshape (int width, int height);
void update_track_bounds ();
void map_key_press (int index); //!! factor out map class?
void show_full_window ();
void set_car_view (Vamos_Body::Car* car);
void set_map_view ();
void set_world_view (const Vamos_Track::Camera& camera);
void set_world_view (const Vamos_Geometry::Three_Vector& camera_position,
const Vamos_Geometry::Three_Vector& target_position,
double vertical_field_angle);
void draw_track (bool draw_sky,
const Vamos_Geometry::Three_Vector&
view_position = Vamos_Geometry::
Three_Vector (0.0, 0.0, 0.0));
void draw_cars (bool draw_interior, bool draw_focused_car = true);
void draw_mirror_views ();
void show_scene ();
void initialize_graphics (int* argc, char** argv);
void set_attributes ();
};
class World_Reader : public Vamos_Media::XML_Parser
{
void on_start_tag (const Vamos_Media::XML_Tag& tag);
void on_end_tag (const Vamos_Media::XML_Tag& tag);
void on_data (std::string data_string);
std::string m_path;
Gl_World* mp_world;
public:
World_Reader (std::string file_name, Gl_World* world);
};
struct Unknown_Function
{
std::string m_function;
Unknown_Function (std::string func) : m_function (func) {};
};
// Controls_Reader is here because it needs Gl_World's
// declarations. If it were in Controls.h, then Controls.h and
// Gl_World.h would be dependent on each other.
class Controls_Reader : public Vamos_Media::XML_Parser
{
void on_start_tag (const Vamos_Media::XML_Tag& tag);
void on_end_tag (const Vamos_Media::XML_Tag& tag);
void on_data (std::string data_string);
void register_callback (std::map <std::string, Callback_Function>::iterator it,
Control_Handler* handler);
Gl_World* mp_world;
std::map <std::string, Callback_Function> m_world_function_map;
std::map <std::string, Callback_Function> m_driver_function_map;
enum Control_Type
{
KEY,
JOYSTICK_BUTTON,
JOYSTICK_AXIS,
MOUSE_BUTTON,
MOUSE_MOTION
};
Control_Type m_type;
int m_control;
Vamos_Geometry::Direction m_direction;
std::string m_function;
double m_factor;
double m_offset;
double m_deadband;
double m_upper_deadband;
double m_time;
public:
Controls_Reader (std::string file_name, Gl_World* world);
};
}
#endif // not _GL_WORLD_