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