|
|
1.1 root 1: /*---------------------------------------------------------------------------*\
2: | POLYBEZIER MODULE
3: | This module contains the routines for demo
4: |
5: | segment: _TEXT (DOS16)
6: | created: 19-Oct-90
7: | history: 19-Oct-90 <paulb> created original polybezier effect.
8: | 20-May-91 <chriswil> adapted for use in MDI application.
9: |
10: \*---------------------------------------------------------------------------*/
11:
12: #include <windows.h>
13: #include "gdidemo.h"
14: #include "poly.h"
15:
16: /*---------------------------------------------------------------------------*\
17: | CREATE BEZIER WINDOW PROCEDURE
18: | Create the bezier MDI-child window.
19: |
20: | created: 20-May-91
21: | history: 20-May-91 <chriswil> created.
22: |
23: \*---------------------------------------------------------------------------*/
24: HWND FAR CreatePolyWindow(HWND hWndClient, int nItem)
25: {
26: HANDLE hInstance;
27: MDICREATESTRUCT mcs;
28:
29:
30: hInstance = GETINSTANCE(hWndClient);
31:
32: /*
33: ** Initialize the MDI create struct for creation of the
34: ** test window.
35: */
36: mcs.szClass = POLYCLASS;
37: mcs.szTitle = POLYTITLE;
38: mcs.hOwner = hInstance;
39: mcs.x = CW_USEDEFAULT;
40: mcs.y = CW_USEDEFAULT;
41: mcs.cx = CW_USEDEFAULT;
42: mcs.cy = CW_USEDEFAULT;
43: mcs.style = 0l;
44: mcs.lParam = (LONG)nItem;
45:
46: return((HWND)SendMessage(hWndClient,WM_MDICREATE,NULL,(LONG)(LPMDICREATESTRUCT)&mcs));
47: }
48:
49:
50: /*---------------------------------------------------------------------------*\
51: | POLYBEZIER WINDOW PROCEDURE
52: | This is the main window function for the polybezier demo window.
53: |
54: | created: 20-May-91
55: | history: 20-May-91 <chriswil> created.
56: |
57: \*---------------------------------------------------------------------------*/
58: LONG APIENTRY PolyProc(HWND hWnd, UINT wMsg, WPARAM wParam, LONG lParam)
59: {
60: switch(wMsg)
61: {
62: case WM_CREATE:
63: PolyCreateProc(hWnd);
64: break;
65:
66:
67: case WM_COMMAND:
68: PolyCommandProc(hWnd,wParam,lParam);
69: break;
70:
71: case WM_MOVE:
72: PolyRedraw(hWnd);
73: break;
74:
75:
76: case WM_TIMER:
77: PolyDrawBez(hWnd);
78: break;
79:
80:
81: case WM_PAINT:
82: PolyPaintProc(hWnd);
83: break;
84:
85:
86: case WM_DESTROY:
87: PolyDestroyProc(hWnd);
88: break;
89:
90:
91: default:
92: return(DefMDIChildProc(hWnd,wMsg,wParam,lParam));
93: }
94: return(0l);
95: }
96:
97:
98: /*---------------------------------------------------------------------------*\
99: | POLYBEZIER CREATE PROCEDURE
100: | Create the polybezier window for the demo application. This is a child
101: | of the MDI client window. Allocate the extra object information for
102: | handling of the polybezier demo.
103: |
104: | created: 20-May-91
105: | history: 20-May-91 <chriswil> created.
106: |
107: \*---------------------------------------------------------------------------*/
108: BOOL PolyCreateProc(HWND hWnd)
109: {
110: PPOLYDATA ppd;
111:
112:
113: if(AllocWindowInfo(hWnd,sizeof(POLYDATA)))
114: {
115: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd))
116: {
117: ppd->nBezTotal = 20;
118: ppd->nBezCurr = 0;
119: ppd->nColor = 0;
120: ppd->hBezBuffer = GlobalAlloc(GHND,(DWORD)(sizeof(BEZBUFFER) * MAX_BEZIER));
121:
122: UnlockWindowInfo(hWnd);
123:
124: PolyInitPoints(hWnd);
125:
126: SetTimer(hWnd,1,50,NULL);
127: return(TRUE);
128: }
129: FreeWindowInfo(hWnd);
130: }
131: return(FALSE);
132: }
133:
134:
135: /*---------------------------------------------------------------------------*\
136: | POLYBEZIER COMMAND PROCEDURE
137: | Process polybezier commands. This is a NOP for now. But who knows what
138: | tomorrow may bring.
139: |
140: | created: 20-May-91
141: | history: 20-May-91 <chriswil> created.
142: |
143: \*---------------------------------------------------------------------------*/
144: BOOL PolyCommandProc(HWND hWnd, WPARAM wParam, LONG lParam)
145: {
146: hWnd = hWnd;
147: wParam = wParam;
148: lParam = lParam;
149:
150: return(TRUE);
151: }
152:
153:
154: /*---------------------------------------------------------------------------*\
155: | POLYBEZIER PAINT PROCEDURE
156: | Repaint the bezier window. All we really do here is validate our window,
157: | and reset the array of bezier objects.
158: |
159: | created: 20-May-91
160: | history: 20-May-91 <chriswil> created.
161: |
162: \*---------------------------------------------------------------------------*/
163: VOID PolyPaintProc(HWND hWnd)
164: {
165: HDC hDC;
166: PAINTSTRUCT ps;
167:
168:
169: if(hDC = BeginPaint(hWnd,&ps))
170: EndPaint(hWnd,&ps);
171:
172: PolyRedraw(hWnd);
173:
174: return;
175: }
176:
177:
178: /*---------------------------------------------------------------------------*\
179: | POLYBEZIER DESTROY PROCEDURE
180: | Kill the polybezier demo. Free up the resources allocated on behalf of
181: | this object.
182: |
183: | created: 20-May-91
184: | history: 20-May-91 <chriswil> created.
185: |
186: \*---------------------------------------------------------------------------*/
187: VOID PolyDestroyProc(HWND hWnd)
188: {
189: PPOLYDATA ppd;
190:
191:
192: KillTimer(hWnd,1);
193: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd))
194: {
195: GlobalFree(ppd->hBezBuffer);
196: UnlockWindowInfo(hWnd);
197: }
198: FreeWindowInfo(hWnd);
199:
200: return;
201: }
202:
203:
204: /*---------------------------------------------------------------------------*\
205: | GET NEW VELOCITY
206: | This routine creates a new velocity for the bezier points. Each bezier
207: | point is randomly chosen. The two inside points should have a speed
208: | less then the endpoints (most of the time-better effect).
209: |
210: | created: 20-May-91
211: | history: 20-May-91 <chriswil> created.
212: |
213: \*---------------------------------------------------------------------------*/
214: int PolyNewVel(int i)
215: {
216: int nRet;
217:
218:
219: if ((i == 1) || (i == 2))
220: nRet = (int)((lRandom() % VELMAX) / 3) + VELMIN;
221: else
222: nRet = (int)(lRandom() % VELMAX) + VELMIN;
223:
224: return((nRet < 0) ? -nRet : nRet);
225: }
226:
227:
228: /*---------------------------------------------------------------------------*\
229: | INITIALIZE POLYBEZIER POINTS
230: | This routine initializes the polybezier points for the first object. This
231: | is performed on startup of the window.
232: |
233: | created: 20-May-91
234: | history: 20-May-91 <chriswil> created.
235: |
236: \*---------------------------------------------------------------------------*/
237: VOID PolyInitPoints(HWND hWnd)
238: {
239: PPOLYDATA ppd;
240: LPBEZBUFFER lpBez;
241: int idx;
242: RECT rect;
243:
244:
245: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd))
246: {
247: if(lpBez = (LPBEZBUFFER)GlobalLock(ppd->hBezBuffer))
248: {
249: GetClientRect(hWnd,&rect);
250:
251: for(idx=0; idx < 4; idx++)
252: {
253: lpBez->pPts[idx].x = lRandom() % rect.right;
254: lpBez->pPts[idx].y = lRandom() % rect.bottom;
255:
256: ppd->pVel[idx].x = PolyNewVel(idx);
257: ppd->pVel[idx].y = PolyNewVel(idx);
258: }
259: GlobalUnlock(ppd->hBezBuffer);
260: }
261: UnlockWindowInfo(hWnd);
262: }
263: return;
264: }
265:
266:
267: /*---------------------------------------------------------------------------*\
268: | POLYBEZIER REDRAW
269: | This routine resets the bezier curves and redraws the poly-bezier client
270: | area.
271: |
272: | created: 20-May-91
273: | history: 20-May-91 <chriswil> created.
274: |
275: \*---------------------------------------------------------------------------*/
276: VOID PolyRedraw(HWND hWnd)
277: {
278: PPOLYDATA ppd;
279: LPBEZBUFFER lpBez,lpCurr;
280: HDC hDC;
281: int i,j;
282: RECT rect;
283:
284:
285: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd))
286: {
287: if(lpBez = (LPBEZBUFFER)GlobalLock(ppd->hBezBuffer))
288: {
289: if(hDC = GetDC(hWnd))
290: {
291: /*
292: ** Save the current bezier. Set the first bezier in the
293: ** array to that curve, and use it as a basis for the next
294: ** series.
295: */
296: lpCurr = lpBez+ppd->nBezCurr;
297: *lpBez = *lpCurr;
298: ppd->nBezCurr = 0;
299:
300:
301: /*
302: ** Clean the curves (all but the first curve).
303: */
304: for(j=1; j < ppd->nBezTotal; j++)
305: {
306: for(i=0; i < 4; i++)
307: {
308: (lpBez+j)->pPts[i].x = -1;
309: (lpBez+j)->pPts[i].y = 0;
310: }
311: }
312:
313:
314:
315: /*
316: ** Clear the display.
317: */
318: GetClientRect(hWnd,&rect);
319: BitBlt(hDC,0,0,rect.right, rect.bottom,(HDC)0,0,0,0);
320:
321:
322: /*
323: ** Draw the first curve in the bezier array.
324: */
325: #ifdef WIN32
326: PolyBezier(hDC,lpBez->pPts,4);
327: #else
328: Polyline(hDC,lpBez->pPts,4);
329: #endif
330: ReleaseDC(hWnd,hDC);
331: }
332: GlobalUnlock(ppd->hBezBuffer);
333: }
334: UnlockWindowInfo(hWnd);
335: }
336: return;
337: }
338:
339:
340:
341:
342: VOID PolyDrawBez(HWND hWnd)
343: {
344: PPOLYDATA ppd;
345: LPBEZBUFFER lpBez,lpCurr,lpPrev;
346: int idx,x,y;
347: RECT rect;
348: HDC hDC;
349: HPEN hPen;
350:
351: static COLORREF crColor[] = {0x000000FF,0x0000FF00,0x00FF0000,0x0000FFFF,
352: 0x00FF00FF,0x00FFFF00,0x00FFFFFF,0x00000080,
353: 0x00008000,0x00800000,0x00008080,0x00800080,
354: 0x00808000,0x00808080,0x000000FF,0x0000FF00,
355: 0x00FF0000,0x0000FFFF,0x00FF00FF,0x00FFFF00};
356:
357:
358: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd))
359: {
360: if(lpBez = (LPBEZBUFFER)GlobalLock(ppd->hBezBuffer))
361: {
362: if(hDC = GetDC(hWnd))
363: {
364: GetClientRect(hWnd,&rect);
365:
366: lpPrev = lpBez+ppd->nBezCurr;
367:
368: ppd->nBezCurr += 1;
369:
370: if(ppd->nBezCurr >= ppd->nBezTotal)
371: {
372: ppd->nBezCurr = 0;
373: ppd->nColor = (++ppd->nColor % 20);
374: }
375: lpCurr = lpBez+ppd->nBezCurr;
376:
377:
378: if(lpCurr->pPts[0].x != -1)
379: {
380: hPen = SelectObject(hDC,GetStockObject(BLACK_PEN));
381: #ifdef WIN32
382: PolyBezier(hDC,lpCurr->pPts,4);
383: #else
384: Polyline(hDC,lpCurr->pPts,4);
385: #endif
386: SelectObject(hDC,hPen);
387: }
388:
389: for(idx=0; idx < 4; idx++)
390: {
391: x = lpPrev->pPts[idx].x;
392: y = lpPrev->pPts[idx].y;
393: x += ppd->pVel[idx].x;
394: y += ppd->pVel[idx].y;
395:
396: if(x >= rect.right)
397: {
398: x = rect.right - ((x - rect.right)+1);
399: ppd->pVel[idx].x = -PolyNewVel(idx);
400: }
401:
402: if(x <= rect.left)
403: {
404: x = rect.left + ((rect.left - x)+1);
405: ppd->pVel[idx].x = PolyNewVel(idx);
406: }
407:
408: if(y >= rect.bottom)
409: {
410: y = rect.bottom - ((y - rect.bottom)+1);
411: ppd->pVel[idx].y = -PolyNewVel(idx);
412: }
413:
414: if(y <= rect.top)
415: {
416: y = rect.top + ((rect.top - y)+1);
417: ppd->pVel[idx].y = PolyNewVel(idx);
418: }
419:
420: lpCurr->pPts[idx].x = x;
421: lpCurr->pPts[idx].y = y;
422: }
423:
424: hPen = SelectObject(hDC,CreatePen(PS_SOLID,1,crColor[ppd->nColor]));
425: #ifdef WIN32
426: PolyBezier(hDC,lpCurr->pPts,4);
427: #else
428: Polyline(hDC,lpCurr->pPts,4);
429: #endif
430: DeleteObject(SelectObject(hDC,hPen));
431:
432: #ifdef WIN32
433: SetROP2(hDC,R2_COPYPEN);
434: #endif
435: ReleaseDC(hWnd,hDC);
436: }
437: GlobalUnlock(ppd->hBezBuffer);
438: }
439: UnlockWindowInfo(hWnd);
440: }
441: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.