home *** CD-ROM | disk | FTP | other *** search
/ Shareware Breakthrough: Entertainment & Education / SharewearBreakthroughEnt_Ed.cdr / games / worm / worm.c < prev    next >
Text File  |  1990-08-10  |  5KB  |  239 lines

  1. /*
  2. worm.c
  3. copyright 1989, Sarmad Adnan, adnan@rice.edu
  4. copyright 1990, Sarmad Adnan, adnan@rice.edu
  5. */
  6.  
  7. #include <windows.h>
  8. #include <math.h>
  9. #include "worm.h"
  10.  
  11.  
  12.  
  13. HANDLE hInst;
  14. HDC    screen;
  15. POINT  segm[MAXSEG];
  16. short  head= -1, tail=MAXSEG;
  17. RECT   wormcage;
  18.  
  19.  
  20. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  21. {
  22. HWND hWnd;
  23. MSG msg;
  24.  
  25.  
  26. if(hPrevInstance)
  27.     {
  28.     MessageBox(NULL, "worm: your screen is already wormed", "Error", MB_OK);
  29.     return(NULL);
  30.     }
  31.  
  32. if(!WormInit(hInstance))
  33.     {
  34.     MessageBox(NULL, "Worm: class registration failed", "Error", MB_OK);
  35.     return(NULL);
  36.     }
  37.  
  38. hInst = hInstance;
  39.  
  40. hWnd = CreateWindow(
  41.         WORM_APPNAME,
  42.         WORM_TITLE,
  43.         WS_OVERLAPPEDWINDOW,
  44.         CW_USEDEFAULT,
  45.         CW_USEDEFAULT,
  46.         0,
  47.         0,
  48.         NULL,
  49.         NULL,
  50.         hInstance,
  51.         NULL
  52.         );
  53.  
  54. if(!hWnd)
  55.     {
  56.     MessageBox(NULL, "worm: could not create window", "Error", MB_OK);
  57.     return (NULL);
  58.     }
  59.  
  60. if(!GetScreenSize())
  61.     {
  62.     MessageBox(hWnd, "worm: could not obtain screen handle", "Error", MB_OK);
  63.     return(NULL);
  64.     }
  65.  
  66. if(!SetTimer(hWnd, NULL, 200, NULL) )
  67.     {
  68.     MessageBox(hWnd, "worm: no timers available", "Error", MB_OK);
  69.     return(NULL);
  70.     }
  71.  
  72. ShowWindow(hWnd, SW_MINIMIZE);
  73. UpdateWindow(hWnd);
  74.  
  75. while( GetMessage( &msg, NULL, NULL, NULL) )
  76.     {
  77.     TranslateMessage(&msg);
  78.     DispatchMessage(&msg);
  79.     }
  80. return(msg.wParam);
  81. }
  82.  
  83.  
  84. BOOL WormInit(HANDLE hInstance)
  85. {
  86. HANDLE hMemory;
  87. PWNDCLASS pWndClass;
  88. BOOL bSuccess;
  89.  
  90. hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  91. pWndClass = (PWNDCLASS) LocalLock(hMemory);
  92.  
  93. pWndClass->style = NULL;
  94. pWndClass->lpfnWndProc = WormWndProc;
  95. pWndClass->hInstance = hInstance;
  96. pWndClass->hIcon = LoadIcon(hInstance, "worm");
  97. pWndClass->hCursor = LoadCursor(NULL, IDC_CROSS);
  98. pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  99. pWndClass->lpszMenuName = (LPSTR) NULL;
  100. pWndClass->lpszClassName = (LPSTR) WORM_APPNAME;
  101. bSuccess = RegisterClass(pWndClass);
  102. LocalUnlock(hMemory);
  103. LocalFree(hMemory);
  104.  
  105. return(bSuccess);
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. long FAR PASCAL WormWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  114. {
  115. FARPROC lpProcAbout;
  116. HMENU hMenu;
  117.  
  118. switch(message) 
  119.     {
  120.     case WM_TIMER:
  121.         WormUpdate(hWnd);
  122.         break;
  123.     case WM_SIZE:
  124.         InvalidateRect(hWnd, NULL, TRUE);
  125.         break;
  126.     case WM_SYSCOMMAND:
  127.         if(wParam == ID_ABOUT)
  128.             {
  129.             lpProcAbout = MakeProcInstance(About, hInst);
  130.             DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  131.             FreeProcInstance(lpProcAbout);
  132.             break;
  133.         }
  134.         else
  135.             return(DefWindowProc(hWnd, message, wParam, lParam));
  136.     case WM_CREATE:
  137.         hMenu = GetSystemMenu(hWnd, FALSE);
  138.         ChangeMenu(hMenu, NULL, NULL, NULL, MF_APPEND | MF_SEPARATOR);
  139.         ChangeMenu(hMenu, NULL, "A&bout Worm...", ID_ABOUT, MF_APPEND | MF_STRING);
  140.         break;
  141.     case WM_QUERYOPEN:
  142.         return 0L;
  143.     case WM_DESTROY:
  144.         KillTimer(hWnd, NULL);
  145.         DeleteDC(screen);
  146.         PostQuitMessage(0);
  147.         break;
  148.     default:
  149.         return (DefWindowProc(hWnd, message, wParam, lParam));
  150.     }
  151. return(NULL);
  152. }
  153.  
  154.  
  155. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  156. {
  157. switch(message)
  158.     {
  159.     case WM_INITDIALOG:
  160.         return (TRUE);
  161.     case WM_COMMAND:
  162.         if (wParam == IDOK) 
  163.             {
  164.             EndDialog(hDlg, NULL);
  165.             return (TRUE);
  166.         }
  167.         break;
  168.     }
  169. return(FALSE);
  170. }
  171.  
  172.  
  173.  
  174. void WormUpdate(HWND hWnd)
  175. {
  176. HDC    hDc;
  177. static double dir=0.0;
  178. POINT  prev;
  179.  
  180.  
  181. if(rand()<16384)
  182.     dir+=INCREMENT;
  183. else
  184.     dir-=INCREMENT;
  185.  
  186.  
  187. prev.x=segm[head].x;
  188. prev.y=segm[head].y;
  189. head--, tail--;
  190. if(head<0) head=MAXSEG-1;
  191. if(tail<0) tail=MAXSEG-1;
  192.  
  193. Ellipse(screen, segm[head].x-SIZE, segm[head].y-SIZE, segm[head].x+SIZE, segm[head].y+SIZE);
  194. segm[head].x=prev.x+(int)(STEPSIZE*cos(dir));
  195. segm[head].y=prev.y+(int)(STEPSIZE*sin(dir));
  196. if(segm[head].x<wormcage.left)
  197.     segm[head].x=wormcage.right-1;
  198. if(segm[head].x>wormcage.right)
  199.     segm[head].x=wormcage.left;
  200. if(segm[head].y<wormcage.top)
  201.     segm[head].y=wormcage.bottom-1;
  202. if(segm[head].y>wormcage.bottom)
  203.     segm[head].y=wormcage.top;
  204.  
  205. Ellipse(screen, segm[head].x-SIZE, segm[head].y-SIZE, segm[head].x+SIZE, segm[head].y+SIZE);
  206. }
  207.  
  208.  
  209.  
  210. BOOL GetScreenSize()
  211. {
  212. POINT extent;
  213. DWORD test;
  214. int count;
  215.  
  216. screen=CreateDC("DISPLAY", NULL, NULL, NULL);
  217. if(!screen)
  218.     return(FALSE);
  219.  
  220. wormcage.right=GetDeviceCaps(screen, HORZRES);
  221. wormcage.bottom=GetDeviceCaps(screen, VERTRES);
  222.  
  223. wormcage.top=0;
  224. wormcage.left=0;
  225.  
  226. for(count=0; count<MAXSEG; count++)
  227.     {
  228.     segm[count].x=0;
  229.     segm[count].y=0;
  230.     }
  231.  
  232. SelectObject(screen, GetStockObject(BLACK_BRUSH) );
  233. SetROP2(screen, R2_NOTXORPEN);
  234. SelectObject(screen, GetStockObject(BLACK_PEN) );
  235.  
  236. return(TRUE);
  237. }
  238.  
  239.