gxscene.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2002-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Sat Nov 23 17:42:51 2002
00010 // commit: $Id: gxscene.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/gfx/gxscene.cc $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_GFX_GXSCENE_CC_UTC20050626084025_DEFINED
00035 #define GROOVX_GFX_GXSCENE_CC_UTC20050626084025_DEFINED
00036 
00037 #include "gxscene.h"
00038 
00039 #include "nub/scheduler.h"
00040 
00041 #include "rutz/shared_ptr.h"
00042 
00043 #include "rutz/trace.h"
00044 #include "rutz/debug.h"
00045 GVX_DBG_REGISTER
00046 
00048 //
00049 // GxScene member definitions
00050 //
00052 
00053 GxScene::GxScene(nub::soft_ref<Gfx::Canvas> canvas,
00054                  rutz::shared_ptr<nub::scheduler> sched) :
00055   itsCanvas(canvas),
00056   itsDrawNode(GxEmptyNode::make()),
00057   itsUndrawNode(GxEmptyNode::make()),
00058   isItVisible(false),
00059   itsCamera(new GxFixedScaleCamera()),
00060   itsWidth(0),
00061   itsHeight(0),
00062   isItHolding(false),
00063   isItRefreshing(true),
00064   isItRefreshed(false),
00065   itsScheduler(sched),
00066   itsTimer(100, true),
00067   slotNodeChanged(nub::slot0::make(this, &GxScene::onNodeChange))
00068 {
00069 GVX_TRACE("GxScene::GxScene");
00070   itsTimer.sig_timeout.connect(this, &GxScene::fullRender);
00071   itsCamera->sigNodeChanged.connect(slotNodeChanged);
00072 }
00073 
00074 GxScene::~GxScene() throw()
00075 {
00076 GVX_TRACE("GxScene::~GxScene");
00077 }
00078 
00079 void GxScene::render()
00080 {
00081 GVX_TRACE("GxScene::render");
00082 
00083   try
00084     {
00085       Gfx::MatrixSaver msaver(*itsCanvas);
00086       Gfx::AttribSaver asaver(*itsCanvas);
00087 
00088       itsCamera->draw(*itsCanvas);
00089       itsDrawNode->draw(*itsCanvas);
00090       itsUndrawNode = itsDrawNode;
00091 
00092       isItRefreshed = true;
00093     }
00094   catch (...)
00095     {
00096       // Here, something failed during rendering, so just go invisible
00097       setVisibility(false);
00098       throw;
00099     }
00100 }
00101 
00102 void GxScene::fullRender()
00103 {
00104 GVX_TRACE("GxScene::fullRender");
00105 
00106   // (1) Clear the screen (but only if we are not "holding")
00107   if( !isItHolding )
00108     {
00109       itsCanvas->clearColorBuffer();
00110     }
00111 
00112   // (2) Render the current object
00113   if ( isItVisible )
00114     {
00115       render();
00116     }
00117 
00118   // (3) Flush the graphics stream
00119   itsCanvas->flushOutput();
00120 }
00121 
00122 void GxScene::undraw()
00123 {
00124 GVX_TRACE("GxScene::undraw");
00125   itsUndrawNode->undraw(*itsCanvas);
00126   itsCanvas->flushOutput();
00127 }
00128 
00129 void GxScene::clearscreen()
00130 {
00131 GVX_TRACE("GxScene::clearscreen");
00132   itsCanvas->clearColorBuffer();
00133   setDrawable(nub::ref<GxNode>(GxEmptyNode::make()));
00134   itsUndrawNode = nub::ref<GxNode>(GxEmptyNode::make());
00135   isItVisible = false;
00136 }
00137 
00138 void GxScene::fullClearscreen()
00139 {
00140 GVX_TRACE("GxScene::fullClearscreen");
00141   clearscreen();
00142   itsCanvas->flushOutput();
00143 }
00144 
00145 void GxScene::setVisibility(bool val)
00146 {
00147 GVX_TRACE("GxScene::setVisibility");
00148   isItVisible = val;
00149   if ( !isItVisible )
00150     {
00151       fullClearscreen();
00152     }
00153 }
00154 
00155 void GxScene::setCamera(const nub::ref<GxCamera>& cam)
00156 {
00157 GVX_TRACE("GxScene::setCamera");
00158   itsCamera->sigNodeChanged.disconnect(slotNodeChanged);
00159 
00160   itsCamera = cam;
00161 
00162   itsCamera->reshape(*itsCanvas, itsWidth, itsHeight);
00163 
00164   itsCamera->sigNodeChanged.connect(slotNodeChanged);
00165 
00166   fullRender();
00167 }
00168 
00169 void GxScene::setDrawable(const nub::ref<GxNode>& node)
00170 {
00171 GVX_TRACE("GxScene::setDrawable");
00172   itsDrawNode->sigNodeChanged.disconnect(slotNodeChanged);
00173 
00174   itsDrawNode = node;
00175 
00176   itsDrawNode->sigNodeChanged.connect(slotNodeChanged);
00177 }
00178 
00179 void GxScene::flushChanges()
00180 {
00181 GVX_TRACE("GxScene::flushChanges");
00182   if (isItRefreshing && !isItRefreshed)
00183     {
00184       itsCamera->reshape(*itsCanvas, itsWidth, itsHeight);
00185       fullRender();
00186     }
00187 }
00188 
00189 void GxScene::onNodeChange()
00190 {
00191 GVX_TRACE("GxScene::onNodeChange");
00192   isItRefreshed = false;
00193   flushChanges();
00194 }
00195 
00196 void GxScene::reshape(int width, int height)
00197 {
00198 GVX_TRACE("GxScene::reshape");
00199   itsWidth = width;
00200   itsHeight = height;
00201   itsCamera->reshape(*itsCanvas, itsWidth, itsHeight);
00202 }
00203 
00204 void GxScene::animate(unsigned int framesPerSecond)
00205 {
00206 GVX_TRACE("GxScene::animate");
00207   if (framesPerSecond == 0)
00208     {
00209       itsTimer.cancel();
00210     }
00211   else
00212     {
00213       itsTimer.set_delay_msec(static_cast<unsigned int>(1000.0/framesPerSecond));
00214       itsTimer.schedule(itsScheduler);
00215     }
00216 }
00217 
00218 static const char __attribute__((used)) vcid_groovx_gfx_gxscene_cc_utc20050626084025[] = "$Id: gxscene.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00219 #endif // !GROOVX_GFX_GXSCENE_CC_UTC20050626084025_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:38 2008 by Doxygen version 1.5.5.