AnoPlib - Animlets are not Particles library
 All Data Structures Namespaces Files Functions Variables
AnoP.h
Go to the documentation of this file.
00001 #ifndef AnoP_H
00002 #define AnoP_H
00003 /* *************************************************************************
00004    @file       AnoP.h
00005    @project    AnoPlib
00006    @module     AnoPlib
00007    @brief      The library classes
00008    @date       25.04.2009
00009    @copyright  Daniel Krajzewicz
00010    @licence    LGPL
00011    @author     Daniel Krajzewicz
00012    @email      d.krajzewicz@googlemail.com
00013    ------------------------------------------------------------------
00014     AnoPlib - small "animlet" library, see http://sf.net/projects/anoplib
00015     Copyright (C) 2009-2010 Daniel Krajzewicz
00016 
00017     This program is free software: you can redistribute it and/or modify
00018     it under the terms of the GNU General Public License as published by
00019     the Free Software Foundation, either version 3 of the License, or
00020     (at your option) any later version.
00021 
00022     This program is distributed in the hope that it will be useful,
00023     but WITHOUT ANY WARRANTY; without even the implied warranty of
00024     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025     GNU General Public License for more details.
00026 
00027     You should have received a copy of the GNU General Public License
00028     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00029    ------------------------------------------------------------------
00030    Remarks:
00031    ------------------------------------------------------------------
00032    ToDo:
00033  * *********************************************************************** */
00034 /* =========================================================================
00035  * class definitions
00036  * ======================================================================= */
00037 namespace AnoP {
00038 
00042     class AnoPsystemBase {
00043     public:
00045         AnoPsystemBase() {}
00046 
00048         virtual ~AnoPsystemBase() {}
00049 
00054         virtual void renderAll(unsigned int timeDiff, void * const something) = 0;
00055 
00056     }; // AnoPsystemBase
00057 
00058 
00062     template<class T>
00063     class AnoPsystem : public AnoPsystemBase {
00064     public:
00068         AnoPsystem(unsigned int bufSize=10) throw() :
00069             myAnimletNumber(0), myBufferSize(bufSize), myAnimletBuffer(new T[bufSize])
00070             { }
00071 
00072 
00074         ~AnoPsystem() throw() { }
00075 
00076 
00091         void renderAll(unsigned int timeDiff, void * const something) {
00092             preRender();
00093             for(unsigned int i=0; i<myAnimletNumber; ) {
00094                 T &animlet = myAnimletBuffer[i];
00095                 if(render(animlet, timeDiff, something)) {
00096                     ++i;
00097                     continue;
00098                 }
00099                 // animlet dies...
00100                 if(deleteAnimlet(animlet)) {
00101                     myAnimletBuffer[i] = myAnimletBuffer[myAnimletNumber-1];
00102                     --myAnimletNumber;
00103                 }
00104             }
00105             postRender();
00106         }
00107 
00108 
00112         void addAnimlet(T &animlet) throw() {
00113             if(myAnimletNumber==myBufferSize) {
00114                 int add = 10;
00115                 T *newBuffer = new T[myBufferSize+add];
00116                 memcpy(newBuffer, myAnimletBuffer, sizeof(T)*myBufferSize);
00117                 std::swap(myAnimletBuffer, newBuffer);
00118                 myBufferSize += add;
00119                 delete[] newBuffer;
00120             }
00121             myAnimletBuffer[myAnimletNumber] = animlet;
00122             ++myAnimletNumber;
00123         }
00124 
00125     
00128 
00134         virtual void preRender() throw() {}
00135 
00136         
00142         virtual void postRender() throw() {}
00143 
00144         
00155         virtual bool render(T &which, unsigned int timeDiff, void * const something) throw() { return false; }
00156 
00157 
00171         virtual bool deleteAnimlet(T &which) throw() { return true; }
00173     
00174 
00175     protected:
00177         unsigned int myAnimletNumber;
00178 
00180         unsigned int myBufferSize;
00181 
00183         T *myAnimletBuffer;
00184 
00185 
00186     }; // AnoPsystem
00187 
00188 
00189 }; // AnoP
00190 
00191 
00192 /* *********************************************************************** */
00193 #endif