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

  1. /*
  2. copyright 1989, Sarmad Adnan, adnan@rice.edu
  3. */
  4.  
  5. #include <windows.h>
  6. #include <math.h>
  7. #include "wineyes.h"
  8.  
  9.  
  10.  
  11. HANDLE hInst;
  12. BOOL   FirstTimeToggle = TRUE;
  13. POINT  mouseloc;
  14. POINT  iamat;
  15. POINT  eyecenter1, eyecenter2;
  16. POINT  eyesize;
  17. POINT  eyeballsize;
  18. RECT   prevloc1={0,0,0,0};
  19. RECT   prevloc2={0,0,0,0};
  20.  
  21. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  22. {
  23. HWND hWnd;
  24. MSG msg;
  25.  
  26.  
  27. if(hPrevInstance)
  28.     {
  29.     MessageBox(NULL, "I only have two eyes", "Error", MB_OK);
  30.     return(NULL);
  31.     }
  32.  
  33. if(!WinEyesInit(hInstance))
  34.     {
  35.     MessageBox(NULL, "Class registration failed", "Error", MB_OK);
  36.     return(NULL);
  37.     }
  38.  
  39. hInst = hInstance;
  40.  
  41. hWnd = CreateWindow(
  42.         WINEYES_APPNAME,
  43.         WINEYES_TITLE,
  44.         WS_OVERLAPPEDWINDOW,
  45.         CW_USEDEFAULT,
  46.         CW_USEDEFAULT,
  47.         161,
  48.         195,
  49.         NULL,
  50.         NULL,
  51.         hInstance,
  52.         NULL
  53.         );
  54.  
  55. if(!hWnd)
  56.     {
  57.     MessageBox(NULL, "Could not create window", "Error", MB_OK);
  58.     return (NULL);
  59.     }
  60.  
  61.  
  62. if(!SetTimer(hWnd, NULL, 300, NULL) )
  63.     {
  64.     MessageBox(hWnd, "No Timers Available", "Error", MB_OK);
  65.     return(NULL);
  66.     }
  67.  
  68. ShowWindow(hWnd, SW_MINIMIZE);
  69. UpdateWindow(hWnd);
  70.  
  71. while( GetMessage( &msg, NULL, NULL, NULL) )
  72.     {
  73.     TranslateMessage(&msg);
  74.     DispatchMessage(&msg);
  75.     }
  76. return(msg.wParam);
  77. }
  78.  
  79.  
  80. BOOL WinEyesInit(HANDLE hInstance)
  81. {
  82. HANDLE hMemory;
  83. PWNDCLASS pWndClass;
  84. BOOL bSuccess;
  85.  
  86. hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  87. pWndClass = (PWNDCLASS) LocalLock(hMemory);
  88.  
  89. pWndClass->style = NULL;
  90. pWndClass->lpfnWndProc = WinEyesWndProc;
  91. pWndClass->hInstance = hInstance;
  92. pWndClass->hIcon = NULL;
  93. pWndClass->hCursor = LoadCursor(NULL, IDC_CROSS);
  94. pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  95. pWndClass->lpszMenuName = (LPSTR) NULL;
  96. pWndClass->lpszClassName = (LPSTR) WINEYES_APPNAME;
  97. bSuccess = RegisterClass(pWndClass);
  98. LocalUnlock(hMemory);
  99. LocalFree(hMemory);
  100.  
  101. return(bSuccess);
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. long FAR PASCAL WinEyesWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  110. {
  111. FARPROC lpProcAbout;
  112. HMENU hMenu;
  113.  
  114. switch(message) 
  115.     {
  116.     case WM_PAINT:
  117.         WinEyesPaint(hWnd);
  118.         break;
  119.     case WM_TIMER:
  120.         WinEyesUpdate(hWnd);
  121.         break;
  122.     case WM_SIZE:
  123.         InvalidateRect(hWnd, NULL, TRUE);
  124.         FirstTimeToggle=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 WinEyes...", ID_ABOUT, MF_APPEND | MF_STRING);
  140.         break;
  141.     case WM_DESTROY:
  142.         KillTimer(hWnd, NULL);
  143.         PostQuitMessage(0);
  144.         break;
  145.     default:
  146.         return (DefWindowProc(hWnd, message, wParam, lParam));
  147.     }
  148. return(NULL);
  149. }
  150.  
  151.  
  152. BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  153. {
  154. switch(message)
  155.     {
  156.     case WM_INITDIALOG:
  157.         return (TRUE);
  158.     case WM_COMMAND:
  159.         if (wParam == IDOK) 
  160.             {
  161.             EndDialog(hDlg, NULL);
  162.             return (TRUE);
  163.         }
  164.         break;
  165.     }
  166. return(FALSE);
  167. }
  168.  
  169.  
  170. void WinEyesPaint(HWND hWnd)
  171. {
  172. PAINTSTRUCT ps;
  173. POINT size, round;
  174. RECT  forEllipse1, forEllipse2, rect;
  175.  
  176. BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  177. SelectObject(ps.hdc, GetStockObject(BLACK_BRUSH) );
  178. GetClientRect( hWnd, &rect );
  179. size.x=rect.right-rect.left;
  180. size.y=rect.bottom-rect.top;
  181. round.x=size.x/10;
  182. round.y=size.y/10;
  183. RoundRect(ps.hdc, rect.left, rect.top, rect.right, rect.bottom, round.x, round.y);
  184.  
  185. forEllipse1.left=rect.left+1, forEllipse1.right=(size.x)/2-1;
  186. forEllipse1.top=rect.top+1, forEllipse1.bottom=(size.y)*2/3;
  187. forEllipse2.right=rect.right-1, forEllipse2.left=(size.x)/2+1;
  188. forEllipse2.top=rect.top+1, forEllipse2.bottom=(size.y)*2/3;
  189.  
  190. SelectObject(ps.hdc, GetStockObject(WHITE_BRUSH) );
  191. SelectObject(ps.hdc, GetStockObject(WHITE_PEN) );
  192. Ellipse(ps.hdc, forEllipse1.left, forEllipse1.top, forEllipse1.right, forEllipse1.bottom);
  193. Ellipse(ps.hdc, forEllipse2.left, forEllipse2.top, forEllipse2.right, forEllipse2.bottom);
  194. EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  195.  
  196. eyecenter1.x=(forEllipse1.right+forEllipse1.left)/2+1;
  197. eyecenter1.y=(forEllipse1.top+forEllipse1.bottom)/2+1;
  198.  
  199. eyecenter2.x=(forEllipse2.right+forEllipse2.left)/2+1;
  200. eyecenter2.y=eyecenter1.y;
  201.  
  202. eyesize.x=size.x/10;
  203. eyesize.y=size.y/6;
  204. eyeballsize.x=size.x/8;
  205. eyeballsize.y=size.y/6;
  206. }
  207.  
  208.  
  209. void WinEyesUpdate(HWND hWnd)
  210. {
  211. PAINTSTRUCT ps;
  212. RECT  rect, forEllipse1, forEllipse2;
  213. POINT current1, current2;
  214. HDC   hDc;
  215. DWORD orgis;
  216. double angle1, angle2;
  217.  
  218. GetCursorPos((LPPOINT)¤t1);
  219. if( (mouseloc.x==current1.x) && (mouseloc.y==current1.y) )
  220.     return;
  221.  
  222. mouseloc.x=current1.x, mouseloc.y=current1.y;
  223.  
  224. hDc=GetDC(hWnd);
  225.  
  226. orgis=GetDCOrg(hDc);
  227. iamat.x=LOWORD(orgis);
  228. iamat.y=HIWORD(orgis);
  229.  
  230. current1.x=mouseloc.x-iamat.x-eyecenter1.x, current1.y=mouseloc.y-iamat.y-eyecenter1.y;
  231. current2.x=mouseloc.x-iamat.x-eyecenter2.x, current2.y=mouseloc.y-iamat.y-eyecenter2.y;
  232.  
  233. angle1=atan2((double)current1.x, (double)current1.y);
  234. angle1= 1.571-angle1;
  235. angle2=atan2((double)current2.x, (double)current2.y);
  236. angle2= 1.571-angle2;
  237. current1.x=(int)( cos(angle1)*eyesize.x+eyecenter1.x), current1.y=(int)(sin(angle1)*eyesize.y+eyecenter1.y);
  238. current2.x=(int)( cos(angle2)*eyesize.x+eyecenter2.x), current2.y=(int)(sin(angle2)*eyesize.y+eyecenter2.y);
  239.  
  240. forEllipse1.left=current1.x-eyeballsize.x, forEllipse1.top=current1.y-eyeballsize.y;
  241. forEllipse1.right=current1.x+eyeballsize.x, forEllipse1.bottom=current1.y+eyeballsize.y;
  242.  
  243. forEllipse2.left=current2.x-eyeballsize.x, forEllipse2.top=current2.y-eyeballsize.y;
  244. forEllipse2.right=current2.x+eyeballsize.x, forEllipse2.bottom=current2.y+eyeballsize.y;
  245.  
  246. if(!FirstTimeToggle)
  247.     {
  248.     SelectObject(hDc, GetStockObject(WHITE_PEN) );
  249.     SelectObject(hDc, GetStockObject(WHITE_BRUSH) );
  250.     Ellipse(hDc, prevloc1.left, prevloc1.top, prevloc1.right, prevloc1.bottom);
  251.     Ellipse(hDc, prevloc2.left, prevloc2.top, prevloc2.right, prevloc2.bottom);
  252.     }
  253. else
  254.     {
  255.     FirstTimeToggle=FALSE;
  256.     }
  257.  
  258. SelectObject(hDc, GetStockObject(BLACK_BRUSH) );
  259. SelectObject(hDc, GetStockObject(BLACK_PEN) );
  260. Ellipse(hDc, forEllipse1.left, forEllipse1.top, forEllipse1.right, forEllipse1.bottom);
  261. Ellipse(hDc, forEllipse2.left, forEllipse2.top, forEllipse2.right, forEllipse2.bottom);
  262.  
  263. prevloc1.left=forEllipse1.left, prevloc1.top=forEllipse1.top;
  264. prevloc1.right=forEllipse1.right, prevloc1.bottom=forEllipse1.bottom;
  265. prevloc2.left=forEllipse2.left, prevloc2.top=forEllipse2.top;
  266. prevloc2.right=forEllipse2.right, prevloc2.bottom=forEllipse2.bottom;
  267.  
  268. ReleaseDC(hWnd, hDc);
  269. }
  270.