random_racer/src/CenterScreenDisplay.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include <vrs/camera.h>
00004 #include <vrs/colorattribute.h>
00005 #include <vrs/font.h>
00006 #include <vrs/opengl/lightmodelgl.h>
00007 #include <vrs/opengl/shapematerialgl.h>
00008 #include <vrs/scaling.h>
00009 #include <vrs/sg/behaviorcallback.h>
00010 #include <vrs/sg/canvas.h>
00011 #include <vrs/sg/resizeevent.h>
00012 #include <vrs/sg/scenething.h>
00013 #include <vrs/text.h>
00014 
00015 #include "ResourceManager.h"
00016 
00017 #include "CenterScreenDisplay.h"
00018 
00019 using namespace VRS;
00020 
00021 
00022 namespace random_racer
00023 {
00024 
00025 const float CenterScreenDisplay::c_pixelsPerChar = 15.0;
00026 
00027 
00028 CenterScreenDisplay::CenterScreenDisplay(SO<Canvas> p_canvas)
00029 {
00030     m_thing = new SceneThing();
00031 
00032     m_cam = Camera::newNormalizedDeviceCoordsCamera();
00033     m_thing->append(m_cam);
00034 
00035     m_thing->append(new LightModelGL(false));
00036 
00037     m_material = new ShapeMaterialGL(Color(1.0, 1.0, 1.0, 1.0));
00038 
00039     m_thing->append(m_material);
00040 
00041     m_font = ResourceManager::get()->loadFont("DejaVuSansMono.ttf",
00042                                               Font::POLYGON);
00043 
00044     m_line1 = new Text(m_font);
00045     m_line2 = new Text(m_font);
00046 
00047     m_line1->setAlignmentH(Text::CENTER_H);
00048     m_line2->setAlignmentH(Text::CENTER_H);
00049 
00050     m_line2->setPosition(Vector(0.0, -1.65, 0.0));
00051 
00052     m_thing->append(m_line1);
00053     m_thing->append(m_line2);
00054 
00055     m_thing->setNodeState(0);
00056 
00057     p_canvas->append(m_thing);
00058 
00059 
00060     m_eventCallback = new BehaviorCallback;
00061     m_eventCallback->setCanvasCallback(new MethodCallback<CenterScreenDisplay>(
00062             this, &CenterScreenDisplay::handleResizeEvents));
00063 
00064     p_canvas->append(m_eventCallback);
00065     m_eventCallback->activate();
00066 }
00067 
00068 CenterScreenDisplay::~CenterScreenDisplay()
00069 {
00070 }
00071 
00072 void
00073 CenterScreenDisplay::timerTick()
00074 {
00075     // std::cout << "TICK (new " << m_newTimer << ")" << std::endl;
00076 
00077     if(m_newTimer)
00078     {
00079         m_newTimer = false;
00080 
00081         stopTimer();
00082         startTimer(50);
00083     }
00084 
00085     Color c = m_material->getPerVertexColor();
00086 
00087     c[3] -= 0.01;
00088 
00089 
00090     if(c[3] <= 0)
00091     {
00092         m_thing->setNodeState(0);
00093 
00094         // std::cout << "STOP" << std::endl;
00095 
00096         stopTimer();
00097     }
00098     else
00099         m_material->setPerVertexColor(c);
00100 }
00101 
00102 void
00103 CenterScreenDisplay::handleResizeEvents()
00104 {
00105     ResizeEvent* event = VRS_Cast(ResizeEvent,
00106                                       m_eventCallback->currentCanvasEvent());
00107     if(event != NULL)
00108     {
00109         double x1, x2, y1, y2, ratio;
00110         ratio = double(event->width()) / double(event->height());
00111 
00112         y2 = double(event->height())/2.0/c_pixelsPerChar;
00113         y1 = -y2;
00114         x2 = y2 * ratio;
00115         x1 = -x2;
00116 
00117         m_thing->remove(m_cam);
00118         m_cam = Camera::newNormalizedDeviceCoordsCamera(x1, x2, y1, y2);
00119         m_thing->prepend(m_cam);
00120     }
00121 }
00122 
00123 void
00124 CenterScreenDisplay::showText(
00125     unsigned int p_msecs,
00126     const std::string& p_line1,
00127     const std::string& p_line2)
00128 {
00129     m_newTimer = true;
00130 
00131     m_thing->setNodeState(
00132         SceneNode::EvaluationOn|SceneNode::TraversalOn);
00133 
00134     m_material->setPerVertexColor(Color(1.0, 1.0, 1.0, 1.0));
00135 
00136     m_line1->setText(p_line1);
00137     m_line2->setText(p_line2);
00138     
00139     if(m_line2->getText().empty())
00140         m_line1->setAlignmentV(Text::CENTER_V);
00141 
00142     else
00143         m_line1->setAlignmentV(Text::BOTTOM);
00144 
00145 
00146     startTimer(p_msecs);
00147 }
00148 
00149 } // namespace random_racer

Generated on Fri May 11 21:01:57 2007 for Random Racer by  doxygen 1.5.1