Panda3D
 All Classes Functions Variables Enumerations
rocketRegion.cxx
00001 // Filename: rocketRegion.cxx
00002 // Created by:  rdb (30Nov11)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "rocketRegion.h"
00016 #include "graphicsOutput.h"
00017 #include "orthographicLens.h"
00018 #include "pStatTimer.h"
00019 
00020 #if defined(HAVE_ROCKET_DEBUGGER) && !defined(CPPPARSER)
00021 #include <Rocket/Debugger.h>
00022 #endif
00023 
00024 TypeHandle RocketRegion::_type_handle;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: RocketRegion::Constructor
00028 //       Access: Protected
00029 //  Description: Make sure that context_name is unique.
00030 ////////////////////////////////////////////////////////////////////
00031 RocketRegion::
00032 RocketRegion(GraphicsOutput *window, const LVecBase4 &dr_dimensions,
00033              const string &context_name) :
00034   DisplayRegion(window, dr_dimensions) {
00035 
00036   // A hack I don't like.  libRocket's decorator system has
00037   // a bug somewhere, and this seems to be a workaround.
00038   if (Rocket::Core::GetRenderInterface() == NULL) {
00039     Rocket::Core::SetRenderInterface(&_interface);
00040   }
00041 
00042   int pl, pr, pb, pt;
00043   get_pixels(pl, pr, pb, pt);
00044   Rocket::Core::Vector2i dimensions (pr - pl, pt - pb);
00045 
00046   rocket_cat.debug()
00047     << "Setting initial context dimensions to ("
00048     << dimensions.x << ", " << dimensions.y << ")\n";
00049 
00050   _context = Rocket::Core::CreateContext(context_name.c_str(),
00051                                          dimensions, &_interface);
00052   nassertv(_context != NULL);
00053 
00054   _lens = new OrthographicLens;
00055   _lens->set_film_size(dimensions.x, -dimensions.y);
00056   _lens->set_film_offset(dimensions.x * 0.5, dimensions.y * 0.5);
00057   _lens->set_near_far(-1, 1);
00058   set_camera(new Camera(context_name, _lens));
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: RocketRegion::Destructor
00063 //       Access: Public, Virtual
00064 //  Description:
00065 ////////////////////////////////////////////////////////////////////
00066 RocketRegion::
00067 ~RocketRegion() {
00068   if (Rocket::Core::GetRenderInterface() == &_interface) {
00069     Rocket::Core::SetRenderInterface(NULL);
00070   }
00071 
00072   if (_context != NULL) {
00073     if (_context->GetReferenceCount() > 1) {
00074       _context->RemoveReference();
00075       return;
00076     }
00077 
00078     // We need to do this because libRocket may call into Python
00079     // code to throw destruction events.
00080 #ifdef HAVE_ROCKET_PYTHON
00081     PyGILState_STATE gstate;
00082     gstate = PyGILState_Ensure();
00083 #endif
00084 
00085     _context->RemoveReference();
00086 
00087 #ifdef HAVE_ROCKET_PYTHON
00088     PyGILState_Release(gstate);
00089 #endif
00090   }
00091 }
00092 
00093 ////////////////////////////////////////////////////////////////////
00094 //     Function: RocketRegion::do_cull
00095 //       Access: Protected, Virtual
00096 //  Description: Performs a cull traversal for this region.
00097 ////////////////////////////////////////////////////////////////////
00098 void RocketRegion::
00099 do_cull(CullHandler *cull_handler, SceneSetup *scene_setup,
00100         GraphicsStateGuardian *gsg, Thread *current_thread) {
00101 
00102   PStatTimer timer(get_cull_region_pcollector(), current_thread);
00103 
00104   // We (unfortunately) need to do this because libRocket
00105   // may call into Python code to throw events.
00106 #ifdef HAVE_ROCKET_PYTHON
00107   PyGILState_STATE gstate;
00108   gstate = PyGILState_Ensure();
00109 #endif
00110 
00111   int pl, pr, pb, pt;
00112   get_pixels(pl, pr, pb, pt);
00113   Rocket::Core::Vector2i dimensions (pr - pl, pt - pb);
00114 
00115   if (_context->GetDimensions() != dimensions) {
00116     rocket_cat.debug() << "Setting context dimensions to ("
00117       << dimensions.x << ", " << dimensions.y << ")\n";
00118 
00119     _context->SetDimensions(dimensions);
00120 
00121     _lens->set_film_size(dimensions.x, -dimensions.y);
00122     _lens->set_film_offset(dimensions.x * 0.5, dimensions.y * 0.5);
00123   }
00124 
00125   if (_input_handler != NULL) {
00126     _input_handler->update_context(_context, pl, pb);
00127   } else {
00128     _context->Update();
00129   }
00130 
00131   CullTraverser *trav = get_cull_traverser();
00132   trav->set_cull_handler(cull_handler);
00133   trav->set_scene(scene_setup, gsg, get_incomplete_render());
00134   trav->set_view_frustum(NULL);
00135 
00136   _interface.render(_context, trav);
00137 
00138 #ifdef HAVE_ROCKET_PYTHON
00139   PyGILState_Release(gstate);
00140 #endif
00141 
00142   trav->end_traverse();
00143 }
00144 
00145 ////////////////////////////////////////////////////////////////////
00146 //     Function: RocketRegion::init_debugger
00147 //       Access: Published
00148 //  Description: Initializes the libRocket debugger.  This will
00149 //               return false if the debugger failed to initialize,
00150 //               or if support for the debugger has not been built
00151 //               in (for example in an optimize=4 build).
00152 ////////////////////////////////////////////////////////////////////
00153 bool RocketRegion::
00154 init_debugger() {
00155 #ifdef HAVE_ROCKET_DEBUGGER
00156   return Rocket::Debugger::Initialise(_context);
00157 #else
00158   return false;
00159 #endif
00160 }
00161 
00162 ////////////////////////////////////////////////////////////////////
00163 //     Function: RocketRegion::set_debugger_visible
00164 //       Access: Published
00165 //  Description: Sets whether the debugger should be visible.
00166 ////////////////////////////////////////////////////////////////////
00167 void RocketRegion::
00168 set_debugger_visible(bool visible) {
00169 #ifdef HAVE_ROCKET_DEBUGGER
00170   Rocket::Debugger::SetVisible(visible);
00171 #endif
00172 }
00173 
00174 ////////////////////////////////////////////////////////////////////
00175 //     Function: RocketRegion::is_debugger_visible
00176 //       Access: Published
00177 //  Description: Returns true if the debugger is visible.
00178 ////////////////////////////////////////////////////////////////////
00179 bool RocketRegion::
00180 is_debugger_visible() const {
00181 #ifdef HAVE_ROCKET_DEBUGGER
00182   return Rocket::Debugger::IsVisible();
00183 #else
00184   return false;
00185 #endif
00186 }
 All Classes Functions Variables Enumerations