New bitmap method SetRGBConversionFunction which can be used to
[tangerine.git] / workbench / demos / winbackfill.c
blob9d994fbb606111853eef1f7db342f4031ad2f78a
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 <string.h>
16 #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;
26 struct Rectangle bounds;
27 LONG offsetx;
28 LONG offsety;
31 extern ULONG HookEntry();
33 struct IntuitionBase *IntuitionBase;
34 struct GfxBase *GfxBase;
35 struct Library *LayersBase;
37 static struct Screen *scr;
38 static struct Window *win;
39 static struct DrawInfo *dri;
40 static struct BitMap *patternbm;
42 static struct Hook backfillhook;
45 static void Cleanup(char *msg)
47 WORD rc;
49 if (msg)
51 printf("winbackfill: %s\n",msg);
52 rc = RETURN_WARN;
54 else
56 rc = RETURN_OK;
59 if (win) CloseWindow(win);
61 if (patternbm)
63 WaitBlit();
64 FreeBitMap(patternbm);
67 if (dri) FreeScreenDrawInfo(scr,dri);
68 UnlockPubScreen(0,scr);
70 if (LayersBase) CloseLibrary(LayersBase);
71 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
72 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
74 exit(rc);
78 static void OpenLibs(void)
80 if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",39)))
82 Cleanup("Can't open intuition.library V39!");
85 if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",39)))
87 Cleanup("Can't open graphics.library V39!");
90 if (!(LayersBase = OpenLibrary("layers.library",39)))
92 Cleanup("Can't open layers.library V39!");
97 static void MyBackfillFunc(struct Hook *hook,struct RastPort *rp,
98 struct LayerHookMsg *msg)
100 struct RastPort myrp;
101 struct Layer *lay;
102 WORD x1,y1,x2,y2,px,py,pw,ph;
104 myrp = *rp;
105 lay = msg->lay;
107 myrp.Layer = 0;
109 x1 = msg->bounds.MinX;
110 y1 = msg->bounds.MinY;
111 x2 = msg->bounds.MaxX;
112 y2 = msg->bounds.MaxY;
114 px = (x1 - lay->bounds.MinX) % PATTERNWIDTH;
116 pw = PATTERNWIDTH - px;
120 y1 = msg->bounds.MinY;
121 py = (y1 - lay->bounds.MinY) % PATTERNHEIGHT;
123 ph = PATTERNHEIGHT - py;
125 if (pw > (x2 - x1 + 1)) pw = x2 - x1 + 1;
129 if (ph > (y2 - y1 + 1)) ph = y2 - y1 + 1;
131 BltBitMap(patternbm,
134 rp->BitMap,
139 192,
140 255,
143 y1 += ph;
145 py = 0;
146 ph = PATTERNHEIGHT;
148 } while (y1 <= y2); /* while(y1 < y2) */
150 x1 += pw;
152 px = 0;
153 pw = PATTERNWIDTH;
155 } while (x1 <= x2); /* while (x1 < x2) */
159 static void InitBackfillHook(void)
161 backfillhook.h_Entry = HookEntry;
162 backfillhook.h_SubEntry = (HOOKFUNC)MyBackfillFunc;
166 static void GetVisual(void)
168 if (!(scr = LockPubScreen(0)))
170 Cleanup("Can't lock pub screen!");
173 if (!(dri = GetScreenDrawInfo(scr)))
175 Cleanup("Can't get drawinfo!");
180 static void MakePattern(void)
182 struct RastPort *temprp;
184 if (!(patternbm = AllocBitMap(PATTERNWIDTH * 2,
185 PATTERNHEIGHT * 2,
186 GetBitMapAttr(scr->RastPort.BitMap,BMA_DEPTH),
187 BMF_CLEAR,
188 scr->RastPort.BitMap)))
190 Cleanup("Can't create pattern bitmap!");
193 if (!(temprp = CreateRastPort()))
195 Cleanup("Can't create rastport!");
198 temprp->BitMap = patternbm;
200 SetAPen(temprp,dri->dri_Pens[PATTERNCOL1]);
202 RectFill(temprp,0,0,10,10);
204 RectFill(temprp,0,0,PATTERNWIDTH - 1,PATTERNHEIGHT - 1);
206 SetAPen(temprp,dri->dri_Pens[PATTERNCOL2]);
207 RectFill(temprp,0,
209 PATTERNWIDTH / 2 - 1,
210 PATTERNHEIGHT / 2 - 1);
212 RectFill(temprp,PATTERNWIDTH / 2,
213 PATTERNHEIGHT / 2,
214 PATTERNWIDTH - 1,
215 PATTERNHEIGHT - 1);
217 FreeRastPort(temprp);
221 static void MakeWin(void)
223 if (!(win = OpenWindowTags(0,WA_PubScreen,(IPTR)scr,
224 WA_Left,10,
225 WA_Top,10,
226 WA_Width,300,
227 WA_Height,150,
228 WA_Title,(IPTR)"Window Backfill Test",
229 WA_SimpleRefresh,TRUE,
230 WA_CloseGadget,TRUE,
231 WA_DepthGadget,TRUE,
232 WA_DragBar,TRUE,
233 WA_SizeGadget,TRUE,
234 WA_MinWidth,50,
235 WA_MinHeight,50,
236 WA_MaxWidth,scr->Width,
237 WA_MaxHeight,scr->Height,
238 WA_IDCMP,IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
239 WA_BackFill,(IPTR)&backfillhook,
240 TAG_DONE)))
242 Cleanup("Can't open window!");
245 ScreenToFront(win->WScreen);
250 static void HandleAll(void)
252 struct IntuiMessage *msg;
254 BOOL quitme = FALSE;
256 while (!quitme)
258 WaitPort(win->UserPort);
260 while ((msg = (struct IntuiMessage *)GetMsg(win->UserPort)))
262 switch (msg->Class)
264 case IDCMP_CLOSEWINDOW:
265 quitme = TRUE;
266 break;
268 case IDCMP_REFRESHWINDOW:
269 BeginRefresh(win);
270 EndRefresh(win,TRUE);
271 break;
274 ReplyMsg((struct Message *)msg);
280 int main(void)
282 OpenLibs();
283 InitBackfillHook();
284 GetVisual();
285 MakePattern();
286 MakeWin();
287 HandleAll();
288 Cleanup(0);
290 return 0;