mElite  1.0
An Elite clone based on TextElite by Jan-Philipp Kappmeier and Melanie Schmidt.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OpenGLWindow.cpp
Go to the documentation of this file.
1 
6 #include "GlobalGL.h"
7 #include "OpenGLWindow.h"
8 
18  // needed handles are initialized to zero
19  m_hWnd = NULL;
20  m_hDC = NULL;
21  m_hRC = NULL;
22 
23  // window position and size
24  m_fullscreen = false;
25  active = true;
26  m_left = 0; // window x Position
27  m_top = 0; // window y Position
28  m_width = 800;
29  m_height = 600;
30  m_screenWidth = 1920;
31  m_screenHeight = 1080;
32 
33  // frustum
34  m_frustum.farDist = 5000.0;
35  m_frustum.nearDist = 1.0;
36 
37  m_bpp = 32; // 32 bit farbtiefe anfordern
38  //m_BitsPerPixel = 16; // Bits Per Pixel
39 
40  // input: characters and mouse
41  for(unsigned char i=0;i<255;i++)
42  keys[i] = false;
43  keys[255] = false;
44  m_mouseClick = false;
45  m_mouseVisible = false;
46 }
47 
52 
53 }
54 
59  DEVMODE dmScreenSettings; // Device Mode
60  ZeroMemory( &dmScreenSettings, sizeof( dmScreenSettings ) );
61 
62  dmScreenSettings.dmSize = sizeof( dmScreenSettings );
63  dmScreenSettings.dmPelsWidth = getWidth(); // the new screen width
64  dmScreenSettings.dmPelsHeight = getHeight(); // the new screen height
65  dmScreenSettings.dmBitsPerPel = m_bpp; // the bits per pixel
66  dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
67 
68  if( ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN )!= DISP_CHANGE_SUCCESSFUL ) {
69  return false;
70  }
71  return true;
72 }
73 
82 bool OpenGLWindow::create( const char * windowTitle, bool fullscreen, const char * className, HINSTANCE hInstance, LPVOID lpParam ) {
83  GLuint PixelFormat;
84  DWORD dwExStyle;
85  DWORD dwStyle;
86 
87  m_fullscreen = fullscreen;// Übernehmen der Vollbild-Einstellung
88 
89  if(m_fullscreen) {
90  if( !changeScreenResolution() ) { // try to change resolution, switch to window mode or quit if failed
91  if( MessageBox( NULL, "The requested fullscreen mode is not supported by\nyour video card. Use windowed mode instead?", "Question", MB_YESNO | MB_ICONEXCLAMATION ) == IDYES ) {
92  m_fullscreen = false;
93  } else {
94  MessageBox( NULL, "Program will now close.", "ERROR", MB_OK | MB_ICONSTOP );
95  return false;
96  }
97  }
98  }
99 
100  RECT windowRect = { getLeft(), getTop(), getLeft() + getWidth(), getTop() + getHeight() };
101 
102  if( fullscreen ) {
103  dwExStyle = WS_EX_APPWINDOW | WS_EX_TOPMOST;
104  dwStyle = WS_POPUP;
105  ShowCursor( FALSE );
106  } else {
107  dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
108  dwStyle = WS_OVERLAPPEDWINDOW;
109 
110  AdjustWindowRectEx( &windowRect, dwStyle, false, dwExStyle );
111  // move window top left corner to visible area
112  if (windowRect.left < 0) {
113  windowRect.right -= windowRect.left;
114  windowRect.left = 0;
115  }
116  if (windowRect.top < 0) {
117  windowRect.bottom -= windowRect.top;
118  windowRect.top = 0;
119  }
120  }
121 
122  // finally create the OpenGL Window
123  if ( !(m_hWnd=CreateWindowEx( dwExStyle, // style
124  className, // window class
125  windowTitle, // title
126  dwStyle | // style
127  WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
128  windowRect.left, windowRect.top, // left, top
129  windowRect.right - windowRect.left, // height
130  windowRect.bottom - windowRect.top, // width
131  HWND_DESKTOP, // desktop as parent
132  NULL, // no menu
133  hInstance, // the instance
134  lpParam))) // application class context
135  {
136  kill();
137  MessageBox( NULL, "Window creation error.", "ERROR", MB_OK | MB_ICONEXCLAMATION );
138  return FALSE;
139  }
140 
141  // the propertys our window should have
142  static PIXELFORMATDESCRIPTOR pfd;
143  ZeroMemory( &pfd, sizeof( pfd ) ); // fill with zeros
144  pfd.nSize = sizeof( PIXELFORMATDESCRIPTOR ); // size of Pixelformat Descriptors
145  pfd.nVersion = 1; // necessary
146  pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; // use double buffering
147  pfd.iPixelType = PFD_TYPE_RGBA; // RGBA colors
148  pfd.cColorBits = m_bpp; // color depth
149  pfd.cDepthBits = 24;// depth buffer size
150  pfd.iLayerType = PFD_MAIN_PLANE;
151 
152  if( !(m_hDC=GetDC( m_hWnd )) ) { // quit if failed
153  kill();
154  MessageBox( NULL, "Can't create a GL Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION );
155  return false;
156  }
157  if( !(PixelFormat = ChoosePixelFormat( m_hDC, &pfd )) ) {
158  kill();
159  MessageBox( NULL, "Can't find a suitable PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION );
160  return false;
161  }
162  if( !SetPixelFormat( m_hDC, PixelFormat, &pfd ) ) {
163  kill();
164  MessageBox( NULL, "Can't set the PixelFormat.", "ERROR", MB_OK | MB_ICONEXCLAMATION );
165  return FALSE;
166  }
167  if( !(m_hRC = wglCreateContext( m_hDC )) ) {
168  kill();
169  MessageBox( NULL, "Can't create a GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION );
170  return FALSE;
171  }
172  if( !wglMakeCurrent( m_hDC, m_hRC ) ) {
173  kill();
174  MessageBox( m_hWnd, "Can't activate the GL Rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION );
175  return FALSE;
176  }
177 
178  // everything fine. finally show window
179  ShowWindow( m_hWnd, SW_SHOW );
180  SetFocus( m_hWnd );
181  reshape();
182  return true;
183 }
184 
186 
191 float OpenGLWindow::getAspect( void ) {
192  return m_frustum.aspect;
193 }
194 
200 unsigned int OpenGLWindow::getHeight( void ) {
201  if( m_fullscreen )
202  return m_screenHeight;
203  else
204  return m_height;
205 }
206 
212 unsigned int OpenGLWindow::getWidth( void ) {
213  if( m_fullscreen )
214  return m_screenWidth;
215  else
216  return m_width;
217 }
218 
224 unsigned int OpenGLWindow::getLeft( void ) {
225  if( m_fullscreen )
226  return 0;
227  else
228  return m_left;
229 }
230 
236 unsigned int OpenGLWindow::getTop( void ) {
237  if( m_fullscreen )
238  return 0;
239  else
240  return m_top;
241 }
242 
248  return m_frustum;
249 }
250 
255  return m_fullscreen;
256 }
257 
259 
262 void OpenGLWindow::kill( void ) {
263  if( m_fullscreen ) {
264  ChangeDisplaySettings( NULL, 0 );
265  ShowCursor( TRUE );
266  }
267 
268  if( m_hRC ) {
269  if( !wglMakeCurrent( NULL, NULL ) )
270  MessageBox( NULL, "Release of DC and RC failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
271  if( !wglDeleteContext( m_hRC ) )
272  MessageBox( NULL, "Release rendering context failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
273  m_hRC = NULL;
274  }
275  if( m_hDC && !ReleaseDC( m_hWnd, m_hDC ) ) {
276  MessageBox( NULL, "Release device context failed.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
277  m_hDC = NULL;
278  }
279  if( m_hWnd && !DestroyWindow( m_hWnd ) ) {
280  MessageBox( NULL, "Could not release hWnd.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
281  m_hWnd = NULL;
282  }
283 }
284 
286 
290  GLsizei width = getWidth();
291  GLsizei height = getHeight();
292  if( height == 0 ) // avoid division by zero
293  height = 1;
294 
295  // Calculate correct perspective and aspect ratio of the window
296  m_frustum.aspect = (float)width / (float)height;
297  glViewport( 0, 0, width, height );
298  glMatrixMode(GL_PROJECTION);
299  glLoadIdentity();
300  gluPerspective( m_frustum.fov, m_frustum.aspect, m_frustum.nearDist, m_frustum.farDist );
301 
302  // do _not_ forget transforming 45 degree angle to degree for use with tangens function!
303  m_frustum.nearHeight = 2 * tan( m_frustum.fov * ANGLE2DEG * 0.5 ) * m_frustum.nearDist;
304  m_frustum.farHeight = 2 * tan( m_frustum.fov * ANGLE2DEG * 0.5 ) * m_frustum.farDist;
305  m_frustum.nearWidth = m_frustum.nearHeight * m_frustum.aspect;
306  m_frustum.farWidth = m_frustum.farHeight * m_frustum.aspect;
307 
308  glMatrixMode( GL_MODELVIEW );
309  glLoadIdentity();
310 }
311 
317 void OpenGLWindow::setLeft( unsigned int left ) {
318  m_left = left;
319 }
320 
326 void OpenGLWindow::setTop( unsigned int top ) {
327  m_top = top;
328 }
329 
335 void OpenGLWindow::setHeight( unsigned int height ) {
336  if ( m_fullscreen )
337  m_screenHeight = height;
338  else
339  m_height = height;
340 }
341 
347 void OpenGLWindow::setWidth( unsigned int width ) {
348  if ( m_fullscreen )
349  m_screenWidth = width;
350  else
351  m_width = width;
352 }
353 
361  m_mousePos.x = x;
362  m_mousePos.y = y;
363 }
364 
371  return m_mousePos;
372 }
373 
379 void OpenGLWindow::setMouseClick( bool click ) {
380  m_mouseClick = click;
381 }
382 
390  return m_mouseClick;
391 }
392 
399 void OpenGLWindow::showMousePointer( bool mouseVisible ) {
400  m_mouseVisible = mouseVisible;
401  if( m_fullscreen )
402  ShowCursor( mouseVisible );
403 }