00001 #ifndef GSGL_FRAMEWORK_SIMULATION_H 00002 #define GSGL_FRAMEWORK_SIMULATION_H 00003 00004 // 00005 // $Id: simulation.hpp 2 2008-03-01 20:58:50Z kulibali $ 00006 // 00007 // Copyright (c) 2008, The Periapsis Project. All rights reserved. 00008 // 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are 00011 // met: 00012 // 00013 // * Redistributions of source code must retain the above copyright notice, 00014 // this list of conditions and the following disclaimer. 00015 // 00016 // * Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // * Neither the name of the The Periapsis Project nor the names of its 00021 // contributors may be used to endorse or promote products derived from 00022 // this software without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00025 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00026 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00027 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00028 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00029 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00030 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00031 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00032 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00033 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00034 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00035 // 00036 00037 #include "scenegraph/scenegraph.hpp" 00038 #include "scenegraph/node.hpp" 00039 #include "scenegraph/context.hpp" 00040 #include "scenegraph/freeview.hpp" 00041 #include "platform/display.hpp" 00042 #include "platform/font.hpp" 00043 #include "data/config.hpp" 00044 #include "data/list.hpp" 00045 00046 00047 namespace gsgl 00048 { 00049 00050 namespace scenegraph 00051 { 00052 00053 /// Encapsulates a simulation. 00054 class SCENEGRAPH_API simulation 00055 : public scenegraph_object 00056 { 00057 bool running; 00058 00059 platform::display *console; ///< The game's single console. 00060 context *sim_context; 00061 00062 node *scenery; ///< The root node of the simulation scene graph. 00063 data::list<node *> non_scenery_nodes; ///< These get removed from the scene graph & deleted when the simulation ends. 00064 00065 freeview *view; ///< The main view node. 00066 00067 time_t start_time; 00068 gsgl::real_t time_scale; 00069 00070 platform::font *info_font; 00071 data::simple_array<gsgl::real_t> frame_deltas; 00072 00073 node::pre_draw_rec pre_rec; 00074 00075 public: 00076 simulation(const data::config_record & sim_config, 00077 platform::display *console, 00078 context *sim_context, 00079 scenegraph::node *scenery); 00080 ~simulation(); 00081 00082 bool is_running() const { return running; } 00083 context *get_context() { return sim_context; } 00084 00085 void init(); ///< This is called at the beginning of a simulation. It will initialize all the nodes in the scene graph. 00086 void pre_draw(); ///< This is called at the beginning of each frame. It records drawing information for all the nodes in the scene graph. 00087 00088 void draw(); ///< This is called to draw the frame. It may be called concurrently with update(), which is updating the next frame while the current one is being drawn. 00089 void update(); ///< This is called to update the scene graph. It may be called concurrently with draw(), which is drawing the previous frame. 00090 void cleanup(); ///< This is called at the end of the simulation. 00091 00092 bool handle_event(sg_event & e); ///< This is called to pass events to the scene graph after they have been left unhandled by the UI. 00093 00094 private: 00095 void init_context(); 00096 void update_context(); 00097 void cleanup_context(); 00098 00099 void init_node(scenegraph::node *n); 00100 void update_node(scenegraph::node *n); 00101 void cleanup_node(scenegraph::node *n); 00102 bool handle_event(sg_event & e, node *n); 00103 }; // class simulation 00104 00105 00106 } // namespace scenegraph 00107 00108 } // namespace gsgl 00109 00110 #endif