Reworked the rule_copy_multi, rule_ref_multi, rule_compile_multi to avoid
[AROS.git] / workbench / demos / roundshape.c
blob32b3c090346c99b33f8a5ad90579008399f3cc64
2 #include <dos/dos.h>
3 #include <intuition/intuition.h>
4 #include <graphics/gfx.h>
5 #include <proto/exec.h>
6 #include <proto/dos.h>
7 #include <proto/graphics.h>
8 #include <proto/intuition.h>
10 #include <math.h>
11 #include <stdio.h>
12 #include <stdlib.h>
14 #define INSTALL_NULL_SHAPE_FIRST 0
16 #define WINWIDTH 150
17 #define WINHEIGHT 150
19 /***********************************************************************************/
21 struct IntuitionBase *IntuitionBase;
22 struct GfxBase *GfxBase;
23 struct Screen *scr;
24 struct Window *win;
25 struct Region *shape;
26 struct RastPort *rp;
28 UBYTE Keys[128];
30 /***********************************************************************************/
32 static void cleanup(char *msg)
34 if (msg)
36 printf("roundshape: %s\n",msg);
39 if (win) CloseWindow(win);
40 if (shape) DisposeRegion(shape);
41 if (scr) UnlockPubScreen(0, scr);
43 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
44 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
46 exit(0);
49 /***********************************************************************************/
51 static void openlibs(void)
53 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39)))
55 cleanup("Can't open intuition.library V39!");
58 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39)))
60 cleanup("Can't open graphics.library V39!");
65 /***********************************************************************************/
67 static void getvisual(void)
69 if (!(scr = LockPubScreen(NULL)))
71 cleanup("Can't lock pub screen!");
75 /***********************************************************************************/
77 static void makewin(void)
79 win = OpenWindowTags(NULL, WA_CustomScreen , (IPTR)scr,
80 WA_InnerWidth , WINWIDTH,
81 WA_InnerHeight , WINHEIGHT,
82 WA_Title , (IPTR)"Round Shape",
83 WA_DragBar , TRUE,
84 WA_DepthGadget , TRUE,
85 WA_CloseGadget , TRUE,
86 WA_Activate , TRUE,
87 WA_MinWidth , 160,
88 WA_MinHeight , 160,
89 WA_MaxWidth , 2000,
90 WA_MaxHeight , 2000,
91 WA_SizeGadget , TRUE,
92 WA_SizeBBottom , TRUE,
93 WA_SizeBRight , TRUE,
94 WA_NoCareRefresh , TRUE,
95 WA_IDCMP , IDCMP_CLOSEWINDOW |
96 IDCMP_RAWKEY |
97 IDCMP_NEWSIZE,
98 TAG_DONE);
100 if (!win) cleanup("Can't open window");
102 rp = win->RPort;
106 /***********************************************************************************/
108 static void renderwin(void)
110 struct Region *oldshape;
111 struct Rectangle rect;
112 WORD cx, cy, r, y;
114 static WORD oldheight = 0;
115 static WORD oldwidth = 0;
117 if (oldheight == win->GZZHeight && oldwidth == win->GZZWidth)
118 return;
120 oldheight = win->GZZHeight;
121 oldwidth = win->GZZWidth;
123 cx = win->BorderLeft + win->GZZWidth / 2;
124 cy = win->BorderTop + win->GZZHeight / 2;
126 r = ((win->GZZWidth < win->GZZHeight) ? win->GZZWidth : win->GZZHeight) / 2 - 10;
128 #if INSTALL_NULL_SHAPE_FIRST
129 oldshape = ChangeWindowShape(win, shape, NULL);
130 if (oldshape) DisposeRegion(oldshape);
131 #endif
133 shape = NewRectRegion(0, 0, win->Width - 1, win->BorderTop - 1);
135 rect.MinX = win->Width - win->BorderRight;
136 rect.MinY = win->Height - win->BorderBottom;
137 rect.MaxX = win->Width - 1;
138 rect.MaxY = win->Height - 1;
140 OrRectRegion(shape, &rect);
142 for(y = 0; y < r; y++)
144 WORD dx = (WORD)sqrt((double)(r * r - y * y));
146 SetAPen(rp, 3);
147 RectFill(rp, cx - dx, cy - y, cx + dx, cy - y);
148 RectFill(rp, cx - dx, cy + y, cx + dx, cy + y);
150 SetAPen(rp, 2);
151 WritePixel(rp, cx - dx, cy - y);
152 WritePixel(rp, cx - dx, cy + y);
154 SetAPen(rp, 1);
155 WritePixel(rp, cx + dx, cy - y);
156 WritePixel(rp, cx + dx, cy + y);
158 rect.MinX = cx - dx;
159 rect.MinY = cy - y;
160 rect.MaxX = cx + dx;
161 rect.MaxY = cy - y;
163 OrRectRegion(shape, &rect);
165 rect.MinX = cx - dx;
166 rect.MinY = cy + y;
167 rect.MaxX = cx + dx;
168 rect.MaxY = cy + y;
170 OrRectRegion(shape, &rect);
173 oldshape = ChangeWindowShape(win, shape, NULL);
174 if (oldshape) DisposeRegion(oldshape);
176 RefreshWindowFrame(win);
179 /***********************************************************************************/
181 #define KC_ESC 0x45
183 /***********************************************************************************/
185 static void action(void)
187 struct IntuiMessage *msg;
191 WaitPort(win->UserPort);
193 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
195 switch (msg->Class)
197 case IDCMP_CLOSEWINDOW:
198 Keys[KC_ESC] = 1;
199 break;
201 case IDCMP_RAWKEY:
203 WORD code = msg->Code & ~IECODE_UP_PREFIX;
205 Keys[code] = (code == msg->Code) ? 1 : 0;
209 break;
211 case IDCMP_NEWSIZE:
212 renderwin();
213 break;
215 ReplyMsg((struct Message *)msg);
218 } while(!Keys[KC_ESC]);
221 /***********************************************************************************/
223 int main(void)
225 openlibs();
226 getvisual();
227 makewin();
228 renderwin();
229 action();
230 cleanup(0);
232 return 0;
235 /***********************************************************************************/