2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
6 #include <intuition/intuition.h>
7 #include <graphics/gfxmacros.h>
8 #include <graphics/gfx.h>
9 #include <devices/rawkeycodes.h>
11 #include <proto/exec.h>
12 #include <proto/dos.h>
13 #include <proto/intuition.h>
14 #include <proto/graphics.h>
20 #include <aros/debug.h>
24 #define MODE_ADDPOINTS 1
25 #define MODE_MOVEPOINTS 2
27 #define MSGMOUSEX ( ((msg->MouseX - win->BorderLeft) < 0) ? 0 : \
28 ((msg->MouseX - win->BorderLeft) >= win->GZZWidth) ? win->GZZWidth - 1 : \
29 (msg->MouseX - win->BorderLeft) )
31 #define MSGMOUSEY ( ((msg->MouseY - win->BorderTop) < 0) ? 0 : \
32 ((msg->MouseY - win->BorderTop) >= win->GZZHeight) ? win->GZZHeight - 1 : \
33 (msg->MouseY - win->BorderTop) )
35 static struct Window
*win
;
36 static struct RastPort
*winrp
, *drawrp
;
37 static struct BitMap
*drawbm
;
38 static struct AreaInfo ai
;
39 static struct TmpRas tr
;
41 static UBYTE aibuf
[(MAX_POINTS
+ 1) * 5];
42 static WORD mode
, actpoint
, hipoint
, numpoints
;
43 static WORD outlinepen
= -1, testfillpen
= -1;
44 static BOOL outlinemode
, testfill
;
45 static WORD points
[MAX_POINTS
+ 1][2];
46 static char wintitle
[256];
48 #include "areatest2_fillpoly.h"
50 static void cleanup(char *msg
)
52 if (msg
) printf("areatest2: %s\n", msg
);
54 if (outlinepen
!= -1) ReleasePen(win
->WScreen
->ViewPort
.ColorMap
, outlinepen
);
55 if (testfillpen
!= -1) ReleasePen(win
->WScreen
->ViewPort
.ColorMap
, testfillpen
);
56 if (drawbm
) FreeBitMap(drawbm
);
57 if (drawrp
) FreeRastPort(drawrp
);
61 FreeRaster(trbuf
, win
->GZZWidth
, win
->GZZHeight
);
64 if (win
) CloseWindow(win
);
68 static void updatetitle(void)
70 char *title
= "AreaTest2";
77 "Create points [%2d/%2d]. Press RETURN when done.",
78 actpoint
+ 1, MAX_POINTS
);
85 "Move points. Press RETURN to make new shape.");
91 SetWindowTitles(win
, title
, (char *)~0);
95 static void makewin(void)
97 win
= OpenWindowTags(0, WA_Title
, (IPTR
)"AreaTest2",
100 WA_GimmeZeroZero
, TRUE
,
101 WA_CloseGadget
, TRUE
,
102 WA_DepthGadget
, TRUE
,
104 WA_IDCMP
, IDCMP_MOUSEBUTTONS
|
110 WA_ReportMouse
, TRUE
,
112 if (!win
) cleanup("Can't open window!");
116 InitArea(&ai
, aibuf
, sizeof(aibuf
) / 5);
117 trbuf
= AllocRaster(win
->GZZWidth
, win
->GZZHeight
);
118 if (!trbuf
) cleanup("TmpRas buffer allocation failed!");
119 InitTmpRas(&tr
, trbuf
, RASSIZE(win
->GZZWidth
, win
->GZZHeight
));
121 drawbm
= AllocBitMap(win
->GZZWidth
, win
->GZZHeight
, 0, BMF_MINPLANES
, win
->RPort
->BitMap
);
122 if (!drawbm
) cleanup("Can't allocate draw bitmap!");
124 drawrp
= CreateRastPort();
125 if (!drawrp
) cleanup("Can't allocate draw rastport!");
126 drawrp
->BitMap
= drawbm
;
128 drawrp
->AreaInfo
= &ai
;
129 drawrp
->TmpRas
= &tr
;
131 outlinepen
= ObtainBestPen(win
->WScreen
->ViewPort
.ColorMap
, 0xFFFFFFFF,
134 OBP_FailIfBad
, FALSE
,
137 testfillpen
= ObtainBestPen(win
->WScreen
->ViewPort
.ColorMap
, 0x44444444,
140 OBP_FailIfBad
, FALSE
,
145 static void hilightpoint(WORD point
)
147 WORD x
= points
[point
][0];
148 WORD y
= points
[point
][1];
150 //kprintf("hilightpoint %d,%d\n", x, y);
152 SetABPenDrMd(winrp
, 2, 0, COMPLEMENT
);
153 Move(winrp
, x
- 3, y
- 3);
154 Draw(winrp
, x
+ 3, y
- 3),
155 Draw(winrp
, x
+ 3, y
+ 3);
156 Draw(winrp
, x
- 3, y
+ 3),
157 Draw(winrp
, x
- 3, y
- 3);
160 static void clear(struct RastPort
*rp
)
162 SetABPenDrMd(rp
, 2, 0, JAM1
);
163 RectFill(rp
, 0, 0, win
->GZZWidth
- 1, win
->GZZHeight
- 1);
166 static void paint(void)
170 if (numpoints
== 0) return;
175 SetABPenDrMd(winrp
, 1, 0, JAM1
);
176 Move(winrp
, points
[0][0], points
[0][1]);
177 PolyDraw(winrp
, numpoints
, (WORD
*)points
);
180 case MODE_MOVEPOINTS
:
182 SetABPenDrMd(drawrp
, testfill
? testfillpen
: 1, 0, JAM1
);
185 SetOutlinePen(drawrp
, outlinepen
);
189 drawrp
->Flags
&= ~AREAOUTLINE
;
194 AreaMove(drawrp
, points
[0][0], points
[0][1]);
195 for(i
= 1; i
< numpoints
; i
++)
197 AreaDraw(drawrp
, points
[i
][0], points
[i
][1]);
203 MyFillPolygon(drawrp
, points
, numpoints
);
206 BltBitMapRastPort(drawbm
, 0, 0, winrp
, 0, 0, win
->GZZWidth
, win
->GZZHeight
, 192);
211 static WORD
pointundermouse(LONG x
, LONG y
)
214 LONG best_i
= 0, best_dist
= 0x7fffffff;
216 for(i
= 0; i
< numpoints
; i
++)
218 dist
= (points
[i
][0] - x
) * (points
[i
][0] - x
) +
219 (points
[i
][1] - y
) * (points
[i
][1] - y
);
221 if (dist
< best_dist
)
228 return (best_dist
< 200) ? best_i
: -1;
231 static void savepoly(WORD n
)
238 snprintf(s
, sizeof(s
), "PROGDIR:polygon%d.dat", n
);
240 if ((fh
= Open(s
, MODE_NEWFILE
)))
244 if (Write(fh
, &i
, sizeof(i
)) == sizeof(i
))
246 for(n
= 0; n
< numpoints
; n
++)
249 if (Write(fh
, &i
, sizeof(i
)) != sizeof(i
)) break;
252 if (Write(fh
, &i
, sizeof(i
)) != sizeof(i
)) break;
256 if (n
== numpoints
) ok
= TRUE
;
270 Fault(err
, "Saving failed", s
, sizeof(s
));
274 strcpy(s
, "Saved polygon");
277 SetWindowTitles(win
, s
, (char *)~0);
283 static BOOL
loadpoly(WORD n
)
291 snprintf(s
, sizeof(s
), "PROGDIR:polygon%d.dat", n
);
293 if ((fh
= Open(s
, MODE_OLDFILE
)))
297 if (Read(fh
, &i
, sizeof(i
)) == sizeof(i
))
299 if ((temppoints
= malloc(sizeof(WORD
) * 2 * i
)))
301 for(n
= 0; n
< i
; n
++)
303 if (Read(fh
, &temppoints
[n
* 2], sizeof(WORD
)) != sizeof(WORD
)) break;
304 if (Read(fh
, &temppoints
[n
* 2 + 1], sizeof(WORD
)) != sizeof(WORD
)) break;
311 for(i
= 0; i
< n
; i
++)
313 points
[i
][0] = temppoints
[i
* 2];
314 points
[i
][1] = temppoints
[i
* 2 + 1];
336 Fault(err
, "Loading failed", s
, sizeof(s
));
337 SetWindowTitles(win
, s
, (char *)~0);
346 static void handleall(void)
348 struct IntuiMessage
*msg
;
350 BOOL quitme
= FALSE
, lmbdown
= FALSE
;
352 mode
= MODE_ADDPOINTS
;
358 WaitPort(win
->UserPort
);
359 while((msg
= (struct IntuiMessage
*)GetMsg(win
->UserPort
)))
363 case IDCMP_CLOSEWINDOW
:
367 case IDCMP_MOUSEBUTTONS
:
368 if (msg
->Code
== SELECTDOWN
)
375 points
[actpoint
][0] = MSGMOUSEX
;
376 points
[actpoint
][1] = MSGMOUSEY
;
379 if (numpoints
== MAX_POINTS
)
381 mode
= MODE_MOVEPOINTS
;
389 case MODE_MOVEPOINTS
:
390 actpoint
= pointundermouse(MSGMOUSEX
, MSGMOUSEY
);
395 else if (msg
->Code
== SELECTUP
)
401 case MODE_MOVEPOINTS
:
410 case IDCMP_MOUSEMOVE
:
413 case MODE_MOVEPOINTS
:
414 if ((actpoint
>= 0) && lmbdown
)
416 points
[actpoint
][0] = MSGMOUSEX
;
417 points
[actpoint
][1] = MSGMOUSEY
;
422 WORD new_hipoint
= pointundermouse(MSGMOUSEX
, MSGMOUSEY
);
424 if (new_hipoint
!= hipoint
)
426 if (hipoint
>= 0) hilightpoint(hipoint
);
427 hipoint
= new_hipoint
;
428 if (hipoint
>= 0) hilightpoint(hipoint
);
436 case IDCMP_VANILLAKEY
:
449 mode
= MODE_MOVEPOINTS
;
457 case MODE_MOVEPOINTS
:
458 actpoint
= numpoints
= 0;
459 mode
= MODE_ADDPOINTS
;
467 if (mode
== MODE_MOVEPOINTS
)
469 for(i
= 0; i
< numpoints
; i
++)
471 if (points
[i
][0] > 0) points
[i
][0]--;
479 if (mode
== MODE_MOVEPOINTS
)
481 for(i
= 0; i
< numpoints
; i
++)
483 if (points
[i
][0] < win
->GZZWidth
- 1) points
[i
][0]++;
491 if (mode
== MODE_MOVEPOINTS
)
493 for(i
= 0; i
< numpoints
; i
++)
495 if (points
[i
][1] > 0) points
[i
][1]--;
503 if (mode
== MODE_MOVEPOINTS
)
505 for(i
= 0; i
< numpoints
; i
++)
507 if (points
[i
][1] < win
->GZZHeight
- 1) points
[i
][1]++;
516 outlinemode
= !outlinemode
;
517 if (mode
== MODE_MOVEPOINTS
)
521 SetWindowTitles(win
, outlinemode
? "Outline Mode: ON" : "Outline Mode: OFF", (char *)~0);
531 testfill
= !testfill
;
532 if (mode
== MODE_MOVEPOINTS
)
536 SetWindowTitles(win
, testfill
? "Test Fillroutine: ON" : "Test Fillroutine: OFF", (char *)~0);
547 case MODE_MOVEPOINTS
:
548 if (lmbdown
&& actpoint
>= 0)
550 BOOL changed
= FALSE
;
555 if (points
[actpoint
][0] > 0)
557 points
[actpoint
][0]--;
563 if (points
[actpoint
][0] < win
->GZZWidth
- 1)
565 points
[actpoint
][0]++;
571 if (points
[actpoint
][1] > 0)
573 points
[actpoint
][1]--;
579 if (points
[actpoint
][1] < win
->GZZHeight
- 1)
581 points
[actpoint
][1]++;
586 } /* switch(msg->Code) */
588 if (changed
) paint();
590 } /* if (!lmbdown && hipoint >= 0) */
605 if (msg
->Qualifier
& (IEQUALIFIER_LSHIFT
| IEQUALIFIER_RSHIFT
))
607 savepoly(msg
->Code
- RAWKEY_F1
+ 1);
611 loadpoly(msg
->Code
- RAWKEY_F1
+ 1);
634 if (!(msg
->Qualifier
& (IEQUALIFIER_LSHIFT
| IEQUALIFIER_RSHIFT
)))
636 if (loadpoly(msg
->Code
- RAWKEY_F1
+ 1))
640 mode
= MODE_MOVEPOINTS
;
651 } /* switch(msg->Class) */
652 ReplyMsg((struct Message
*)msg
);
654 } /* while((msg = (struct IntuiMessage *)GetMsg(win->UserPort))) */
656 } /*while(!quitme) */