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.

154 lines
6.0 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. // include GDI+ headers
  26. #include <gdiplus.h>
  27. // and another headers
  28. #include <stdio.h>
  29. // tag linking library
  30. #pragma comment(lib, "gdiplus.lib")
  31. // global specific variables
  32. Gdiplus::Bitmap * g_pBitmap; // main drawing bitmap
  33. ULONG_PTR gdiplusToken;
  34. Gdiplus::GdiplusStartupInput gdiplusStartupInput;// GDI+
  35. //! display system error
  36. bool DisplayError(LPSTR lpstrErr, HRESULT hres)
  37. {
  38. static bool InError = false;
  39. int retval = 0;
  40. if (!InError)
  41. {
  42. InError = true;
  43. LPCSTR lpMsgBuf;
  44. if(!hres) hres = GetLastError();
  45. FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  46. NULL, hres, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL );
  47. retval = MessageBox(g_hAppWnd, lpstrErr, lpMsgBuf, MB_OK|MB_ICONERROR);
  48. LocalFree( (HLOCAL)lpMsgBuf );
  49. InError = false;
  50. }
  51. return false;
  52. }
  53. //! Win event processing function
  54. LRESULT CALLBACK InternalWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  55. {
  56. switch (iMsg)
  57. {
  58. case WM_MOVE:
  59. // Check to make sure our window exists before we tell it to repaint.
  60. // This will fail the first time (while the window is being created).
  61. if (hwnd) {
  62. InvalidateRect(hwnd, NULL, FALSE);
  63. UpdateWindow(hwnd);
  64. }
  65. return 0L;
  66. case WM_PAINT:
  67. {
  68. PAINTSTRUCT ps;
  69. Gdiplus::Graphics graphics( BeginPaint(hwnd, &ps) );
  70. // redraw just requested area. This call is as fast as simple DrawImage() call.
  71. if(g_video->updating) graphics.DrawImage(g_pBitmap, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.left, ps.rcPaint.top,
  72. ps.rcPaint.right, ps.rcPaint.bottom, Gdiplus::UnitPixel);
  73. EndPaint(hwnd, &ps);
  74. }
  75. return 0L;
  76. // Proccess all mouse and keyboard events
  77. case WM_LBUTTONDOWN: g_video->on_mouse( (int)LOWORD(lParam), (int)HIWORD(lParam), 1); break;
  78. case WM_LBUTTONUP: g_video->on_mouse( (int)LOWORD(lParam), (int)HIWORD(lParam), -1); break;
  79. case WM_RBUTTONDOWN: g_video->on_mouse( (int)LOWORD(lParam), (int)HIWORD(lParam), 2); break;
  80. case WM_RBUTTONUP: g_video->on_mouse( (int)LOWORD(lParam), (int)HIWORD(lParam), -2); break;
  81. case WM_MBUTTONDOWN: g_video->on_mouse( (int)LOWORD(lParam), (int)HIWORD(lParam), 3); break;
  82. case WM_MBUTTONUP: g_video->on_mouse( (int)LOWORD(lParam), (int)HIWORD(lParam), -3); break;
  83. case WM_CHAR: g_video->on_key( (int)wParam); break;
  84. // some useless stuff
  85. case WM_ERASEBKGND: return 1; // keeps erase-background events from happening, reduces chop
  86. case WM_DISPLAYCHANGE: return 0;
  87. // Now, shut down the window...
  88. case WM_DESTROY: PostQuitMessage(0); return 0;
  89. }
  90. // call user defined proc, if exists
  91. return g_pUserProc? g_pUserProc(hwnd, iMsg, wParam, lParam) : DefWindowProc(hwnd, iMsg, wParam, lParam);
  92. }
  93. ///////////// video functions ////////////////
  94. bool video::init_window(int sizex, int sizey)
  95. {
  96. assert(win_hInstance != 0);
  97. g_sizex = sizex; g_sizey = sizey;
  98. if (!WinInit(win_hInstance, win_iCmdShow, gWndClass, title, true)) {
  99. DisplayError("Unable to initialize the program's window.");
  100. return false;
  101. }
  102. ShowWindow(g_hAppWnd, SW_SHOW);
  103. Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  104. g_pImg = new unsigned int[sizex*sizey];
  105. g_pBitmap = new Gdiplus::Bitmap(g_sizex, g_sizey, 4*g_sizex, PixelFormat32bppRGB, (BYTE*)g_pImg );
  106. running = true;
  107. return true;
  108. }
  109. void video::terminate()
  110. {
  111. if(g_pBitmap) { delete g_pBitmap; g_pBitmap = 0; }
  112. Gdiplus::GdiplusShutdown(gdiplusToken);
  113. g_video = 0; running = false;
  114. if(g_pImg) { delete[] g_pImg; g_pImg = 0; }
  115. }
  116. //////////// drawing area constructor & destructor /////////////
  117. drawing_area::drawing_area(int x, int y, int sizex, int sizey)
  118. : start_x(x), start_y(y), size_x(sizex), size_y(sizey), pixel_depth(24),
  119. base_index(y*g_sizex + x), max_index(g_sizex*g_sizey), index_stride(g_sizex), ptr32(g_pImg)
  120. {
  121. assert(x < g_sizex); assert(y < g_sizey);
  122. assert(x+sizex <= g_sizex); assert(y+sizey <= g_sizey);
  123. index = base_index; // current index
  124. }
  125. void drawing_area::update()
  126. {
  127. if(g_video->updating) {
  128. RECT r;
  129. r.left = start_x; r.right = start_x + size_x;
  130. r.top = start_y; r.bottom = start_y + size_y;
  131. InvalidateRect(g_hAppWnd, &r, false);
  132. }
  133. }