Visual Servoing Platform  version 3.0.0
vpWin32API.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Filip Novotny
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpConfig.h>
39 #include <iostream>
40 
41 #if ( defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) )
42 #include <visp3/gui/vpWin32API.h>
43 #include <visp3/core/vpTime.h>
44 DWORD vpProcessErrors(const std::string &api_name){
45  LPVOID lpMsgBuf;
46  DWORD err = GetLastError();
47 
48  FormatMessage(
49  FORMAT_MESSAGE_ALLOCATE_BUFFER |
50  FORMAT_MESSAGE_FROM_SYSTEM |
51  FORMAT_MESSAGE_IGNORE_INSERTS,
52  NULL,
53  err,
54  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
55  (LPTSTR) &lpMsgBuf,
56  0, NULL );
57  std::cout << "call to " << api_name << " failed with the following error code: " << err << "(" << (LPTSTR)lpMsgBuf << ")" << std::endl;
58  return err;
59 }
60 
61 BOOL vpLineTo(HDC hdc, int nXEnd, int nYEnd){
62  BOOL ret = LineTo(hdc, nXEnd, nYEnd);
63  if(ret == 0)
64  vpProcessErrors("LineTo");
65  return ret;
66 }
67 
68 BOOL vpMoveToEx(HDC hdc, int X, int Y, LPPOINT lpPoint){
69  BOOL ret = MoveToEx(hdc, X, Y, lpPoint);
70  if(ret == 0)
71  vpProcessErrors("MoveToEx");
72  return ret;
73 }
74 
75 BOOL vpBitBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop){
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  BOOL ret = InvalidateRect(hWnd,lpRect,bErase);
84  if(ret == 0)
85  vpProcessErrors("InvalidateRect");
86  return ret;
87 }
88 
89 void vpSelectObject(HWND hWnd, HDC hDC, HDC hDCMem, HGDIOBJ h){
90 
91  HGDIOBJ ret = SelectObject(hDCMem,h);
92  if(ret==NULL){
93  vpProcessErrors("SelectObject");
94 
95  double ms = vpTime::measureTimeMs();
96 
97  while(ret==NULL && 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  BOOL ret = ReleaseSemaphore(hSemaphore,IReleaseCount,lpPreviousCount);
107 #ifndef __MINGW32__
108  if(ret==0){
109  vpProcessErrors("ReleaseSemaphore");
110  }
111 #endif
112  return ret;
113 }
114 
115 void vpEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection){
116  EnterCriticalSection(lpCriticalSection);
117 }
118 void vpLeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection){
119  LeaveCriticalSection(lpCriticalSection);
120 }
121 
122 COLORREF vpSetPixel(HDC hdc, int X, int Y, COLORREF crColor){
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  HBITMAP ret = CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
131  if (ret==NULL)
132  vpProcessErrors("CreateBitmap");
133 
134  return ret;
135 }
136 
137 #elif !defined(VISP_BUILD_SHARED_LIBS)
138 // Work arround to avoid warning: libvisp_core.a(vpWin32API.cpp.o) has no symbols
139 void dummy_vpWin32API() {};
140 #endif
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93