dib.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. ** Command & Conquer Renegade(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. /***********************************************************************************************
  19. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /G/wwlib/dib.cpp $*
  25. * *
  26. * $Author:: Eric_c $*
  27. * *
  28. * $Modtime:: 3/29/99 5:11p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * DIB8C::DIB8Class -- constructor *
  35. * DIB8C::~DIB8Class -- destructor *
  36. * DIB8C::Clear -- clears the DIB *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "always.h"
  39. #include "dib.h"
  40. #include <math.h>
  41. /***********************************************************************************************
  42. * DIB8C::DIB8Class -- constructor *
  43. * *
  44. * INPUT: *
  45. * *
  46. * OUTPUT: *
  47. * *
  48. * WARNINGS: *
  49. * *
  50. * HISTORY: *
  51. * 04/18/1997 GH : Created. *
  52. *=============================================================================================*/
  53. DIB8Class::DIB8Class(HWND hwnd,int width,int height,PaletteClass & pal):
  54. IsZombie(false),
  55. Info(NULL),
  56. Handle(0),
  57. Pixels(NULL),
  58. Width(width),
  59. Height(height),
  60. PixelBase(NULL),
  61. Pitch(NULL),
  62. Surface(NULL)
  63. {
  64. // Allocate a BITMAPINFO structure
  65. Info = (BITMAPINFO *) new char [sizeof(BITMAPINFO) + 256*sizeof(RGBQUAD)];
  66. if (Info == NULL) {
  67. IsZombie = true;
  68. return;
  69. }
  70. // Describe the type of DIB we want.
  71. Info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  72. Info->bmiHeader.biWidth = width;
  73. Info->bmiHeader.biHeight = -height; //ask for a top-down dib.
  74. Info->bmiHeader.biPlanes = 1;
  75. Info->bmiHeader.biBitCount = 8;
  76. Info->bmiHeader.biCompression = BI_RGB;
  77. Info->bmiHeader.biSizeImage = 0;
  78. Info->bmiHeader.biXPelsPerMeter = 0;
  79. Info->bmiHeader.biYPelsPerMeter = 0;
  80. Info->bmiHeader.biClrUsed = 256;
  81. Info->bmiHeader.biClrImportant = 256;
  82. // Fill in the DIB's palette.
  83. for (int i=0; i<256; i++) {
  84. Info->bmiColors[i].rgbBlue = (unsigned char)pal[i].Get_Blue();
  85. Info->bmiColors[i].rgbGreen = (unsigned char)pal[i].Get_Green();
  86. Info->bmiColors[i].rgbRed = (unsigned char)pal[i].Get_Red();
  87. Info->bmiColors[i].rgbReserved = 0;
  88. }
  89. // Create the DIB.
  90. HDC hdc = GetDC(hwnd);
  91. Handle = CreateDIBSection(hdc, Info, DIB_RGB_COLORS,(void**)&Pixels, NULL, 0);
  92. ReleaseDC(hwnd, hdc);
  93. if (!Handle) {
  94. IsZombie = true;
  95. return;
  96. }
  97. Width = Info->bmiHeader.biWidth;
  98. Height = abs(Info->bmiHeader.biHeight);
  99. Pitch = (Width + 3) & 0xfffffffC;
  100. // Check if the DIB is bottom-up or top-down.
  101. // (it better be top-down, thats what I'm asking for!!!)
  102. if (Info->bmiHeader.biHeight > 0) {
  103. // bottom-up DIB
  104. PixelBase = (Pixels + (Height - 1) * Width);
  105. Pitch = -Pitch;
  106. } else {
  107. // top-down DIB
  108. PixelBase = Pixels;
  109. Pitch = Pitch;
  110. }
  111. // Now, wrap a BSurface around the DIB.
  112. int surfwid = abs(Pitch);
  113. Surface = new BSurface(surfwid,Height,1,Pixels);
  114. if (Surface == NULL) {
  115. IsZombie = true;
  116. return;
  117. }
  118. }
  119. /***********************************************************************************************
  120. * DIB8C::~DIB8Class -- destructor *
  121. * *
  122. * INPUT: *
  123. * *
  124. * OUTPUT: *
  125. * *
  126. * WARNINGS: *
  127. * *
  128. * HISTORY: *
  129. * 04/18/1997 GH : Created. *
  130. *=============================================================================================*/
  131. DIB8Class::~DIB8Class(void)
  132. {
  133. if (Info) delete [] Info;
  134. if (Handle) DeleteObject(Handle);
  135. if (Surface) delete Surface;
  136. }
  137. /***********************************************************************************************
  138. * DIB8C::Clear -- clears the DIB *
  139. * *
  140. * INPUT: *
  141. * *
  142. * OUTPUT: *
  143. * *
  144. * WARNINGS: *
  145. * *
  146. * HISTORY: *
  147. * 04/18/1997 GH : Created. *
  148. *=============================================================================================*/
  149. void DIB8Class::Clear(unsigned char color)
  150. {
  151. if (Pixels) {
  152. memset(Pixels, color, Width*Height);
  153. }
  154. }