renderable.cpp

Go to the documentation of this file.
00001 /*
00002  * renderable.cpp
00003  *
00004  * Copyright (C) 2008,2009  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  */
00032 
00033 // includes --------------------------------------------------------------------
00034 #include "renderable.h"                 // always include our own header first!
00035 
00036 
00037 namespace glut {
00038 
00039 
00040 // interface destructor
00041 Renderable::~Renderable(void) throw() { }
00042 RenderQueue::~RenderQueue(void) throw() { }
00043 
00044 
00045 ////////////////////////////////////////////////////////////////////////////////
00046 //
00047 //      RQImpl -- class that implements the glut::RenderQueue interface
00048 //
00049 ////////////////////////////////////////////////////////////////////////////////
00050 
00051 class RQImpl : public RenderQueue {
00052 public:
00053         // constructor, destructor ---------------------------------------------
00054         RQImpl(void) throw();
00055         ~RQImpl(void) throw();
00056 
00057         // public class methods ------------------------------------------------
00058         void initialize(IN int nSlots);
00059 
00060         // glut::RenderQueue class interface methods ---------------------------
00061         poly_request_t * grabRequestSlot(void) throw();
00062         poly_request_t * popRequest(void) throw();
00063 
00064 private:
00065         // private member data -------------------------------------------------
00066         int                     m_nMax;     // max number of requests (slots)
00067         int                     m_nCurr;    // current number of requests taken
00068         poly_request_t *        m_polys;    // array of polygon requests
00069 };
00070 
00071 
00072 
00073 RQImpl::RQImpl
00074 (
00075 void
00076 )
00077 throw()
00078 {
00079         m_nMax = 0;
00080         m_nCurr = 0;
00081         m_polys = NULL;
00082 }
00083 
00084 
00085 
00086 RQImpl::~RQImpl
00087 (
00088 void
00089 )
00090 throw()
00091 {
00092         if (m_polys) {
00093                 delete[] m_polys;
00094         }
00095 }
00096 
00097 
00098 
00099 void
00100 RQImpl::initialize
00101 (
00102 IN int nSlots
00103 )
00104 {
00105         ASSERT(nSlots > 0, "bad slot count: %d", nSlots);
00106 
00107         m_nMax = nSlots;
00108         m_nCurr = 0;
00109 
00110         ASSERT(!m_polys, "already have polygon array?");
00111         m_polys = new poly_request_t[m_nMax];
00112         ASSERT(m_polys, "out of memory");
00113 }
00114 
00115 
00116 
00117 poly_request_t *
00118 RQImpl::grabRequestSlot
00119 (
00120 void
00121 )
00122 throw()
00123 {
00124         ASSERT(m_polys, "null");
00125         ASSERT(m_nMax >  0, "Bad max size: %d", m_nMax);
00126         ASSERT(m_nCurr >= 0 && m_nCurr <= m_nMax, "Bad current size: %d",
00127             m_nCurr);
00128         if (m_nCurr >= m_nMax) {
00129                 return NULL;    // no more empty slots
00130         }
00131 
00132         // we have an open slot!  Hand it over
00133         poly_request_t * ptr = m_polys + m_nCurr;
00134         ++m_nCurr;
00135         ptr->clear();   // reset before returning
00136         return ptr;
00137 }
00138 
00139 
00140 
00141 poly_request_t *
00142 RQImpl::popRequest
00143 (
00144 void
00145 )
00146 throw()
00147 {
00148         ASSERT(m_polys, "null");
00149         ASSERT(m_nMax >  0, "Bad max size: %d", m_nMax);
00150         ASSERT(m_nCurr >= 0 && m_nCurr <= m_nMax, "Bad current size: %d",
00151             m_nCurr);
00152         if (m_nCurr < 1) {
00153                 return NULL;    // queue is empty!
00154         }
00155 
00156         --m_nCurr;
00157         return m_polys + m_nCurr;
00158 }
00159 
00160 
00161 
00162 ////////////////////////////////////////////////////////////////////////////////
00163 //
00164 //      Renderable -- default interface method implementations
00165 //
00166 ////////////////////////////////////////////////////////////////////////////////
00167 
00168 bool
00169 Renderable::isAnimateable
00170 (
00171 void
00172 )
00173 const
00174 throw()
00175 {
00176         return false;
00177 }
00178 
00179 
00180 
00181 void
00182 Renderable::getAnimationState
00183 (
00184 OUT std::string& state
00185 )
00186 {
00187         state.clear();
00188 }
00189 
00190 
00191 
00192 bool
00193 Renderable::setAnimationState
00194 (
00195 IN const char * state
00196 )
00197 {
00198         ASSERT(state, "null");
00199 
00200         return false;
00201 }
00202 
00203 
00204 
00205 void
00206 Renderable::getAnimationVerbs
00207 (
00208 OUT VecString& verbs
00209 )
00210 {
00211         verbs.clear();
00212 }
00213 
00214 
00215 
00216 bool
00217 Renderable::setAnimation
00218 (
00219 IN const char * verb
00220 )
00221 {
00222         return false;
00223 }
00224 
00225 
00226 
00227 ////////////////////////////////////////////////////////////////////////////////
00228 //
00229 //      public API
00230 //
00231 ////////////////////////////////////////////////////////////////////////////////
00232 
00233 smart_ptr<RenderQueue>
00234 RenderQueue::create
00235 (
00236 IN int nSlots
00237 )
00238 {
00239         ASSERT(nSlots > 0, "Bad queue slot count: %d", nSlots);
00240 
00241         smart_ptr<RQImpl> local = new RQImpl;
00242         ASSERT(local, "out of memory");
00243 
00244         local->initialize(nSlots);
00245 
00246         return local;
00247 }
00248 
00249 
00250 
00251 };      // glut namespace
00252