
/*
**
**  Opticks frame buffer PORT interface example.
**  CopyRight 1989 (c) by Brian Reed
**
**  This is a sample program to gain access to the MESSAGE PORTS
**  in Opticks.  This message port signals the end of each rendered line,
**  for optional drawing to a frame buffer, a line at a time.
**
*/

/*  MANX make's
 ----->  cc +l -s p.c
 ----->  ln p.o m32.lib c32.lib
*/


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

/*  This is the OPTICKS end-of-line MESSAGE struct.  */
/*    1)  If scr OR wnd = 0 then NO HAM screen open  */
/*    2)  If pixel_count = 0 then END of image  */
struct Pixel_v1 {
  struct Message message;  /*  Intuition Message                         */
  struct Screen  *scr;     /*  Intuition screen pointer if open          */
  struct Window  *wnd;     /*  Intuition screen pointer if open          */
  FLOAT size_x;            /*  Real size of pixel plane in X             */
  FLOAT size_y;            /*    and in Y                                */
  ULONG page_size_x;       /*  Full size of image in X                   */
  ULONG page_size_y;       /*    and in Y                                */
  ULONG offset_x;          /*  Offset of image data in 'PAGE_size' in X  */
  ULONG offset_y;          /*    and in Y                                */
  ULONG pixel_bit_count;   /*  Bits per pixel (24)                       */
  ULONG pixel_count;       /*  This many across (from offset_x)          */
  ULONG resolution_mult;   /*  Do this many line repetitions             */
  ULONG reserved1;         /*  future use                                */
  ULONG reserved2;         /*  future use                                */
  TEXT  *name;             /*  Pointer to ASCII .scene name              */
  UBYTE *pixel_line;       /*  Pointer to pixel data                     */
};

struct MsgPort *mp, *WaitPort(), *GetMsg();


main()
{
struct Message *msg=0;

  /*  Make a 'PORT'.  */
  if ( (mp=CreatePort("OPTICKS_Pixels_v1", 0)) == NULL)
    done("Couldn't open port");

  printf("Port Waiting.....\n");

  while (1) {   /*  FOREVER  */

    /*  Wait for a message.  */
    msg = WaitPort(mp);
    msg = GetMsg(mp);

    /*  This is the only way out!  */
    /*  Send in msg->pixel_count = 0  */
    if (msg->pixel_count == 0) {
      /*  Let 'R' program know we are done with message. OK to continue.  */
      ReplyMsg(msg);
      done("Clean exit!");
    }



    /*                                                             */
    /*  PUT YOUR OWN CODE HERE !!!!                                */
    /*  Message data extracted here !                              */
    /*                                                             */
    /*  The RGB data is in the UBYTE array at msg->pixel_line.     */
    /*  It is in the format, 8 bits Blue, 8 Green, 8 Red...        */
    /*  Yes, they are in an unusual order.  Values may range from  */
    /*  0 to 255 each color.                                       */
    /*                                                             */
      printf("** OPTICKS_Pixels_v1 MSG **   (RECEIVED...)\n");
      printf("Page size %u %u -*- Offset %d %d\n",
        msg->page_size_x, msg->page_size_y, msg->offset_x, msg->offset_y);
      printf("Pixel plane size %f %f\n", msg->size_x, msg->size_y);
      printf("Pixel_bit_count = %2d  pixel_count = %d  res_mult = %d\n",
        msg->pixel_bit_count, msg->pixel_count, msg->resolution_mult);
      if (msg->wnd != NULL)
        printf("Screen pointer %d  Window pointer %d\n", msg->scr, msg->wnd);



    /*  Let 'R' program know we are done with message. OK to continue.  */
    ReplyMsg(msg);
  }
}

done(s)
char *s;
{
struct Message *msg=0;

  if (mp) {
    while (msg = GetMsg(mp))  /*  Eat up extra's for clean exit!  */
      ReplyMsg(msg);
    RemPort(mp);              /* Remove PORT.  */
  }
  printf("OPTICKS_DEMO_PORT: %s\n", s);
  exit(0);
}

