Crystal Space: csgfx/vertexlistwalker.h Source File (Crystal Space Public API Reference)

| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csgfx/vertexlistwalker.h

Go to the documentation of this file.

    00001 /*
    00002   Copyright (C) 2005 by Marten Svanfeldt
    00003             (C) 2006 by Frank Richter
    00004
    00005   This library is free software; you can redistribute it and/or
    00006   modify it under the terms of the GNU Library General Public
    00007   License as published by the Free Software Foundation; either
    00008   version 2 of the License, or (at your option) any later version.
    00009
    00010   This library is distributed in the hope that it will be useful,
    00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013   Library General Public License for more details.
    00014
    00015   You should have received a copy of the GNU Library General Public
    00016   License along with this library; if not, write to the Free
    00017   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_CSGFX_VERTEXLISTWALKER_H__
    00021 #define __CS_CSGFX_VERTEXLISTWALKER_H__
    00022
    00023 #include "cstool/rbuflock.h"
    00024
    00041 template<typename Tbase, typename Tcomplex = Tbase>
    00042 class csVertexListWalker
    00043 {
    00044 public:
    00055   csVertexListWalker (iRenderBuffer* buffer, size_t desiredComponents = 0,
    00056     const Tbase* defaultComponents = 0) : currElement (0), bufLock (buffer),
    00057     defaultComponents (defaultComponents)
    00058   {
    00059     bufferComponents = buffer->GetComponentCount ();
    00060     components = (desiredComponents != 0) ? desiredComponents :
    00061       bufferComponents;
    00062     CS_ASSERT (components <= maxComponents);
    00063     elements = buffer->GetElementCount();
    00064     compType = buffer->GetComponentType();
    00065     FetchCurrentElement();
    00066   }
    00067
    00069
    00070   operator Tbase const*() const
    00071   {
    00072     CS_ASSERT(currElement<elements);
    00073     return convertedComps;
    00074   }
    00075   const Tcomplex& operator*() const
    00076   {
    00077     CS_ASSERT(currElement<elements);
    00078     const Tbase* x = convertedComps;
    00079     const Tcomplex* y = (Tcomplex*)x;
    00080     return *y;
    00081   }
    00083
    00085
    00086   csVertexListWalker& operator++ ()
    00087   {
    00088     currElement++;
    00089     FetchCurrentElement();
    00090     return *this;
    00091   }
    00093
    00095   void ResetState ()
    00096   {
    00097     currElement = 0;
    00098     FetchCurrentElement();
    00099   }
    00100 private:
    00102   size_t elements;
    00104   size_t currElement;
    00106   csRenderBufferLock<uint8> bufLock;
    00107
    00109   size_t components;
    00111   size_t bufferComponents;
    00113   enum { maxComponents = 4 };
    00115   Tbase convertedComps[maxComponents];
    00117   const Tbase* const defaultComponents;
    00119   csRenderBufferComponentType compType;
    00120
    00122   const Tbase GetDefaultComponent (size_t n)
    00123   {
    00124     return (defaultComponents != 0) ? defaultComponents[n] :
    00125       ((n == 3) ? Tbase(1) : Tbase(0));
    00126   }
    00128   template<typename C>
    00129   void FetchCurrentElementReal()
    00130   {
    00131     uint8* data = bufLock + (currElement * bufferComponents * sizeof (C));
    00132     for (size_t c = 0; c < components; c++)
    00133     {
    00134       convertedComps[c] =
    00135         (c < bufferComponents) ? Tbase(*(C*)data) :
    00136         GetDefaultComponent (c);
    00137       data += sizeof (C);
    00138     }
    00139   }
    00141   void FetchCurrentElement()
    00142   {
    00143     /* Avoid reading beyond buffer end
    00144      * Don't assert here since FetchCurrentElement() is also called in
    00145      * legitimate cases (i.e. operator++ while on the last valid element.
    00146      * Assert in retrieval operators, though.
    00147      */
    00148     if (currElement >= elements) return;
    00149     switch (compType)
    00150     {
    00151       default:
    00152         CS_ASSERT(false);
    00153       case CS_BUFCOMP_BYTE:
    00154         FetchCurrentElementReal<char>();
    00155         break;
    00156       case CS_BUFCOMP_UNSIGNED_BYTE:
    00157         FetchCurrentElementReal<unsigned char>();
    00158         break;
    00159       case CS_BUFCOMP_SHORT:
    00160         FetchCurrentElementReal<short>();
    00161         break;
    00162       case CS_BUFCOMP_UNSIGNED_SHORT:
    00163         FetchCurrentElementReal<unsigned short>();
    00164         break;
    00165       case CS_BUFCOMP_INT:
    00166         FetchCurrentElementReal<int>();
    00167         break;
    00168       case CS_BUFCOMP_UNSIGNED_INT:
    00169         FetchCurrentElementReal<unsigned int>();
    00170         break;
    00171       case CS_BUFCOMP_FLOAT:
    00172         FetchCurrentElementReal<float>();
    00173         break;
    00174       case CS_BUFCOMP_DOUBLE:
    00175         FetchCurrentElementReal<double>();
    00176         break;
    00177     }
    00178   }
    00179 };
    00180
    00181 #endif // __CS_CSGFX_VERTEXLISTWALKER_H__