1 /* 2 Copyright (c) 2005-2021 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // common Windows parts 18 #include "winvideo.hpp" 19 // include GDI+ headers 20 #include <gdiplus.h> 21 // and another headers 22 #include <stdio.h> 23 24 // tag linking library 25 #pragma comment(lib, "gdiplus.lib") 26 27 // global specific variables 28 Gdiplus::Bitmap* g_pBitmap; // main drawing bitmap 29 ULONG_PTR gdiplusToken; 30 Gdiplus::GdiplusStartupInput gdiplusStartupInput; // GDI+ 31 32 //! display system error 33 bool DisplayError(LPSTR lpstrErr, HRESULT hres) { 34 static bool InError = false; 35 int retval = 0; 36 if (!InError) { 37 InError = true; 38 LPCSTR lpMsgBuf; 39 if (!hres) 40 hres = GetLastError(); 41 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 42 FORMAT_MESSAGE_IGNORE_INSERTS, 43 NULL, 44 hres, 45 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 46 (LPTSTR)&lpMsgBuf, 47 0, 48 NULL); 49 retval = MessageBox(g_hAppWnd, lpstrErr, lpMsgBuf, MB_OK | MB_ICONERROR); 50 LocalFree((HLOCAL)lpMsgBuf); 51 InError = false; 52 } 53 return false; 54 } 55 56 //! Win event processing function 57 LRESULT CALLBACK InternalWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { 58 switch (iMsg) { 59 case WM_MOVE: 60 // Check to make sure our window exists before we tell it to repaint. 61 // This will fail the first time (while the window is being created). 62 if (hwnd) { 63 InvalidateRect(hwnd, NULL, FALSE); 64 UpdateWindow(hwnd); 65 } 66 return 0L; 67 68 case WM_PAINT: { 69 PAINTSTRUCT ps; 70 Gdiplus::Graphics graphics(BeginPaint(hwnd, &ps)); 71 // redraw just requested area. This call is as fast as simple DrawImage() call. 72 if (g_video->updating) 73 graphics.DrawImage(g_pBitmap, 74 ps.rcPaint.left, 75 ps.rcPaint.top, 76 ps.rcPaint.left, 77 ps.rcPaint.top, 78 ps.rcPaint.right, 79 ps.rcPaint.bottom, 80 Gdiplus::UnitPixel); 81 EndPaint(hwnd, &ps); 82 } 83 return 0L; 84 85 // Process all mouse and keyboard events 86 case WM_LBUTTONDOWN: g_video->on_mouse((int)LOWORD(lParam), (int)HIWORD(lParam), 1); break; 87 case WM_LBUTTONUP: g_video->on_mouse((int)LOWORD(lParam), (int)HIWORD(lParam), -1); break; 88 case WM_RBUTTONDOWN: g_video->on_mouse((int)LOWORD(lParam), (int)HIWORD(lParam), 2); break; 89 case WM_RBUTTONUP: g_video->on_mouse((int)LOWORD(lParam), (int)HIWORD(lParam), -2); break; 90 case WM_MBUTTONDOWN: g_video->on_mouse((int)LOWORD(lParam), (int)HIWORD(lParam), 3); break; 91 case WM_MBUTTONUP: g_video->on_mouse((int)LOWORD(lParam), (int)HIWORD(lParam), -3); break; 92 case WM_CHAR: g_video->on_key((int)wParam); break; 93 94 // some useless stuff 95 case WM_ERASEBKGND: return 1; // keeps erase-background events from happening, reduces chop 96 case WM_DISPLAYCHANGE: return 0; 97 98 // Now, shut down the window... 99 case WM_DESTROY: PostQuitMessage(0); return 0; 100 } 101 // call user defined proc, if exists 102 return g_pUserProc ? g_pUserProc(hwnd, iMsg, wParam, lParam) 103 : DefWindowProc(hwnd, iMsg, wParam, lParam); 104 } 105 106 ///////////// video functions //////////////// 107 108 bool video::init_window(int sizex, int sizey) { 109 assert(win_hInstance != 0); 110 g_sizex = sizex; 111 g_sizey = sizey; 112 if (!WinInit(win_hInstance, win_iCmdShow, gWndClass, title, true)) { 113 DisplayError("Unable to initialize the program's window."); 114 return false; 115 } 116 ShowWindow(g_hAppWnd, SW_SHOW); 117 Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 118 g_pImg = new unsigned int[sizex * sizey]; 119 g_pBitmap = 120 new Gdiplus::Bitmap(g_sizex, g_sizey, 4 * g_sizex, PixelFormat32bppRGB, (BYTE*)g_pImg); 121 running = true; 122 return true; 123 } 124 125 void video::terminate() { 126 if (g_pBitmap) { 127 delete g_pBitmap; 128 g_pBitmap = 0; 129 } 130 Gdiplus::GdiplusShutdown(gdiplusToken); 131 g_video = 0; 132 running = false; 133 if (g_pImg) { 134 delete[] g_pImg; 135 g_pImg = 0; 136 } 137 } 138 139 //////////// drawing area constructor & destructor ///////////// 140 141 drawing_area::drawing_area(int x, int y, int sizex, int sizey) 142 : base_index(y * g_sizex + x), 143 max_index(g_sizex * g_sizey), 144 index_stride(g_sizex), 145 pixel_depth(24), 146 ptr32(g_pImg), 147 start_x(x), 148 start_y(y), 149 size_x(sizex), 150 size_y(sizey) { 151 assert(x < g_sizex); 152 assert(y < g_sizey); 153 assert(x + sizex <= g_sizex); 154 assert(y + sizey <= g_sizey); 155 156 index = base_index; // current index 157 } 158 159 void drawing_area::update() { 160 if (g_video->updating) { 161 RECT r; 162 r.left = start_x; 163 r.right = start_x + size_x; 164 r.top = start_y; 165 r.bottom = start_y + size_y; 166 InvalidateRect(g_hAppWnd, &r, false); 167 } 168 } 169