LAYER.CPP (9128B)
1 // 2 // Copyright 2020 Electronic Arts Inc. 3 // 4 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free 5 // software: you can redistribute it and/or modify it under the terms of 6 // the GNU General Public License as published by the Free Software Foundation, 7 // either version 3 of the License, or (at your option) any later version. 8 9 // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed 10 // in the hope that it will be useful, but with permitted additional restrictions 11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 12 // distributed with this program. You should have received a copy of the 13 // GNU General Public License along with permitted additional restrictions 14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection 15 16 /* $Header: /CounterStrike/LAYER.CPP 1 3/03/97 10:25a Joe_bostic $ */ 17 /*********************************************************************************************** 18 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S *** 19 *********************************************************************************************** 20 * * 21 * Project Name : Command & Conquer * 22 * * 23 * File Name : LAYER.CPP * 24 * * 25 * Programmer : Joe L. Bostic * 26 * * 27 * Start Date : May 31, 1994 * 28 * * 29 * Last Update : March 10, 1995 [JLB] * 30 * * 31 *---------------------------------------------------------------------------------------------* 32 * Functions: * 33 * LayerClass::Sort -- Perform an incremental sort pass on the layer's objects. * 34 * LayerClass::Sorted_Add -- Adds object in sorted order to layer. * 35 * LayerClass::Submit -- Adds an object to a layer list. * 36 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 37 38 #include "function.h" 39 #include "layer.h" 40 41 42 /*********************************************************************************************** 43 * LayerClass::Submit -- Adds an object to a layer list. * 44 * * 45 * This routine is used to add an object to the layer list. If the list is full, then the * 46 * object is not added. * 47 * * 48 * INPUT: object -- Pointer to the object to add. * 49 * * 50 * OUTPUT: bool; Was the object added successfully? * 51 * * 52 * WARNINGS: none * 53 * * 54 * HISTORY: * 55 * 05/31/1994 JLB : Created. * 56 * 05/31/1994 JLB : Allows sorted insert. * 57 * 01/02/1995 JLB : Fixed to work with EMSListOf template. * 58 *=============================================================================================*/ 59 bool LayerClass::Submit(ObjectClass const * object, bool sort) 60 { 61 /* 62 ** Add the object to the layer. Either at the end (if "sort" is false) or at the 63 ** appropriately sorted position. 64 */ 65 if (sort) { 66 return(Sorted_Add(object)); 67 } 68 return(Add((ObjectClass *)object)); 69 } 70 71 72 /*********************************************************************************************** 73 * LayerClass::Sort -- Handles sorting the objects in the layer. * 74 * * 75 * This routine is used if the layer objects must be sorted and sorting is to occur now. * 76 * * 77 * INPUT: none * 78 * * 79 * OUTPUT: none * 80 * * 81 * WARNINGS: Don't call this routine too often since it does take a bit of time to * 82 * execute. It is a single pass binary sort and thus isn't horribly slow, * 83 * but it does take some time. * 84 * * 85 * HISTORY: * 86 * 10/17/1994 JLB : Created. * 87 * 03/10/1995 JLB : Uses comparison operator. * 88 *=============================================================================================*/ 89 void LayerClass::Sort(void) 90 { 91 for (int index = 0; index < Count()-1; index++) { 92 if (*(*this)[index+1] < *(*this)[index]) { 93 ObjectClass * temp; 94 95 temp = (*this)[index+1]; 96 (*this)[index+1] = (*this)[index]; 97 (*this)[index] = temp; 98 } 99 } 100 } 101 102 103 /*********************************************************************************************** 104 * DynamicVectorClass<T>::Sorted_Add -- Adds object in sorted order to vector. * 105 * * 106 * Use this routine to add an object to the vector but it will be inserted in sorted * 107 * order. This depends on the ">" operator being defined for the vector object. * 108 * * 109 * INPUT: object -- Reference to the object that will be added to the vector. * 110 * * 111 * OUTPUT: bool; Was the object added to the vector successfully? * 112 * * 113 * WARNINGS: none * 114 * * 115 * HISTORY: * 116 * 03/10/1995 JLB : Created. * 117 *=============================================================================================*/ 118 int LayerClass::Sorted_Add(ObjectClass const * const object) 119 { 120 if ((unsigned)ActiveCount >= Length()) { 121 if ((IsAllocated || !VectorMax) && GrowthStep > 0) { 122 if (!Resize(Length() + GrowthStep)) { 123 124 /* 125 ** Failure to increase the size of the vector is an error condition. 126 ** Return with the error flag. 127 */ 128 return(false); 129 } 130 } else { 131 132 /* 133 ** Increasing the size of this vector is not allowed! Bail this 134 ** routine with the error code. 135 */ 136 return(false); 137 } 138 } 139 140 /* 141 ** There is room for the new object now. Add it to the right sorted position. 142 */ 143 int index; 144 for (index = 0; index < ActiveCount; index++) { 145 if ((*(*this)[index]) > (*object)) { 146 break; 147 } 148 } 149 150 /* 151 ** Make room if the insertion spot is not at the end of the vector. 152 */ 153 for (int i = ActiveCount-1; i >= index; i--) { 154 (*this)[i+1] = (*this)[i]; 155 } 156 (*this)[index] = (ObjectClass *)object; 157 ActiveCount++; 158 return(true); 159 } 160 161