ViSP  2.6.2
vpWin32API.cpp
1 /****************************************************************************
2  *
3  * $Id: vpWin32API.cpp 3719 2012-05-10 05:50:23Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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) )
48 DWORD vpProcessErrors(char* 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  DWORD err = 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 }
110 
111 BOOL vpReleaseSemaphore(HANDLE hSemaphore,LONG IReleaseCount,LPLONG lpPreviousCount){
112  BOOL ret = ReleaseSemaphore(hSemaphore,IReleaseCount,lpPreviousCount);
113  if(ret==0){
114  vpProcessErrors("ReleaseSemaphore");
115  }
116  return ret;
117 
118 }
119 
120 void vpEnterCriticalSection(LPCRITICAL_SECTION lpCriticalSection){
121  EnterCriticalSection(lpCriticalSection);
122 }
123 void vpLeaveCriticalSection(LPCRITICAL_SECTION lpCriticalSection){
124  LeaveCriticalSection(lpCriticalSection);
125 }
126 
127 COLORREF vpSetPixel(HDC hdc, int X, int Y, COLORREF crColor){
128  COLORREF ret = SetPixel(hdc, X, Y, crColor);
129  if(ret == 0)
130  vpProcessErrors("SetPixel");
131  return ret;
132 }
133 
134 HBITMAP vpCreateBitmap(int nWidth, int nHeight, UINT cPlanes, UINT cBitsPerPel, const VOID *lpvBits){
135  HBITMAP ret = CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
136  if (ret==NULL)
137  vpProcessErrors("CreateBitmap");
138 
139  return ret;
140 }
141 
142 #endif
static double measureTimeMs()
Definition: vpTime.cpp:86