;/*
gcc -noixemul -Wall -O2 truecolortest.c -o truecolortest
quit
*/

/*
 * Quick'n'dirty example on how to use MorphOS truecolor rendering
 *
 * Written by Harry "Piru" Sintonen <sintonen@iki.fi>.
 * Public Domain.
 *
 */

#include <graphics/rastport.h>
#include <graphics/rpattr.h>
#include <graphics/modeid.h>
#include <intuition/screens.h>

#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/dos.h>

#define MKRGB(r,g,b) (((r) << 16) | ((g) << 8) | (b))

#define WIDTH   640
#define HEIGHT  480
#define DEPTH   24

int main(void)
{
  struct Screen *scr;
  ULONG modeid;
  int ret = RETURN_ERROR;

  modeid = BestModeID(BIDTAG_NominalWidth,  WIDTH,
                      BIDTAG_NominalHeight, HEIGHT,
                      BIDTAG_Depth,         DEPTH,
                      TAG_DONE);

  if (modeid == INVALID_ID)
  {
    Printf("could not find modeid for %lux%lux%lu mode\n",
           WIDTH, HEIGHT, DEPTH);

    return RETURN_WARN;
  }

  scr = OpenScreenTags(NULL,
                       SA_DisplayID, modeid,
                       SA_Width,     WIDTH,
                       SA_Height,    HEIGHT,
                       SA_Depth,     DEPTH,
                       SA_Quiet,     TRUE,
                       TAG_DONE);
  if (scr)
  {
    struct BitMap *cookiebm;

    cookiebm = AllocBitMap(WIDTH, HEIGHT, 1, 0, NULL);
    if (cookiebm)
    {
      struct RastPort tmprp, *cookierp = &tmprp;
      struct RastPort *rp = &scr->RastPort;
      STRPTR txt;
      WORD len;
      LONG xmid = WIDTH / 2;
      LONG ymid = HEIGHT / 2;

      /* Init cookie rastport */
      InitRastPort(cookierp);
      cookierp->BitMap = cookiebm;
      SetABPenDrMd(cookierp, 1, 0, JAM1);

      /* Set screen background */
      SetRPAttrs(rp, RPTAG_PenMode, FALSE,
                     RPTAG_FgColor, MKRGB(0xaa, 0xbb, 0xcc),
                     TAG_DONE);
      RectFill(rp, 0, 0, WIDTH - 1, HEIGHT - 1);


      /* Draw filled ellipse to cookiebm */
      SetRast(cookierp, 0);
      DrawEllipse(cookierp, xmid, ymid, 200, 200);
      Flood(cookierp, 0, xmid, ymid);

      /* Blast the cookie cut image to display */
      SetRPAttrs(rp, RPTAG_DrMd, JAM1,
                     RPTAG_PenMode, FALSE,
                     RPTAG_FgColor, MKRGB(222, 100, 70),
                     TAG_DONE);
      BltTemplate(cookiebm->Planes[0], 0,
                  GetBitMapAttr(cookiebm, BMA_WIDTH) / 8,
                  rp, 0, 0, WIDTH, HEIGHT);


      /* Draw filled box */
      SetRPAttrs(rp, RPTAG_PenMode, FALSE,
                     RPTAG_FgColor, MKRGB(40, 70, 90),
                     TAG_DONE);
      RectFill(rp, xmid - 100, ymid - 100, xmid + 100, ymid + 100);


      /* Put some text on screen, too */
      SetRPAttrs(rp, RPTAG_DrMd, JAM1,
                     RPTAG_PenMode, FALSE,
                     RPTAG_FgColor, MKRGB(255, 244, 244),
                     TAG_DONE);
      txt = "MorphOS rules!";
      len = strlen(txt);
      Move(rp, xmid - TextLength(rp, txt, len) / 2, ymid);
      Text(rp, txt, len);

      /* Wait a bit */
      Delay(5 * 50);

      ret = RETURN_OK;

      FreeBitMap(cookiebm);
    }
    else Printf("no memory for %lux%lux1 cookie bitmap\n",
                 WIDTH, HEIGHT);

    CloseScreen(scr);
  }
  else Printf("could not open %lux%lux%lu screen\n",
              WIDTH, HEIGHT, DEPTH);

  return ret;
}