xref: /oneTBB/examples/common/gui/gdivideo.cpp (revision c21e688a)
1 /*
2     Copyright (c) 2005-2022 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
DisplayError(LPSTR lpstrErr,HRESULT hres)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                       nullptr,
44                       hres,
45                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
46                       (LPTSTR)&lpMsgBuf,
47                       0,
48                       nullptr);
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
InternalWndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)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, nullptr, 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 
init_window(int sizex,int sizey)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, nullptr);
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 
terminate()125 void video::terminate() {
126     delete g_pBitmap;
127     g_pBitmap = nullptr;
128 
129     Gdiplus::GdiplusShutdown(gdiplusToken);
130     g_video = nullptr;
131     running = false;
132 
133     delete[] g_pImg;
134     g_pImg = nullptr;
135 }
136 
137 //////////// drawing area constructor & destructor /////////////
138 
drawing_area(int x,int y,int sizex,int sizey)139 drawing_area::drawing_area(int x, int y, int sizex, int sizey)
140         : base_index(y * g_sizex + x),
141           max_index(g_sizex * g_sizey),
142           index_stride(g_sizex),
143           pixel_depth(24),
144           ptr32(g_pImg),
145           start_x(x),
146           start_y(y),
147           size_x(sizex),
148           size_y(sizey) {
149     assert(x < g_sizex);
150     assert(y < g_sizey);
151     assert(x + sizex <= g_sizex);
152     assert(y + sizey <= g_sizey);
153 
154     index = base_index; // current index
155 }
156 
update()157 void drawing_area::update() {
158     if (g_video->updating) {
159         RECT r;
160         r.left = start_x;
161         r.right = start_x + size_x;
162         r.top = start_y;
163         r.bottom = start_y + size_y;
164         InvalidateRect(g_hAppWnd, &r, false);
165     }
166 }
167