Advertisement
Guest User

OpenGL

a guest
Nov 7th, 2014
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "MathHelper.h"
  6. #include "GL.h"
  7. #include "GLEnums.h"
  8. #include "GameColorPalette.h"
  9. #include <string>
  10. #include "Point.h"
  11. #include "Size.h"
  12. #include "GameMouse.h"
  13. #include "GameConsole.h"
  14.  
  15. HINSTANCE hInstance;
  16. HWND hwnd;
  17. HDC   hdc;
  18. HGLRC hglrc;
  19. Size windowSize;
  20. GameMouse mouse;
  21. std::string windowText;
  22. bool isGameRunning = true;
  23. /*
  24.     Frames Per Second Variables
  25.     */
  26. int frameCount = 0;
  27. float fps = 0;
  28. int currentTime = 0;
  29. int previousTime = 0;
  30. //
  31.  
  32. LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
  33. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );
  34. void draw();
  35. void setTitle( const std::string& text );
  36.  
  37. void initializeGL() {
  38.     GL::clearColor( GameColorPalette::Cornflowerblue );
  39. }
  40.  
  41. void calculateFPS() {
  42.     frameCount++;
  43.     currentTime = timeGetTime();
  44.     int timeInterval = currentTime - previousTime;
  45.     if( timeInterval > 1000 ) {
  46.         fps = frameCount / ( timeInterval / 1000.0f );
  47.         previousTime = currentTime;
  48.         frameCount = 0;
  49.     }
  50. }
  51.  
  52. void toggleVSync( bool toggle ) {
  53.     typedef BOOL( WINAPI *PFNWGLSWAPINTERVALEXTPROC )( int interval );
  54.     PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
  55.     wglSwapIntervalEXT = ( PFNWGLSWAPINTERVALEXTPROC )wglGetProcAddress( "wglSwapIntervalEXT" );
  56.     if( wglSwapIntervalEXT )
  57.         wglSwapIntervalEXT( toggle );
  58. }
  59.  
  60. int WINAPI WinMain( HINSTANCE h_Instance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow ) {
  61.     hInstance = h_Instance;
  62.     WNDCLASS wc;
  63.     wc.cbClsExtra = 0;
  64.     wc.cbWndExtra = 0;
  65.     wc.hbrBackground = ( HBRUSH )GetStockObject( BLACK_BRUSH );
  66.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  67.     wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  68.     wc.hInstance = hInstance;
  69.     wc.lpfnWndProc = WndProc;
  70.     wc.lpszClassName = TEXT( "Client" );
  71.     wc.lpszMenuName = 0;
  72.     wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  73.     RegisterClass( &wc );
  74.     RECT rect;
  75.     SetRect( &rect, 50,
  76.         50,
  77.         850,
  78.         650 );
  79.     windowSize.setWidth( rect.right - rect.left );
  80.     windowSize.setHeight( rect.bottom - rect.top );
  81.     AdjustWindowRect( &rect, WS_OVERLAPPEDWINDOW, false );
  82.     hwnd = CreateWindow( TEXT( "Client" ),
  83.         TEXT( "Game Window" ),
  84.         WS_OVERLAPPEDWINDOW,
  85.         rect.left, rect.top,
  86.         rect.right - rect.left, rect.bottom - rect.top,
  87.         NULL, NULL,
  88.         hInstance, NULL );
  89.     if( hwnd == NULL ) {
  90.         FatalAppExit( NULL, TEXT( "CreateWindow() failed!" ) );
  91.     }
  92.     ShowWindow( hwnd, iCmdShow );
  93.     hdc = GetDC( hwnd );
  94.     PIXELFORMATDESCRIPTOR pfd = { 0 };
  95.     pfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );
  96.     pfd.nVersion = 1;
  97.     pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;
  98.     pfd.iPixelType = PFD_TYPE_RGBA;
  99.     pfd.cColorBits = 24;
  100.     pfd.cDepthBits = 32;
  101.     int chosenPixelFormat = ChoosePixelFormat( hdc, &pfd );
  102.     if( chosenPixelFormat == 0 ) {
  103.         FatalAppExit( NULL, TEXT( "ChoosePixelFormat() failed!" ) );
  104.     }
  105.     int result = SetPixelFormat( hdc, chosenPixelFormat, &pfd );
  106.     if( result == NULL ) {
  107.         FatalAppExit( NULL, TEXT( "SetPixelFormat() failed!" ) );
  108.     }
  109.     hglrc = wglCreateContext( hdc );
  110.     wglMakeCurrent( hdc, hglrc );
  111.     MSG msg;
  112.     GameConsole::create();
  113.     toggleVSync( false );
  114.     //
  115.     initializeGL();
  116.     while( isGameRunning ) {
  117.         if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
  118.             if( msg.message == WM_QUIT ) {
  119.                 break;
  120.             }
  121.             TranslateMessage( &msg );
  122.             DispatchMessage( &msg );
  123.         } else {
  124.             draw();
  125.         }
  126.     }
  127.     /*
  128.         Destroy Game Client
  129.         */
  130.     wglMakeCurrent( NULL, NULL );
  131.     wglDeleteContext( hglrc );
  132.     ReleaseDC( hwnd, hdc );
  133.     AnimateWindow( hwnd, 200, AW_HIDE | AW_BLEND );
  134.     GameConsole::destroy();
  135.     return msg.wParam;
  136. }
  137.  
  138. void draw() {
  139.     GL::viewport( 0, 0, windowSize.getWidth(), windowSize.getHeight() );
  140.     GL::matrixMode( MatrixMode::Projection );
  141.     GL::loadIdentity();
  142.     GL::perspective( 45.0, ( float )windowSize.getWidth() / ( float )windowSize.getHeight(), 0.1f, 1000.0f );
  143.     GL::matrixMode( MatrixMode::Modelview );
  144.     GL::loadIdentity();
  145.     GL::lookAt(
  146.         0, 0, 10,
  147.         0, 0, 0,
  148.         0, 1, 0 );
  149.     GL::clear( ClearBufferMask::ColorBufferBit | ClearBufferMask::DepthBufferBit );
  150.     GL::begin( PrimitiveType::Triangles );
  151.     GL::color3( GameColorPalette::Red );
  152.     GL::vertex3( 1, 0, 0 );
  153.     GL::color3( GameColorPalette::Green );
  154.     GL::vertex3( 0, 1, 0 );
  155.     GL::color3( GameColorPalette::Blue );
  156.     GL::vertex3( -1, 0, 0 );
  157.     GL::end();
  158.     SwapBuffers( hdc );
  159.     calculateFPS();
  160.     windowText = std::string( "Game Window" ) + " FPS: " + std::to_string( fps );
  161.     setTitle( windowText );
  162. }
  163.  
  164. LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam ) {
  165.     switch( message ) {
  166.         case WM_CREATE:
  167.             return 0;
  168.             break;
  169.         case WM_LBUTTONDOWN:
  170.             return 0;
  171.             break;
  172.         case WM_LBUTTONUP:
  173.             return 0;
  174.             break;
  175.         case WM_RBUTTONDOWN:
  176.             return 0;
  177.             break;
  178.         case WM_RBUTTONUP:
  179.             return 0;
  180.             break;
  181.         case WM_SIZE:
  182.             windowSize.width = LOWORD( lparam );
  183.             windowSize.height = HIWORD( lparam );
  184.             GameConsole::writeLine( "Game Window Resized" );
  185.             GameConsole::writeLine( std::string( "Width: " ) + std::to_string( windowSize.width ) );
  186.             GameConsole::writeLine( std::string( "Height: " ) + std::to_string( windowSize.height ) );
  187.             return 0;
  188.             break;
  189.         case WM_MOUSEMOVE:
  190.             mouse.location.x = LOWORD( lparam );
  191.             mouse.location.y = HIWORD( lparam );
  192.             return 0;
  193.             break;
  194.         case WM_PAINT:
  195.             HDC hdc;
  196.             PAINTSTRUCT ps;
  197.             hdc = BeginPaint( hwnd, &ps );
  198.             EndPaint( hwnd, &ps );
  199.             return 0;
  200.             break;
  201.         case WM_KEYDOWN:
  202.             switch( wparam ) {
  203.                 case VK_ESCAPE:
  204.                     isGameRunning = false;
  205.                     break;
  206.                 default:
  207.                     break;
  208.             }
  209.             return 0;
  210.         case WM_DESTROY:
  211.             PostQuitMessage( 0 );
  212.             return 0;
  213.             break;
  214.     }
  215.     return DefWindowProc( hwnd, message, wparam, lparam );
  216. }
  217.  
  218. void setTitle( const std::string& text ) {
  219.     SetWindowText( hwnd, ( LPSTR )text.c_str() );
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement