| 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 | #include "array.h" | 
|---|
| 17 |  | 
|---|
| 18 | /** | 
|---|
| 19 |    \brief creates a new Array | 
|---|
| 20 | */ | 
|---|
| 21 | Array::Array () | 
|---|
| 22 | { | 
|---|
| 23 |   initializeArray (); | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | /** | 
|---|
| 27 |    \brief deletes an Array. | 
|---|
| 28 |    It does this by first deleting all the array-entries, and then delete the array[] itself | 
|---|
| 29 | */ | 
|---|
| 30 | Array::~Array() | 
|---|
| 31 | { | 
|---|
| 32 |   if (verbose >= 2) | 
|---|
| 33 |     printf("deleting array\n"); | 
|---|
| 34 |   Entry* walker = firstEntry; | 
|---|
| 35 |   Entry* last; | 
|---|
| 36 |   while (walker != NULL) | 
|---|
| 37 |     { | 
|---|
| 38 |       last = walker; | 
|---|
| 39 |       walker = walker->next; | 
|---|
| 40 |       delete last; | 
|---|
| 41 |     } | 
|---|
| 42 |   if (finalized) | 
|---|
| 43 |     delete [] array; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | /** | 
|---|
| 47 |    \brief initializes an Array | 
|---|
| 48 |    the Function does this by setting up a fistEntry, and setting the entryCount. | 
|---|
| 49 | */ | 
|---|
| 50 | void Array::initializeArray () | 
|---|
| 51 | { | 
|---|
| 52 |   if (verbose >= 2) | 
|---|
| 53 |     printf ("crating new Array\n"); | 
|---|
| 54 |   firstEntry = new Entry; | 
|---|
| 55 |   firstEntry->next =NULL; | 
|---|
| 56 |   currentEntry=firstEntry; | 
|---|
| 57 |   finalized = false; | 
|---|
| 58 |   entryCount = 0; //0 means one entry | 
|---|
| 59 |   return; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | /** | 
|---|
| 63 |    \brief finalizes an array. | 
|---|
| 64 |    This Function creates the array, and makes it ready to be sent to the application. | 
|---|
| 65 | */ | 
|---|
| 66 | void Array::finalizeArray (void) | 
|---|
| 67 | { | 
|---|
| 68 |   if (verbose >= 3) | 
|---|
| 69 |     printf ("Finalizing array.\n");   | 
|---|
| 70 |   if ((array = (GLfloat*)malloc( entryCount* sizeof(GLfloat))) == NULL) | 
|---|
| 71 |     printf ("could not allocate %i data Blocks\n", entryCount); | 
|---|
| 72 |   Entry* walker = firstEntry; | 
|---|
| 73 |   for (int i=0; i<entryCount; i++) | 
|---|
| 74 |     { | 
|---|
| 75 |       array[i] = walker->value; | 
|---|
| 76 |       walker = walker->next; | 
|---|
| 77 |     } | 
|---|
| 78 |   finalized = true; | 
|---|
| 79 |   return; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | /** | 
|---|
| 83 |    \brief adds a new Entry to the Array | 
|---|
| 84 |    \param entry Entry to add. | 
|---|
| 85 | */ | 
|---|
| 86 | void Array::addEntry (GLfloat entry) | 
|---|
| 87 | { | 
|---|
| 88 |   if (!finalized) | 
|---|
| 89 |     { | 
|---|
| 90 |       if (verbose >= 3) | 
|---|
| 91 |         printf ("adding new Entry to Array: %f\n", entry); | 
|---|
| 92 |        | 
|---|
| 93 |       entryCount++; | 
|---|
| 94 |       currentEntry->value = entry; | 
|---|
| 95 |       currentEntry->next = new Entry; | 
|---|
| 96 |       currentEntry = currentEntry->next; | 
|---|
| 97 |       currentEntry->next = NULL; | 
|---|
| 98 |     } | 
|---|
| 99 |   else  | 
|---|
| 100 |     if (verbose >= 1) | 
|---|
| 101 |       printf ("adding failed, because list has been finalized\n"); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | /** | 
|---|
| 105 |    \brief Adds 3 entries at once (convenience) | 
|---|
| 106 | */ | 
|---|
| 107 | void Array::addEntry (GLfloat entry0, GLfloat entry1, GLfloat entry2) | 
|---|
| 108 | { | 
|---|
| 109 |   addEntry (entry0); | 
|---|
| 110 |   addEntry (entry1); | 
|---|
| 111 |   addEntry (entry2); | 
|---|
| 112 | } | 
|---|
| 113 |    | 
|---|
| 114 | /** | 
|---|
| 115 |    \brief Gives back the array !! MUST be executed AFTER finalize. | 
|---|
| 116 |    \returns The created array. | 
|---|
| 117 | */ | 
|---|
| 118 | GLfloat* Array::getArray () | 
|---|
| 119 | { | 
|---|
| 120 |   return array; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /** | 
|---|
| 124 |    \returns The Count of entries in the Array | 
|---|
| 125 | */ | 
|---|
| 126 | int Array::getCount() | 
|---|
| 127 | { | 
|---|
| 128 |   return entryCount; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 |    \brief Simple debug info about the Array | 
|---|
| 133 | */ | 
|---|
| 134 | void Array::debug () | 
|---|
| 135 | { | 
|---|
| 136 |   printf ("entryCount=%i, address=%p\n", entryCount, array); | 
|---|
| 137 | } | 
|---|