[comp.sources.amiga] v89i173: dobbs-examples - from dr. dobbs july 89 article

page%swap@Sun.COM (Bob Page) (08/23/89)

Submitted-by: cmcmanis@Eng.Sun.COM (Chuck McManis)
Posting-number: Volume 89, Issue 173
Archive-name: graphics/dobbs-ex.1

These are the sources for the two examples in the July '89 Dr. Dobbs
article "Multitasking OS and Graphics Co-processors".

# This is a shell archive.
# Remove anything above and including the cut line.
# Then run the rest of the file through 'sh'.
# Unpacked files will be owned by you and have default permissions.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar: SHell ARchive
# Run the following text through 'sh' to create:
#	README
#	blit.c
#	dualvp.c
# This is archive 1 of a 1-part kit.
# This archive created: Tue Aug 22 23:55:07 1989
echo "extracting README"
sed 's/^X//' << \SHAR_EOF > README
XThese are the sources for the two examples in the Dr. Dobbs article
X"Multitasking OS and Graphics Co-processors". This article appears in 
Xthe July 1989 issue. 
X
X--Chuck McManis
Xuucp: {anywhere}!sun!cmcmanis   BIX: cmcmanis  ARPAnet: cmcmanis@sun.com
XThese opinions are my own and no one elses, but you knew that didn't you.
SHAR_EOF
echo "extracting blit.c"
sed 's/^X//' << \SHAR_EOF > blit.c
X/*
X *	blit.c - Demonstrates the benefit of the blitter.
X *
X *	Written 4/9/89 by C. McManis using Lattice C 5.02
X *	
X *	The difference on my machine between waiting for the blitter
X *	to complete before calculating the next set of draw parameters
X *	is 1.6 vs 1.4 seconds, about a 12.5% increase in speed.
X */
X
X#include <exec/types.h>
X#include <exec/memory.h>
X#include <intuition/intuition.h>
X#include <graphics/gfx.h>
X
Xextern struct IntuitionBase *IntuitionBase;
Xextern struct GfxBase	    *GfxBase;
X
X
Xstruct NewScreen NS = {
X		0, 0,		/* Position on the display 	     */
X		320, 200, 4,	/* Attributes (Width, Height, Depth) */
X		1,0,		/* Detail and Block pens	     */
X		0,		/* ViewModes nothing special	     */
X		CUSTOMSCREEN,	/* It is our own screen we want	     */
X		0,		/* Using the Default font	     */
X		"Blitter Test",	/* With a simple title.		     */
X		0,		/* No special gadgets		     */
X		0		/* And no special bitmap	     */
X	};
Xstruct Screen	*MyScreen;
Xstruct RastPort	*RPort;
X
Xvoid
Xcleanup(n)
X	int	n;
X{
X	if (GfxBase) 
X		CloseLibrary(GfxBase);
X
X	if (MyScreen)
X		CloseScreen(MyScreen);
X
X	if (IntuitionBase)
X		CloseLibrary(IntuitionBase);
X	exit(n);
X}
X	
Xvoid
Xmain()
X{
X
X
X	int	i,	 /* Loop counter */
X		x, y, c, /* some random draw parameters */
X		t0[2],	 /* Start Time */
X		t1[2];	 /* End time */
X
X	IntuitionBase = (struct IntuitionBase *)
X				OpenLibrary("intuition.library",0L);
X	if (! IntuitionBase) 
X		cleanup(1);
X
X	GfxBase = (struct GfxBase *)
X				OpenLibrary("graphics.library", 0L);
X	if (! GfxBase)
X		cleanup(1);
X
X
X	/* This does all of the view construction for us */
X
X	MyScreen = (struct Screen *) OpenScreen(&NS);
X	
X	if (!MyScreen) 
X		cleanup(1);
X
X	timer(t0);	/* Start the clock running */
X
X	/* Get the RastPort of this screen */
X	RPort = &(MyScreen->RastPort);
X
X	SetAPen(RPort, 1);	    /* Foreground pen = 1 */
X	SetBPen(RPort, 0);	    /* Background pen = 0 */
X
X	srand(42);		    /* set the seed */
X	Move(RPort, 160, 100); 	    /* Move to the moiddle of the screen */  
X
X	/*
X	 * Note we generate psuedo random numbers (eg the same set of 
X 	 * random numbers every time.
X	 */
X	for (i=0; i<100000; i++) {
X		x = (rand() % 300) + 10;
X		y = (rand() % 180) + 10;
X		c = rand() % 16;
X		SetAPen(RPort, c);
X		Draw(RPort, x, y);
X#ifdef WAIT_BLIT
X		WaitBlit();	/* Simulate non-coprocessor */
X#endif
X	}
X
X	timer(t1);	/* stop the clock */
X
X#ifdef WAIT_BLIT
X	printf("With waiting for the blitter, we took %d microseconds.\n",
X			(t1[0] - t0[0]) * 1000000 + (t1[1] - t0[1]));
X#else
X	printf("Without waiting for the blitter, we took %d microseconds.\n",
X			(t1[0] - t0[0]) * 1000000 + (t1[1] - t0[1]));
X#endif	    
X	    
X	cleanup(0);
X}
SHAR_EOF
echo "extracting dualvp.c"
sed 's/^X//' << \SHAR_EOF > dualvp.c
X/* 
X *	dualvp.c - Dual Viewports on the amiga
X *	Written 4/4/89 by C. McManis using Lattice C 5.02
X */
X
X#include <exec/types.h>
X#include <exec/memory.h>
X#include <graphics/gfx.h>
X#include <graphics/view.h>
X#include <graphics/gfxbase.h>
X#include <graphics/rastport.h>
X
Xextern struct GfxBase *GfxBase;
X
Xchar	*TextString = "Amiga Graphics Example";
X
X			/* Viewport 0 colors */
XUWORD			colors0[4] = {0xccc, 0x000, 0x0f0, 0x00f},
X			/* Viewport 1 colors */
X			colors1[4] = {0x0f0, 0xc0c, 0xf00, 0xfff};
Xvoid
X_main()
X{
X	struct View	MyView, *OldView;
X	struct ViewPort	Vp0, Vp1;
X	struct BitMap	Bits;
X	struct RasInfo	MyRaster;
X    	struct RastPort	rp;
X	int		i;
X
X	/* Open the resident graphics library */
X	GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L);
X	if (!GfxBase) 
X		exit(1);
X
X	OldView = GfxBase->ActiView; /* Save this away */
X
X	/* Initialize the View structures */
X	InitView(&MyView);
X	InitVPort(&Vp0);
X	InitVPort(&Vp1);
X
X	Vp1.Next = NULL;
X	Vp0.Next = &Vp1;	/* create a linked list of viewports */
X	MyView.ViewPort = &Vp0;	/* With the first one being Vp0 */
X
X	/* Set up some display memory */
X
X	InitBitMap(&Bits, 2, 640, 200); 
X	Bits.Planes[0] = (PLANEPTR)
X			 AllocMem(2*RASSIZE(640, 200),MEMF_CHIP+MEMF_CLEAR);
X	Bits.Planes[1] = Bits.Planes[0] + RASSIZE(640, 200);
X	if (!Bits.Planes[0]) 
X		goto cleanup;
X
X	MyRaster.BitMap = &Bits;
X	MyRaster.RxOffset = 0;
X	MyRaster.RyOffset = 0;
X	MyRaster.Next = NULL;
X
X	/* Both viewports are looking at the same display memory but have
X         * different sets of colors
X	 */
X
X	Vp0.RasInfo = &MyRaster;
X	Vp0.DWidth  = 320;
X	Vp0.DHeight = 175;
X	Vp0.ColorMap = (struct ColorMap *)GetColorMap(4);
X	LoadRGB4(&Vp0, colors0, 4);
X	
X	Vp1.RasInfo = &MyRaster;
X	Vp1.DWidth  = 640;
X	Vp1.DHeight = 20;
X	Vp1.DyOffset = 179;
X	Vp1.Modes   = HIRES;
X	Vp1.ColorMap = (struct ColorMap *)GetColorMap(4);
X	LoadRGB4(&Vp1, colors1, 4);
X
X	
X	/* Initialize a RastPort so that we can draw into that memory. */
X	InitRastPort(&rp);
X	rp.BitMap = &Bits;
X	SetAPen(&rp, 1);	/* Foreground color */
X	SetBPen(&rp, 0);	/* Background color */
X	Move(&rp, 3, 12);	/* Move the graphics cursor to (3, 12) */
X	/* Write something */
X	Text(&rp, TextString, strlen(TextString));
X	
X	MakeVPort(&MyView, &Vp0); /* Build the copper list for Viewport 0 */
X	MakeVPort(&MyView, &Vp1); /* Build the copper list for Viewport 1 */
X	MrgCop(&MyView); 	  /* Merge it into the final list */
X
X	LoadView(&MyView);	  /* Show it off */
X
X	/* SPIN FOR A WHILE */
X	for (i=0; i<1000000; i++)
X		;
X
X	LoadView(OldView); 	  /* Return to the old view */
X
X	
Xcleanup:
X	/* Now give back the memory other tasks may need it */
X
X	if (!Vp0.ColorMap)
X		FreeColorMap(Vp0.ColorMap);
X
X	if (!Vp1.ColorMap)
X		FreeColorMap(Vp1.ColorMap);
X
X	FreeVPortCopLists(&Vp0);
X	FreeVPortCopLists(&Vp1);
X	FreeCprList(MyView.LOFCprList);
X
X
X	if (!Bits.Planes[0]) 
X		FreeMem(Bits.Planes[0], 2*RASSIZE(640, 200));
X
X	if (!GfxBase) 
X		CloseLibrary(GfxBase);
X
X	exit(0);
X}
SHAR_EOF
echo "End of archive 1 (of 1)"
# if you want to concatenate archives, remove anything after this line
exit