[net.micro.amiga] Visual Memory

ewhac@well.UUCP (Leo 'Bols Ewhac' Schwab) (08/06/86)

[ Line eaters are people, too. ]

	This crock is a direct descendant of the "bigmap" program I posted
yesterday.  This one, however, is a little different.  This program does not
allocate any bitplanes at all.  It simply assigns the plane pointer to RAM
and then makes its display.  The practical upshot of this is that it allows
you to dynamically see what's happening in RAM, since this program sets up a
direct window into it.

	This is written for the MANX compiler; conversion to Lettuce should
be trivial (FLAME_ON | SMILEY_FACE_ON:  If you are still using Lettuce and
don't have MANX, you're an idiot.  I don't care if you got Lettuce for
"nothing."  After a few hacking sessions with MANX, you'll get used to the
16 bit int problem, and you'll be amazed at how much faster and easier
everything is.  Trust me.  FLAME_OFF SMILEY_FACE_OFF).  To make this
beastie, you say:

1> cc memview.c
1> ln memview.o -lc -o memview

	Edit according to your setup.

	To use the program, plug a joystick into port 2 and start the
program.  Tilt joystick down to move forward through memory, up to move
backward.  Tilt left for a lo-res screen, right for a hi-res screen.  Press
fire button to exit.

	At this point, I would like to ask someone/anyone at Amiga to
comment on this code.  While I'm certain that I'm not mashing any sacred
system variables, I have this funny feeling that I'm breaking a lot of rules
here, and that I've overlooked some big pitfall that's waiting to ensnare
me in a big way.  So could someone at Amiga say something about this?
Thanks.  BTW, so far I've had no trouble with it.

	Remember, this is only a parlor trick.  Have phun!

_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 ________                ___
           \            /___--__                Leo L. Schwab
  ___  ___ /\               ---##\              ihnp4!ptsfa!well!ewhac
      /   X  \_____    |  __ _---))                      ..or..
     /   /_\--    -----+==____\ // \  _         well ---\
