Panda3D

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 TypeHandle RocketRegion::_type_handle;
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: RocketRegion::Constructor
00024 //       Access: Protected
00025 //  Description: Make sure that context_name is unique.
00026 ////////////////////////////////////////////////////////////////////
00027 RocketRegion::
00028 RocketRegion(GraphicsOutput *window, const LVecBase4 &dr_dimensions,
00029              const string &context_name) :
00030   DisplayRegion(window, dr_dimensions) {
00031 
00032   int pl, pr, pb, pt;
00033   get_pixels(pl, pr, pb, pt);
00034   Rocket::Core::Vector2i dimensions (pr - pl, pt - pb);
00035 
00036   rocket_cat.debug()
00037     << "Setting initial context dimensions to ("
00038     << dimensions.x << ", " << dimensions.y << ")\n";
00039 
00040   _context = Rocket::Core::CreateContext(context_name.c_str(),
00041                                          dimensions, &_interface);
00042   nassertv(_context != NULL);
00043 
00044   _lens = new OrthographicLens;
00045   _lens->set_film_size(dimensions.x, -dimensions.y);
00046   _lens->set_film_offset(dimensions.x * 0.5, dimensions.y * 0.5);
00047   _lens->set_near_far(-1, 1);
00048   set_camera(new Camera(context_name, _lens));
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: RocketRegion::Destructor
00053 //       Access: Public, Virtual
00054 //  Description:
00055 ////////////////////////////////////////////////////////////////////
00056 RocketRegion::
00057 ~RocketRegion() {
00058   if (_context != NULL) {
00059     if (_context->GetReferenceCount() > 1) {
00060       _context->RemoveReference();
00061       return;
00062     }
00063 
00064     // We need to do this because libRocket may call into Python
00065     // code to throw destruction events.
00066 #ifdef HAVE_ROCKET_PYTHON
00067     PyGILState_STATE gstate;
00068     gstate = PyGILState_Ensure();
00069 #endif
00070 
00071     _context->RemoveReference();
00072 
00073 #ifdef HAVE_ROCKET_PYTHON
00074     PyGILState_Release(gstate);
00075 #endif
00076   }
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: RocketRegion::do_cull
00081 //       Access: Protected, Virtual
00082 //  Description: Performs a cull traversal for this region.
00083 ////////////////////////////////////////////////////////////////////
00084 void RocketRegion::
00085 do_cull(CullHandler *cull_handler, SceneSetup *scene_setup,
00086         GraphicsStateGuardian *gsg, Thread *current_thread) {
00087 
00088   PStatTimer timer(get_cull_region_pcollector(), current_thread);
00089 
00090   // We (unfortunately) need to do this because libRocket
00091   // may call into Python code to throw events.
00092 #ifdef HAVE_ROCKET_PYTHON
00093   PyGILState_STATE gstate;
00094   gstate = PyGILState_Ensure();
00095 #endif
00096 
00097   int pl, pr, pb, pt;
00098   get_pixels(pl, pr, pb, pt);
00099   Rocket::Core::Vector2i dimensions (pr - pl, pt - pb);
00100 
00101   if (_context->GetDimensions() != dimensions) {
00102     rocket_cat.debug() << "Setting context dimensions to ("
00103       << dimensions.x << ", " << dimensions.y << ")\n";
00104 
00105     _context->SetDimensions(dimensions);
00106 
00107     _lens->set_film_size(dimensions.x, -dimensions.y);
00108     _lens->set_film_offset(dimensions.x * 0.5, dimensions.y * 0.5);
00109   }
00110 
00111   if (_input_handler != NULL) {
00112     _input_handler->update_context(_context, pl, pb);
00113   } else {
00114     _context->Update();
00115   }
00116 
00117   CullTraverser *trav = get_cull_traverser();
00118   trav->set_cull_handler(cull_handler);
00119   trav->set_scene(scene_setup, gsg, get_incomplete_render());
00120   trav->set_view_frustum(NULL);
00121 
00122   _interface.render(_context, trav);
00123 
00124 #ifdef HAVE_ROCKET_PYTHON
00125   PyGILState_Release(gstate);
00126 #endif
00127 
00128   trav->end_traverse();
00129 }
 All Classes Functions Variables Enumerations