| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | /*! |
|---|
| 17 | \file array.h |
|---|
| 18 | \brief Contains the Array Class that handles float arrays. |
|---|
| 19 | this class creates a Array of a semi-Dynamic length. |
|---|
| 20 | beware, that after finalizing the array may not be resized again. |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #ifndef _ARRAY_H |
|---|
| 24 | #define _ARRAY_H |
|---|
| 25 | #include "debug.h" |
|---|
| 26 | |
|---|
| 27 | //! Array Class that handles dynamic-float arrays. |
|---|
| 28 | template<class T> class Array |
|---|
| 29 | { |
|---|
| 30 | public: |
|---|
| 31 | Array (); |
|---|
| 32 | ~Array(); |
|---|
| 33 | |
|---|
| 34 | void finalizeArray (void); |
|---|
| 35 | void addEntry (T entry); |
|---|
| 36 | void addEntry(T entry0, T entry1, T entry2); |
|---|
| 37 | |
|---|
| 38 | /** \returns The array */ |
|---|
| 39 | inline const T* getArray () const { return this->array; }; |
|---|
| 40 | /** \returns The Count of entries in the Array*/ |
|---|
| 41 | inline unsigned int getCount(void)const { return this->entryCount; }; |
|---|
| 42 | void debug(void) const ; |
|---|
| 43 | |
|---|
| 44 | private: |
|---|
| 45 | //! One entry of the Array |
|---|
| 46 | struct Entry |
|---|
| 47 | { |
|---|
| 48 | T value; //!< The value of this Entry. |
|---|
| 49 | Entry* next; //!< Pointer to the Next entry. |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | T* array; //!< The array that will be produced when finalizing the Array. |
|---|
| 53 | unsigned int entryCount; //!< The count of Entries in this Array. |
|---|
| 54 | bool finalized; //!< If this variable is set to true, the Array can not be changed anymore. true if finalized, false else (initially). |
|---|
| 55 | Entry* firstEntry; //!< Pointer to the first Entry of this Array |
|---|
| 56 | Entry* currentEntry; //!< Pointer to the current Entry of this Array. The one Entry we are working with. |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | \brief creates a new Array |
|---|
| 62 | */ |
|---|
| 63 | template<class T> |
|---|
| 64 | Array<T>::Array () |
|---|
| 65 | { |
|---|
| 66 | PRINTF(4)("crating new Array\n"); |
|---|
| 67 | this->firstEntry = new Entry; |
|---|
| 68 | this->firstEntry->next =NULL; |
|---|
| 69 | this->currentEntry = this->firstEntry; |
|---|
| 70 | this->finalized = false; |
|---|
| 71 | this->entryCount = 0; //0 means one entry |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | \brief deletes an Array. |
|---|
| 76 | It does this by first deleting all the array-entries, and then delete the array[] itself |
|---|
| 77 | */ |
|---|
| 78 | template<class T> |
|---|
| 79 | Array<T>::~Array() |
|---|
| 80 | { |
|---|
| 81 | PRINTF(4)("deleting array\n"); |
|---|
| 82 | Entry* walker = this->firstEntry; |
|---|
| 83 | Entry* previous; |
|---|
| 84 | while (walker) |
|---|
| 85 | { |
|---|
| 86 | previous = walker; |
|---|
| 87 | walker = walker->next; |
|---|
| 88 | delete previous; |
|---|
| 89 | } |
|---|
| 90 | if (finalized) |
|---|
| 91 | delete []this->array; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | \brief finalizes an array. |
|---|
| 96 | This Function creates the array, and makes it ready to be sent to the application. |
|---|
| 97 | */ |
|---|
| 98 | template<class T> |
|---|
| 99 | void Array<T>::finalizeArray (void) |
|---|
| 100 | { |
|---|
| 101 | PRINTF(4)("Finalizing array. Length: %i\n", entryCount); |
|---|
| 102 | // if ((array = (GLfloat*)malloc( entryCount* sizeof(GLfloat))) == NULL) |
|---|
| 103 | if (!(this->array = new GLfloat [this->entryCount])) |
|---|
| 104 | PRINTF(1)("could not allocate %i data Blocks\n", this->entryCount); |
|---|
| 105 | Entry* walker = this->firstEntry; |
|---|
| 106 | for (int i=0; i<this->entryCount; i++) |
|---|
| 107 | { |
|---|
| 108 | this->array[i] = walker->value; |
|---|
| 109 | walker = walker->next; |
|---|
| 110 | } |
|---|
| 111 | walker = this->firstEntry; |
|---|
| 112 | Entry* previous; |
|---|
| 113 | while (walker) |
|---|
| 114 | { |
|---|
| 115 | previous = walker; |
|---|
| 116 | walker = walker->next; |
|---|
| 117 | delete previous; |
|---|
| 118 | } |
|---|
| 119 | this->firstEntry = NULL; |
|---|
| 120 | this->finalized = true; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | \brief adds a new Entry to the Array |
|---|
| 125 | \param entry Entry to add. |
|---|
| 126 | */ |
|---|
| 127 | template<class T> |
|---|
| 128 | void Array<T>::addEntry (T entry) |
|---|
| 129 | { |
|---|
| 130 | if (!this->finalized) |
|---|
| 131 | { |
|---|
| 132 | PRINTF(5)("adding new Entry to Array: %f\n", entry); |
|---|
| 133 | |
|---|
| 134 | this->currentEntry->value = entry; |
|---|
| 135 | this->currentEntry->next = new Entry; |
|---|
| 136 | this->currentEntry = currentEntry->next; |
|---|
| 137 | this->currentEntry->next = NULL; |
|---|
| 138 | ++this->entryCount; |
|---|
| 139 | } |
|---|
| 140 | else |
|---|
| 141 | PRINTF(2)("adding failed, because array has already been finalized\n"); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /** |
|---|
| 145 | \brief Adds 3 entries at once (convenience) |
|---|
| 146 | */ |
|---|
| 147 | template<class T> |
|---|
| 148 | void Array<T>::addEntry (T entry0, T entry1, T entry2) |
|---|
| 149 | { |
|---|
| 150 | this->addEntry(entry0); |
|---|
| 151 | this->addEntry(entry1); |
|---|
| 152 | this->addEntry(entry2); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | \brief Simple debug info about the Array |
|---|
| 157 | */ |
|---|
| 158 | template<class T> |
|---|
| 159 | void Array<T>::debug (void) const |
|---|
| 160 | { |
|---|
| 161 | PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array); |
|---|
| 162 | } |
|---|
| 163 | #endif |
|---|