home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / qe4 / win_xy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-28  |  6.7 KB  |  285 lines

  1. // win_xy.c -- windows specific xy view code
  2.  
  3. #include "qe3.h"
  4.  
  5. static HDC   s_hdcXY;
  6. static HGLRC s_hglrcXY;
  7.  
  8. static unsigned s_stipple[32] =
  9. {
  10.     0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  11.     0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  12.     0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  13.     0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  14.     0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  15.     0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  16.     0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  17.     0xaaaaaaaa, 0x55555555,0xaaaaaaaa, 0x55555555,
  18. };
  19.  
  20. /*
  21. ============
  22. WXY_WndProc
  23. ============
  24. */
  25. LONG WINAPI WXY_WndProc (
  26.     HWND    hWnd,
  27.     UINT    uMsg,
  28.     WPARAM  wParam,
  29.     LPARAM  lParam)
  30. {
  31.     int        fwKeys, xPos, yPos;
  32.     RECT    rect;
  33.  
  34.  
  35.     GetClientRect(hWnd, &rect);
  36.  
  37.     switch (uMsg)
  38.     {
  39.     case WM_CREATE:
  40.  
  41.         s_hdcXY = GetDC(hWnd);
  42.  
  43.         QEW_SetupPixelFormat(s_hdcXY, false);
  44.  
  45.         if ( ( s_hglrcXY = wglCreateContext( s_hdcXY ) ) == 0 )
  46.             Error( "wglCreateContext in WXY_WndProc failed" );
  47.  
  48.         if (!wglMakeCurrent( s_hdcXY, s_hglrcXY ))
  49.             Error ("wglMakeCurrent failed");
  50.  
  51.         if (!wglShareLists( g_qeglobals.d_hglrcBase, s_hglrcXY ) )
  52.             Error( "wglShareLists in WXY_WndProc failed" );
  53.  
  54.         glPolygonStipple ((char *)s_stipple);
  55.         glLineStipple (3, 0xaaaa);
  56.  
  57.         return 0;
  58.  
  59.     case WM_DESTROY:
  60.         QEW_StopGL( hWnd, s_hglrcXY, s_hdcXY );
  61.         return 0;
  62.  
  63.     case WM_PAINT:
  64.         { 
  65.             PAINTSTRUCT    ps;
  66.  
  67.             BeginPaint(hWnd, &ps);
  68.  
  69.             if (!wglMakeCurrent( s_hdcXY, s_hglrcXY ))
  70.                 Error ("wglMakeCurrent failed");
  71.  
  72.             QE_CheckOpenGLForErrors();
  73.             XY_Draw ();
  74.             QE_CheckOpenGLForErrors();
  75.  
  76.             SwapBuffers(s_hdcXY);
  77.  
  78.             EndPaint(hWnd, &ps);
  79.         }
  80.         return 0;
  81.  
  82.     case WM_KEYDOWN:
  83.         return QE_KeyDown (wParam);
  84.         
  85.     case WM_MBUTTONDOWN:
  86.     case WM_RBUTTONDOWN:
  87.     case WM_LBUTTONDOWN:
  88.         if ( GetTopWindow( g_qeglobals.d_hwndMain ) != hWnd)
  89.             BringWindowToTop(hWnd);
  90.         SetFocus( g_qeglobals.d_hwndXY );
  91.         SetCapture( g_qeglobals.d_hwndXY );
  92.         fwKeys = wParam;        // key flags 
  93.         xPos = (short)LOWORD(lParam);  // horizontal position of cursor 
  94.         yPos = (short)HIWORD(lParam);  // vertical position of cursor 
  95.         yPos = (int)rect.bottom - 1 - yPos;
  96.         XY_MouseDown (xPos, yPos, fwKeys);
  97.         return 0;
  98.  
  99.     case WM_MBUTTONUP:
  100.     case WM_RBUTTONUP:
  101.     case WM_LBUTTONUP:
  102.         fwKeys = wParam;        // key flags 
  103.         xPos = (short)LOWORD(lParam);  // horizontal position of cursor 
  104.         yPos = (short)HIWORD(lParam);  // vertical position of cursor 
  105.         yPos = (int)rect.bottom - 1 - yPos;
  106.         XY_MouseUp (xPos, yPos, fwKeys);
  107.         if (! (fwKeys & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
  108.             ReleaseCapture ();
  109.         return 0;
  110.  
  111.     case WM_MOUSEMOVE:
  112.         fwKeys = wParam;        // key flags 
  113.         xPos = (short)LOWORD(lParam);  // horizontal position of cursor 
  114.         yPos = (short)HIWORD(lParam);  // vertical position of cursor 
  115.         yPos = (int)rect.bottom - 1 - yPos;
  116.         XY_MouseMoved (xPos, yPos, fwKeys);
  117.         return 0;
  118.  
  119.     case WM_SIZE:
  120.         g_qeglobals.d_xy.width = rect.right;
  121.         g_qeglobals.d_xy.height = rect.bottom;
  122.         InvalidateRect( g_qeglobals.d_hwndXY, NULL, false);
  123.         return 0;
  124.  
  125.     case WM_NCCALCSIZE:// don't let windows copy pixels
  126.         DefWindowProc (hWnd, uMsg, wParam, lParam);
  127.         return WVR_REDRAW;
  128.  
  129.     case WM_KILLFOCUS:
  130.     case WM_SETFOCUS:
  131.         SendMessage( hWnd, WM_NCACTIVATE, uMsg == WM_SETFOCUS, 0 );
  132.         return 0;
  133.  
  134.        case WM_CLOSE:
  135.         DestroyWindow (hWnd);
  136.         return 0;
  137.     }
  138.  
  139.     return DefWindowProc (hWnd, uMsg, wParam, lParam);
  140. }
  141.  
  142.  
  143. /*
  144. ==============
  145. WXY_Create
  146. ==============
  147. */
  148. void WXY_Create (HINSTANCE hInstance)
  149. {
  150.     WNDCLASS   wc;
  151.  
  152.     /* Register the camera class */
  153.     memset (&wc, 0, sizeof(wc));
  154.  
  155.     wc.style         = 0;
  156.     wc.lpfnWndProc   = (WNDPROC)WXY_WndProc;
  157.     wc.cbClsExtra    = 0;
  158.     wc.cbWndExtra    = 0;
  159.     wc.hInstance     = hInstance;
  160.     wc.hIcon         = 0;
  161.     wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
  162.     wc.hbrBackground = NULL; //(HBRUSH)(COLOR_WINDOW+1);
  163.     wc.lpszMenuName  = NULL;
  164.     wc.lpszClassName = XY_WINDOW_CLASS;
  165.  
  166.     if (!RegisterClass (&wc) )
  167.         Error ("RegisterClass: failed");
  168.  
  169.     g_qeglobals.d_hwndXY = CreateWindow (XY_WINDOW_CLASS ,
  170.         "XY View",
  171.         QE3_STYLE ,
  172.         ZWIN_WIDTH,
  173.         (int)(screen_height*CWIN_SIZE)-20,
  174.         screen_width-ZWIN_WIDTH,
  175.         (int)(screen_height*(1.0-CWIN_SIZE)-38),    // size
  176.  
  177.         g_qeglobals.d_hwndMain,    // parent
  178.         0,        // no menu
  179.         hInstance,
  180.         NULL);
  181.  
  182.     if (!g_qeglobals.d_hwndXY )
  183.         Error ("Couldn't create XY View");
  184.  
  185.     LoadWindowState(g_qeglobals.d_hwndXY, "xywindow");
  186.     ShowWindow(g_qeglobals.d_hwndXY, SW_SHOWDEFAULT);
  187. }
  188.  
  189. static void WXY_InitPixelFormat( PIXELFORMATDESCRIPTOR *pPFD )
  190. {
  191.     memset( pPFD, 0, sizeof( *pPFD ) );
  192.  
  193.     pPFD->nSize    = sizeof( PIXELFORMATDESCRIPTOR );
  194.     pPFD->nVersion = 1;
  195.     pPFD->dwFlags  = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  196.     pPFD->iPixelType = PFD_TYPE_RGBA;
  197.     pPFD->cColorBits = 24;
  198.     pPFD->cDepthBits = 32;
  199.     pPFD->iLayerType = PFD_MAIN_PLANE;
  200.  
  201. }
  202.  
  203. void WXY_Print( void )
  204. {
  205.     DOCINFO di;
  206.  
  207.     PRINTDLG pd;
  208.  
  209.     /*
  210.     ** initialize the PRINTDLG struct and execute it
  211.     */
  212.     memset( &pd, 0, sizeof( pd ) );
  213.     pd.lStructSize = sizeof( pd );
  214.     pd.hwndOwner = g_qeglobals.d_hwndXY;
  215.     pd.Flags = PD_RETURNDC;
  216.     pd.hInstance = 0;
  217.     if ( !PrintDlg( &pd ) || !pd.hDC )
  218.     {
  219.         MessageBox( g_qeglobals.d_hwndMain, "Could not PrintDlg()", "QE4 Print Error", MB_OK | MB_ICONERROR );
  220.         return;
  221.     }
  222.  
  223.     /*
  224.     ** StartDoc
  225.     */
  226.     memset( &di, 0, sizeof( di ) );
  227.     di.cbSize = sizeof( di );
  228.     di.lpszDocName = "QE4";
  229.     if ( StartDoc( pd.hDC, &di ) <= 0 )
  230.     {
  231.         MessageBox( g_qeglobals.d_hwndMain, "Could not StartDoc()", "QE4 Print Error", MB_OK | MB_ICONERROR );
  232.         return;
  233.     }
  234.  
  235.     /*
  236.     ** StartPage
  237.     */
  238.     if ( StartPage( pd.hDC ) <= 0 )
  239.     {
  240.         MessageBox( g_qeglobals.d_hwndMain, "Could not StartPage()", "QE4 Print Error", MB_OK | MB_ICONERROR );
  241.         return;
  242.     }
  243.  
  244.     /*
  245.     ** read pixels from the XY window
  246.     */
  247.     {
  248.         int bmwidth = 320, bmheight = 320;
  249.         int pwidth, pheight;
  250.  
  251.         RECT r;
  252.  
  253.         GetWindowRect( g_qeglobals.d_hwndXY, &r );
  254.  
  255.         bmwidth  = r.right - r.left;
  256.         bmheight = r.bottom - r.top;
  257.  
  258.         pwidth  = GetDeviceCaps( pd.hDC, PHYSICALWIDTH ) - GetDeviceCaps( pd.hDC, PHYSICALOFFSETX );
  259.         pheight = GetDeviceCaps( pd.hDC, PHYSICALHEIGHT ) - GetDeviceCaps( pd.hDC, PHYSICALOFFSETY );
  260.  
  261.         StretchBlt( pd.hDC,
  262.             0, 0,
  263.             pwidth, pheight,
  264.             s_hdcXY,
  265.             0, 0,
  266.             bmwidth, bmheight,
  267.             SRCCOPY );
  268.     }
  269.  
  270.     /*
  271.     ** EndPage and EndDoc
  272.     */
  273.     if ( EndPage( pd.hDC ) <= 0 )
  274.     {
  275.         MessageBox( g_qeglobals.d_hwndMain, "QE4 Print Error", "Could not EndPage()", MB_OK | MB_ICONERROR );
  276.         return;
  277.     }
  278.  
  279.     if ( EndDoc( pd.hDC ) <= 0 )
  280.     {
  281.         MessageBox( g_qeglobals.d_hwndMain, "QE4 Print Error", "Could not EndDoc()", MB_OK | MB_ICONERROR );
  282.         return;
  283.     }
  284. }
  285.