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
OpenGLApplication.cpp
Go to the documentation of this file.
1 
6 #include "GlobalParameters.h"
7 #include "OpenGLApplication.h"
8 #include "World.h"
9 #include "Textures.h"
10 #include "Objects.h"
11 #include "FrameCounter.h"
12 
27 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
28  int ret = -1;
29  OpenGLApplication * app = OpenGLApplication::create("OpenGLapldk3hd"); // create application class
30  if (app != 0) {
31  ret = app->main( hInstance, hPrevInstance, lpCmdLine, nCmdShow ); // execute main program
32  delete app;
33  } else { // error creating application
34  MessageBox( HWND_DESKTOP, "Error Creating Application Class", "Error", MB_OK | MB_ICONEXCLAMATION );
35  }
36  return ret;
37 }
38 
49 LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
50  LONG userData = GetWindowLong(hWnd, GWL_USERDATA); // Load The Application Class Context
51  if ( userData == 0 ) { // Application Class Context Not Available
52  if ( uMsg == WM_CREATE ) { // Evaluate Window Message and Creation message received
53  // Get Window Structure Pointer Which Contains The Application Class Context
54  CREATESTRUCT * creation = reinterpret_cast<CREATESTRUCT *>( lParam );
55  // Get The Application Class Context
56  OpenGLApplication * app = reinterpret_cast<OpenGLApplication *>( creation->lpCreateParams );
57  // Store The Application Class Context
58  SetWindowLong( hWnd, GWL_USERDATA, reinterpret_cast<LONG>(app) );
59  app->m_isVisible = true;
60  return 0;
61  }
62  } else { // Get The Application Class Context
63  OpenGLApplication * app = reinterpret_cast<OpenGLApplication *>( userData );
64  return app->message( hWnd, uMsg, wParam, lParam ); // Call Class Message Handler
65  }
66  return DefWindowProc( hWnd, uMsg, wParam, lParam ); // Pass Unhandled Messages To DefWindowProc
67 }
68 
79 OpenGLApplication::OpenGLApplication( const char * className ) {
80  // initialize the variables
81  m_className = className; // Store Class Name
82  m_isProgramLooping = true; // Program Looping Is Set To true
83  m_createFullScreen = true; // Enable FullScreen Mode
84  m_isVisible = false;
85  m_resizeDraw = false; // Disable Redraw During Window Resize
86 
87  // don' have to be initialized cuz they are initialized if framelimit is set on
88  useFrameLimit = false;// soll frame limit benutzt werden?
89 
90  // initializing the used objects, create new instances
91  fc = new FrameCounter();
92 
93  // create global objects
97 }
98 
103  // do some cleanup, delete all created objects
104  delete Objects::getObjects();
105  delete Textures::getTextures();
106  delete World::getWorld();
107  delete fc;
108 }
109 
117  glDisable( GL_CULL_FACE );
118  glDisable( GL_DEPTH_TEST );
119  glDisable( GL_LIGHTING );// Licht deaktivieren
120  glDisable( GL_LIGHT0 );// Licht0 (Sonne) aktivieren
121 }
122 
127  glEnable( GL_DEPTH_TEST );
128  glEnable( GL_CULL_FACE );
129  glEnable( GL_LIGHTING );// Licht wieder aktivieren
130  glEnable( GL_LIGHT0 );// Licht0 (Sonne) aktivieren
131 }
132 
141  return fc;
142 }
143 
153  return this->useFrameLimit ? m_frameLimit : 0;
154 }
155 
160  return &m_window;
161 }
162 
167 void OpenGLApplication::resizeDraw( bool draw ) {
168  m_resizeDraw = draw;
169 }
170 
180  PostMessage( m_window, WM_QUIT, 0, 0 );
181  m_isProgramLooping = false;
182 }
183 
188  PostMessage(m_window, WM_TOGGLEFULLSCREEN, 0, 0); // Send A WM_TOGGLEFULLSCREEN Message
189 }
190 
198 LRESULT OpenGLApplication::message( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
199  switch (uMsg) {
200  case WM_SYSCOMMAND: // Intercept System Commands
201  switch (wParam) { // Check System Calls
202  case SC_SCREENSAVE: // screensaver starting
203  case SC_MONITORPOWER: // monitor power save
204  return 0; // do not allow that
205  break;
206  }
207  break;
208  case WM_CLOSE: // window closing
209  terminate(); // terminate
210  return 0;
211  break;
212  case WM_EXITMENULOOP:
213  case WM_EXITSIZEMOVE:
214  fc->newFrame();
215  return 0;
216  break;
217  case WM_MOVE: // moving. update position
218  m_window.setLeft( LOWORD(lParam) ); // x position in lo word
219  m_window.setTop( HIWORD(lParam) ); // y position in hi word
220  return 0;
221  break;
222  case WM_PAINT:
223  if( m_resizeDraw ) {
224  m_window.reshape();
225  drawTest();
226  }
227  break;
228 
229  case WM_SIZING:
230  {
231  RECT * rect = (RECT *)lParam;
232  m_window.setWidth( rect->right - rect->left ); // width in lo word
233  m_window.setHeight( rect->bottom - rect->top ); // height in hi word
234  return TRUE;
235  }
236  break;
237 
238  case WM_SIZE: // resize has occured
239  switch (wParam)
240  {
241  case SIZE_MINIMIZED: // hide if minimized
242  m_isVisible = false;
243  return 0;
244  break;
245 
246  case SIZE_MAXIMIZED: // draw and update position
247  case SIZE_RESTORED: // if becoming visible again
248  m_isVisible = true;
249  m_window.setWidth( LOWORD(lParam) );
250  m_window.setHeight( HIWORD(lParam) );
251  m_window.reshape();
252  fc->newFrame();
253  return 0;
254  break;
255  }
256  break;
257 
258  case WM_KEYDOWN: // key pressed. update buffers.
259  m_window.keyDown( (unsigned char)wParam );
260  return 0;
261  break;
262  case WM_KEYUP:
263  m_window.keyUp( (unsigned char)wParam );
264  return 0;
265  break;
266 
267  case WM_MOUSEMOVE:
268  m_window.setMousePosition( LOWORD (lParam), HIWORD (lParam) );
269  break;
270 
271  case WM_LBUTTONUP:
272  m_window.setMousePosition( LOWORD (lParam), HIWORD (lParam) );
273  m_window.setMouseClick( true );
274  break;
275 
276  case WM_LBUTTONDOWN:
277  break;
278 
279  // user defined window message to toggle between fullscreen and windowed mode. leads to quit window!
280  case WM_TOGGLEFULLSCREEN:
281  m_createFullScreen = !m_createFullScreen;
282  PostMessage( hWnd, WM_QUIT, 0, 0 );
283  break;
284  }
285 
286  return DefWindowProc( hWnd, uMsg, wParam, lParam ); // let everything else be handled by the default window function
287 }
288 
302 int OpenGLApplication::main( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
303  //WNDCLASSEX wc; // commented for extended classes
304  WNDCLASS wc; // we use only default window classes
305  ZeroMemory( &wc, sizeof(wc) ); // reset memory
306  //wc.cbSize = sizeof(WNDCLASSEX); // update only for extended classes
307  wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // redraw if resized
308  wc.lpfnWndProc = (WNDPROC) WndProc; // the window function
309  wc.hInstance = hInstance;
310  //wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE); // background brush, not needed
311  wc.hIcon = LoadIcon( NULL, IDI_WINLOGO ); // load an icon. standard.
312  wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // show normal arrow mouse-pointer
313  wc.lpszClassName = m_className;
314 
315  if( !RegisterClass(&wc) ) { // return, if somehow failed.
316  MessageBox( NULL, "Failed to register the Window-Class.", "ERROR", MB_OK | MB_ICONEXCLAMATION );
317  return false;
318  }
319 
320  m_createFullScreen = START_IN_FULLSCREEN;
321 
322  while( m_isProgramLooping ) { // loop until WM_QUIT received
323  bool result = m_window.create( "mElite - Grafica al Calcolatore Progetto da Melanie und Jan-Philipp", m_createFullScreen, m_className, hInstance, this );
324  if ( result ) {
325  // window should have been created
326  if( initialize() == false ) { // terminate if initialization fails
327  terminate();
328  } else { // otherwise handle messages
329  MSG msg;
330  bool isMessagePumpActive = true;
331  fc->newFrame();
332  while( isMessagePumpActive ) {
333  if( PeekMessage( &msg, m_window, 0, 0, PM_REMOVE ) != 0) { // get a message and handle
334  if ( msg.message != WM_QUIT ) {
335  DispatchMessage( &msg );
336  } else {
337  isMessagePumpActive = false;
338  }
339  } else {
340  if (m_isVisible == false) {
341  WaitMessage();
342  } else {
343  drawTest();
344  }
345  }
346  }
347  }
348  deinitialize(); // clean up at the end
349  m_window.kill();
350  } else { // creation failed.
351  MessageBox(HWND_DESKTOP, "Error creating OpenGL Window", "Error", MB_OK | MB_ICONEXCLAMATION);
352  m_isProgramLooping = false;
353  }
354  }
355 
356  if( !UnregisterClass( m_className, hInstance ) ) {
357  MessageBox( NULL, "Could not unregister Window-Class.", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION );
358  }
359 
360  return 0;
361 }
362 
369 void OpenGLApplication::setFrameLimit( unsigned int framesPerSecond ) {
370  if(framesPerSecond == 0) // 0 sets frame limiting off
371  useFrameLimit = false;
372  else {
373  useFrameLimit = true;
374  m_frameLimit = framesPerSecond;
375  this->frameTime = 1000.0 / framesPerSecond;
376  }
377 }
378 
383 void OpenGLApplication::drawTest() {
384  if( useFrameLimit )
385  if( fc->getFrameTimeCurrent() < frameTime )
386  return;
387 
388  update( fc->getFrameTimeCurrent() );
389  fc->newFrame();
390  draw();
391  m_window.swap();
392 }