simpdib.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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:: /Commando/Code/Tools/max2w3d/simpdib.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 10/14/97 3:07p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "simpdib.h"
  36. SimpleDIBClass::SimpleDIBClass(HWND hwnd,int width,int height,PaletteClass & pal):
  37. IsZombie(false),
  38. Info(NULL),
  39. Handle(0),
  40. Pixels(NULL),
  41. Width(width),
  42. Height(height),
  43. PixelBase(NULL),
  44. Pitch(NULL)
  45. {
  46. // Allocate a BITMAPINFO structure
  47. Info = (BITMAPINFO *) new char [sizeof(BITMAPINFO) + 256*sizeof(RGBQUAD)];
  48. if (Info == NULL) {
  49. IsZombie = true;
  50. return;
  51. }
  52. // Describe the type of DIB we want.
  53. Info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  54. Info->bmiHeader.biWidth = width;
  55. Info->bmiHeader.biHeight = -height; //ask for a top-down dib.
  56. Info->bmiHeader.biPlanes = 1;
  57. Info->bmiHeader.biBitCount = 8;
  58. Info->bmiHeader.biCompression = BI_RGB;
  59. Info->bmiHeader.biSizeImage = 0;
  60. Info->bmiHeader.biXPelsPerMeter = 0;
  61. Info->bmiHeader.biYPelsPerMeter = 0;
  62. Info->bmiHeader.biClrUsed = 256;
  63. Info->bmiHeader.biClrImportant = 256;
  64. // Fill in the DIB's palette.
  65. for (int i=0; i<256; i++) {
  66. Info->bmiColors[i].rgbBlue = (unsigned char)pal[i].Get_Blue();
  67. Info->bmiColors[i].rgbGreen = (unsigned char)pal[i].Get_Green();
  68. Info->bmiColors[i].rgbRed = (unsigned char)pal[i].Get_Red();
  69. Info->bmiColors[i].rgbReserved = 0;
  70. }
  71. // Create the DIB.
  72. HDC hdc = GetDC(hwnd);
  73. Handle = CreateDIBSection(hdc, Info, DIB_RGB_COLORS,(void**)&Pixels, NULL, 0);
  74. ReleaseDC(hwnd, hdc);
  75. if (!Handle) {
  76. IsZombie = true;
  77. return;
  78. }
  79. Width = Info->bmiHeader.biWidth;
  80. Height = abs(Info->bmiHeader.biHeight);
  81. Pitch = (Width + 3) & 0xfffffffC;
  82. // Check if the DIB is bottom-up or top-down.
  83. // (it better be top-down, thats what I'm asking for!!!)
  84. if (Info->bmiHeader.biHeight > 0) {
  85. // bottom-up DIB
  86. PixelBase = (Pixels + (Height - 1) * Width);
  87. Pitch = -Pitch;
  88. } else {
  89. // top-down DIB
  90. PixelBase = Pixels;
  91. Pitch = Pitch;
  92. }
  93. }
  94. SimpleDIBClass::~SimpleDIBClass(void)
  95. {
  96. if (Info) delete [] Info;
  97. if (Handle) DeleteObject(Handle);
  98. }
  99. void SimpleDIBClass::Clear(unsigned char color)
  100. {
  101. if (Pixels) {
  102. memset(Pixels, color, abs(Pitch)*Height);
  103. }
  104. }
  105. void SimpleDIBClass::Set_Pixel(int i,int j,unsigned char color)
  106. {
  107. if ((i < 0) || (j < 0) || (i >= Width) || (j >= Height)) {
  108. return;
  109. }
  110. *(PixelBase + j*Pitch + i) = color;
  111. }