dialog-draw.cpp

Go to the documentation of this file.
00001 /*
00002  * dialog-draw.cpp
00003  *
00004  * Copyright (C) 2009,2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Implementation of an OpenGL-based dialog drawer.  See dialog-draw.h
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "dialog-draw.h"                // always include our own header first!
00036 
00037 #include "common/wave_ex.h"
00038 #include "glut-font/glut-font.h"
00039 #include "perf/perf.h"
00040 #include "wave-glut/glut_2d.h"
00041 #include "wave-glut/wave-glut.h"
00042 
00043 
00044 namespace dialog {
00045 
00046 
00047 ////////////////////////////////////////////////////////////////////////////////
00048 //
00049 //      static helper methods
00050 //
00051 ////////////////////////////////////////////////////////////////////////////////
00052 
00053 
00054 ////////////////////////////////////////////////////////////////////////////////
00055 //
00056 //      OGLDrawer -- implementation for the dialog::Drawer interface
00057 //
00058 ////////////////////////////////////////////////////////////////////////////////
00059 
00060 class OGLDrawer : public Drawer {
00061 public:
00062         ~OGLDrawer(void) throw() { }
00063 
00064         // public class methods ------------------------------------------------
00065         void initialize(IN ogl_dialog_draw_t& odd);
00066 
00067         // dialog::Drawer class interface methods ------------------------------
00068         font_size_t getFontSizing(IN eElementType type,
00069                                 IN const char * text);
00070         border_size_t getBorderSizing(IN eElementType type);
00071         void drawString(IN eElementType type,
00072                                 IN const point_t& pos,
00073                                 IN const char * text);
00074         void drawRectWithBorder(IN eElementType type,
00075                                 IN const rect_t& r);
00076         void drawCircle(IN const point_t& pos,
00077                                 IN float radius);
00078 
00079 private:
00080         // private helper methods ----------------------------------------------
00081         glut::Font * getFont(IN eElementType type) { return m_font; }
00082 
00083         // private member data -------------------------------------------------
00084         smart_ptr<glut::Font>           m_font;
00085 };
00086 
00087 
00088 
00089 void
00090 OGLDrawer::initialize
00091 (
00092 IN ogl_dialog_draw_t& odd
00093 )
00094 {
00095         ASSERT(odd.isValid(), "invalid OpenGL dialog drawer data");
00096 
00097         ASSERT(odd.font, "null font");
00098         m_font = odd.font;
00099 }
00100 
00101 
00102 
00103 ////////////////////////////////////////////////////////////////////////////////
00104 //
00105 //      OGLDrawer -- dialog::Drawer class interface methods
00106 //
00107 ////////////////////////////////////////////////////////////////////////////////
00108 
00109 font_size_t
00110 OGLDrawer::getFontSizing
00111 (
00112 IN eElementType type,
00113 IN const char * text
00114 )
00115 {
00116         ASSERT(text, "null");
00117 
00118         font_size_t fs;
00119 
00120         glut::font_rect_t fr = this->getFont(type)->getBoundingRect(text);
00121 
00122         fs.lead = fr.lead;
00123         fs.trail = fr.trail;
00124         fs.rise = fr.rise;
00125         fs.drop = fr.drop;
00126 
00127         return fs;
00128 }
00129 
00130 
00131 
00132 border_size_t
00133 OGLDrawer::getBorderSizing
00134 (
00135 IN eElementType type
00136 )
00137 {
00138         border_size_t bs;
00139         bs.size = 4;
00140         return bs;
00141 }
00142 
00143 
00144 
00145 void
00146 OGLDrawer::drawString
00147 (
00148 IN eElementType type,
00149 IN const point_t& pos,
00150 IN const char * text
00151 )
00152 {
00153         float z = 0;
00154         glut::Font * font = this->getFont(type);
00155         ASSERT(font, "null");
00156         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00157 
00158 /*
00159         // debug only: display font rect
00160         glut::font_rect_t fr = font->getBoundingRect(text);
00161         glColor3f(0.8, 0.8, 0.8);
00162  */     
00163 
00164         // display font
00165         glColor3f(1.0, 1.0, 1.0);
00166         font->display(pos.x, pos.y, z, text);
00167 }
00168 
00169 
00170 
00171 void
00172 OGLDrawer::drawRectWithBorder
00173 (
00174 IN eElementType type,
00175 IN const rect_t& r
00176 )
00177 {
00178 //      glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
00179 //      glEnable(GL_COLOR_MATERIAL);
00180 //      glDisable(GL_LIGHTING);
00181 
00182         //r.dump("Drawing!");
00183 
00184 //      glNormal3f(0, 0, -1);
00185 //      if (eElement_Dialog == type) {
00186                 // draw background if dialog
00187                 glEnable(GL_BLEND);
00188                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00189                 glColor4f(0.0, 0.0, 0.0, 0.5);
00190                 glRecti(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1);
00191                 glDisable(GL_BLEND);
00192 //      } else if (eElement_Container == type) {
00193                 // draw background if dialog
00194 //              glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00195 //              glColor3f(0.5, 0.5, 0.5);
00196 //              glRecti(r.left, r.top, r.right, r.bottom);
00197 //      }
00198 
00199         // draw border
00200         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00201         glColor3f(1.0, 1.0, 1.0);
00202         glRecti(r.left, r.top, r.right, r.bottom);
00203 
00204 //      glDisable(GL_COLOR_MATERIAL);
00205 }
00206 
00207 
00208 
00209 void
00210 OGLDrawer::drawCircle
00211 (
00212 IN const point_t& pos,
00213 IN float radius
00214 )
00215 {
00216         ASSERT_THROW(radius >= 0, "Bad radius " << radius);
00217 
00218         DPRINTF("Circle: %f", radius);
00219 }
00220 
00221 
00222 
00223 ////////////////////////////////////////////////////////////////////////////////
00224 //
00225 //      OGLDrawer -- private helper methods
00226 //
00227 ////////////////////////////////////////////////////////////////////////////////
00228 
00229 ////////////////////////////////////////////////////////////////////////////////
00230 //
00231 //      public API
00232 //
00233 ////////////////////////////////////////////////////////////////////////////////
00234 
00235 smart_ptr<Drawer>
00236 createOpenGLDrawer
00237 (
00238 IN ogl_dialog_draw_t& odd
00239 )
00240 {
00241         ASSERT(odd.isValid(), "Invalid OpenGL drawer data");
00242 
00243         smart_ptr<OGLDrawer> local = new OGLDrawer;
00244         ASSERT(local, "out of memory");
00245 
00246         local->initialize(odd);
00247 
00248         return local;
00249 }
00250 
00251 
00252 
00253 };      // dialog namespace
00254