small correction.
[AROS.git] / workbench / demos / scrbackfill.c
blob6d62f28127516994301b89b9e5baef87aaacb4d3
2 #include <dos/dos.h>
3 #include <intuition/intuition.h>
4 #include <graphics/gfx.h>
5 #include <utility/hooks.h>
7 #include <proto/exec.h>
8 #include <proto/intuition.h>
9 #include <proto/graphics.h>
10 #include <proto/layers.h>
12 #include <clib/alib_protos.h>
14 #include <stdio.h>
15 #include <stdlib.h>
18 #define PATTERNWIDTH 32
19 #define PATTERNHEIGHT 32
20 #define PATTERNCOL1 SHADOWPEN
21 #define PATTERNCOL2 SHINEPEN
23 struct LayerHookMsg
25 struct Layer *lay; /* not valid for layerinfo backfill hook!!! */
26 struct Rectangle bounds;
27 LONG offsetx;
28 LONG offsety;
31 struct IntuitionBase *IntuitionBase;
32 struct GfxBase *GfxBase;
33 struct Library *LayersBase;
35 static struct Screen *scr;
36 static struct Window *win;
37 static struct DrawInfo *dri;
38 static struct BitMap *patternbm;
40 static struct Hook backfillhook, *old_layerinfohook;
41 static BOOL hook_installed;
44 static void Cleanup(char *msg)
46 struct Window *tempwin;
48 WORD rc;
50 if (msg)
52 printf("scrbackfill: %s\n",msg);
53 rc = RETURN_WARN;
54 } else {
55 rc = RETURN_OK;
58 if (win) CloseWindow(win);
60 if (hook_installed)
62 InstallLayerInfoHook(&scr->LayerInfo,old_layerinfohook);
64 tempwin = OpenWindowTags(0,WA_PubScreen,(IPTR)scr,
65 WA_Left,0,
66 WA_Top,0,
67 WA_Width,scr->Width,
68 WA_Height,scr->Height,
69 WA_Borderless,TRUE,
70 WA_Backdrop,TRUE,
71 WA_BackFill,(IPTR)LAYERS_NOBACKFILL,
72 TAG_DONE);
74 if (tempwin) CloseWindow(tempwin);
77 if (patternbm)
79 WaitBlit();
80 FreeBitMap(patternbm);
83 if (dri) FreeScreenDrawInfo(scr,dri);
84 UnlockPubScreen(0,scr);
86 if (LayersBase) CloseLibrary(LayersBase);
87 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
88 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
90 exit(rc);
94 static void OpenLibs(void)
96 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",39)))
98 Cleanup("Can't open intuition.library V39!");
101 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",39)))
103 Cleanup("Can't open graphics.library V39!");
106 if (!(LayersBase = OpenLibrary("layers.library",39)))
108 Cleanup("Can't open layers.library V39!");
113 static void MyBackfillFunc(struct Hook *hook,struct RastPort *rp,
114 struct LayerHookMsg *msg)
116 WORD x1,y1,x2,y2,px,py,pw,ph;
118 x1 = msg->bounds.MinX;
119 y1 = msg->bounds.MinY;
120 x2 = msg->bounds.MaxX;
121 y2 = msg->bounds.MaxY;
123 px = x1 % PATTERNWIDTH;
125 pw = PATTERNWIDTH - px;
129 y1 = msg->bounds.MinY;
130 py = y1 % PATTERNHEIGHT;
132 ph = PATTERNHEIGHT - py;
134 if (pw > (x2 - x1 + 1)) pw = x2 - x1 + 1;
138 if (ph > (y2 - y1 + 1)) ph = y2 - y1 + 1;
140 BltBitMap(patternbm,
143 rp->BitMap,
148 192,
149 255,
152 y1 += ph;
154 py = 0;
155 ph = PATTERNHEIGHT;
157 } while (y1 <= y2); /* while(y1 < y2) */
159 x1 += pw;
161 px = 0;
162 pw = PATTERNWIDTH;
164 } while (x1 <= x2); /* while (x1 < x2) */
169 static void InitBackfillHook(void)
171 backfillhook.h_Entry = HookEntry;
172 backfillhook.h_SubEntry = (HOOKFUNC)MyBackfillFunc;
176 static void GetVisual(void)
178 if (!(scr = LockPubScreen(0)))
180 Cleanup("Can't lock pub screen!");
183 if (!(dri = GetScreenDrawInfo(scr)))
185 Cleanup("Can't get drawinfo!");
189 static struct RastPort temprp;
191 static void MakePattern(void)
193 if (!(patternbm = AllocBitMap(PATTERNWIDTH * 2,
194 PATTERNHEIGHT * 2,
195 GetBitMapAttr(scr->RastPort.BitMap,BMA_DEPTH),
196 BMF_CLEAR,
197 scr->RastPort.BitMap)))
199 Cleanup("Can't create pattern bitmap!");
202 InitRastPort(&temprp);
203 temprp.BitMap = patternbm;
205 SetAPen(&temprp, dri->dri_Pens[PATTERNCOL1]);
207 RectFill(&temprp, 0, 0, 10, 10);
208 RectFill(&temprp, 0, 0, PATTERNWIDTH - 1, PATTERNHEIGHT - 1);
210 SetAPen(&temprp, dri->dri_Pens[PATTERNCOL2]);
211 RectFill(&temprp, 0, 0, PATTERNWIDTH / 2 - 1, PATTERNHEIGHT / 2 - 1);
212 RectFill(&temprp, PATTERNWIDTH / 2, PATTERNHEIGHT / 2, PATTERNWIDTH - 1, PATTERNHEIGHT - 1);
216 static void StartBackfillHook(void)
218 struct Window *tempwin;
220 old_layerinfohook = InstallLayerInfoHook(&scr->LayerInfo,&backfillhook);
222 hook_installed = TRUE;
224 /* The non-window area is not automatically backefilled with
225 the new hook, which is ok, since this is also true for the
226 real Amiga.
228 So we usa a little trick: We Open a backdrop window with the
229 size of the screen and close it immediately. Layers library will then
230 automatically backfill in DeleteLayer().*/
232 tempwin = OpenWindowTags(0,WA_PubScreen,(IPTR)scr,
233 WA_Left,0,
234 WA_Top,0,
235 WA_Width,scr->Width,
236 WA_Height,scr->Height,
237 WA_Borderless,TRUE,
238 WA_Backdrop,TRUE,
239 WA_BackFill,(IPTR)LAYERS_NOBACKFILL,
240 TAG_DONE);
242 if (tempwin) CloseWindow(tempwin);
246 static void MakeWin(void)
248 if (!(win = OpenWindowTags(0,WA_PubScreen,(IPTR)scr,
249 WA_Left,10,
250 WA_Top,10,
251 WA_Width,200,
252 WA_Height,dri->dri_Font->tf_YSize + scr->WBorTop + 1,
253 WA_AutoAdjust,TRUE,
254 WA_Title,(IPTR)"Screen Backfill Test",
255 WA_SimpleRefresh,TRUE,
256 WA_CloseGadget,TRUE,
257 WA_DepthGadget,TRUE,
258 WA_DragBar,TRUE,
259 WA_IDCMP,IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
260 TAG_DONE)))
262 Cleanup("Can't open window!");
265 ScreenToFront(win->WScreen);
269 static void HandleAll(void)
271 struct IntuiMessage *msg;
273 BOOL quitme = FALSE;
275 while (!quitme)
277 WaitPort(win->UserPort);
279 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
281 switch (msg->Class)
283 case IDCMP_CLOSEWINDOW:
284 quitme = TRUE;
285 break;
287 case IDCMP_REFRESHWINDOW:
288 BeginRefresh(win);
289 EndRefresh(win,TRUE);
290 break;
293 ReplyMsg((struct Message *)msg);
299 int main(void)
301 OpenLibs();
302 InitBackfillHook();
303 GetVisual();
304 MakePattern();
305 StartBackfillHook();
306 MakeWin();
307 HandleAll();
308 Cleanup(0);
310 return 0;