___ (   o---+------------------O/   \/ \        dual ----> !unicom!ewhac
     \     /                ___ \_  (`o )       hplabs -/       ("AE-wack")
 ____ \___/                          \_/
              Recumbent Bikes:                  "Work FOR?  I don't work FOR
            The _O_n_l_y Way To Fly!                anybody!  I'm just having fun."
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-This is not a shell archive, but cutting here is a good idea.-_-_-_-_
/*  :ts=8 bk=0
 * memview:  A window into memory.
 * Jon would call this a parlor trick, too.
 *
 * Leo L. Schwab		8608.5
 */

/*  I shouldn't have to include graphics/copper.h myself  */
#include <exec/types.h>
#include <exec/memory.h>
#include <graphics/gfxbase.h>
#include <graphics/copper.h>
#include <graphics/view.h>
#include <graphics/rastport.h>
#include <devices/gameport.h>
#include <devices/inputevent.h>

#define REV		0L
#define DEPTH		1L
#define WIDTH		320L
#define	HEIGHT		200L
#define ever		(;;)

extern void	*OpenLibrary(), *AllocMem(), *GetColorMap(), *CreateStdIO(),
		*CreatePort();
extern long	OpenDevice(), DoIO();


struct GamePortTrigger	gpt = {
	GPTF_UPKEYS | GPTF_DOWNKEYS,
	0,
	1, 1
};
UWORD		colors[] = { 0, 0xfff };

struct View	v, *oldview;
struct ViewPort	vp;
struct ColorMap	*cm;
struct RasInfo	ri;
struct BitMap	*bm;
struct GfxBase	*GfxBase;
struct InputEvent	joyreport;
struct IOStdReq	*gameio;
struct MsgPort	*gameport;



main ()
{
	int inc = 40, lx;

	openstuff ();
	makescreen ();		/*  NOT Intuition call  */
	initjoystick ();

	SendIO (gameio);
	for ever {
		WaitIO (gameio);
		if (joyreport.ie_Code == IECODE_LBUTTON)
			/*  Fire button pressed; exit program  */
			break;

		if (joyreport.ie_X != lx) {
			lx = joyreport.ie_X;
			if (joyreport.ie_X < 0)
				inc = 40;
			else if (joyreport.ie_X > 0)
				inc = 80;
			if (lx)
				remakedisp (inc);
		}

		if (joyreport.ie_Y) {
			if (joyreport.ie_Y > 0)
				bm -> Planes[0] += inc;
			else if (joyreport.ie_Y < 0)
				bm -> Planes[0] -= inc;
			WaitTOF ();
			ScrollVPort (&vp);
		}
		SendIO (gameio);
	}
	closeeverything ();
}


openstuff ()
{
	long err;

	if (!(GfxBase = OpenLibrary ("graphics.library", REV)))
		die ("Art shop closed.\n");

	if (!(gameport = CreatePort (0L, 0L)))
		die ("Can't make msgport.\n");

	if (!(gameio = CreateStdIO (gameport)))
		die ("Can't make IO packet.\n");

	if (err = OpenDevice ("gameport.device", 1L, gameio, 0L))
		die ("Games closed.\n");

	if (!(bm = AllocMem ((long) sizeof (*bm), MEMF_CHIP | MEMF_CLEAR)))
		die ("Can't allocate BitMap.\n");
}

makescreen ()
{
	InitView (&v);
	InitVPort (&vp);
	InitBitMap (bm, DEPTH, WIDTH, HEIGHT);

	v.ViewPort = &vp;

	ri.BitMap = bm;
	ri.RxOffset = ri.RyOffset = ri.Next = NULL;

	vp.DWidth = WIDTH;
	vp.DHeight = HEIGHT;
	vp.RasInfo = &ri;
	vp.ColorMap = GetColorMap (2L);

	bm -> Planes[0] = NULL;    /*  Start looking at address 0  */

	MakeVPort (&v, &vp);
	MrgCop (&v);
	LoadRGB4 (&vp, colors, 2L);
	oldview = GfxBase -> ActiView;
	LoadView (&v);
}

closeeverything ()
{
	register int i;

	if (oldview) {
		LoadView (oldview);
		WaitTOF ();	/*  Make sure copper is using old view  */
		FreeVPortCopLists (&vp);
		FreeCprList (v.LOFCprList);
	}
	if (vp.ColorMap)
		FreeColorMap (vp.ColorMap);
	if (bm)
		FreeMem (bm, (long) sizeof (*bm));
	if (gameio) {
		if (gameio -> io_Device)
			CloseDevice (gameio);
		DeleteStdIO (gameio);
	}
	if (gameport)
		DeletePort (gameport);
	if (GfxBase)
		CloseLibrary (GfxBase);
}

die (str)
char *str;
{
	puts (str);
	closeeverything ();
	exit (100);
}

initjoystick ()
{
	UBYTE type = GPCT_RELJOYSTICK;

	gameio -> io_Command = GPD_SETCTYPE;
	gameio -> io_Length = 1;
	gameio -> io_Data = &type;
	if (DoIO (gameio))
		die ("Error in setting controller type.\n");

	gameio -> io_Command = GPD_SETTRIGGER;
	gameio -> io_Length = sizeof (gpt);
	gameio -> io_Data = &gpt;
	if (DoIO (gameio))
		die ("Error in setting trigger values.\n");

	gameio -> io_Command = GPD_READEVENT;
	gameio -> io_Length = sizeof (joyreport);
	gameio -> io_Data = &joyreport;
}

remakedisp (line)
int line;
{
	void *sav1, *sav2;

	LoadView (oldview);
	WaitTOF ();	/*  Make sure copper is using old view  */
	FreeVPortCopLists (&vp);
	FreeCprList (v.LOFCprList);
	sav1 = bm -> Planes[0];
	sav2 = vp.ColorMap;

	InitView (&v);
	InitVPort (&vp);

	v.ViewPort = &vp;

	vp.DHeight = HEIGHT;
	vp.RasInfo = &ri;
	vp.ColorMap = sav2;

	if (line == 80) {
		InitBitMap (bm, DEPTH, WIDTH+WIDTH, HEIGHT);
		vp.Modes |= HIRES;
		vp.DWidth = WIDTH + WIDTH;
	} else {
		InitBitMap (bm, DEPTH, WIDTH, HEIGHT);
		vp.Modes &= ~HIRES;
		vp.DWidth = WIDTH;
	}
	bm -> Planes[0] = sav1;

	MakeVPort (&v, &vp);
	MrgCop (&v);
	LoadRGB4 (&vp, colors, 2L);
	oldview = GfxBase -> ActiView;
	LoadView (&v);
}