1 #include <intuition/intuition.h>
2 #include <graphics/gfxmacros.h>
3 #include <graphics/gfx.h>
4 #include <devices/rawkeycodes.h>
6 #include <proto/exec.h>
8 #include <proto/intuition.h>
9 #include <proto/graphics.h>
15 #include <aros/debug.h>
19 #define MODE_ADDPOINTS 1
20 #define MODE_MOVEPOINTS 2
22 #define MSGMOUSEX ( ((msg->MouseX - win->BorderLeft) < 0) ? 0 : \
23 ((msg->MouseX - win->BorderLeft) >= win->GZZWidth) ? win->GZZWidth - 1 : \
24 (msg->MouseX - win->BorderLeft) )
26 #define MSGMOUSEY ( ((msg->MouseY - win->BorderTop) < 0) ? 0 : \
27 ((msg->MouseY - win->BorderTop) >= win->GZZHeight) ? win->GZZHeight - 1 : \
28 (msg->MouseY - win->BorderTop) )
30 static struct Window
*win
;
31 static struct RastPort
*winrp
, *drawrp
;
32 static struct BitMap
*drawbm
;
33 static struct AreaInfo ai
;
34 static struct TmpRas tr
;
36 static UBYTE aibuf
[(MAX_POINTS
+ 1) * 5];
37 static WORD mode
, actpoint
, hipoint
, numpoints
;
38 static WORD outlinepen
= -1, testfillpen
= -1;
39 static BOOL outlinemode
, testfill
;
40 static WORD points
[MAX_POINTS
+ 1][2];
41 static char wintitle
[256];
43 #include "areatest2_fillpoly.h"
45 static void cleanup(char *msg
)
47 if (msg
) printf("areatest2: %s\n", msg
);
49 if (outlinepen
!= -1) ReleasePen(win
->WScreen
->ViewPort
.ColorMap
, outlinepen
);
50 if (testfillpen
!= -1) ReleasePen(win
->WScreen
->ViewPort
.ColorMap
, testfillpen
);
51 if (drawbm
) FreeBitMap(drawbm
);
52 if (drawrp
) FreeRastPort(drawrp
);
56 FreeRaster(trbuf
, win
->GZZWidth
, win
->GZZHeight
);
59 if (win
) CloseWindow(win
);
63 static void updatetitle(void)
65 char *title
= "AreaTest2";
72 "Create points [%2d/%2d]. Press RETURN when done.",
73 actpoint
+ 1, MAX_POINTS
);
80 "Move points. Press RETURN to make new shape.");
86 SetWindowTitles(win
, title
, (char *)~0);
90 static void makewin(void)
92 win
= OpenWindowTags(0, WA_Title
, (IPTR
)"AreaTest2",
95 WA_GimmeZeroZero
, TRUE
,
99 WA_IDCMP
, IDCMP_MOUSEBUTTONS
|
105 WA_ReportMouse
, TRUE
,
107 if (!win
) cleanup("Can't open window!");
111 InitArea(&ai
, aibuf
, sizeof(aibuf
) / 5);
112 trbuf
= AllocRaster(win
->GZZWidth
, win
->GZZHeight
);
113 if (!trbuf
) cleanup("TmpRas buffer allocation failed!");
114 InitTmpRas(&tr
, trbuf
, RASSIZE(win
->GZZWidth
, win
->GZZHeight
));
116 drawbm
= AllocBitMap(win
->GZZWidth
, win
->GZZHeight
, 0, BMF_MINPLANES
, win
->RPort
->BitMap
);
117 if (!drawbm
) cleanup("Can't allocate draw bitmap!");
119 drawrp
= CreateRastPort();
120 if (!drawrp
) cleanup("Can't allocate draw rastport!");
121 drawrp
->BitMap
= drawbm
;
123 drawrp
->AreaInfo
= &ai
;
124 drawrp
->TmpRas
= &tr
;
126 outlinepen
= ObtainBestPen(win
->WScreen
->ViewPort
.ColorMap
, 0xFFFFFFFF,
129 OBP_FailIfBad
, FALSE
,
132 testfillpen
= ObtainBestPen(win
->WScreen
->ViewPort
.ColorMap
, 0x44444444,
135 OBP_FailIfBad
, FALSE
,
140 static void hilightpoint(WORD point
)
142 WORD x
= points
[point
][0];
143 WORD y
= points
[point
][1];
145 //kprintf("hilightpoint %d,%d\n", x, y);
147 SetABPenDrMd(winrp
, 2, 0, COMPLEMENT
);
148 Move(winrp
, x
- 3, y
- 3);
149 Draw(winrp
, x
+ 3, y
- 3),
150 Draw(winrp
, x
+ 3, y
+ 3);
151 Draw(winrp
, x
- 3, y
+ 3),
152 Draw(winrp
, x
- 3, y
- 3);
155 static void clear(struct RastPort
*rp
)
157 SetABPenDrMd(rp
, 2, 0, JAM1
);
158 RectFill(rp
, 0, 0, win
->GZZWidth
- 1, win
->GZZHeight
- 1);
161 static void paint(void)
165 if (numpoints
== 0) return;
170 SetABPenDrMd(winrp
, 1, 0, JAM1
);
171 Move(winrp
, points
[0][0], points
[0][1]);
172 PolyDraw(winrp
, numpoints
, (WORD
*)points
);
175 case MODE_MOVEPOINTS
:
177 SetABPenDrMd(drawrp
, testfill
? testfillpen
: 1, 0, JAM1
);
180 SetOutlinePen(drawrp
, outlinepen
);
184 drawrp
->Flags
&= ~AREAOUTLINE
;
189 AreaMove(drawrp
, points
[0][0], points
[0][1]);
190 for(i
= 1; i
< numpoints
; i
++)
192 AreaDraw(drawrp
, points
[i
][0], points
[i
][1]);
198 MyFillPolygon(drawrp
, points
, numpoints
);
201 BltBitMapRastPort(drawbm
, 0, 0, winrp
, 0, 0, win
->GZZWidth
, win
->GZZHeight
, 192);
206 static WORD
pointundermouse(LONG x
, LONG y
)
209 LONG best_i
= 0, best_dist
= 0x7fffffff;
211 for(i
= 0; i
< numpoints
; i
++)
213 dist
= (points
[i
][0] - x
) * (points
[i
][0] - x
) +
214 (points
[i
][1] - y
) * (points
[i
][1] - y
);
216 if (dist
< best_dist
)
223 return (best_dist
< 200) ? best_i
: -1;
226 static void savepoly(WORD n
)
233 snprintf(s
, sizeof(s
), "PROGDIR:polygon%d.dat", n
);
235 if ((fh
= Open(s
, MODE_NEWFILE
)))
239 if (Write(fh
, &i
, sizeof(i
)) == sizeof(i
))
241 for(n
= 0; n
< numpoints
; n
++)
244 if (Write(fh
, &i
, sizeof(i
)) != sizeof(i
)) break;
247 if (Write(fh
, &i
, sizeof(i
)) != sizeof(i
)) break;
251 if (n
== numpoints
) ok
= TRUE
;
265 Fault(err
, "Saving failed", s
, sizeof(s
));
269 strcpy(s
, "Saved polygon");
272 SetWindowTitles(win
, s
, (char *)~0);
278 static BOOL
loadpoly(WORD n
)
286 snprintf(s
, sizeof(s
), "PROGDIR:polygon%d.dat", n
);
288 if ((fh
= Open(s
, MODE_OLDFILE
)))
292 if (Read(fh
, &i
, sizeof(i
)) == sizeof(i
))
294 if ((temppoints
= malloc(sizeof(WORD
) * 2 * i
)))
296 for(n
= 0; n
< i
; n
++)
298 if (Read(fh
, &temppoints
[n
* 2], sizeof(WORD
)) != sizeof(WORD
)) break;
299 if (Read(fh
, &temppoints
[n
* 2 + 1], sizeof(WORD
)) != sizeof(WORD
)) break;
306 for(i
= 0; i
< n
; i
++)
308 points
[i
][0] = temppoints
[i
* 2];
309 points
[i
][1] = temppoints
[i
* 2 + 1];
331 Fault(err
, "Loading failed", s
, sizeof(s
));
332 SetWindowTitles(win
, s
, (char *)~0);
341 static void handleall(void)
343 struct IntuiMessage
*msg
;
345 BOOL quitme
= FALSE
, lmbdown
= FALSE
;
347 mode
= MODE_ADDPOINTS
;
353 WaitPort(win
->UserPort
);
354 while((msg
= (struct IntuiMessage
*)GetMsg(win
->UserPort
)))
358 case IDCMP_CLOSEWINDOW
:
362 case IDCMP_MOUSEBUTTONS
:
363 if (msg
->Code
== SELECTDOWN
)
370 points
[actpoint
][0] = MSGMOUSEX
;
371 points
[actpoint
][1] = MSGMOUSEY
;
374 if (numpoints
== MAX_POINTS
)
376 mode
= MODE_MOVEPOINTS
;
384 case MODE_MOVEPOINTS
:
385 actpoint
= pointundermouse(MSGMOUSEX
, MSGMOUSEY
);
390 else if (msg
->Code
== SELECTUP
)
396 case MODE_MOVEPOINTS
:
405 case IDCMP_MOUSEMOVE
:
408 case MODE_MOVEPOINTS
:
409 if ((actpoint
>= 0) && lmbdown
)
411 points
[actpoint
][0] = MSGMOUSEX
;
412 points
[actpoint
][1] = MSGMOUSEY
;
417 WORD new_hipoint
= pointundermouse(MSGMOUSEX
, MSGMOUSEY
);
419 if (new_hipoint
!= hipoint
)
421 if (hipoint
>= 0) hilightpoint(hipoint
);
422 hipoint
= new_hipoint
;
423 if (hipoint
>= 0) hilightpoint(hipoint
);
431 case IDCMP_VANILLAKEY
:
444 mode
= MODE_MOVEPOINTS
;
452 case MODE_MOVEPOINTS
:
453 actpoint
= numpoints
= 0;
454 mode
= MODE_ADDPOINTS
;
462 if (mode
== MODE_MOVEPOINTS
)
464 for(i
= 0; i
< numpoints
; i
++)
466 if (points
[i
][0] > 0) points
[i
][0]--;
474 if (mode
== MODE_MOVEPOINTS
)
476 for(i
= 0; i
< numpoints
; i
++)
478 if (points
[i
][0] < win
->GZZWidth
- 1) points
[i
][0]++;
486 if (mode
== MODE_MOVEPOINTS
)
488 for(i
= 0; i
< numpoints
; i
++)
490 if (points
[i
][1] > 0) points
[i
][1]--;
498 if (mode
== MODE_MOVEPOINTS
)
500 for(i
= 0; i
< numpoints
; i
++)
502 if (points
[i
][1] < win
->GZZHeight
- 1) points
[i
][1]++;
511 outlinemode
= !outlinemode
;
512 if (mode
== MODE_MOVEPOINTS
)
516 SetWindowTitles(win
, outlinemode
? "Outline Mode: ON" : "Outline Mode: OFF", (char *)~0);
526 testfill
= !testfill
;
527 if (mode
== MODE_MOVEPOINTS
)
531 SetWindowTitles(win
, testfill
? "Test Fillroutine: ON" : "Test Fillroutine: OFF", (char *)~0);
542 case MODE_MOVEPOINTS
:
543 if (lmbdown
&& actpoint
>= 0)
545 BOOL changed
= FALSE
;
550 if (points
[actpoint
][0] > 0)
552 points
[actpoint
][0]--;
558 if (points
[actpoint
][0] < win
->GZZWidth
- 1)
560 points
[actpoint
][0]++;
566 if (points
[actpoint
][1] > 0)
568 points
[actpoint
][1]--;
574 if (points
[actpoint
][1] < win
->GZZHeight
- 1)
576 points
[actpoint
][1]++;
581 } /* switch(msg->Code) */
583 if (changed
) paint();
585 } /* if (!lmbdown && hipoint >= 0) */
600 if (msg
->Qualifier
& (IEQUALIFIER_LSHIFT
| IEQUALIFIER_RSHIFT
))
602 savepoly(msg
->Code
- RAWKEY_F1
+ 1);
606 loadpoly(msg
->Code
- RAWKEY_F1
+ 1);
629 if (!(msg
->Qualifier
& (IEQUALIFIER_LSHIFT
| IEQUALIFIER_RSHIFT
)))
631 if (loadpoly(msg
->Code
- RAWKEY_F1
+ 1))
635 mode
= MODE_MOVEPOINTS
;
646 } /* switch(msg->Class) */
647 ReplyMsg((struct Message
*)msg
);
649 } /* while((msg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
651 } /*while(!quitme) */