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/glut.h"
00039 #include "glut/glut_2d.h"
00040 #include "glut-font/glut-font.h"
00041 #include "perf/perf.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(void);
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)
00082             { return glut::getDefaultFont(); }
00083 
00084         // private member data -------------------------------------------------
00085 };
00086 
00087 
00088 
00089 void
00090 OGLDrawer::initialize
00091 (
00092 void
00093 )
00094 {
00095 }
00096 
00097 
00098 
00099 ////////////////////////////////////////////////////////////////////////////////
00100 //
00101 //      OGLDrawer -- dialog::Drawer class interface methods
00102 //
00103 ////////////////////////////////////////////////////////////////////////////////
00104 
00105 font_size_t
00106 OGLDrawer::getFontSizing
00107 (
00108 IN eElementType type,
00109 IN const char * text
00110 )
00111 {
00112         ASSERT(text, "null");
00113 
00114         font_size_t fs;
00115 
00116         glut::font_rect_t fr = this->getFont(type)->getBoundingRect(text);
00117 
00118         fs.width = fr.lead + fr.trail;
00119         fs.height = fr.drop + fr.rise;
00120 
00121         return fs;
00122 }
00123 
00124 
00125 
00126 border_size_t
00127 OGLDrawer::getBorderSizing
00128 (
00129 IN eElementType type
00130 )
00131 {
00132         border_size_t bs;
00133         bs.size = 4;
00134         return bs;
00135 }
00136 
00137 
00138 
00139 void
00140 OGLDrawer::drawString
00141 (
00142 IN eElementType type,
00143 IN const point_t& pos,
00144 IN const char * text
00145 )
00146 {
00147         float z = 0;
00148         glColor3f(1.0, 1.0, 1.0);
00149         this->getFont(type)->display(
00150             pos.x,
00151             pos.y + 11,
00152             z,
00153             text);
00154 }
00155 
00156 
00157 
00158 void
00159 OGLDrawer::drawRectWithBorder
00160 (
00161 IN eElementType type,
00162 IN const rect_t& r
00163 )
00164 {
00165 //      glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
00166 //      glEnable(GL_COLOR_MATERIAL);
00167         glDisable(GL_LIGHTING);
00168 
00169         //r.dump("Drawing!");
00170 
00171 //      glNormal3f(0, 0, -1);
00172 //      if (eElement_Dialog == type) {
00173                 // draw background if dialog
00174                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00175                 glColor3f(0.0, 0.0, 0.0);
00176                 glRecti(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1);
00177 //      } else if (eElement_Container == type) {
00178                 // draw background if dialog
00179 //              glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00180 //              glColor3f(0.5, 0.5, 0.5);
00181 //              glRecti(r.left, r.top, r.right, r.bottom);
00182 //      }
00183 
00184         // draw border
00185         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00186         glColor3f(1.0, 1.0, 1.0);
00187         glRecti(r.left, r.top, r.right, r.bottom);
00188 
00189 //      glDisable(GL_COLOR_MATERIAL);
00190 }
00191 
00192 
00193 
00194 void
00195 OGLDrawer::drawCircle
00196 (
00197 IN const point_t& pos,
00198 IN float radius
00199 )
00200 {
00201         ASSERT_THROW(radius >= 0, "Bad radius " << radius);
00202 
00203         DPRINTF("Circle: %f", radius);
00204 }
00205 
00206 
00207 
00208 ////////////////////////////////////////////////////////////////////////////////
00209 //
00210 //      OGLDrawer -- private helper methods
00211 //
00212 ////////////////////////////////////////////////////////////////////////////////
00213 
00214 ////////////////////////////////////////////////////////////////////////////////
00215 //
00216 //      public API
00217 //
00218 ////////////////////////////////////////////////////////////////////////////////
00219 
00220 smart_ptr<Drawer>
00221 createOpenGLDrawer
00222 (
00223 void
00224 )
00225 {
00226         smart_ptr<OGLDrawer> local = new OGLDrawer;
00227         ASSERT(local, "out of memory");
00228 
00229         local->initialize();
00230 
00231         return local;
00232 }
00233 
00234 
00235 
00236 };      // dialog namespace
00237