00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
00097 setVisibility(false);
00098 throw;
00099 }
00100 }
00101
00102 void GxScene::fullRender()
00103 {
00104 GVX_TRACE("GxScene::fullRender");
00105
00106
00107 if( !isItHolding )
00108 {
00109 itsCanvas->clearColorBuffer();
00110 }
00111
00112
00113 if ( isItVisible )
00114 {
00115 render();
00116 }
00117
00118
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