LAYER.CPP 9.0 KB

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