home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / anipal.zip / PMPAL.C < prev    next >
C/C++ Source or Header  |  1993-11-01  |  11KB  |  443 lines

  1. /* pmpal, Summer 1993, David Fortin, Plymouth, IN */
  2. #include <stdlib.h>
  3. #define INCL_GPI
  4. #define INCL_WIN
  5. #define INCL_DOS
  6. #include <os2.h>
  7. #include <mem.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <math.h>
  11.  
  12. #define MAX_VAL 255
  13. #define clientClass "DPalstuff"
  14.  
  15.  
  16. MRESULT EXPENTRY ClientWinProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2);
  17. void CyclePaletteThread(void);
  18. int Palette1(ULONG *tbl);
  19. void GeneralTunnel1(HPS hps,int xc,int yc,int size);
  20. void GeneralTunnel2(HPS hps,int xc,int yc,int size);
  21. void CosWavV(HPS hps,int sizex,int sizey,int side);
  22. void CosWavH(HPS hps,int sizex,int sizey,int side);
  23. void DoPaint(void);
  24.  
  25. static USHORT redTarg;
  26. static USHORT greenTarg;
  27. static USHORT blueTarg;
  28. static HPAL   hpal;
  29. ULONG  Tbl[MAX_VAL];
  30.  
  31. static LONG   hasPalMan;
  32. static LONG   colorsToShow;
  33. static HAB    hab;
  34. QMSG  qmsg;
  35. HMTX hmtxScreenWrite;
  36. static HPS    hps;
  37. static USHORT sizex, sizey;
  38.  
  39.  
  40. #pragma argsused
  41. int main (int argc, char *argv[])
  42. {
  43.   HMQ   hmq;
  44.   HWND  hwnd,
  45.         hwndClient;
  46.  
  47.   ULONG createFlags  =  FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER
  48.                                      | FCF_MINMAX | FCF_SHELLPOSITION
  49.                                      | FCF_TASKLIST;
  50.  
  51.   LONG sColors;
  52.   HDC hdcScr;
  53.   HPS hps;
  54.   hdcScr = GpiQueryDevice (hps = WinGetPS (HWND_DESKTOP));
  55.   DevQueryCaps (hdcScr, CAPS_COLORS, 1L, &sColors);
  56.   DevQueryCaps (hdcScr, CAPS_ADDITIONAL_GRAPHICS, 1, &hasPalMan);
  57.   hasPalMan &= CAPS_PALETTE_MANAGER;
  58.   WinReleasePS(hps);
  59.  
  60.   hab = WinInitialize (0);
  61.   hmq = WinCreateMsgQueue (hab, 0);
  62.   WinRegisterClass (hab, clientClass, (PFNWP) ClientWinProc, CS_SIZEREDRAW, 0);
  63.   if (hasPalMan)
  64.   { 
  65.     DosCreateMutexSem((PSZ)NULL,&hmtxScreenWrite,DC_SEM_SHARED,FALSE);
  66.     hwnd = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE, &createFlags,
  67.                                clientClass, "Palette Stuff - by David Fortin, Plymouth, IN", 0L,
  68.                                0UL, 0, &hwndClient);
  69.     {
  70.       MENUITEM mi;
  71.       HWND hWndSysSubMenu;
  72.       WinSendMsg(WinWindowFromID(hwnd,FID_SYSMENU),0x01f3,0L,((PVOID)MAKEULONG(LOUSHORT(&mi),(HIUSHORT(&mi)<<3)|7)));  //MM_QUERYITEMBYPOS
  73.       hWndSysSubMenu = mi.hwndSubMenu;
  74.       mi.iPosition = MIT_END;
  75.       mi.afStyle = MIS_SEPARATOR;
  76.       mi.afAttribute = 0;
  77.       mi.id = -1;
  78.       mi.hwndSubMenu = 0;
  79.       mi.hItem = 0;
  80.       WinSendMsg(hWndSysSubMenu,MM_INSERTITEM,MPFROMP(&mi),NULL);
  81.       mi.afStyle = MIS_TEXT;
  82.       mi.id = 2000;  // idm_about
  83.       WinSendMsg(hWndSysSubMenu,MM_INSERTITEM,MPFROMP(&mi),"About...");
  84.     }
  85.   }
  86.   else
  87.   {
  88.     WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,"The program cycles the palette.  You have no Palette, pff...","Palette Stuff",0,MB_OK);
  89.     return 0;
  90.   }
  91.   while (WinGetMsg (hab, &qmsg, NULLHANDLE, 0, 0))
  92.   {   
  93.     WinDispatchMsg (hab, &qmsg);
  94.   }
  95.   WinDestroyWindow (hwnd);  
  96.   WinDestroyMsgQueue (hmq); 
  97.   WinTerminate (hab);       
  98.   DosExit(1,1);
  99.   return 0;
  100. }
  101.  
  102.  
  103.  
  104. MRESULT EXPENTRY ClientWinProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  105. {
  106.   static HDC    hdc;
  107.   static BOOL   firstPaint = TRUE;
  108.   USHORT rj,gj,bj;
  109.   int    j;
  110.   char szBuffer[100];
  111.                                   
  112.   TID ThreadID;
  113.  
  114.   switch (msg)
  115.   {
  116.     case WM_CREATE:
  117.       {
  118.         SIZEL sizl = {0L, 0L};
  119.         hdc = WinOpenWindowDC (hwnd);
  120.         hps = GpiCreatePS (hab, hdc, &sizl, PU_PELS | GPIF_DEFAULT
  121.                  | GPIT_MICRO | GPIA_ASSOC);
  122.         colorsToShow = Palette1(Tbl);
  123.         hpal = GpiCreatePalette (hab, 0L, LCOLF_CONSECRGB,colorsToShow, Tbl);
  124.         GpiSelectPalette (hps, hpal);
  125.         DosCreateThread(&ThreadID,(PFNTHREAD)CyclePaletteThread,(ULONG)NULL,0,0x5000);
  126.       }
  127.       return (MRESULT) FALSE;
  128.     case WM_DESTROY:
  129.       if (hasPalMan)
  130.       {
  131.         GpiSelectPalette (hps, NULLHANDLE);
  132.         GpiDeletePalette (hpal);
  133.       }
  134.       GpiDestroyPS (hps);
  135.       return (MRESULT) FALSE;
  136.     case WM_ERASEBACKGROUND:
  137.       return (MRESULT) TRUE;
  138.     case WM_PAINT:
  139.       {
  140.         POINTL ptl;
  141.         LONG   j;
  142.  
  143.         WinBeginPaint (hwnd, hps, NULL);
  144.         if (firstPaint)
  145.         {
  146.           ULONG palSize = colorsToShow;
  147.           WinRealizePalette (hwnd, hps, &palSize);
  148.           firstPaint = FALSE;
  149.         }
  150.         WinEndPaint (hps);
  151.         DosCreateThread(&ThreadID,(PFNTHREAD)DoPaint,(ULONG)NULL,0,0x5000);
  152.       }
  153.       return (MRESULT) FALSE;
  154.     case WM_REALIZEPALETTE:
  155.       {
  156.         ULONG palSize = colorsToShow;
  157.         if (WinRealizePalette (hwnd, hps, &palSize))
  158.         {
  159.           WinInvalidateRect (hwnd, NULL, FALSE);
  160.         }
  161.         return (MRESULT) FALSE;
  162.       }
  163.     case WM_SIZE:
  164.       sizex = SHORT1FROMMP (mp2);
  165.       sizey = SHORT2FROMMP (mp2);
  166.       return (MRESULT) FALSE;
  167.      case WM_SYSCOMMAND:
  168.      case WM_COMMAND:
  169.        if(LOUSHORT(mp1)==2000)
  170.          WinMessageBox(hwnd,hwnd,"Palette Stuff, by David Fortin, Plymouth, IN","Summer 1993",0,MB_OK);
  171.     default:
  172.       return (WinDefWindowProc (hwnd, msg, mp1, mp2));
  173.   }
  174. }
  175.  
  176.  
  177.  
  178.  
  179. void DoPaint(void)
  180. {
  181.   int cx,cy,size;
  182.  
  183.   DosRequestMutexSem(hmtxScreenWrite,SEM_INDEFINITE_WAIT);
  184.   cx = sizex/2;
  185.   cy = sizey/2;
  186.   size = min(sizex,sizey);
  187.   GeneralTunnel1(hps,cx,cy,max(sizex,sizey));
  188.   GeneralTunnel1(hps,sizex/5,sizey/5,size/5);
  189.   GeneralTunnel2(hps,sizex-sizex/5,sizey/5,size/5);
  190.   GeneralTunnel1(hps,sizex-sizex/5,sizey-sizey/5,size/5);
  191.   GeneralTunnel2(hps,sizex/5,sizey-sizey/5,size/5);
  192.   CosWavV(hps,sizex,sizey,0);
  193.   CosWavH(hps,sizex,sizey,0);
  194.   DosReleaseMutexSem(hmtxScreenWrite);
  195.   return;
  196. }
  197.  
  198.  
  199.  
  200.  
  201.  
  202. int Palette1(ULONG *tbl)
  203. {
  204.   USHORT i;
  205.   USHORT rj,gj,bj;
  206.  
  207.   int j= 0;
  208.   for(i=10;i<31;i++)
  209.   {
  210.     rj = 6*((BYTE)30+10-i);
  211.     gj = 6*((BYTE)30+10-i);
  212.     bj = 6*((BYTE)30);
  213.     tbl[j]= PC_RESERVED*16777216 + 65536 * rj + 256 * gj + bj;
  214.     j++;
  215.   }
  216.   for(i=10;i<31;i++)
  217.   {
  218.     rj = 6*((BYTE)i);
  219.     gj = 6*((BYTE)10);
  220.     bj = 6*((BYTE)30);
  221.     tbl[j] = PC_RESERVED*16777216 + 65536 * rj + 256 * gj + bj;
  222.     j++;
  223.   }
  224.   for(i=10;i<31;i++)
  225.   {
  226.     rj = 6*((BYTE)30);
  227.     gj = 6*((BYTE)10);
  228.     bj = 6*((BYTE)30+10-i);
  229.     tbl[j] = PC_RESERVED*16777216 + 65536 * rj + 256 * gj + bj;
  230.     j++;
  231.   }
  232.   for(i=10;i<31;i++)
  233.   {
  234.     rj = 6*((BYTE)30);
  235.     gj = 6*((BYTE)i);
  236.     bj = 6*((BYTE)10);
  237.     tbl[j] = PC_RESERVED*16777216 + 65536 * rj + 256 * gj + bj;
  238.     j++;
  239.   }
  240.   for(i=10;i<31;i++)
  241.   {
  242.     rj = 6*((BYTE)30+10-i);
  243.     gj = 6*((BYTE)30);
  244.     bj = 6*((BYTE)10);
  245.     tbl[j] = PC_RESERVED*16777216 + 65536 * rj + 256 * gj + bj;
  246.     j++;
  247.   }
  248.   for(i=10;i<31;i++)
  249.   {
  250.     rj = 6*((BYTE)10);
  251.     gj = 6*((BYTE)30);
  252.     bj = 6*((BYTE)i);
  253.     tbl[j] = PC_RESERVED*16777216 + 65536 * rj + 256 * gj + bj;
  254.     j++;
  255.   }
  256.   for(i=10;i<31;i++)
  257.   {
  258.     rj = 6*((BYTE)i);
  259.     gj = 6*((BYTE)30);
  260.     bj = 6*((BYTE)30);
  261.     tbl[j] = PC_RESERVED*16777216 + 65536 * rj + 256 * gj + bj;
  262.     j++;
  263.   }
  264.   return j;
  265. }
  266.  
  267. void GeneralTunnel1(HPS hps,int xc,int yc,int size)
  268. {
  269.   int i;
  270.   int x;
  271.   POINTL ptl;
  272.   for(i=0;i<size;i++)
  273.   {
  274.     GpiSetColor(hps,colorsToShow - i % colorsToShow);
  275.     ptl.y = yc-i;
  276.     ptl.x = xc-i;
  277.     GpiMove (hps, &ptl);
  278.     ptl.x = xc-i;
  279.     ptl.y = yc+i;
  280.     GpiLine(hps,&ptl);
  281.     ptl.x = xc+i;
  282.     ptl.y = yc+i;
  283.     GpiLine(hps,&ptl);
  284.     ptl.x = xc+i;
  285.     ptl.y = yc-i;
  286.     GpiLine(hps,&ptl);
  287.     ptl.x = xc-i;
  288.     ptl.y = yc-i;
  289.     GpiLine(hps,&ptl);
  290.   }
  291.   return;
  292. }
  293.  
  294.  
  295. void GeneralTunnel2(HPS hps,int xc,int yc,int size)
  296. {
  297.   int i;
  298.   int x;
  299.   POINTL ptl;
  300.   for(i=0;i<size;i++)
  301.   {
  302.     GpiSetColor(hps,i % colorsToShow);
  303.     ptl.y = yc-i;
  304.     ptl.x = xc-i;
  305.     GpiMove (hps, &ptl);
  306.     ptl.x = xc-i;
  307.     ptl.y = yc+i;
  308.     GpiLine(hps,&ptl);
  309.     ptl.x = xc+i;
  310.     ptl.y = yc+i;
  311.     GpiLine(hps,&ptl);
  312.     ptl.x = xc+i;
  313.     ptl.y = yc-i;
  314.     GpiLine(hps,&ptl);
  315.     ptl.x = xc-i;
  316.     ptl.y = yc-i;
  317.     GpiLine(hps,&ptl);
  318.   }
  319.   return;
  320. }
  321.  
  322.  
  323.  
  324. void CosWavV(HPS hps,int sizex,int sizey,int side)
  325. {
  326.   int i;
  327.   RECTL rect;
  328.   POINTL ptl;
  329.   double c,s,amp;
  330.   int rb;
  331.   int rl,rr,screeny;
  332.  
  333.   rb = sizey/30;
  334.   rl = sizex/2-sizex/30;
  335.   rr = sizex/2+sizex/30;
  336.   amp = sizex/5;
  337.   
  338.  
  339.   for(i=-sizey/10;i<sizey;i+=1)
  340.   {
  341.     c=cos((double)i*0.0349*(180.0/(double)sizey))*amp;
  342.     s=sin((double)i*0.0349*(180.0/(double)sizey))*amp;
  343.     rect.xLeft = rl+c;
  344.     rect.xRight = rr+s;
  345.     rect.yTop = i;
  346.     rect.yBottom = rb + i;
  347.     GpiSetColor(hps,((i+sizex/10))%colorsToShow);
  348.     if(side==1 || side==0)
  349.     {
  350.       ptl.x= rect.xLeft;
  351.       ptl.y = rect.yTop;
  352.       GpiMove(hps,&ptl);
  353.       ptl.x= rect.xRight;
  354.       ptl.y = rect.yBottom;
  355.       GpiBox (hps, DRO_FILL, &ptl, 0L, 0L);
  356.     }
  357.     rect.yBottom = sizey-i;
  358.     rect.yTop = sizey -(rb + i);
  359.     rect.xLeft = rl-c;
  360.     rect.xRight = rr-s;
  361.     if(side==0 || side == 2)
  362.     {
  363.       ptl.x= rect.xLeft;
  364.       ptl.y = rect.yTop;
  365.       GpiMove(hps,&ptl);
  366.       ptl.x= rect.xRight;
  367.       ptl.y = rect.yBottom;
  368.       GpiBox (hps, DRO_FILL, &ptl, 0L, 0L);
  369.     }
  370.   }
  371.   return;
  372. }
  373.  
  374.  
  375. void CosWavH(HPS hps,int sizex,int sizey,int side)
  376. {
  377.   int i;
  378.   RECTL rect;
  379.   POINTL ptl;
  380.   double c,s,amp;
  381.   int rb;
  382.   int rl,rr,screeny;
  383.  
  384.   rb = sizex/10;
  385.   rl = sizey/2-sizey/20;
  386.   rr = sizey/2+sizey/20;
  387.   amp = sizey/3;
  388.  
  389.   
  390.   for(i=-sizex/10;i<sizex;i+=1)
  391.   {
  392.     c=cos((double)i*0.0349*(360.0/(double)sizex))*amp;
  393.     rect.yTop = rl+c;
  394.     rect.yBottom = rr+c;
  395.     rect.xLeft = i;
  396.     rect.xRight = rb + i;
  397.     GpiSetColor(hps,(i/3+sizex/10)%colorsToShow);
  398.     if(side==1 || side==0)
  399.     {
  400.       ptl.x= rect.xLeft;
  401.       ptl.y = rect.yTop;
  402.       GpiMove(hps,&ptl);
  403.       ptl.x= rect.xRight;
  404.       ptl.y = rect.yBottom;
  405.       GpiBox (hps, DRO_FILL, &ptl, 0L, 0L);
  406.     }
  407.     rect.xRight = sizex-i;
  408.     rect.xLeft = sizex -(rb + i);
  409.     rect.yTop = rl-c;
  410.     rect.yBottom = rr-c;
  411.     GpiSetColor(hps,(i/2+sizex/10)%colorsToShow);
  412.     if(side==0 || side == 2)
  413.     {
  414.       ptl.x= rect.xLeft;
  415.       ptl.y = rect.yTop;
  416.       GpiMove(hps,&ptl);
  417.       ptl.x= rect.xRight;
  418.       ptl.y = rect.yBottom;
  419.       GpiBox (hps, DRO_FILL, &ptl, 0L, 0L);
  420.     }
  421.   }
  422.   return;
  423. }
  424.  
  425.  
  426. void CyclePaletteThread(void)
  427. {
  428.     ULONG hold[20];
  429.     INT    j;
  430.     int blockSize = 5;
  431.     while(1)
  432.     {
  433.       DosRequestMutexSem(hmtxScreenWrite,SEM_INDEFINITE_WAIT);
  434.       memcpy(hold,&Tbl[0],sizeof(Tbl[0])*(blockSize));
  435.       memcpy(Tbl,&Tbl[blockSize],sizeof(Tbl[0])*(colorsToShow-blockSize));
  436.       memcpy(&Tbl[colorsToShow-blockSize],hold,sizeof(Tbl[0])*(blockSize));
  437.       GpiAnimatePalette(hpal,LCOLF_CONSECRGB,0,colorsToShow,Tbl);
  438.       DosReleaseMutexSem(hmtxScreenWrite);
  439.       DosSleep(20);
  440.     }
  441.  }
  442.  
  443.