Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpWin32API.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software 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 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * GDI renderer for windows 32 display
32  */
33 
34 #include <iostream>
35 #include <visp3/core/vpConfig.h>
36 
37 #if (defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9))
38 #include <visp3/core/vpTime.h>
39 #include <visp3/gui/vpWin32API.h>
40 
41 BEGIN_VISP_NAMESPACE
42 
43 DWORD vpProcessErrors(const std::string &api_name)
44 {
45  LPVOID lpMsgBuf;
46  DWORD err = GetLastError();
47 
48  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, err,
49  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, nullptr);
50  std::cout << "call to " << api_name << " failed with the following error code: " << err << "(" << (LPTSTR)lpMsgBuf
51  << ")" << std::endl;
52  return err;
53 }
54 
55 BOOL vpLineTo(HDC hdc, int nXEnd, int nYEnd)
56 {
57  BOOL ret = LineTo(hdc, nXEnd, nYEnd);
58  if (ret == 0)
59  vpProcessErrors("LineTo");
60  return ret;
61 }
62 
63 BOOL vpMoveToEx(HDC hdc, int X, int Y, LPPOINT lpPoint)
64 {
65  BOOL ret = MoveToEx(hdc, X, Y, lpPoint);
66  if (ret == 0)
67  vpProcessErrors("MoveToEx");
68  return ret;
69 }
70 
71 BOOL vpBitBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc,
72  DWORD dwRop)
73 {
74  BOOL ret = BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop);
75  if (ret == 0)
76  vpProcessErrors("BitBlt");
77  return ret;
78 }
79 
80 BOOL vpInvalidateRect(HWND hWnd, const RECT *lpRect, BOOL bErase)
81 {
82  BOOL ret = InvalidateRect(hWnd, lpRect, bErase);
83  if (ret == 0)
84  vpProcessErrors("InvalidateRect");
85  return ret;
86 }
87 
88 void vpSelectObject(HWND hWnd, HDC hDC, HDC hDCMem, HGDIOBJ h)
89 {
90 
91  HGDIOBJ ret = SelectObject(hDCMem, h);
92  if (ret == nullptr) {
93  vpProcessErrors("SelectObject");
94 
95  double ms = vpTime::measureTimeMs();
96 
97  while (ret == nullptr && vpTime::measureTimeMs() - ms < 5000) {
98  DeleteObject(h);
99  DeleteDC(hDCMem);
100  ReleaseDC(hWnd, hDC);
101  }
102  }
103 }
104 
105 BOOL vpReleaseSemaphore(HANDLE hSemaphore, LONG IReleaseCount, LPLONG lpPreviousCount)
106 {
107  BOOL ret = ReleaseSemaphore(hSemaphore, IReleaseCount, lpPreviousCount);
108 #ifndef __MINGW32__
109  if (ret == 0) {
110  vpProcessErrors("ReleaseSemaphore");
111  }
112 #endif
113  return ret;
114 }
115 
116 void vpEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection) { EnterCriticalSection(lpCriticalSection); }
117 void vpLeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection) { LeaveCriticalSection(lpCriticalSection); }
118 
119 COLORREF vpSetPixel(HDC hdc, int X, int Y, COLORREF crColor)
120 {
121  COLORREF ret = SetPixel(hdc, X, Y, crColor);
122  if (ret == 0)
123  vpProcessErrors("SetPixel");
124  return ret;
125 }
126 
127 HBITMAP vpCreateBitmap(int nWidth, int nHeight, UINT cPlanes, UINT cBitsPerPel, const VOID *lpvBits)
128 {
129  HBITMAP ret = CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
130  if (ret == nullptr)
131  vpProcessErrors("CreateBitmap");
132 
133  return ret;
134 }
135 
136 END_VISP_NAMESPACE
137 
138 #elif !defined(VISP_BUILD_SHARED_LIBS)
139 // Work around to avoid warning: libvisp_gui.a(vpWin32API.cpp.o) has no symbols
140 void dummy_vpWin32API() { };
141 #endif
VISP_EXPORT double measureTimeMs()