PreviewProc.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. ** Command & Conquer Generals(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. // FILE: PreviewProc.cpp //////////////////////////////////////////////////////
  19. //-----------------------------------------------------------------------------
  20. //
  21. // Westwood Studios Pacific.
  22. //
  23. // Confidential Information
  24. // Copyright (C) 2001 - All Rights Reserved
  25. //
  26. //-----------------------------------------------------------------------------
  27. //
  28. // Project: ImagePacker
  29. //
  30. // File name: PreviewProc.cpp
  31. //
  32. // Created: Colin Day, August 2001
  33. //
  34. // Desc: Window procedure for the preview window
  35. //
  36. //-----------------------------------------------------------------------------
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  39. #include <windows.h>
  40. #include <stdio.h>
  41. // USER INCLUDES //////////////////////////////////////////////////////////////
  42. #include "ImagePacker.h"
  43. #include "WinMain.h"
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // DEFINES ////////////////////////////////////////////////////////////////////
  46. ///////////////////////////////////////////////////////////////////////////////
  47. #define PREVIEW_STYLE WS_CAPTION
  48. // PRIVATE TYPES //////////////////////////////////////////////////////////////
  49. // PRIVATE DATA ///////////////////////////////////////////////////////////////
  50. // PUBLIC DATA ////////////////////////////////////////////////////////////////
  51. // PRIVATE PROTOTYPES /////////////////////////////////////////////////////////
  52. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  53. ///////////////////////////////////////////////////////////////////////////////
  54. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  55. ///////////////////////////////////////////////////////////////////////////////
  56. // PreviewProc ================================================================
  57. /** */
  58. //=============================================================================
  59. LRESULT CALLBACK PreviewProc( HWND hWnd, UINT message,
  60. WPARAM wParam, LPARAM lParam )
  61. {
  62. switch( message )
  63. {
  64. case WM_PAINT:
  65. {
  66. PAINTSTRUCT ps;
  67. HDC hdc;
  68. HBRUSH whiteBrush = (HBRUSH)GetStockObject( WHITE_BRUSH );
  69. hdc = BeginPaint(hWnd, &ps);
  70. SelectObject( hdc, GetStockObject( WHITE_PEN ) );
  71. // find the target texture page
  72. TexturePage *page;
  73. for( page = TheImagePacker->getFirstTexturePage();
  74. page;
  75. page = page->m_next )
  76. {
  77. // if this is not the target page, ignore it
  78. if( page->getID() != TheImagePacker->getTargetPreviewPage() )
  79. continue;
  80. // draw image based on the generated texture or silhouette
  81. if( TheImagePacker->getUseTexturePreview() )
  82. {
  83. Int x, y;
  84. Byte r, g, b;
  85. HPEN prevPen, pen;
  86. for( y = 0; y < page->getHeight(); y++ )
  87. {
  88. for( x = 0; x < page->getWidth(); x++ )
  89. {
  90. // get the color here
  91. page->getPixel( x, y, &r, &g, &b );
  92. // create a new pen of the right color
  93. pen = CreatePen( 1, 1, RGB( r, g, b, ) );
  94. // select pen into hdc
  95. prevPen = (HPEN)SelectObject( hdc, pen );
  96. // draw ... what is the Win32 put pixel function???
  97. MoveToEx( hdc, x, y, NULL );
  98. LineTo( hdc, x + 1, y );
  99. // put the old pen back
  100. SelectObject( hdc, prevPen );
  101. // delete the created pen
  102. DeleteObject( pen );
  103. } // end for x
  104. } // end for y
  105. } // end if
  106. else
  107. {
  108. // go through all the images on this page
  109. ImageInfo *image;
  110. for( image = page->getFirstImage();
  111. image;
  112. image = image->m_nextPageImage )
  113. {
  114. RECT rect;
  115. rect.left = image->m_pagePos.lo.x;
  116. rect.top = image->m_pagePos.lo.y;
  117. rect.right = image->m_pagePos.hi.x + 1; // FillRect not inclusive
  118. rect.bottom = image->m_pagePos.hi.y + 1; // FillRect not inclusive
  119. FillRect( hdc, &rect, whiteBrush );
  120. } // end for image
  121. } // end else
  122. } // end for page
  123. EndPaint( hWnd, &ps );
  124. break;
  125. } // end paint
  126. } // end switch
  127. return DefWindowProc( hWnd, message, wParam, lParam );
  128. } // end PreviewProc
  129. // MakePreviewDisplay =========================================================
  130. /** */
  131. //=============================================================================
  132. HWND MakePreviewDisplay( void )
  133. {
  134. WNDCLASSEX wcex;
  135. char *className = "PreviewDisplay";
  136. HWND hWnd;
  137. wcex.cbSize = sizeof( WNDCLASSEX );
  138. wcex.style = CS_HREDRAW | CS_VREDRAW;
  139. wcex.lpfnWndProc = (WNDPROC)PreviewProc;
  140. wcex.cbClsExtra = 0;
  141. wcex.cbWndExtra = 0;
  142. wcex.hInstance = ApplicationHInstance;
  143. wcex.hIcon = NULL;
  144. wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
  145. wcex.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
  146. wcex.lpszMenuName = NULL;
  147. wcex.lpszClassName = className;
  148. wcex.hIconSm = NULL;
  149. RegisterClassEx( &wcex );
  150. // create app window and keep handle
  151. hWnd = CreateWindowEx( 0, // extended style
  152. className, // window class name
  153. "Preview", // window name
  154. PREVIEW_STYLE, // window styles
  155. 30, // x position
  156. 30, // y position
  157. TheImagePacker->getTargetWidth(),
  158. TheImagePacker->getTargetHeight(),
  159. NULL, // parent
  160. NULL, // menu
  161. ApplicationHInstance, // instance
  162. NULL ); // creation data
  163. if( hWnd == NULL )
  164. return NULL;
  165. // display the window
  166. ShowWindow( hWnd, SW_SHOW );
  167. return hWnd;
  168. } // end MakePreviewDisplay
  169. // UpdatePreviewWindow ========================================================
  170. /** Update the preview window, if present */
  171. //=============================================================================
  172. void UpdatePreviewWindow( void )
  173. {
  174. HWND preview;
  175. // sanity
  176. if( TheImagePacker == NULL )
  177. return;
  178. // get preview window
  179. preview = TheImagePacker->getPreviewWindow();
  180. // if window not here don't bother
  181. if( preview == NULL )
  182. return;
  183. // make the title
  184. char title[ 256 ];
  185. // construct title
  186. sprintf( title, "Page #%d of %d",
  187. TheImagePacker->getTargetPreviewPage(),
  188. TheImagePacker->getPageCount() );
  189. SetWindowText( preview, title );
  190. // adjust the window rect so the client area is the target packed size
  191. RECT clientRect;
  192. POINT p;
  193. p.x = 0;
  194. p.y = 0;
  195. ClientToScreen( preview, &p );
  196. clientRect.left = p.x;
  197. clientRect.right = clientRect.left + TheImagePacker->getTargetWidth();
  198. clientRect.top = p.y;
  199. clientRect.bottom = clientRect.top + TheImagePacker->getTargetHeight();
  200. AdjustWindowRect( &clientRect, PREVIEW_STYLE, FALSE );
  201. MoveWindow( preview,
  202. clientRect.left,
  203. clientRect.top,
  204. clientRect.right - clientRect.left,
  205. clientRect.bottom - clientRect.top,
  206. TRUE );
  207. // invalidate the client area for redraw
  208. InvalidateRect( preview, NULL, TRUE );
  209. } // end UpdatePreviewWindow