home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible / OpenGL_Superbible_Waite_Group_Press_1996.iso / book / chapt10 / tank / microsoft / compasswnd.c < prev    next >
C/C++ Source or Header  |  1996-07-07  |  7KB  |  270 lines

  1. // CompassWnd.c
  2. // This file contains the window procedure and code related to the
  3. // Compass window. This is also an OpenGL Window and displays a 
  4. // directional Compass
  5.  
  6.  
  7. #include <windows.h>    // Normal Windows stuff
  8. #include <math.h>
  9. #include <gl\gl.h>
  10. #include <gl\glu.h>
  11. #include "externs.h"    // Data shared between files
  12. #include "glutils.h"
  13.  
  14.  
  15.  
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // Setup the Rendering context for the Compass window
  18. void SetupCompassRC(void)
  19.     {
  20.     GLint    nDegrees;
  21.  
  22.     // Black background
  23.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  24.  
  25.     // Green drawing color
  26.     glRGB(0,255,0);
  27.  
  28.     // Just draw the outlines of the polygons
  29.     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  30.  
  31.     // Create the display list
  32.     glNewList(nCompassList,GL_COMPILE);
  33.  
  34.         // Draw the outline of an N
  35.         glBegin(GL_LINE_STRIP);
  36.             glVertex2f(-0.05f, 0.85f);
  37.             glVertex2f(-0.05f, 0.99f);
  38.             glVertex2f(0.05f, 0.85f);
  39.             glVertex2f(0.05f, 0.99f);
  40.         glEnd();
  41.  
  42.         // Draw the outline of an E
  43.         glBegin(GL_LINE_STRIP);
  44.             glVertex2f(0.97f, 0.08f);
  45.             glVertex2f(0.86f, 0.08f);
  46.             glVertex2f(0.86f, -0.08f);
  47.             glVertex2f(0.98f, -0.08f);
  48.         glEnd();
  49.  
  50.         glBegin(GL_LINES);
  51.             glVertex2f(0.93f, 0.0f);
  52.             glVertex2f(0.86f, 0.0f);
  53.         glEnd();
  54.  
  55.         // Draw the outline of a W
  56.         glBegin(GL_LINE_STRIP);
  57.             glVertex2f(-0.99f, 0.05f);
  58.             glVertex2f(-0.95f, -0.05f);
  59.             glVertex2f(-0.90f, 0.05f);
  60.             glVertex2f(-0.86f, -0.05f);
  61.             glVertex2f(-0.81f, 0.05f);
  62.         glEnd();
  63.  
  64.         // Draw the outline of an S
  65.         glBegin(GL_LINE_STRIP);
  66.             glVertex2f(0.05f, -0.84f);
  67.             glVertex2f(-0.05f, -0.84f);
  68.             glVertex2f(-0.05f, -0.90f);
  69.             glVertex2f(0.05f, -0.90f);
  70.             glVertex2f(0.05f, -0.96f);
  71.             glVertex2f(-0.06f,-0.96f);
  72.         glEnd();
  73.  
  74.  
  75.         // Draw circular tick marks
  76.         for(nDegrees = 0; nDegrees < 360; nDegrees += 10)
  77.             {
  78.             glPushMatrix();
  79.                 glRotatef((GLfloat)nDegrees,0.0f, 0.0f, 1.0f);
  80.  
  81.                 glBegin(GL_LINES);
  82.                     glVertex2f(0.0f, 0.70f);
  83.  
  84.                     // Longer tick marks at NSEW    
  85.                     if(nDegrees == 0 || nDegrees == 90 || nDegrees == 180 || nDegrees == 270)
  86.                         glVertex2f(0.0f, 0.82f);
  87.                     else
  88.                         glVertex2f(0.0f, 0.77f);
  89.                 
  90.                 glEnd();
  91.                 
  92.             glPopMatrix();
  93.             }
  94.  
  95.     glEndList();
  96.     }
  97.  
  98.  
  99.  
  100. ////////////////////////////////////////////////////////////
  101. ////////////////////////////////////////////////////////////
  102. // Window procedure, handles all messages for this window
  103. LRESULT CALLBACK WndProcCompass(HWND    hWnd,
  104.                             UINT    message,
  105.                             WPARAM  wParam,
  106.                             LPARAM  lParam)
  107.     {
  108.     static HDC      hDC;    // Keep the Device Context
  109.     static HGLRC  hRC;    // Keep the Rendering Context
  110.  
  111.     switch (message)
  112.         {
  113.         // Window creation, setup here
  114.         case WM_CREATE:
  115.             // Save the device context
  116.             hDC = GetDC(hWnd);
  117.  
  118.             // Set the pixel format
  119.             SetDCPixelFormat(hDC);
  120.  
  121.             // Create palette if needed, this will return NULL if 
  122.             // no palette is required
  123.             hPalette = GetOpenGLPalette(hDC);
  124.     
  125.             // Create the rendering context and make it current
  126.             hRC = wglCreateContext(hDC);
  127.             wglMakeCurrent(hDC, hRC);
  128.  
  129.             // Do some setup here
  130.             SetupCompassRC();
  131.  
  132.             wglMakeCurrent(hDC,NULL);
  133.             break;
  134.  
  135.         // Window is being destroyed, cleanup
  136.         case WM_DESTROY:
  137.             // Cleanup...
  138.             // Deselect the current rendering context and delete it
  139.             wglMakeCurrent(hDC,NULL);
  140.             wglDeleteContext(hRC);
  141.  
  142.             // Destroy the palette if it was created 
  143.             if(hPalette != NULL)
  144.                 DeleteObject(hPalette);
  145.  
  146.             // Release the device context
  147.             ReleaseDC(hWnd,hDC);
  148.  
  149.             break;
  150.  
  151.         // Window is resized. Setup the Viewing transformation
  152.         case WM_SIZE:
  153.             {
  154.             int nWidth,nHeight;
  155.  
  156.             nWidth = LOWORD(lParam);  // width of client area 
  157.             nHeight = HIWORD(lParam); // height of client area 
  158.  
  159.             // Make this windows rendering context current
  160.             wglMakeCurrent(hDC, hRC);
  161.  
  162.             // Set the viewport to be the entire window
  163.             glViewport(0, 0, nWidth, nHeight);
  164.  
  165.  
  166.             glMatrixMode(GL_PROJECTION);
  167.             glLoadIdentity();
  168.  
  169.             // Establishes a 2 by 2 square with zero in the center
  170.             gluOrtho2D(-1.0, 1.0, -1.0, 1.0); 
  171.  
  172.             glMatrixMode(GL_MODELVIEW);
  173.             glLoadIdentity();
  174.  
  175.             // Deselect this windows device context
  176.             wglMakeCurrent(hDC,NULL);
  177.             }
  178.             break;
  179.  
  180.         case WM_PAINT:
  181.             {
  182.             GLfloat fDegrees;
  183.  
  184.             wglMakeCurrent(hDC, hRC);
  185.  
  186.             // Clear the window with current clearing color
  187.             glClear(GL_COLOR_BUFFER_BIT);
  188.  
  189.             glPushMatrix();
  190.  
  191.             // Draw the fixed position needle (CCW winding)
  192.             glBegin(GL_QUADS);
  193.                 glVertex2f(0.0f,0.8f);
  194.                 glVertex2f(0.1f, 0.0f);
  195.                 glVertex2f(0.0f, -0.4f);
  196.                 glVertex2f(-0.1f, 0.0f);
  197.             glEnd();
  198.  
  199.             // Convert radians to degrees
  200.             fDegrees =  57.2957795f * (GLfloat)(pObject->radsFromEast);
  201.  
  202.             // Offset because stored value is angle from due east
  203.             fDegrees = 90.0f - fDegrees;
  204.             glRotatef(fDegrees,0.0f, 0.0f, 1.0f);
  205.  
  206.             glCallList(nCompassList);
  207.             glPopMatrix();
  208.  
  209.             glFlush();
  210.                                     
  211.             SwapBuffers(hDC);
  212.             wglMakeCurrent(hDC,NULL);
  213.  
  214.             // Validate the newly painted client area
  215.             ValidateRect(hWnd,NULL);
  216.             }
  217.             break;
  218.  
  219.         // Windows is telling the application that it may modify
  220.         // the system palette.  This message in essance asks the 
  221.         // application for a new palette.
  222.         case WM_QUERYNEWPALETTE:
  223.             // If the palette was created.
  224.             if(hPalette)
  225.                 {
  226.                 int nRet;
  227.  
  228.                 // Selects the palette into the current device context
  229.                 SelectPalette(hDC, hPalette, FALSE);
  230.  
  231.                 // Map entries from the currently selected palette to
  232.                 // the system palette.  The return value is the number 
  233.                 // of palette entries modified.
  234.                 nRet = RealizePalette(hDC);
  235.  
  236.                 // Repaint, forces remap of palette in current window
  237.                 InvalidateRect(hWnd,NULL,FALSE);
  238.  
  239.                 return nRet;
  240.                 }
  241.             break;
  242.  
  243.     
  244.         // This window may set the palette, even though it is not the 
  245.         // currently active window.
  246.         case WM_PALETTECHANGED:
  247.             // Don't do anything if the palette does not exist, or if
  248.             // this is the window that changed the palette.
  249.             if((hPalette != NULL) && ((HWND)wParam != hWnd))
  250.                 {
  251.                 // Select the palette into the device context
  252.                 SelectPalette(hDC,hPalette,FALSE);
  253.  
  254.                 // Map entries to system palette
  255.                 RealizePalette(hDC);
  256.                 
  257.                 // Remap the current colors to the newly realized palette
  258.                 UpdateColors(hDC);
  259.                 return 0;
  260.                 }
  261.             break;
  262.  
  263.     
  264.         default:   // Passes it on if unproccessed
  265.             return (DefWindowProc(hWnd, message, wParam, lParam));
  266.         }
  267.  
  268.     return (0L);
  269.     }
  270.