Advertisement
Guest User

C++ WIN32 OPENGL

a guest
Feb 8th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <GL/GL.h>
  3. #include <iostream>
  4. #include <string.h>
  5. #include <tchar.h>
  6.  
  7. #pragma comment (lib, "opengl32.lib")
  8.  
  9. static TCHAR szWindowClass[] = _T("Windows32App");
  10. static TCHAR szTitle[] = _T("OpenGL 3.3 Demo");
  11.  
  12. bool active = TRUE;
  13. bool keys[256];
  14. int DRAW_OPENGL(GLvoid)
  15. {
  16.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  17.     glTranslatef(3.0f,0.0f,0.0f);
  18.     glBegin(GL_TRIANGLES);
  19.     glVertex3f(0.0f,1.0f,0.0f);
  20.     glVertex3f(-1.0f,-1.0f,0.0f);
  21.     glVertex3f(1.0f,-1.0f,0.0f);
  22.     glEnd();
  23.    
  24.    
  25.     return TRUE;
  26.  
  27. }
  28.  
  29. LRESULT CALLBACK WindowProcedure(HWND WindowHandle, UINT Messages, WPARAM wParam, LPARAM lParam)
  30. {
  31.     //PAINTSTRUCT ps;
  32.     HDC hdc = NULL;
  33.     TCHAR greeting[] = _T("Test");
  34.     COLORREF NewColor = RGB(255,0,0);
  35.     switch(Messages)
  36.     {
  37.     case WM_ACTIVATE:
  38.         {
  39.             if(!HIWORD(wParam))
  40.             {
  41.                     active = TRUE;
  42.             }
  43.             else
  44.             {
  45.                 active = FALSE;
  46.             }
  47.             return 0;
  48.         }
  49.     case WM_CREATE:
  50.         {
  51.            
  52.         PIXELFORMATDESCRIPTOR pfd =
  53.             {
  54.                 sizeof(PIXELFORMATDESCRIPTOR),
  55.                 1,
  56.                 PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
  57.                 PFD_TYPE_RGBA,
  58.                 32,
  59.                 0,0,0,0,0,0
  60.                 ,0
  61.                 ,0
  62.                 ,0
  63.                 ,0,0,0,0,
  64.                 16,
  65.                 0,
  66.                 0,
  67.                 PFD_MAIN_PLANE,
  68.                 0,
  69.                 0,0,0
  70.             };
  71.  
  72.         hdc = GetDC(WindowHandle);
  73.  
  74.         GLuint ChooseWindowFormat = ChoosePixelFormat(hdc,&pfd);
  75.        
  76.         if(!SetPixelFormat(hdc,ChooseWindowFormat,&pfd))
  77.         {
  78.             MessageBox(NULL,L"Can't Set The PixelFormat.",L"ERROR",MB_OK|MB_ICONEXCLAMATION);
  79.         };
  80.  
  81.         HGLRC DummyGLContext = wglCreateContext(hdc);
  82.         wglMakeCurrent(hdc, DummyGLContext);
  83.  
  84.         MessageBoxA(0, (char*)glGetString(GL_VERSION), "OPENGL VERSION",0);
  85.         //wglDeleteContext(0);
  86.         glViewport(0,0,640,480);
  87.         glMatrixMode(GL_PROJECTION);
  88.         glLoadIdentity();          
  89.         glOrtho(0,640,0,480,-1,1);
  90.         glMatrixMode(GL_MODELVIEW);
  91.         glLoadIdentity();          
  92.         glClearColor(255,0,0,0);
  93.    
  94.         }
  95.         break;
  96.     //case WM_PAINT:
  97.         //hdc = BeginPaint(WindowHandle, &ps);
  98.    
  99.        
  100.         //EndPaint(WindowHandle, &ps);
  101.     /* 
  102.        
  103.         SetTextColor(hdc, NewColor);
  104.         TextOut(hdc,5,5,greeting, _tcslen(greeting));
  105.        
  106.         */
  107.         //break;
  108.     case WM_CLOSE:
  109.         DestroyWindow(WindowHandle);
  110.         break;
  111.     case WM_DESTROY:
  112.         PostQuitMessage(0);
  113.         break;
  114.  
  115.     default:
  116.         return DefWindowProc(WindowHandle,Messages,wParam,lParam);
  117.    
  118.     }
  119.     return 0;
  120. }
  121.  
  122. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
  123.     {
  124.  
  125.        
  126.         MSG Messages;
  127.         WNDCLASSEX WindowClass;
  128.         HDC hdc = NULL;
  129.         bool Running = true;
  130.         hInstance = GetModuleHandle(NULL);
  131.         WindowClass.cbSize = sizeof(WNDCLASSEX);
  132.         WindowClass.style  = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;
  133.         WindowClass.lpfnWndProc = WindowProcedure;
  134.         WindowClass.cbClsExtra = 0;
  135.         WindowClass.cbWndExtra = 0;
  136.         WindowClass.hInstance = hInstance;
  137.         WindowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  138.         WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  139.         WindowClass.hbrBackground = NULL;
  140.         WindowClass.lpszMenuName = NULL;
  141.         WindowClass.lpszClassName = szWindowClass;
  142.         WindowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  143.        
  144.         if(!RegisterClassEx(&WindowClass)) {
  145.  
  146.             MessageBox(NULL,
  147.                 _T("Window Creation Failed"),
  148.                 _T("ERROR") ,
  149.                 NULL);
  150.             return 1;
  151.         }
  152.  
  153.         HWND WindowHandle = CreateWindow(
  154.                 szWindowClass,
  155.                 szTitle,
  156.                 WS_OVERLAPPEDWINDOW,
  157.                 CW_USEDEFAULT, CW_USEDEFAULT,
  158.                 640,480,
  159.                 NULL,
  160.                 NULL,
  161.                 hInstance,
  162.                 NULL
  163.             );
  164.  
  165.         if(!WindowHandle)
  166.             {
  167.                 MessageBox(NULL,
  168.                     _T("Window Registration Failed!"),
  169.                     _T("ERROR"),
  170.                     NULL);
  171.                     return 1;
  172.             }
  173.  
  174.         ShowWindow(WindowHandle, nCmdShow);
  175.         UpdateWindow(WindowHandle);
  176.  
  177.         while(Running == true)
  178.             {
  179.                 while(PeekMessage(&Messages,NULL,0,0,PM_NOREMOVE))
  180.                 {
  181.                     if(GetMessage(&Messages, NULL, 0, 0))
  182.                     {
  183.                         TranslateMessage(&Messages);   
  184.                         DispatchMessage(&Messages);
  185.                     }
  186.                     else
  187.                     {
  188.                         return Messages.wParam;
  189.                     }
  190.                 }
  191.                
  192.                     DRAW_OPENGL();
  193.  
  194.                     SwapBuffers(hdc);
  195.            
  196.                 }
  197.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement