[comp.sys.amiga.programmer] Drawing images in intuition. HELP!

morgan@cory.Berkeley.EDU (Alan Morgan) (01/20/91)

I need help.  I am trying to learn Amiga programming through
Inside the Amiga with C (Waite Group) and

..short aside..

This is a terrible book.  Do not buy it.  NONE of the programs work
as written.  Most of the errors are pretty stupid and easily fixed
for someone who knows C, some are not.  Why am I still using it?
My local bookstores don't carry any others and I need a little more
handholding than the RKM's offer.

..aside off..

I cannot get this program to work, however, simple though it is.  Could
some kind soul tell me (email please, I am sure most other people on the
net know the answer) why the borderless window pops up but nothing gets 
drawn.

#include <exec/types.h>
#include <intuition/intuition.h>

struct Window *NoBorder;
struct RastPort *r;

USHORT imagepts[]={	0xffff,0x4ffe,0x3ffc,0x1ff8,
			0x0ff0,0x17e0,0x03c0,0x03c0,
			0x03c0,0x03c3,0x07f0,0x1fc0,
			0x1ff8,0x3ffc,0x4ffe,0xffff };

struct Image picture = { 0,0,16,16,3,NULL,0x0001,0x0000,NULL};
			       /*  ^ I also tried 1 here */

main()
{
	ULONG flags;
	SHORT x,y,w,h;
	VOID OpenAll();
	int i;

	OpenAll();   /* Opens up intuition for me */

	x=y=0;
	w=640;
	h=200;
	flags=ACTIVATE|SMART_REFRESH|BORDERLESS;

	NoBorder = (struct Window *)
		makeWindow(x,y,w,h,NULL,flags,NULL,-0x01,-0x01,NULL);

		/* Creates a window with several boring characteristics */

	picture.ImageData=imagepts;

	r=NoBorder->RPort;

	DrawImage(r,&picture,10,10);

	for(i=0;i<500000;i++);

	CloseWindow(NoBorder);
}

This is basically what is in the book, nothing of substance has been changed.

I suppose I should add that the program compiles without error.

Thanks in advance for erasing my ignorance (or at least the part of it
extending to images).

Alan Morgan
morgan@cory.berkeley.edu

n8643084@unicorn.cc.wwu.edu (owings matthew) (01/20/91)

In article <10330@pasteur.Berkeley.EDU> morgan@cory.Berkeley.EDU (Alan Morgan) writes:
>

>#include <exec/types.h>
>#include <intuition/intuition.h>
>
>struct Window *NoBorder;
>struct RastPort *r;
>
>USHORT imagepts[]={	0xffff,0x4ffe,0x3ffc,0x1ff8,
>			0x0ff0,0x17e0,0x03c0,0x03c0,
>			0x03c0,0x03c3,0x07f0,0x1fc0,
>			0x1ff8,0x3ffc,0x4ffe,0xffff };
>
>struct Image picture = { 0,0,16,16,3,NULL,0x0001,0x0000,NULL};
>			       /*  ^ I also tried 1 here */
>
>main()
>{
>	ULONG flags;
>	SHORT x,y,w,h;
>	VOID OpenAll();
>	int i;
>
>	OpenAll();   /* Opens up intuition for me */
>
>	x=y=0;
>	w=640;
>	h=200;
>	flags=ACTIVATE|SMART_REFRESH|BORDERLESS;
>
>	NoBorder = (struct Window *)
>		makeWindow(x,y,w,h,NULL,flags,NULL,-0x01,-0x01,NULL);
>
>		/* Creates a window with several boring characteristics */
>
>	picture.ImageData=imagepts;
>
>	r=NoBorder->RPort;
>
>	DrawImage(r,&picture,10,10);
	If you are using Manx 3.6a or earlier, you will have to cast constants
in workbench calls to long.  Try this
	DrawImage ((struct RastPort *)r,(struct Image *image)&picture,10L,10L);

	This book assumes you are using lattice which defaults to long ints.

	You should also include the header file functions.h.  It has all the
functions declared and the types they return and this will make it easier when
using assignment statements (no more ptr to int problems).

	Hope this helps.
>
>	for(i=0;i<500000;i++);
>
>	CloseWindow(NoBorder);
>}

aliu@aludra.usc.edu (Alex C. Liu) (01/20/91)

In article <10330@pasteur.Berkeley.EDU> morgan@cory.Berkeley.EDU (Alan Morgan) writes:
>
>USHORT imagepts[]={	0xffff,0x4ffe,0x3ffc,0x1ff8,
>			0x0ff0,0x17e0,0x03c0,0x03c0,
>			0x03c0,0x03c3,0x07f0,0x1fc0,
>			0x1ff8,0x3ffc,0x4ffe,0xffff };
>
Something simmilar happened to me ( I am just a novice programmer, you
know) and the way to solve it was to use the _chip modifier in
lattice.  I am not sure what to do with Aztec.  But basicly, in order
to be able to draw the image, you must have that image store in Chip
Mem.  So you either use a compiler trick, use FixHunk or make a
routine to AllocMem to chip and copy the data to chip.  (I did the
later)

ken@cbmvax.commodore.com (Ken Farinsky - CATS) (01/24/91)

In article <10330@pasteur.Berkeley.EDU> morgan@cory.Berkeley.EDU (Alan Morgan) writes:
>
>USHORT imagepts[]={ /* stuff */ };
>struct Image picture = { 0,0,16,16,3,imagepts,1,0,NULL};
>
>main()
>{
>struct Window *NoBorder;
>
>OpenAll();   /* Opens up intuition for me */
>NoBorder = (struct Window *)makeWindow( /* window parameters */ );
>DrawImage(NoBorder->RPort,&picture,10,10);
>for(i=0;i<500000;i++);
>CloseWindow(NoBorder);
>}

Sorry about hacking your program to death, here are some comments:

Your image data must be in chip memory.  You can do this with AllocMem()
then copy the data to the new space.  Don't forget to FreeMem the memory
when you are done.  And to check for successful allocation.

You have planepick for the image set to 1, and planeonoff set to 0.
You probably want planepick to be 7 (binary 0111) to tell the system
to put data in the lower 3 bitplanes.  PlaneOnOff of 0 is fine, this
tells the system not to change any of the unused bitplanes.

You have only supplied 1 bitplane worth of data, but the code specifies
3 bitplanes.  (You define the image as 16 wide, 16 high and 3 deep.)

Don't busy wait, not even in example code!  You can replace the
"for(i=0;i<500000;i++);" with something like "Delay(100);"

Don't forget to close Intuition.  You opened it in OpenAll(), but
then never close it.

You open intuition and a window without checking for success.  If
these fail, your program will blow up.  Error check with code like:

	if (NULL == (win = OpenWindow(...)))
		{
		/* error reporting/handling code goes here */
		}
	else
		{
		/* do stuff */
		CloseWindow(win);
		}
-- 
--
Ken Farinsky - CATS - (215) 431-9421 - Commodore Business Machines
uucp: ken@cbmvax.commodore.com   or  ...{uunet,rutgers}!cbmvax!ken
bix:  kfarinsky