SURFACE.CPP (5677B)
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/SURFACE.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 : SURFACE.CPP * 24 * * 25 * Programmer : Joe L. Bostic * 26 * * 27 * Start Date : 09/08/96 * 28 * * 29 * Last Update : September 8, 1996 [JLB] * 30 * * 31 *---------------------------------------------------------------------------------------------* 32 * Functions: * 33 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 34 #if (0)//PG 35 #include "FUNCTION.H" 36 #include "surface.h" 37 38 39 Surface::Surface(int w, int h, Buffer const * buffer, int pitch) : 40 Width(w), 41 Height(h), 42 Pitch(pitch) 43 { 44 /* 45 ** If a buffer was specified, then this means that the surface will use the buffer memory 46 ** and thus not allocate and manage its own memory. 47 */ 48 if (buffer != NULL) { 49 SurfaceData = *buffer; 50 51 /* 52 ** Reduce the dimensions if the buffer is not big enough. This size bounding is only 53 ** possible if the buffer size is known. Otherwise, presume that it is big enough. 54 */ 55 if (buffer->Get_Size() > 0 && Get_Size() > buffer->Get_Size()) { 56 57 Height = buffer->Get_Size() / (Width+Pitch); 58 if (Height == 0) { 59 Height = 1; 60 Width = buffer->Get_Size(); 61 } 62 } 63 } else { 64 /* 65 ** A new buffer without existing memory specified, will allocate and manage its 66 ** own memory for the surface. 67 */ 68 new(&SurfaceData) Buffer(Logical_Size()); 69 } 70 } 71 72 73 Surface::Surface(Surface const & surface, int x, int y, int w, int h) : 74 Width(w), 75 Height(h), 76 Pitch(surface.Bytes_Per_Line() % w) 77 { 78 new(&SurfaceData) Buffer((char*)surface.Get_Buffer() + y*surface.Bytes_Per_Line() + x); 79 } 80 81 82 void Surface::Copy_To(Buffer & buffer, int x, int y, int w, int h) const 83 { 84 assert(buffer.Is_Valid()); 85 86 /* 87 ** Determine the width of the region to copy from this surface. 88 */ 89 int width = w; 90 if (width == -1) { 91 width = Width; 92 } 93 94 /* 95 ** Determine the height of the region to copy from this surface. 96 */ 97 int height = h; 98 if (height == -1) { 99 height = Height; 100 } 101 102 Copy_To(Rect(x, y, width, height), buffer); 103 } 104 105 106 void Surface::Copy_To(Rect const & fromrect, Buffer & tobuffer) const 107 { 108 assert(fromrect.Is_Valid()); 109 assert(tobuffer.Is_Valid()); 110 111 /* 112 ** Determine the copy-from rectangle. The size is bounded to the source 113 ** size of the surface, regardless of what was specified as the source 114 ** rectangle. 115 */ 116 Rect rect = fromrect.Intersect(Rect(0, 0, Width, Height)); 117 118 /* 119 ** Determine the number of bytes to copy. If this number would be 120 ** larger than the size of the destination buffer (presuming the size 121 ** of the destination buffer is known), then limit the copy size 122 ** to the buffer size. 123 */ 124 int tocopy = rect.Size(); 125 if (tobuffer.Get_Size() > 0 && tobuffer.Get_Size() > tocopy) { 126 tocopy = tobuffer.Get_Size(); 127 } 128 129 /* 130 ** Determine the source starting byte pointer. 131 */ 132 char * source = (char*)Get_Buffer() + rect.Y*Bytes_Per_Line() + rect.X; 133 134 /* 135 ** Determine the destination buffer pointer. This will always be the 136 ** start of the destination buffer object specified. 137 */ 138 char * dest = tobuffer; 139 140 /* 141 ** Determine the working pitch value to use. For full width surface 142 ** copies on surfaces that have no inherent pitch, then a very fast 143 ** copy-in-one-fast-step operation can be performed. 144 */ 145 int pitch = Bytes_Per_Line() % rect.Width; 146 if (pitch == 0) { 147 memmove(dest, source, tocopy); 148 } else { 149 150 /* 151 ** Copy the source to the destination in line segments. 152 */ 153 for (int y = 0; y < rect.Height; y++) { 154 memmove(dest, source, width); 155 dest += rect.Width; 156 source += rect.Width + pitch; 157 } 158 } 159 } 160 #endif