home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / texpaint / win_pal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-12  |  4.9 KB  |  236 lines

  1. #include "texpaint.h"
  2.  
  3. #define    PALETTE_WINDOW_CLASS    "TPPalette"
  4.  
  5. HDC        paldc;
  6. int        pal_width, pal_height;
  7. int        blocks_x, blocks_y;
  8.  
  9. int            selected_index;
  10. unsigned    selected_rgb;
  11.  
  12. byte    palette[768];
  13.  
  14. float SnapAspect (float aspect)
  15. {
  16.     if (aspect > 128)
  17.         return 256;
  18.     if (aspect > 32)
  19.         return 128;
  20.     if (aspect > 8)
  21.         return 64;
  22.     if (aspect > 2)
  23.         return 32;
  24.     return 16;
  25. }
  26.  
  27. void Pal_SetIndex (int index)
  28. {
  29.     selected_index = index;
  30.     selected_rgb = palette[index*3] + (palette[index*3+1]<<8) + (palette[index*3+2]<<16);
  31.     InvalidateRect (palettewindow, NULL, false);
  32. }
  33.  
  34. void Pal_Draw (void)
  35. {
  36.     int        x, y;
  37.     float    aspect;
  38.     float    xs, ys;
  39.     int        c;
  40.  
  41.     if (pal_width < 1 || pal_height < 1)
  42.         return;
  43.  
  44.     //
  45.     // determine the block arrangement
  46.     //
  47.     if (pal_width > pal_height)
  48.     {
  49.         aspect = SnapAspect (pal_width / pal_height);
  50.         blocks_x = aspect;
  51.         blocks_y = 256/blocks_x;
  52.     }
  53.     else
  54.     {
  55.         aspect = SnapAspect (pal_height / pal_width);
  56.         blocks_y = aspect;
  57.         blocks_x = 256/blocks_y;
  58.     }
  59.  
  60.     //
  61.     // draw it
  62.     //
  63.     glViewport (0,0,pal_width, pal_height);
  64.     glMatrixMode (GL_PROJECTION);
  65.     glLoadIdentity ();
  66.     glOrtho (0,1,0,1,-100,100);
  67.     glMatrixMode (GL_MODELVIEW);
  68.     glLoadIdentity ();
  69.  
  70.     glClear (GL_COLOR_BUFFER_BIT);
  71.     glDisable (GL_DEPTH_TEST);
  72.     glDisable (GL_CULL_FACE);
  73.     glDisable (GL_TEXTURE_2D);
  74.  
  75.     xs = 1.0/blocks_x;
  76.     ys = 1.0/blocks_y;
  77.  
  78.     for (x=0 ; x<blocks_x ; x++)
  79.     {
  80.         for (y=0 ; y<blocks_y ; y++)
  81.         {
  82.             c = x*blocks_y+(blocks_y-1-y);
  83.             glColor3ubv (palette+c*3);
  84.             glRectf (x*xs, y*ys, (x+1)*xs, (y+1)*ys);
  85.         }
  86.     }
  87.  
  88.     // highlight the selected texture
  89.     y = selected_index % blocks_y;
  90.     x = selected_index / blocks_y;
  91.     y = blocks_y-1-y;
  92.  
  93.     glColor3f (0,0,0);
  94.     glRectf ( (x+0.4)*xs, (y+0.4)*ys, (x+0.6)*xs, (y+0.6)*ys);
  95. }
  96.  
  97. void Pal_Click (int x, int y)
  98. {
  99.     int        index;
  100.  
  101.     x = x*blocks_x/pal_width;
  102.     y = y*blocks_y/pal_height;
  103.     y = blocks_y-1-y;
  104.  
  105.     index = x*blocks_y + y;
  106.     Pal_SetIndex (index);
  107. }
  108.  
  109. /*
  110. ============
  111. Palette_WndProc
  112. ============
  113. */
  114. LONG WINAPI Palette_WndProc (
  115.     HWND    hWnd,
  116.     UINT    uMsg,
  117.     WPARAM  wParam,
  118.     LPARAM  lParam)
  119. {
  120.     LONG    lRet = 1;
  121.     int        fwKeys, xPos, yPos;
  122.     RECT    rect;
  123.  
  124.     GetClientRect(hWnd, &rect);
  125.     pal_width = rect.right-rect.left;
  126.     pal_height = rect.bottom-rect.top;
  127.  
  128.     switch (uMsg)
  129.     {
  130.     case WM_CREATE:
  131.         paldc = GetDC(hWnd);
  132.         bSetupPixelFormat(paldc);
  133.         break;
  134.     case WM_PAINT:
  135.         { 
  136.             PAINTSTRUCT    ps;
  137.  
  138.             BeginPaint(hWnd, &ps);
  139.             if (!wglMakeCurrent( paldc, baseRC ))
  140.                 Error ("wglMakeCurrent failed");
  141.             Pal_Draw ();
  142.             EndPaint(hWnd, &ps);
  143.             SwapBuffers(paldc);
  144.         }
  145.         break;
  146.  
  147.         case WM_MOUSEMOVE:
  148.             if (wParam != MK_LBUTTON)
  149.                 break;
  150.         case WM_LBUTTONDOWN:
  151.             if (GetTopWindow(mainwindow) != hWnd)
  152.                 BringWindowToTop(hWnd);
  153.  
  154.             xPos = (short)LOWORD(lParam);  // horizontal position of cursor 
  155.             yPos = (short)HIWORD(lParam);  // vertical position of cursor 
  156.             yPos = (int)rect.bottom - 1 - yPos;
  157.  
  158.             Pal_Click (xPos, yPos);
  159.             break;
  160.  
  161.         case WM_MBUTTONUP:
  162.         case WM_RBUTTONUP:
  163.         case WM_LBUTTONUP:
  164.             fwKeys = wParam;        // key flags 
  165.             xPos = (short)LOWORD(lParam);  // horizontal position of cursor 
  166.             yPos = (short)HIWORD(lParam);  // vertical position of cursor 
  167.             yPos = (int)rect.bottom - 1 - yPos;
  168.             ReleaseCapture ();
  169.             break;
  170.  
  171.         case WM_SIZE:
  172.             InvalidateRect(skinwindow, NULL, false);
  173.             break;
  174.         case WM_NCCALCSIZE:// don't let windows copy pixels
  175.             lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  176.             return WVR_REDRAW;
  177.            case WM_CLOSE:
  178.             /* call destroy window to cleanup and go away */
  179.             DestroyWindow (hWnd);
  180.         break;
  181.  
  182.         default:
  183.             /* pass all unhandled messages to DefWindowProc */
  184.             lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  185.         break;
  186.     }
  187.  
  188.     /* return 1 if handled message, 0 if not */
  189.     return lRet;
  190. }
  191.  
  192.  
  193. /*
  194. ==============
  195. WPal_Create
  196. ==============
  197. */
  198. void WPal_Create (HINSTANCE hInstance)
  199. {
  200.     WNDCLASS   wc;
  201.  
  202.     /* Register the skin class */
  203.     memset (&wc, 0, sizeof(wc));
  204.  
  205.     wc.style         = 0;
  206.     wc.lpfnWndProc   = (WNDPROC)Palette_WndProc;
  207.     wc.cbClsExtra    = 0;
  208.     wc.cbWndExtra    = 0;
  209.     wc.hInstance     = hInstance;
  210.     wc.hIcon         = 0;
  211.     wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
  212.     wc.hbrBackground = NULL;
  213.     wc.lpszMenuName  = 0;
  214.     wc.lpszClassName = PALETTE_WINDOW_CLASS;
  215.  
  216.     if (!RegisterClass (&wc) )
  217.         Error ("RegisterClass failed");
  218.  
  219.     palettewindow = CreateWindow (PALETTE_WINDOW_CLASS ,
  220.         "Palette View",
  221.         QE3_STYLE,
  222.         (int)(screen_width*0.5),
  223.         0,
  224.         (int)(screen_width*0.5),
  225.         (int)(screen_height*.2),    // size
  226.         mainwindow,    // parent window
  227.         0,        // no menu
  228.         hInstance,
  229.         0);
  230.     if (!palettewindow)
  231.         Error ("Couldn't create palettewindow");
  232.  
  233. //    RestoreWindowState(palettewindow, "palettewindow");
  234.     ShowWindow (palettewindow, SW_SHOWDEFAULT);
  235. }
  236.