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