SURFACE.CPP 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/SURFACE.CPP 1 3/03/97 10:25a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : SURFACE.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 09/08/96 *
  30. * *
  31. * Last Update : September 8, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "surface.h"
  37. Surface::Surface(int w, int h, Buffer const * buffer, int pitch) :
  38. Width(w),
  39. Height(h),
  40. Pitch(pitch)
  41. {
  42. /*
  43. ** If a buffer was specified, then this means that the surface will use the buffer memory
  44. ** and thus not allocate and manage its own memory.
  45. */
  46. if (buffer != NULL) {
  47. SurfaceData = *buffer;
  48. /*
  49. ** Reduce the dimensions if the buffer is not big enough. This size bounding is only
  50. ** possible if the buffer size is known. Otherwise, presume that it is big enough.
  51. */
  52. if (buffer->Get_Size() > 0 && Get_Size() > buffer->Get_Size()) {
  53. Height = buffer->Get_Size() / (Width+Pitch);
  54. if (Height == 0) {
  55. Height = 1;
  56. Width = buffer->Get_Size();
  57. }
  58. }
  59. } else {
  60. /*
  61. ** A new buffer without existing memory specified, will allocate and manage its
  62. ** own memory for the surface.
  63. */
  64. new(&SurfaceData) Buffer(Logical_Size());
  65. }
  66. }
  67. Surface::Surface(Surface const & surface, int x, int y, int w, int h) :
  68. Width(w),
  69. Height(h),
  70. Pitch(surface.Bytes_Per_Line() % w)
  71. {
  72. new(&SurfaceData) Buffer((char*)surface.Get_Buffer() + y*surface.Bytes_Per_Line() + x);
  73. }
  74. void Surface::Copy_To(Buffer & buffer, int x, int y, int w, int h) const
  75. {
  76. assert(buffer.Is_Valid());
  77. /*
  78. ** Determine the width of the region to copy from this surface.
  79. */
  80. int width = w;
  81. if (width == -1) {
  82. width = Width;
  83. }
  84. /*
  85. ** Determine the height of the region to copy from this surface.
  86. */
  87. int height = h;
  88. if (height == -1) {
  89. height = Height;
  90. }
  91. Copy_To(Rect(x, y, width, height), buffer);
  92. }
  93. void Surface::Copy_To(Rect const & fromrect, Buffer & tobuffer) const
  94. {
  95. assert(fromrect.Is_Valid());
  96. assert(tobuffer.Is_Valid());
  97. /*
  98. ** Determine the copy-from rectangle. The size is bounded to the source
  99. ** size of the surface, regardless of what was specified as the source
  100. ** rectangle.
  101. */
  102. Rect rect = fromrect.Intersect(Rect(0, 0, Width, Height));
  103. /*
  104. ** Determine the number of bytes to copy. If this number would be
  105. ** larger than the size of the destination buffer (presuming the size
  106. ** of the destination buffer is known), then limit the copy size
  107. ** to the buffer size.
  108. */
  109. int tocopy = rect.Size();
  110. if (tobuffer.Get_Size() > 0 && tobuffer.Get_Size() > tocopy) {
  111. tocopy = tobuffer.Get_Size();
  112. }
  113. /*
  114. ** Determine the source starting byte pointer.
  115. */
  116. char * source = Get_Buffer() + rect.Y*Bytes_Per_Line() + rect.X;
  117. /*
  118. ** Determine the destination buffer pointer. This will always be the
  119. ** start of the destination buffer object specified.
  120. */
  121. char * dest = tobuffer;
  122. /*
  123. ** Determine the working pitch value to use. For full width surface
  124. ** copies on surfaces that have no inherent pitch, then a very fast
  125. ** copy-in-one-fast-step operation can be performed.
  126. */
  127. int pitch = Bytes_Per_Line() % rect.Width;
  128. if (pitch == 0) {
  129. memmove(dest, source, tocopy);
  130. } else {
  131. /*
  132. ** Copy the source to the destination in line segments.
  133. */
  134. for (int y = 0; y < rect.Height; y++) {
  135. memmove(dest, source, width);
  136. dest += rect.Width;
  137. source += rect.Width + pitch;
  138. }
  139. }
  140. }