Advertisement
Vilsol

Untitled

Jan 5th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.10 KB | None | 0 0
  1.  
  2. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  3. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  4. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  5.  
  6. // OpenGL without using GLUT - 2013 by Neil Dansey, Tim Dykes and Ian Cant, and using excerpts from here:
  7. // http://bobobobo.wordpress.com/2008/02/11/opengl-in-a-proper-windows-app-no-glut/
  8. // Feel free to adapt this for what you need, but please leave these comments in.
  9.  
  10. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  11. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  12. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  13.  
  14.  
  15. #pragma once
  16.  
  17. #include <windows.h>    // need this file if you want to create windows etc
  18. #include <gl/gl.h>      // need this file to do graphics with opengl
  19. #include <gl/glu.h>     // need this file to set up a perspective projection easily
  20.  
  21.  
  22. // include the opengl and glu libraries
  23. #pragma comment(lib, "opengl32.lib")   
  24. #pragma comment(lib, "glu32.lib")
  25.  
  26.  
  27.  
  28.  
  29. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  30. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  31. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  32.  
  33.  
  34. // function prototypes:
  35. LRESULT CALLBACK WndProc(   HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );   
  36. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );
  37.  
  38.  
  39. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  40. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  41. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  42.  
  43. // In a C++ Windows app, the starting point is WinMain() rather than _tmain() or main().
  44. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
  45. {
  46.     // some basic numbers to hold the position and size of the window
  47.     int windowWidth = 800;
  48.     int windowHeight = 600;
  49.     int windowTopLeftX = 50;
  50.     int windowTopLeftY = 50;
  51.  
  52.     // some other variables we need for our game...
  53.     MSG msg;                                // this will be used to store messages from the operating system
  54.     bool keepPlaying = true;                // whether or not we want to keep playing
  55.  
  56.  
  57.  
  58.     // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  59.     // this section contains all the window initialisation code,
  60.     // and should probably be collapsed for the time being to avoid confusion  
  61. #pragma region  <-- click the plus/minus sign to collapse/expand!
  62.  
  63.     // this bit creates a window class, basically a template for the window we will make later, so we can do more windows the same.
  64.     WNDCLASS myWindowClass;
  65.     myWindowClass.cbClsExtra = 0;                                           // no idea
  66.     myWindowClass.cbWndExtra = 0;                                           // no idea
  67.     myWindowClass.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );    // background fill black
  68.     myWindowClass.hCursor = LoadCursor( NULL, IDC_ARROW );                  // arrow cursor      
  69.     myWindowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );                // type of icon to use (default app icon)
  70.     myWindowClass.hInstance = hInstance;                                    // window instance name (given by the OS when the window is created)  
  71.     myWindowClass.lpfnWndProc = WndProc;                                    // window callback function - this is our function below that is called whenever something happens
  72.     myWindowClass.lpszClassName = TEXT("my window class name");             // our new window class name
  73.     myWindowClass.lpszMenuName = 0;                                         // window menu name (0 = default menu?)
  74.     myWindowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;               // redraw if the window is resized horizontally or vertically, allow different context for each window instance
  75.  
  76.     // Register that class with the Windows OS..
  77.     RegisterClass(&myWindowClass);
  78.    
  79.     // create a rect structure to hold the dimensions of our window
  80.     RECT rect;
  81.     SetRect( &rect, windowTopLeftX,             // the top-left corner x-coordinate
  82.                     windowTopLeftY,             // the top-left corner y-coordinate
  83.                     windowTopLeftX + windowWidth,       // far right
  84.                     windowTopLeftY + windowHeight );    // far left
  85.    
  86.     // adjust the window, no idea why.
  87.     AdjustWindowRect( &rect, WS_OVERLAPPEDWINDOW, false );
  88.  
  89.     // call CreateWindow to create the window
  90.    HWND myWindow = CreateWindow(TEXT("my window class name"),       // window class to use - in this case the one we created a minute ago
  91.                           TEXT("CT4TOGA Coursework Template"),      // window title
  92.                           WS_OVERLAPPEDWINDOW,                      // ??
  93.                           windowTopLeftX, windowTopLeftY,           // x, y
  94.                           windowWidth, windowHeight,                            // width and height
  95.                           NULL, NULL,                               // ??
  96.                           hInstance, NULL);                         // ??
  97.  
  98.    
  99.    // check to see that the window created okay
  100.     if( myWindow == NULL )
  101.     {
  102.         FatalAppExit( NULL, TEXT("CreateWindow() failed!") );
  103.     }
  104.  
  105.     // if so, show it
  106.     ShowWindow( myWindow, iCmdShow );
  107.    
  108.  
  109.     // get a device context from the window
  110.     HDC myDeviceContext = GetDC( myWindow );
  111.  
  112.  
  113.     // we create a pixel format descriptor, to describe our desired pixel format.
  114.     // we set all of the fields to 0 before we do anything else
  115.     // this is because PIXELFORMATDESCRIPTOR has loads of fields that we won't use
  116.     PIXELFORMATDESCRIPTOR myPfd = { 0 };
  117.  
  118.  
  119.     // now set only the fields of the pfd we care about:
  120.     myPfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );      // size of the pfd in memory
  121.     myPfd.nVersion = 1;                                 // always 1
  122.  
  123.     myPfd.dwFlags = PFD_SUPPORT_OPENGL |                // OpenGL support - not DirectDraw
  124.                     PFD_DOUBLEBUFFER   |                // double buffering support
  125.                     PFD_DRAW_TO_WINDOW;                 // draw to the app window, not to a bitmap image
  126.  
  127.     myPfd.iPixelType = PFD_TYPE_RGBA ;                  // red, green, blue, alpha for each pixel
  128.     myPfd.cColorBits = 24;                              // 24 bit == 8 bits for red, 8 for green, 8 for blue.
  129.                                                         // This count of color bits EXCLUDES alpha.
  130.  
  131.     myPfd.cDepthBits = 32;                              // 32 bits to measure pixel depth.
  132.  
  133.  
  134.     // now we need to choose the closest pixel format to the one we wanted...  
  135.     int chosenPixelFormat = ChoosePixelFormat( myDeviceContext, &myPfd );
  136.  
  137.     // if windows didnt have a suitable format, 0 would have been returned...
  138.     if( chosenPixelFormat == 0 )
  139.     {
  140.         FatalAppExit( NULL, TEXT("ChoosePixelFormat() failed!") );
  141.     }
  142.  
  143.     // if we get this far it means we've got a valid pixel format
  144.     // so now we need to set the device context up with that format...
  145.     int result = SetPixelFormat( myDeviceContext, chosenPixelFormat, &myPfd );
  146.  
  147.     // if it failed...
  148.     if (result == NULL)
  149.     {
  150.         FatalAppExit( NULL, TEXT("SetPixelFormat() failed!") );
  151.     }
  152.  
  153.     // we now need to set up a render context (for opengl) that is compatible with the device context (from windows)...
  154.     HGLRC myRenderContext = wglCreateContext( myDeviceContext );
  155.  
  156.     // then we connect the two together
  157.     wglMakeCurrent( myDeviceContext, myRenderContext );
  158.  
  159.  
  160.  
  161.     // opengl display setup
  162.     glMatrixMode(GL_PROJECTION);    // select the projection matrix, i.e. the one that controls the "camera"
  163.     glLoadIdentity();               // reset it
  164.     gluPerspective(45.0,(float)windowWidth/(float)windowHeight, 1, 1000);   // set up fov, and near / far clipping planes
  165.     glViewport(0, 0, windowWidth, windowHeight);                            // make the viewport cover the whole window
  166.     glClearColor( 0.5, 0, 0, 1.0 );                                         // set the colour used for clearing the screen
  167.  
  168. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  169. #pragma endregion
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.     // main game loop starts here!
  177.  
  178.  
  179.     // keep doing this as long as the player hasnt exited the app:
  180.     while( keepPlaying == true )
  181.     {
  182.  
  183.         // we need to listen out for OS messages.
  184.         // if there is a windows message to process...
  185.         if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  186.         {
  187.             // and if the message is a "quit" message...
  188.             if( msg.message == WM_QUIT )
  189.             {
  190.                 keepPlaying = false;    // we want to quit asap
  191.             }
  192.             // or if it was any other type of message (i.e. one we don't care about), process it as normal...
  193.             else
  194.             {
  195.                 TranslateMessage( &msg );
  196.                 DispatchMessage( &msg );  
  197.             }
  198.         }
  199.  
  200.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // clear screen
  201.         SwapBuffers(myDeviceContext);                           // update graphics
  202.  
  203.     }
  204.  
  205.  
  206.     // the next bit will therefore happen when the player quits the app,
  207.     // because they are trapped in the previous section as long as (keepPlaying == true).
  208.  
  209.     // UNmake our rendering context (make it 'uncurrent')
  210.     wglMakeCurrent( NULL, NULL );
  211.  
  212.     // delete the rendering context, we no longer need it.
  213.     wglDeleteContext( myRenderContext );
  214.  
  215.     // release our window's DC from the window
  216.     ReleaseDC( myWindow, myDeviceContext );
  217.  
  218.     // end the program
  219.     return msg.wParam;
  220. }
  221.  
  222. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  223. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  224. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  225.  
  226. // this part contains some code that should be collapsed for now too...
  227. #pragma region keep_this_bit_collapsed_too!
  228.  
  229. // this function is called when any events happen to our window
  230. LRESULT CALLBACK WndProc(   HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
  231. {
  232.  
  233.     switch( message )
  234.     {
  235.         // if they exited the window...
  236.         case WM_DESTROY:
  237.             // post a message "quit" message to the main windows loop
  238.             PostQuitMessage( 0 ) ;
  239.             return 0;
  240.             break;
  241.     }
  242.  
  243.     // must do this as a default case (i.e. if no other event was handled)...
  244.     return DefWindowProc( hwnd, message, wparam, lparam );
  245.  
  246. }
  247.  
  248. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement