home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / palett.zip / PALETTE.CPP next >
C/C++ Source or Header  |  1994-09-05  |  11KB  |  377 lines

  1. //=======================================================================
  2. //  PALETTE.CPP - Sample PM Sphere mapper
  3. //=======================================================================
  4.  
  5. #define INCL_BASE
  6. #define INCL_PM
  7.  
  8. #include <os2.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <math.h>
  14.  
  15. // Include identifiers used in SPRITE.RC file (resources)
  16. #include "palette.h"
  17. #include "pmgraph.h"
  18.  
  19. // Turn off parameter not used warning in Borland C++
  20. #pragma warn -par
  21. #pragma warn -rch
  22.  
  23. // Flags
  24. int FirstDraw=1;
  25.  
  26. // Color tables
  27. LONG lColorsIdx[512];
  28. int lColorsIdxCnt;
  29.  
  30. LONG lColorsRGB[256];
  31. int lColorsRGBCnt;
  32.  
  33. // Window Dimensions (setup in WM_PAINT)
  34. HPS     hps;
  35. HDC     hdc;
  36. SWP     swp;
  37. RECTL   rcl;
  38. RECTL   rclSize;
  39. RECTL   rclsave;
  40.  
  41. POINTL  currpos,oppos;
  42.  
  43. //--------------------------------------------------------------
  44. //  Global variables
  45. //--------------------------------------------------------------
  46.  
  47. HWND hwndMain=0L,                    // Handle to the main frame window
  48.      hwndClient;                     // Handle to the main client window
  49. CHAR szAppName[MAXNAMEL] = "PALETTE"; // Buffer for application name string
  50. HMQ  hmq;                            // Handle to the process' message queue
  51. HAB  hab;                            // Handle to the anchor block
  52. QMSG qmsg;                           // Queue message holder
  53. HPAL hpal;
  54.  
  55. POINTL ptlMouse;                     // Mouse pointer location
  56. POINTL ptlMouseClick;                // Location of mouse when button 1 pressed
  57.  
  58. //--------------------------------------------------------------
  59. //  Entry point declarations
  60. //--------------------------------------------------------------
  61.  
  62. MRESULT EXPENTRY _export MainWndProc(HWND, ULONG, MPARAM, MPARAM);
  63. MRESULT EXPENTRY _export CameraDialogProc(HWND, ULONG, MPARAM, MPARAM);
  64. HDC WinGetDC(HWND);
  65. VOID SetupPalette(HWND,HPS);
  66. VOID RestorePalette(HWND,HPS);
  67. VOID DoMenuCommand(HWND,USHORT);
  68. VOID PaintScreen(HWND,HPS);
  69.  
  70. // Start of program source
  71.  
  72. INT main(int argc, char *argv[])
  73. {
  74.     ULONG    flCtlData;                        /* Frame control data */
  75.     LONG     sHold;
  76.     int      i;
  77.  
  78.     // Set initial values for application
  79.  
  80.     // Set the main frame window creation flags
  81.  
  82.     flCtlData =
  83.         FCF_TITLEBAR         |   // Title bar
  84.         FCF_SIZEBORDER       |   // Size Border
  85.         FCF_MINMAX           |   // Minimize Maximize Buttons
  86.         FCF_SYSMENU          |   // System menu
  87.         FCF_ICON             |   // Specify an ICON (POINTER) resource
  88.                                  // is being provided
  89.         FCF_MENU             |   // Specify a MENU is being provided
  90. //        FCF_SHELLPOSITION    |   // System default size and position
  91.         FCF_TASKLIST ;           // Add name to task list
  92.  
  93.     // Initialize the application for PM
  94.         hab = WinInitialize(0L);
  95.  
  96.     // Create the application message queue
  97.         hmq = WinCreateMsgQueue(hab, 0L);
  98.  
  99.     // register the class for the client window.  The main thing that
  100.     // happens here is passing the pointer to the function which will
  101.     // serve as the window procedure: MainWndProc
  102.  
  103.     WinRegisterClass(
  104.         hab,                 // Anchor block handle
  105.         (PSZ)szAppName,      // Name of class being registered
  106.         (PFNWP)MainWndProc,  // Window Procedure function pointer
  107.         CS_SIZEREDRAW |      // Class style
  108.         CS_CLIPCHILDREN,     // Class style
  109.         0L);                 // Extra bytes to reserve
  110.  
  111.  
  112.     // Create a top-level frame window with a client window that
  113.     // belongs to the window class - szAppName = 'palette'
  114.     // The window is "top level" due to indicating its parent is
  115.     // the PM desktop (HWND_DESKTOP).
  116.  
  117.     hwndMain = WinCreateStdWindow(
  118.                    HWND_DESKTOP,        // Parent is desktop window
  119.                    WS_VISIBLE,          // Make window frame visible
  120.                    &flCtlData,          // Pointer to frame control style
  121.                    szAppName,           // Name of client class
  122.                    "Palette Play",       // Window Title
  123.                    WS_VISIBLE,          // Make Client Window Visible
  124.                    (HMODULE)0L,         // Resources are in executable
  125.                                         // vs. dynamically loaded
  126.                    ID_RESOURCE,         // Resource/Window identifier
  127.                    (PHWND)&hwndClient); // Pointer to client window
  128.  
  129.     // Get size of desktop window
  130.     WinQueryWindowPos(HWND_DESKTOP,(PSWP)&swp);
  131.  
  132.     // Size Window "squarish" within Desktop area
  133.  
  134.     rclSize.xLeft   = 10;
  135.     rclSize.yBottom = 10;
  136.     rclSize.yTop    = swp.cy - 100;
  137.     rclSize.xRight  = rclSize.yTop;
  138.  
  139.     WinSetWindowPos(hwndMain,HWND_TOP,rclSize.xLeft,rclSize.yBottom,
  140.                     rclSize.xRight,rclSize.yTop,
  141.                     SWP_ACTIVATE| SWP_SIZE | SWP_MOVE);
  142.  
  143.      // Get/Dispatch Message loop
  144.     while(WinGetMsg(hab,(PQMSG)&qmsg, 0L, 0L, 0L))
  145.     {
  146.         WinDispatchMsg(hab, (PQMSG)&qmsg);
  147.     }
  148.  
  149.  
  150.     return(0);
  151.  
  152. }
  153.  
  154. MRESULT EXPENTRY _export MainWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  155. {
  156.     CHAR    szMsg[80];      // Sprintf buffer
  157.     MRESULT sRC;
  158.     LONG    i;
  159.     SIZEL   sizl = {0,0};    // Use same size as device
  160.  
  161.     switch(msg)
  162.     {
  163.         case WM_CREATE:
  164.  
  165.             hdc = WinGetDC(hwnd);
  166.             hps = GpiCreatePS(hab,hdc,&sizl,PU_PELS|GPIT_MICRO|GPIA_ASSOC);
  167.             break;
  168.  
  169.         case WM_PAINT:
  170.  
  171.             if (FirstDraw)
  172.             {
  173.                 FirstDraw=0;
  174.                 SetupPalette(hwnd,hps);
  175.             }
  176.  
  177.             // Begin painting
  178.             WinBeginPaint(hwnd,hps,&rcl);
  179.  
  180.             // Get Dimensions of window to be used when window is redrawn.
  181.             WinQueryWindowRect(hwnd,&rcl);
  182.  
  183.             // Fill client background with BLACK
  184.             WinFillRect(hps,&rcl,CLR_BLACK);
  185.  
  186.             PaintScreen(hwnd,hps);
  187.  
  188.             // End paint
  189.             WinEndPaint(hps);
  190.             break;
  191.  
  192.         case WM_COMMAND:
  193.             DoMenuCommand(hwnd,(USHORT)SHORT1FROMMP(mp1));
  194.             break;
  195.  
  196.         case WM_CHAR:
  197.  
  198.             // Only process key presses - not key releasesups
  199.  
  200.             if (CHARMSG(&msg)->fs & KC_KEYUP)
  201.                 break;
  202.  
  203.             if (CHARMSG(&msg)->fs & KC_VIRTUALKEY)
  204.             {
  205.                 if (CHARMSG(&msg)->fs & KC_SHIFT)
  206.                 {
  207.                     switch (CHARMSG(&msg)->vkey)
  208.                     {
  209.                         case VK_UP:
  210.                             break;
  211.                         case VK_DOWN:
  212.                             break;
  213.                     }
  214.                 }
  215.                 else
  216.                 {
  217.                     switch (CHARMSG(&msg)->vkey)
  218.                     {
  219.                         case VK_UP:
  220.                             break;
  221.                         case VK_DOWN:
  222.                             break;
  223.                         case VK_LEFT:
  224.                             break;
  225.                         case VK_RIGHT:
  226.                             break;
  227.                     }
  228.                 }
  229.             }
  230.             break;
  231.  
  232.         case WM_BUTTON1DOWN:
  233.  
  234.             ptlMouseClick.x = (LONG) SHORT1FROMMP(mp1);
  235.             ptlMouseClick.y = (LONG) SHORT2FROMMP(mp1);
  236.  
  237.             // Pass Mouse Click for default window procedure
  238.             sRC = WinDefWindowProc(hwnd, msg, mp1, mp2);
  239.             return sRC;
  240.             break;
  241.  
  242.         case WM_BUTTON2DOWN:
  243.  
  244.             ptlMouseClick.x = (LONG) SHORT1FROMMP(mp1);
  245.             ptlMouseClick.y = (LONG) SHORT2FROMMP(mp1);
  246.  
  247.             // Pass Mouse Click for default window procedure
  248.             sRC = WinDefWindowProc(hwnd, msg, mp1, mp2);
  249.             return sRC;
  250.             break;
  251.  
  252.         case WM_CLOSE:
  253.  
  254.             RestorePalette(hwnd,hps);
  255.  
  256.         default:
  257.             // Pass unprocessed message types to default window procedure
  258.             sRC = WinDefWindowProc(hwnd, msg, mp1, mp2);
  259.             return sRC;
  260.     }
  261.     return (MRESULT)0L;
  262. }
  263.  
  264. VOID DoMenuCommand(HWND hwnd, USHORT cm)
  265. {
  266.     switch (cm)
  267.     {
  268.         case IDM_DRAW:
  269.             break;
  270.  
  271.         default:
  272.             break;
  273.     }
  274. }
  275.  
  276. VOID PaintScreen(HWND hwnd,HPS hps)
  277. {
  278.  
  279.     int i;
  280.     int clr;
  281.     POINTL ptl;
  282.  
  283.     // Draw using current color table
  284.     for (i=0;i<256;i++)
  285.     {
  286.         Line(hps,0,0+i,100,0+i,i);
  287.     }
  288.  
  289.     // Pick up pixel colors from the screen and use to draw another set
  290.     // of lines - if they look like the original lines, the
  291.     // palettes are correctly synchronized.
  292.  
  293.     for (i=0;i<256;i++)
  294.     {
  295.         ptl.x = 0;
  296.         ptl.y = i;
  297.         clr = GpiQueryPel(hps,&ptl);
  298.         Line(hps,101,0+i,201,0+i,clr);
  299.     }
  300.  
  301. }
  302.  
  303. VOID SetupPalette(HWND hwnd,HPS hps)
  304. {
  305.     int i;
  306.     unsigned char red   = 0;
  307.     unsigned char green = 0;
  308.     unsigned char blue  = 0;
  309.     ULONG cclr;
  310.  
  311.     // Create indexed and RGB form of color tables
  312.  
  313.     // Obtain the first 16 default colors
  314.     GpiQueryLogColorTable(hps,LCOLOPT_INDEX,0,16*2,lColorsIdx);
  315.  
  316.     lColorsIdxCnt = 16;
  317.  
  318.     // Fill in remaining 240 entries with a continuous blue value.
  319.     // At this point we can really fill the palette with any colors
  320.     // we choose.  This is just an example.
  321.  
  322.     for (i=16;i<256;i++)
  323.     {
  324.         lColorsIdx[i*2] = i;
  325.         lColorsIdx[(i*2)+1] = (0<<32)+(red<<16)+(green<<8)+(blue+i);
  326.         lColorsIdxCnt++;
  327.     }
  328.  
  329.     lColorsRGBCnt = lColorsIdxCnt;
  330.  
  331.     for (i=0;i<256;i++)
  332.     {
  333.         lColorsRGB[i] = lColorsIdx[(i*2)+1];
  334.     }
  335.  
  336.     // Create palette (must use RGB type color table for this)
  337.     hpal = GpiCreatePalette(hab,LCOL_OVERRIDE_DEFAULT_COLORS,LCOLF_CONSECRGB,
  338.         lColorsRGBCnt,(PULONG)lColorsRGB);
  339.  
  340.     // Select new palette into presentation space
  341.     GpiSelectPalette(hps,hpal);
  342.  
  343.     // Request current palette be applied to physical device palette
  344.     WinRealizePalette(hwnd,hps,&cclr);
  345.  
  346.     // Create logical color table using indexed color table
  347.     GpiCreateLogColorTable(hps,0L,LCOLF_INDRGB,0L,
  348.         lColorsIdxCnt*2,lColorsIdx);
  349. }
  350.  
  351. VOID RestorePalette(HWND hwnd,HPS hps)
  352. {
  353.     ULONG cclr;
  354.  
  355.     // Deselect Palette from PS
  356.     GpiSelectPalette(hps,NULLHANDLE);
  357.  
  358.     // Restore physical palette
  359.     WinRealizePalette(hwnd,hps,&cclr);
  360. }
  361.  
  362. HDC WinGetDC(HWND hwnd)
  363. {
  364.     HDC hdc;
  365.     if (!(hdc = WinQueryWindowDC(hwnd)))
  366.         hdc = WinOpenWindowDC(hwnd);
  367.  
  368.     return hdc;
  369. }
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.