small correction.
[AROS.git] / workbench / demos / roundshape.c
blobf484133ffd1251a6e6347102137db21476d38fbb
2 #include <dos/dos.h>
3 #include <intuition/intuition.h>
4 #include <graphics/gfx.h>
5 #include <proto/alib.h>
6 #include <proto/exec.h>
7 #include <proto/dos.h>
8 #include <proto/graphics.h>
9 #include <proto/intuition.h>
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
15 #define INSTALL_NULL_SHAPE_FIRST 0
17 #define WINWIDTH 150
18 #define WINHEIGHT 150
20 /***********************************************************************************/
22 struct IntuitionBase *IntuitionBase;
23 struct GfxBase *GfxBase;
24 struct Screen *scr;
25 struct Window *win;
26 struct Region *shape;
27 struct RastPort *rp;
29 UBYTE Keys[128];
31 /***********************************************************************************/
33 static void cleanup(char *msg)
35 if (msg)
37 printf("roundshape: %s\n",msg);
40 if (win) CloseWindow(win);
41 if (shape) DisposeRegion(shape);
42 if (scr) UnlockPubScreen(0, scr);
44 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
45 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
47 exit(0);
50 /***********************************************************************************/
52 static void openlibs(void)
54 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 39)))
56 cleanup("Can't open intuition.library V39!");
59 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39)))
61 cleanup("Can't open graphics.library V39!");
66 /***********************************************************************************/
68 static void getvisual(void)
70 if (!(scr = LockPubScreen(NULL)))
72 cleanup("Can't lock pub screen!");
76 /***********************************************************************************/
78 static void makewin(void)
80 win = OpenWindowTags(NULL, WA_CustomScreen , (IPTR)scr,
81 WA_InnerWidth , WINWIDTH,
82 WA_InnerHeight , WINHEIGHT,
83 WA_Title , (IPTR)"Round Shape",
84 WA_DragBar , TRUE,
85 WA_DepthGadget , TRUE,
86 WA_CloseGadget , TRUE,
87 WA_Activate , TRUE,
88 WA_MinWidth , 160,
89 WA_MinHeight , 160,
90 WA_MaxWidth , 2000,
91 WA_MaxHeight , 2000,
92 WA_SizeGadget , TRUE,
93 WA_SizeBBottom , TRUE,
94 WA_SizeBRight , TRUE,
95 WA_NoCareRefresh , TRUE,
96 WA_ShapeRegion , 0,
97 WA_IDCMP , IDCMP_CLOSEWINDOW |
98 IDCMP_RAWKEY |
99 IDCMP_NEWSIZE,
100 TAG_DONE);
102 if (!win) cleanup("Can't open window");
104 rp = win->RPort;
108 /***********************************************************************************/
110 static void renderwin(void)
112 struct Region *oldshape;
113 struct Rectangle rect;
114 WORD cx, cy, r, y;
116 static WORD oldheight = 0;
117 static WORD oldwidth = 0;
119 if (oldheight == win->GZZHeight && oldwidth == win->GZZWidth)
120 return;
122 oldheight = win->GZZHeight;
123 oldwidth = win->GZZWidth;
125 cx = win->BorderLeft + win->GZZWidth / 2;
126 cy = win->BorderTop + win->GZZHeight / 2;
128 r = ((win->GZZWidth < win->GZZHeight) ? win->GZZWidth : win->GZZHeight) / 2 - 10;
130 #if INSTALL_NULL_SHAPE_FIRST
131 oldshape = ChangeWindowShape(win, shape, NULL);
132 if (oldshape) DisposeRegion(oldshape);
133 #endif
135 shape = NewRectRegion(0, 0, win->Width - 1, win->BorderTop - 1);
137 rect.MinX = win->Width - win->BorderRight;
138 rect.MinY = win->Height - win->BorderBottom;
139 rect.MaxX = win->Width - 1;
140 rect.MaxY = win->Height - 1;
142 OrRectRegion(shape, &rect);
144 for(y = 0; y < r; y++)
146 WORD dx = (WORD)sqrt((double)(r * r - y * y));
148 SetAPen(rp, 3);
149 RectFill(rp, cx - dx, cy - y, cx + dx, cy - y);
150 RectFill(rp, cx - dx, cy + y, cx + dx, cy + y);
152 SetAPen(rp, 2);
153 WritePixel(rp, cx - dx, cy - y);
154 WritePixel(rp, cx - dx, cy + y);
156 SetAPen(rp, 1);
157 WritePixel(rp, cx + dx, cy - y);
158 WritePixel(rp, cx + dx, cy + y);
160 rect.MinX = cx - dx;
161 rect.MinY = cy - y;
162 rect.MaxX = cx + dx;
163 rect.MaxY = cy - y;
165 OrRectRegion(shape, &rect);
167 rect.MinX = cx - dx;
168 rect.MinY = cy + y;
169 rect.MaxX = cx + dx;
170 rect.MaxY = cy + y;
172 OrRectRegion(shape, &rect);
175 oldshape = ChangeWindowShape(win, shape, NULL);
176 if (oldshape) DisposeRegion(oldshape);
178 RefreshWindowFrame(win);
181 /***********************************************************************************/
183 #define KC_ESC 0x45
185 /***********************************************************************************/
187 static void action(void)
189 struct IntuiMessage *msg;
193 WaitPort(win->UserPort);
195 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
197 switch (msg->Class)
199 case IDCMP_CLOSEWINDOW:
200 Keys[KC_ESC] = 1;
201 break;
203 case IDCMP_RAWKEY:
205 WORD code = msg->Code & ~IECODE_UP_PREFIX;
207 Keys[code] = (code == msg->Code) ? 1 : 0;
211 break;
213 case IDCMP_NEWSIZE:
214 renderwin();
215 break;
217 ReplyMsg((struct Message *)msg);
220 } while(!Keys[KC_ESC]);
223 /***********************************************************************************/
225 int main(void)
227 openlibs();
228 getvisual();
229 makewin();
230 renderwin();
231 action();
232 cleanup(0);
234 return 0;
237 /***********************************************************************************/