You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.8 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. // common Windows parts
  24. #include "winvideo.h"
  25. // and another headers
  26. #include <cassert>
  27. #include <stdio.h>
  28. #include <dxsdkver.h>
  29. #if _DXSDK_PRODUCT_MAJOR < 9
  30. #error DXSDK Version 9 and above required.
  31. #endif
  32. #include <d2d1.h>
  33. #include <d2d1helper.h>
  34. #pragma comment(lib, "d2d1.lib")
  35. ID2D1Factory *m_pD2DFactory;
  36. ID2D1HwndRenderTarget *m_pRenderTarget;
  37. ID2D1Bitmap *m_pBitmap;
  38. D2D1_SIZE_U bitmapSize;
  39. HANDLE g_hVSync;
  40. #include <DXErr.h>
  41. #pragma comment(lib, "DxErr.lib")
  42. //! Create a dialog box and tell the user what went wrong
  43. bool DisplayError(LPSTR lpstrErr, HRESULT hres)
  44. {
  45. if(hres != S_OK){
  46. static bool InError = false;
  47. int retval = 0;
  48. if (!InError)
  49. {
  50. InError = true;
  51. const char *message = hres?DXGetErrorString(hres):0;
  52. retval = MessageBoxA(g_hAppWnd, lpstrErr, hres?message:"Error!", MB_OK|MB_ICONERROR);
  53. InError = false;
  54. }
  55. }
  56. return false;
  57. }
  58. void DrawBitmap()
  59. {
  60. HRESULT hr = S_OK;
  61. if (m_pRenderTarget) {
  62. m_pRenderTarget->BeginDraw();
  63. if (m_pBitmap)
  64. hr = m_pBitmap->CopyFromMemory(NULL,(BYTE*)g_pImg, 4*g_sizex);
  65. DisplayError( "DrawBitmap error", hr );
  66. m_pRenderTarget->DrawBitmap(m_pBitmap);
  67. m_pRenderTarget->EndDraw();
  68. }
  69. return;
  70. }
  71. inline void mouse(int k, LPARAM lParam)
  72. {
  73. int x = (int)LOWORD(lParam);
  74. int y = (int)HIWORD(lParam);
  75. RECT rc;
  76. GetClientRect(g_hAppWnd, &rc);
  77. g_video->on_mouse( x*g_sizex/(rc.right - rc.left), y*g_sizey/(rc.bottom - rc.top), k );
  78. }
  79. //! Win event processing function
  80. LRESULT CALLBACK InternalWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  81. {
  82. switch (iMsg)
  83. {
  84. case WM_MOVE:
  85. // Check to make sure our window exists before we tell it to repaint.
  86. // This will fail the first time (while the window is being created).
  87. if (hwnd) {
  88. InvalidateRect(hwnd, NULL, FALSE);
  89. UpdateWindow(hwnd);
  90. }
  91. return 0L;
  92. case WM_SIZE:
  93. case WM_PAINT:
  94. if( g_video->running && g_video->updating ) {
  95. DrawBitmap();
  96. Sleep(0);
  97. }
  98. break;
  99. // Proccess all mouse and keyboard events
  100. case WM_LBUTTONDOWN: mouse( 1, lParam ); break;
  101. case WM_LBUTTONUP: mouse(-1, lParam ); break;
  102. case WM_RBUTTONDOWN: mouse( 2, lParam ); break;
  103. case WM_RBUTTONUP: mouse(-2, lParam ); break;
  104. case WM_MBUTTONDOWN: mouse( 3, lParam ); break;
  105. case WM_MBUTTONUP: mouse(-3, lParam ); break;
  106. case WM_CHAR: g_video->on_key( (int)wParam); break;
  107. // some useless stuff
  108. case WM_ERASEBKGND: return 1; // keeps erase-background events from happening, reduces chop
  109. case WM_DISPLAYCHANGE: return 0;
  110. // Now, shut down the window...
  111. case WM_DESTROY: PostQuitMessage(0); return 0;
  112. }
  113. // call user defined proc, if exists
  114. return g_pUserProc? g_pUserProc(hwnd, iMsg, wParam, lParam) : DefWindowProc(hwnd, iMsg, wParam, lParam);
  115. }
  116. bool video::init_window(int sizex, int sizey)
  117. {
  118. assert(win_hInstance != 0);
  119. g_sizex = sizex; g_sizey = sizey;
  120. if (!WinInit(win_hInstance, win_iCmdShow, gWndClass, title, false)) {
  121. DisplayError("Unable to initialize the program's window.");
  122. return false;
  123. }
  124. ShowWindow(g_hAppWnd, SW_SHOW);
  125. g_pImg = new unsigned int[sizex*sizey];
  126. HRESULT hr = S_OK;
  127. hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory);
  128. // Create a Direct2D render target.
  129. if (SUCCEEDED(hr) && !m_pRenderTarget){
  130. RECT rc;
  131. GetClientRect(g_hAppWnd, &rc);
  132. bitmapSize = D2D1::SizeU(
  133. rc.right - rc.left,
  134. rc.bottom - rc.top
  135. );
  136. hr = m_pD2DFactory->CreateHwndRenderTarget(
  137. D2D1::RenderTargetProperties(),
  138. D2D1::HwndRenderTargetProperties(g_hAppWnd, bitmapSize),
  139. &m_pRenderTarget
  140. );
  141. if (SUCCEEDED(hr) && !m_pBitmap){
  142. D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat(
  143. DXGI_FORMAT_B8G8R8A8_UNORM,
  144. D2D1_ALPHA_MODE_IGNORE
  145. );
  146. D2D1_BITMAP_PROPERTIES bitmapProperties;
  147. bitmapProperties.pixelFormat = pixelFormat;
  148. m_pRenderTarget->GetDpi( &bitmapProperties.dpiX, &bitmapProperties.dpiY );
  149. m_pRenderTarget->CreateBitmap(bitmapSize,bitmapProperties,&m_pBitmap);
  150. m_pRenderTarget->DrawBitmap(m_pBitmap);
  151. }
  152. }
  153. running = true;
  154. return true;
  155. }
  156. void video::terminate()
  157. {
  158. if (m_pBitmap) m_pBitmap->Release();
  159. if (m_pRenderTarget) m_pRenderTarget->Release();
  160. if (m_pD2DFactory) m_pD2DFactory->Release();
  161. g_video = 0; running = false;
  162. if(g_pImg) { delete[] g_pImg; g_pImg = 0; }
  163. }
  164. //////////// drawing area constructor & destructor /////////////
  165. drawing_area::drawing_area(int x, int y, int sizex, int sizey)
  166. : start_x(x), start_y(y), size_x(sizex), size_y(sizey), pixel_depth(24),
  167. base_index(y*g_sizex + x), max_index(g_sizex*g_sizey), index_stride(g_sizex), ptr32(g_pImg)
  168. {
  169. assert(x < g_sizex); assert(y < g_sizey);
  170. assert(x+sizex <= g_sizex); assert(y+sizey <= g_sizey);
  171. index = base_index; // current index
  172. }
  173. void drawing_area::update()
  174. {
  175. if(g_video->updating) {
  176. RECT r;
  177. r.left = start_x; r.right = start_x + size_x;
  178. r.top = start_y; r.bottom = start_y + size_y;
  179. InvalidateRect(g_hAppWnd, &r, false);
  180. }
  181. }