home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dlib06.zip / DEMO2 / DEMO2.C next >
Text File  |  1994-07-04  |  7KB  |  280 lines

  1. #define INCL_WIN
  2. #define INCL_DOS
  3. #define INCL_GPI
  4. #include <os2.h>
  5. #include <stdio.h>
  6. #include <process.h>
  7. #include "skeleton.h"
  8. #include "dlib.h"
  9.  
  10.  
  11. #define NUM 5
  12. struct POS {
  13.    int x,y;
  14. };
  15.  
  16. struct MY_DEMO {
  17.    struct POS pos;
  18.    int spx,spy;
  19.    char xdir,ydir;
  20. };
  21.  
  22.  
  23. MRESULT EXPENTRY ClientWndProc (HWND,ULONG,MPARAM,MPARAM);
  24.  
  25. #define MSG_SPRITES     1001
  26. #define MSG_SCROLL      1002
  27. #define MSG_START       1003
  28. #define MSG_LINES       1004
  29. #define MSG_STRETCH     1005
  30.  
  31. #define GetYSize() ScreenHeight
  32.  
  33. HAB   hab;
  34. HWND  hWndFrame, hWndClient;
  35. int   ScreenHeight;
  36. CHAR  szTitle[64];
  37. int   TheKey;
  38. TID   tid;
  39.  
  40. int main()
  41. {
  42.     HMQ   hmq;
  43.     QMSG  qmsg;
  44.     ULONG flFrameFlags    = FCF_TITLEBAR | FCF_SYSMENU       | FCF_DLGBORDER |
  45.                             FCF_MINMAX   | FCF_SHELLPOSITION | FCF_TASKLIST  |
  46.                             FCF_ICON     | FCF_MENU;
  47.     CHAR  szClientClass[] = "CLIENT";
  48.  
  49.     hab = WinInitialize (0);
  50.     hmq = WinCreateMsgQueue (hab, 0);
  51.  
  52.     WinRegisterClass (hab, szClientClass, (PFNWP)ClientWndProc, CS_SIZEREDRAW|CS_MOVENOTIFY, 0);
  53.     WinLoadString (hab, 0, ID_APPNAME, sizeof(szTitle), szTitle);
  54.  
  55.     hWndFrame = WinCreateStdWindow (HWND_DESKTOP, 0,
  56.         &flFrameFlags, szClientClass, "g_blackl@kai.ee.cit.ac.nz", 0, 0, ID_APPNAME, &hWndClient);
  57.     WinSetWindowPos(hWndFrame,HWND_TOP,5,GetYSize()-295,370,290,SWP_SIZE |SWP_SHOW |SWP_ZORDER |SWP_MOVE | SWP_ACTIVATE);
  58.     dCreateVirtualScreen(700,500);
  59.  
  60.     while (WinGetMsg (hab, &qmsg, 0, 0, 0))
  61.         WinDispatchMsg (hab, &qmsg);
  62.  
  63.     WinDestroyWindow (hWndFrame);
  64.     WinDestroyMsgQueue (hmq);
  65.     WinTerminate (hab);
  66.     return (0);
  67. }
  68.  
  69.  
  70.  
  71. void iSprites(struct MY_DEMO *spr,char number)
  72. /* init sprite positions and speed */
  73. {
  74.     char i;
  75.  
  76.     for (i=0;i<number;i++) {
  77.        spr[i].pos.x=rand()%400;
  78.        spr[i].pos.y=rand()%300;
  79.        spr[i].spx=(rand()%2)+1;
  80.        spr[i].spy=(rand()%2)+1;
  81.        spr[i].xdir=1;
  82.        spr[i].ydir=1;
  83.     }
  84. }
  85.  
  86.  
  87. void uSprite(struct MY_DEMO *spr)
  88. /* Moves the sprites, rebounding off edges */
  89. {
  90.     if (spr->xdir == 1) {
  91.        spr->pos.x = spr->pos.x + spr->spx;
  92.     } else {
  93.        spr->pos.x = spr->pos.x - spr->spx;
  94.     }
  95.     if (spr->ydir == 1) {
  96.        spr->pos.y = spr->pos.y + spr->spx;
  97.     } else {
  98.        spr->pos.y = spr->pos.y - spr->spx;
  99.     }
  100.  
  101.     if (spr->pos.x > 400)
  102.        spr->xdir = 0;
  103.     if (spr->pos.x < 10)
  104.        spr->xdir = 1;
  105.     if (spr->pos.y > 340)
  106.        spr->ydir = 0;
  107.     if (spr->pos.y < 10)
  108.        spr->ydir = 1;
  109. }
  110.  
  111.  
  112.  
  113.  
  114. void AnimThread(void *arg)
  115. /* show the functions in action. Function descriptions in dlib.h
  116.    NOTE: dCreateVirtualScreen and dSetViewArea have already been called. */
  117. {
  118.     char *scr;
  119.     FILE *palette;
  120.     char rgb[256*3];
  121.     PIMAGE pic1;
  122.     int i;
  123.     struct MY_DEMO sprite[NUM];
  124.  
  125.     palette = fopen("bad1.pal","rb");
  126.     fread(rgb,3,256,palette);
  127.     fclose(palette);
  128.     dSetVirtualPalette(rgb);                /* load and setup a virtual palette */
  129.  
  130.     pic1 = dLoadImage("bad1.vga");
  131.     dCompileSprite(pic1);                   /* enable dBltSprite to be called */
  132.     WinPostMsg(hWndFrame,MSG_LINES,0,0);
  133.     for (i=0;i<256; i+=4) {
  134.        dCircle(rand()%500,rand()%350,i,i);
  135.        dLine(rand()%600,rand()%400,rand()%600,rand()%400,i);
  136.     } 
  137.     dShowView();
  138.     DosSleep(5000);
  139.  
  140.     WinPostMsg(hWndFrame,MSG_SCROLL,0,0);
  141.     for (i=0; i<450; i++) {                 /* scroll right */
  142.         dSetOrigin(i,0);
  143.         dWaitRetrace();
  144.         dShowView();
  145.     }
  146.     for (i=450; i>0; i--) {                 /* scroll left */
  147.         dSetOrigin(i,0);
  148.         dWaitRetrace();
  149.         dShowView();
  150.     }
  151.  
  152.     WinPostMsg(hWndFrame,MSG_SPRITES,0,0);
  153.     dSetOrigin(30,30);                      /* create border around view area */
  154.     for (i=4; i<150; i+=10) {
  155.         dBltStretchImage(pic1,30,30,30+i,30+i);
  156.         dShowView();
  157.     }
  158.     dEnableDoubleBuf();                     /* prepare for animation */
  159.  
  160.     iSprites(sprite,NUM);
  161.     while (1) {
  162.         for (i=0;i<NUM;i++) 
  163.           dBltSprite(sprite[i].pos.x,sprite[i].pos.y,pic1);
  164.         dShowView();
  165.         for (i=0;i<NUM;i++) 
  166.           dReplaceFromDoubleBuf(sprite[i].pos.x, sprite[i].pos.y, sprite[i].pos.x+pic1->xs, sprite[i].pos.y+pic1->ys);
  167.         for (i=0;i<NUM;i++)
  168.           uSprite(&sprite[i]);
  169.     }
  170. }
  171.  
  172.  
  173.  
  174.  
  175. void SetupViewArea(void)
  176. {
  177.     SWP     swp;
  178.     RECTL   rect;
  179.     WinQueryWindowPos( hWndFrame, (PSWP)&swp );
  180.     rect.xLeft = swp.x;
  181.     rect.yBottom = swp.y;
  182.     rect.xRight = swp.x + swp.cx;
  183.     rect.yTop = swp.y + swp.cy;
  184.     WinCalcFrameRect(hWndFrame, &rect, TRUE);
  185.     dSetViewArea(rect.xLeft,ScreenHeight-(rect.yTop),rect.xRight-rect.xLeft,rect.yTop-rect.yBottom);
  186. }
  187.  
  188.  
  189.  
  190.  
  191. MRESULT EXPENTRY ClientWndProc (HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  192. {
  193.     HPS     hps;
  194.     BOOL    bHandled = TRUE;
  195.     MRESULT mReturn  = 0;
  196.     RECTL   rect;
  197.     char    buf[50];
  198.     POINTL  pointl = {50,50};
  199.     APIRET  rc;
  200.     SWP     swp;
  201.     TID     tid;
  202.     USHORT  flag;
  203.  
  204.     switch (msg)
  205.     {
  206.          case MSG_SCROLL:
  207.             WinSetWindowText(hWndFrame,"Scrolling (moving Origin)");
  208.             break;
  209.  
  210.          case MSG_LINES:
  211.             WinSetWindowText(hWndFrame,"Lines, Circles...simple stuff");
  212.             break;
  213.  
  214.          case MSG_SPRITES:
  215.             WinSetWindowText(hWndFrame,"Bitmaps/Sprites/Palette Mapping");
  216.             break;
  217.  
  218.          case WM_CREATE:
  219.             ScreenHeight = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
  220.             WinShowWindow(hWnd, TRUE);
  221.             _beginthread(AnimThread,NULL,32768,NULL);
  222.             break;
  223.  
  224.          case WM_MOVE:
  225.             SetupViewArea();
  226.             break;
  227.  
  228.          case WM_DESTROY:
  229.             bHandled = FALSE;
  230.             break;
  231.  
  232.          case WM_FOCUSCHANGE:
  233.             flag = SHORT1FROMMP(mp2);
  234.             if (flag == TRUE) {
  235.                DosExitCritSec();
  236.             } else {
  237.                DosEnterCritSec();
  238.             } 
  239.             bHandled = FALSE;
  240.             WinInvalidateRect(hWnd,NULL,FALSE);
  241.             break;
  242.  
  243.         case WM_PAINT:
  244.             SetupViewArea();
  245.             hps = WinBeginPaint (hWnd,0,0);
  246.             WinQueryWindowRect(hWnd,&rect);
  247.             WinFillRect(hps, &rect, CLR_BLACK);
  248.             WinEndPaint (hps);
  249.             break;
  250.  
  251.         case WM_COMMAND:
  252.             switch (LOUSHORT(mp1))
  253.             {
  254.                 case IDM_ABOUT:
  255.                     DosEnterCritSec();
  256.                     WinMessageBox(HWND_DESKTOP,
  257.                         hWnd,
  258.                         "This is a simple demonstration of some dLib functions. This demo"
  259.                         " isn't as good as it could be. I wanted to keep the code short and"
  260.                         " easy to follow."
  261.                         "\n\nGraeme Blackley\ng_blackl@kai.ee.cit.ac.nz",
  262.                         "dLib information",
  263.                         0,
  264.                         MB_INFORMATION | MB_OK);
  265.                     DosExitCritSec();
  266.                     break;
  267.             }
  268.  
  269.         default:
  270.             bHandled = FALSE;
  271.             break;
  272.     }
  273.  
  274.     if (!bHandled)
  275.         mReturn = WinDefWindowProc (hWnd,msg,mp1,mp2);
  276.  
  277.     return (mReturn);
  278. }
  279.  
  280.