/*

    ---
  < WBD >
    ---

    set workbench screen depth

    © 1990 by Oliver Wagner
	      Landsberge 5
	      4322 Sprockhövel
	      Tel.: 02339/7536

    Usage: WBD depth [1..4]
    CLI only!

    Make with Lettuce C V5.04

	lc -rr -v -mt -rr -csmuq wbd
	blink wbd.o

    No startup module necessary (Hi John °¡°)

*/

int main(char*);
int alloc(struct BitMap*,int);
void free(struct BitMap*,int);

#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <string.h>
#include <exec/memory.h>

#define print(s) Write(output,s,strlen(s))

int main(line)
char *line;
{
    struct DosBase *DOSBase=OldOpenLibrary("dos.library");
    struct IntuitionBase *IntuitionBase=OldOpenLibrary("intuition.library");
    struct Screen *scr=(struct Screen*)OpenWorkBench();
    struct BitMap *bm=&scr->BitMap;
    BPTR output=Output();
    int depth=*line-'0';

    if(depth<1||depth>4) {
	print("WBD: depth [1..4]\n");
	return(20);
    }
    if(depth<4) free(bm,4);
    if(depth<3) free(bm,3);
    if(depth<2) free(bm,2);
    if(depth>1) if(!alloc(bm,2)) {
	print("\x07WBD: No memory!\n");
	depth=1;
    }
    if(depth>2)  if(!alloc(bm,3)) {
	print("\x07WBD: No memory!\n");
	depth=2;
    }
    if(depth>3) if(!alloc(bm,4)) {
	print("\x07WBD: No memory!\n");
	depth=3;
    }
    bm->Depth=depth;
    RemakeDisplay();
    return(0);
}

int alloc(bm,p)
int p;
struct BitMap *bm;
{
    if(bm->Depth<p) {
	return((int)(bm->Planes[p-1]=AllocMem(bm->BytesPerRow*bm->Rows,MEMF_CHIP|MEMF_CLEAR)));
    }
    return(-1);
}

void free(bm,p)
int p;
struct BitMap *bm;
{
    if(bm->Depth>=p)
	FreeMem(bm->Planes[p-1],bm->BytesPerRow*bm->Rows);
}

