Advertisement
Guest User

Untitled

a guest
Oct 13th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.18 KB | None | 0 0
  1. #include <windows.h>
  2. #include <gl/gl.h>
  3. #include <gl/glu.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. HDC hdc;
  8. HWND hwnd;
  9. WNDCLASS wnd;
  10. HGLRC hglrc;
  11. MSG msg;
  12.  
  13. LPSTR szClassName = "MyClass";
  14. HINSTANCE hInstance;
  15.  
  16. LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
  17. void startGL();
  18. void initwindow();
  19. void closewindow();
  20. void draw();
  21.  
  22.  
  23. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
  24. {
  25.         hInstance = hInst;
  26.  
  27.         initwindow();
  28.         startGL();
  29.  
  30.         while(1) {
  31.                 if(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE)) {
  32.                         if(msg.message == WM_QUIT) {
  33.                                 break;
  34.                         }
  35.                                
  36.                         TranslateMessage(&msg);
  37.                         DispatchMessage(&msg);
  38.                 } else
  39.                         draw();
  40.  
  41.                 Sleep(100);   //crappy way of stopping 10000000000000 loops a second
  42.                        
  43.         }
  44.         closewindow();
  45.         return msg.wParam;
  46. }
  47.  
  48. void initwindow()
  49. {
  50.         wnd.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; //CS_OWNDC required for OPEGL - HREDRAW and VREDRAW update the screen on resize.
  51.         wnd.lpfnWndProc = MyWndProc;
  52.         wnd.cbClsExtra = 0;
  53.         wnd.cbWndExtra = 0;
  54.         wnd.hInstance = hInstance;
  55.         wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); //Icon of the program - default
  56.         wnd.hCursor = LoadCursor(NULL, IDC_ARROW);   //Mouse cursor - default
  57.         wnd.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  58.         wnd.lpszMenuName = NULL;                     //no menu
  59.         wnd.lpszClassName = szClassName;
  60.  
  61.         if(!RegisterClass(&wnd)) {
  62.                 MessageBox(NULL, "couldn't register class", //cant continue
  63.                            "Error", MB_OK);
  64.                 return 0;
  65.         }
  66.  
  67.         hwnd = CreateWindow(szClassName, "Arcticcu leetpro swagfagets!",WS_OVERLAPPEDWINDOW,
  68.                             CW_USEDEFAULT,CW_USEDEFAULT,
  69.                             1000,1000,NULL,NULL,hInstance,NULL);            
  70.         ShowWindow(hwnd, iCmdShow);            //Shows and updates window.
  71.         UpdateWindow(hwnd);
  72. }
  73.  
  74. void startGL()
  75. {
  76.         hdc = GetDC(hwnd);
  77.         PIXELFORMATDESCRIPTOR pfd = {0};
  78.  
  79.         pfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );    // just its size
  80.         pfd.nVersion = 1;   // always 1
  81.         pfd.dwFlags = PFD_SUPPORT_OPENGL |  PFD_DOUBLEBUFFER   |  PFD_DRAW_TO_WINDOW;  // openglsupport - doublebuffer support - draw to window not bitmap
  82.         pfd.iPixelType = PFD_TYPE_RGBA ;    // red, green, blue, alpha for each pixel
  83.         pfd.cColorBits = 24;                // 24 bit == 8 bits for red, 8 for green, 8 for blue.
  84.         pfd.cDepthBits = 32;
  85.         int pick;
  86.         pick = ChoosePixelFormat(hdc, &pfd);
  87.         SetPixelFormat(hdc,pick,&pfd);
  88.         hglrc = wglCreateContext(hdc);
  89.         wglMakeCurrent(hdc, hglrc);
  90. }
  91.  
  92. void closewindow()
  93. {
  94.         wglMakeCurrent(NULL,NULL);  // makes the opengl context not-current
  95.         wglDeleteContext(hglrc);  // deletes the opengl context
  96.         ReleaseDC(hwnd, hdc);     // releases Device context
  97.         CloseWindow(hwnd);          //closes the window, duh
  98. }
  99.  
  100. void draw()
  101. {
  102.         glMatrixMode(GL_PROJECTION);
  103.         glLoadIdentity();
  104.         glMatrixMode(GL_MODELVIEW);             //Basic opengl drawing.. a triangle :P
  105.         glTranslatef(0,0,0);
  106.  
  107.         glClear(GL_COLOR_BUFFER_BIT);
  108.  
  109.         glBegin(GL_POLYGON);
  110.         glColor3f(1,0,0);
  111.         glVertex2f(-0.6, -0.75);
  112.         glColor3f(0, 1, 0);
  113.         glVertex2f(0.6, -0.75);
  114.         glColor3f(0, 0, 1);
  115.         glVertex2f(0, 0.75);
  116.         glEnd();
  117.         SwapBuffers(hdc); //Swap the buffers so what was drawn is shown on the screen, must be done on each redraw.
  118. }
  119.  
  120. LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  121. {
  122.         if(msg == WM_DESTROY) {
  123.                 PostQuitMessage(0);
  124.                 break;
  125.         }
  126.         if(msg == WM_CLOSE) {
  127.                 DestroyWindow(hwnd);
  128.                 break;
  129.         }
  130.        
  131.         return DefWindowProc(hwnd, msg, wParam, lParam);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement