aboutsummaryrefslogtreecommitdiff
path: root/DPINIT.C
blob: b36912acc6c99f4cd5523f98b67359bb0b0ed441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*----------------------------------------------------------------------*/
/*									*/
/*		dpinit.c -- initialization code				*/
/*									*/
/*----------------------------------------------------------------------*/

#include <system.h>
#include <prism.h>
#include <librarie\dosexten.h>
#include <dphook.h>
#include <workbenc\startup.h>
	
#define CLOSEWBNCH

#define local static
#define MaxWidth	640
#define MaxHeight	400

extern BOOL modeHelp;
extern void mainRefresh();
extern struct Menu *MainMenu; 
extern BMOB curpenob,curbr,origBr;
extern void mainCproc(),mainPproc(),mainMproc();
extern long GfxBase;
extern long IntuitionBase;
extern long IconBase;

struct Window *OpenWindow();
struct InputEvent *Intuition();
struct Screen *OpenScreen();

extern struct Window *mainW;
extern struct Screen *screen;
extern struct RastPort *mainRP;
extern struct ViewPort *vport;
extern BOOL haveWBench;
extern BOOL underWBench;
extern Box mainBox;
extern Pane mainP;
extern SHORT curFormat; 		/* current screen format */
extern SHORT curDepth;
extern SHORT curMBarH;
extern SHORT xShft,yShft;
extern SHORT nColors;
    
extern SHORT prevColors[32];
extern UBYTE initBGCol[], initFGCol[];
extern Box screenBox;
extern struct BitMap hidbm;
extern struct TmpRas tmpRas;
extern struct RastPort tempRP;
extern struct RastPort screenRP;

extern UBYTE picname[];
extern struct TextAttr TestFont;
extern SHORT fmt,deep;
extern struct Process *myProcess;
extern struct Window *svWindowPtr;
extern UBYTE dpaintTitle[];

local struct NewScreen ns = {
    0, 0, 		/* start position 		*/
    640, 200, 3,	/* width, height, depth 	*/
    0, 1,		/* detail pen, block pen 	*/
    HIRES,		/* viewing mode 		*/
    CUSTOMSCREEN,	/* screen type 			*/
    &TestFont,		/* font to use 			*/
    dpaintTitle,	/* default title for screen 	*/
    NULL 		/* pointer to additional gadgets */
    };

local struct NewWindow nw = {0};

TryOpenLib(name) char *name; {
    LONG libadr = OpenLibrary(name, 0);
    if (libadr == NULL) { InfoMessage("Can't open library",name); exit(); }
    return(libadr);
    }

CloseWB() {
    struct Window *tw,*dosWin;
    if (!underWBench) {
	nw.Type = WBENCHSCREEN;
	nw.Width = nw.Height = 1;
	tw = OpenWindow(&nw);
	dosWin = tw->Parent;
	CloseWindow(tw);
	CloseWindow(dosWin);
	}
    if (CloseWorkBench()) haveWBench = NO;
    else InfoMessage(" Couldn't close", "WorkBench");
    }

BailOut() { 
    myProcess->pr_WindowPtr = (APTR)svWindowPtr;
    OpenWorkBench(); 
    CloseLibrary(GfxBase);
    CloseLibrary(IntuitionBase);
    exit(); 
    }

extern MagContext magCntxt;
extern Pane magP;
extern void magRefresh();
extern void RecChange();

InitMag() {
    magCntxt.srcRP = mainRP;
    magCntxt.srcBox = &mainP.box;
    magCntxt.magBM = mainRP->BitMap;
    magCntxt.magBox = &magP.box;
    magCntxt.updtProc = &RecChange;
    magCntxt.srcPos.x = 0;
    magCntxt.srcPos.y = curMBarH;
    magCntxt.magN = PMapX(10);
    PaneCreate(&magP, &screenBox ,HANGON, mainCproc, mainMproc, magRefresh, MAGWIN);
    SetMagContext(&magCntxt);
    MagState(NO);
    }

struct TextFont *screenFont= NULL;
        
InitScreenFont() {
    screenFont = (struct TextFont *)OpenFont(&TestFont);
    SetFont(&screenRP, screenFont);
    }

/*------initialize current brush ----------------*/
extern struct BitMap brSVBM,brBM,penSVBM,penBM;
extern BMOB curbr,curpenob;

/** init default picture path **/
extern UBYTE picpath[], *picpths[];
local UBYTE *picpths[3] = {"lo-res","med-res","hi-res"};
InitPicPath() {strcpy(picpath,picpths[curFormat]); }

extern struct BitMap *clientBitMap;
extern DPHook *dphook;
extern BOOL myDPHook;
extern UBYTE DPName[];
extern BOOL largeMemory, loadAFile;

#ifdef DOWB
extern struct WBStartup *WBenchMsg;
#endif
#define HALFMEG (1<<19)
    
DPInit(argc,argv) int argc; char *argv[]; {
    SHORT fmt,deep;
    SHORT initbg,initfg;
    BOOL closeWB= NO,doOverlay = NO, keepWB = NO;
    LONG avMem;
    
    FreeUpMem(); /* free up any memory with 0 ref count */
    
    avMem = AvailMem(MEMF_PUBLIC);
    largeMemory =  (avMem>(256*1024));
    
    GfxBase = TryOpenLib("graphics.library");
    IntuitionBase = TryOpenLib("intuition.library");
    IconBase = TryOpenLib("icon.library");

    myProcess = (struct Process *)FindTask(0);
    svWindowPtr = (struct Window *)myProcess->pr_WindowPtr;

    fmt = 0; deep = 5;		/* default to LORES, 5 planes */

#ifdef DOWB
    /* --- Test if we were called from the CLI or WORKBENCH --- */
    if (argc==0) {
	underWBench = YES;
	if (WBenchMsg->sm_NumArgs>0) {
	    strcpy(picname,WBenchMsg->sm_ArgList->wa_Name);
	    loadAFile = YES;
	    }                       
	}
    else { underWBench = NO; 	}
#endif    
    
    /* -----  See if Deluxe Video or anybody has planted hook */

    dphook = (DPHook *)FindHook(DPName);
    
    if ((dphook!=NULL)/*&&largeMemory*/) {
	fmt = dphook->format;
	clientBitMap = dphook->bitmap;
	if (clientBitMap != NULL) {
	    hidbm = *clientBitMap; /* copy record, not pointer */
	    deep = MIN(hidbm.Depth,5);
	    doOverlay = YES;
	    }
	else InfoMessage(" Null Client"," Bitmap");
	}
    else {
	if (argc>1)
	    switch(*argv[1]) {
		case 'l': fmt = 0; break;
		case 'm': fmt = 1; break;
		case 'h': fmt = 2; break;
		}
    
	if (argc>2) deep = MAX(MIN((*argv[2]-'0'),5),1);

	if (argc>3)  switch (*argv[3]) {
	    case 'c': closeWB = YES; break;
	    case 'o': doOverlay = YES; break;
	    case 'n': keepWB = YES; break;
	    default: doOverlay = closeWB = YES; break;
	    }
		
	if (!largeMemory) { /* 256K machine */
	    if (fmt==2) BailOut(); /* no way */
	    else closeWB = doOverlay = YES;
	    }

	else if ((avMem<HALFMEG)&&(fmt==2)&&(deep>3)) closeWB = doOverlay = YES;
    
	}

    if (closeWB&&(!keepWB)) CloseWB();

    SetOverlay(doOverlay);
    
    SetFormat(fmt,deep); /* opens Screen, mainW, allocs hidbm, tmpRas */

    
    if (dphook==NULL) { /* --- plant my own hook --- */
	myDPHook = YES;
	dphook = (DPHook *)DAlloc(sizeof(DPHook),MEMF_PUBLIC);
	dphook->bitmap = &hidbm;
	dphook->dpScreenBitMap = mainRP->BitMap;
	dphook->format = fmt;
	dphook->refCount = 0;
	dphook->version = 1;
	SetHook(DPName,dphook);
	}
    else {
	myDPHook = NO;
	dphook->refCount++;
	}
    
    dphook->vport = vport;
    dphook->brush = &curbr;
    
    InitMenu(); 
    
    BasicBandW();

    SetMenuStrip(mainW, MainMenu);

    InitRastPort(&tempRP);	/* multi-purpose RasterPort */
    InitRastPort(&screenRP);	/* always points at bare screen bitmap */
    screenRP.BitMap = mainRP->BitMap;
   
    InitScreenFont();
 
    initbg = initBGCol[curDepth];
    initfg = initFGCol[curDepth];
    SetRast(mainRP,initbg);
    tempRP.BitMap = &hidbm;

    if (clientBitMap==NULL) SetRast(&tempRP,initbg);

    PaneCreate(&mainP,&mainBox,HANGON,mainCproc,mainMproc,
		mainRefresh,MAINWIN);
    PaneInstall(&mainP);

    InitPGraph();
    PushGrDest(mainRP,&mainBox);
    SolidPattern();

    CPInit();	
    InitMag();

    CPChgPen(0);
    CPChgCol(initfg);
    CPChgBg(initbg);

    SymSet(6, YES, PMapX(MaxWidth/2), PMapY(MaxHeight/2));

    TogBoth();	/* show the control panel initially */
    ResetCursor();
    SetModeMenu(modeHelp ? Color: Mask);
    DispPntMode();
	}

local USHORT fmtMode[3] = {0,HIRES,HIRES|LACE};
local SHORT fmtXShft[3] = {1,0,0};
local SHORT fmtYShft[3] = {1,1,0};

#ifdef ldkfg
CloseDisp() {        
    ShowTitle(screen,NO);
    ClearMenuStrip(mainW);
    CloseFont(screenFont);
    KillCCyc();
    FreeSpare();
    BMOBFreeTmp();
    FreeBMOB(&origBr);
    FreeBMOB(&curbr);
    FreeBMOB(&curpenob);
    FreeBitMap(&hidbm);
    FreeTmpRas();
    myProcess->pr_WindowPtr = (APTR)svWindowPtr;
    CloseWindow(mainW);
    CloseScreen(screen); 
    }

ShutDown() {
    FreeFonts();
    CloseLibrary(GfxBase);
    CloseLibrary(IntuitionBase);
    OpenWorkBench();
    }

#endif
    
CloseDisplay() {
    ShowTitle(screen,NO);
    ClearMenuStrip(mainW);
    FreeFonts();
    KillCCyc();
    FreeSpare();
    BMOBFreeTmp();
    FreeBMOB(&origBr);
    FreeBMOB(&curbr);
    FreeBMOB(&curpenob);
    
    if (clientBitMap == NULL) FreeBitMap(&hidbm);
    if (myDPHook){ RemHook(DPName);DFree(dphook);}
    else dphook->refCount--;
    
    FreeTmpRas();
    myProcess->pr_WindowPtr = (APTR)svWindowPtr;
    CloseWindow(mainW);
    CloseScreen(screen); 
    CloseLibrary(GfxBase);
    CloseLibrary(IntuitionBase);
    OpenWorkBench();
    }

/*----------------------------------------------------------------------*/
/*									*/
/*  Memory requirement							*/
/*
    2*depth*pagesize	for visible display and hidbm
    pagesize		for tmpRas
    (depth*pagesize)/WorkSpaceFactor	for brush (minimal)
    MINSPACE		spare for overlays,menus, etc, fixed size

    total = depth*(2*pagesize + pagesize/WorkSpaceFactor) + MINSPACE + pagesize;
    max depth = (memavail-pagesize-MINSPACE)/(2*pagesize + pagesize/WorkSpaceFactor);

	 		    (mem-pagesize-MINSPACE)
	=	-----------------------------------------
	 	  (2*pagesize + pagesize/WorkSpaceFactor)
------------------------------------------------------------------------*/

/* require 1/3 of screen worth of workspace */
#define WorkSpaceFactor 3

/* minimum additional space of 12k */
#define MINSPACE 12*1024

/*----assumes screenBox already setup */
int MaxDepth(fmt) SHORT fmt; {
    ULONG psz = PlaneSize(screenBox.w,screenBox.h);
    ULONG mem = AvailMem(MEMF_CHIP|MEMF_PUBLIC);
    return((int) ( (mem-psz-MINSPACE)/(2*psz + psz/WorkSpaceFactor)) );
    }
 
SetFormat(fmt,depth) 
    SHORT fmt;	  /* 0 => 320x200xn, 1 => 640x200xn, 2 => 640x400xn*/
    SHORT depth;  /* number of planes 	*/ 
    {
    if (fmt>0) depth = MIN(depth,4);
    ns.ViewModes = fmtMode[fmt];
    curFormat = fmt;
    xShft = fmtXShft[fmt];
    yShft = fmtYShft[fmt];
    screenBox.w = ns.Width = PMapX(MaxWidth);
    screenBox.h = ns.Height = PMapY(MaxHeight);
    curMBarH =  /*(curFormat==2)? 19:*/ 11;
    mainBox.x = 0;
    mainBox.y = curMBarH;
    mainBox.w = screenBox.w;
    mainBox.h = screenBox.h - mainBox.y;

    depth = MIN(MaxDepth(fmt),depth);
    if (depth==0) BailOut(); 
    
    ns.Depth = curDepth = depth;
    
    screen = OpenScreen(&ns); 
    if (screen == NULL)  BailOut();
    
    nw.Screen = screen;	
    nw.DetailPen = 0;
    nw.BlockPen = 1;

#ifdef THEIRWAY
    nw.LeftEdge = screenBox.x;
    nw.TopEdge = screenBox.y + curMBarH ;
    nw.Width = screenBox.w;
    nw.Height = screenBox.h - curMBarH;
#else
    nw.LeftEdge = screenBox.x;
    nw.TopEdge = screenBox.y;
    nw.Width = screenBox.w;
    nw.Height = screenBox.h;
#endif

    nw.IDCMPFlags = RAWKEY |MOUSEBUTTONS |MENUPICK |
	MENUVERIFY | REFRESHWINDOW| ACTIVEWINDOW | INACTIVEWINDOW;
    nw.Flags = ACTIVATE |BACKDROP |BORDERLESS |REPORTMOUSE|
	/* NOCAREREFRESH |*/  SIMPLE_REFRESH | RMBTRAP; 
    nw.Type = CUSTOMSCREEN;	/* type of screen in which to open */	
    mainW = OpenWindow(&nw);	/* open a window */

    if (mainW == NULL) BailOut();

    /* fix so Dos message requestors come up in my window */
    myProcess->pr_WindowPtr = (APTR)mainW; 
    
    mainRP = mainW->RPort;
    vport = &mainW->WScreen->ViewPort;
    
    nColors = 1<<depth;

    /* initialize hidden raster */
    if (clientBitMap==NULL) NewSizeBitMap(&hidbm,depth,screenBox.w,screenBox.h); 
    AllocTmpRas();
    mainRP->TmpRas = &tmpRas;	/* temp raster for area filling*/
    InitPicPath();
    DefaultPalette();
    }