/*
**
**      Project     : 24BitFillDemo - this is the main file
**      Version     : 1.1
**      File        : 24BitFillDemo - Demonstration of using the Blitter in 24 Bit mode
**                                    Fills some coloured rectangles into the screen.
**                                    Can be aborted with keystroke or mouseclick.
**
**      Authors     : David Göhler, Uwe Röhm
**      Date   Init : 21. November 1992
**             Last : 28. April 1993
**      Company     : Village Tronic Marketing GmbH, Hannover, Germany
**
**      To compile with
**         DICE          type "make"
**         SAS/C V6.x    type "sc LINK OPT PARMS=REGS NOSTKCHK 24BitFillDemo.c vilintuisup.c"
**      For other compilers you may have to do minor changes.
**
**      This programm will only run, if the monitor file supplied
**      with your graphics card is installed and running.
**
**      THIS IS NOT PD. THIS IS COMMERCIAL SOFTWARE.
**
*/
#include "SysInclude.h"
#include "vilintuisup.h"
#include <stdlib.h>

BOOL Demo24Bit(struct Screen *s, struct Dimensions *dm, int additive,
               int loopcount, ULONG sigs);

static char Version[] = "$VER: 24BitFillDemo 1.1 (28.04.93)  (c) 1993 by Village Tronic Marketing GmbH";

extern struct Library *SysBase;
extern struct Library *DOSBase;
struct Library *VilIntuiBase;
struct Library *IntuitionBase;
struct Library *GfxBase;

main()
{
   struct Dimensions     dm;  /* Struktur mit den Dimensionen des Screens */
   struct NewWindow      nw;  /* Struktur für das IDCMP Fenster           */
   struct Screen        *s = NULL;
   struct Window        *w = NULL;

   /*
    *    Wir benötigen eine Reihe von Libraries.
    *    Wenn keine Picasso im System vorhanden ist, kann die vilintuisup
    *    Library nicht geöffnet werden!
    */
   if (VilIntuiBase = OpenLibrary("vilintuisup.library",0))
   {
     if (IntuitionBase = OpenLibrary("intuition.library",0))
     {
       if (GfxBase = OpenLibrary("graphics.library",0))
       {
         /*
          *    Initialisieren der Dimensionen für den Bildschirm
          */
         dm.width  = 640;    /* Breite in Pixeln (Maximum für 1MB Picasso)      */
         dm.height = 480;    /* Höhe   in Pixeln (Maximum für 1MB Picasso)      */
         dm.depth  = 24;     /* Tiefe in Bit - 24 steht für 2^24 Farben         */

         /*
          *    Den 24 Bit tiefen TrueColor Screen öffnen
          */
         if ( s = OpenVillageScreen(&dm) )
         {
           /*
            *    Die NewScreen-Struktur vorbereiten. Auf dem Bildschirm wird ein
            *    Backdrop Fenster geöffnet, um von der Intuition IDCMP Messages
            *    bekommen zu können (Hier im Beispiel: Tasten und Mausklicks).
            *    Das Fenster müßte nicht unbedingt mit BACKDROP und BORDERLESS
            *    geöffnet werden, da die Screenverwaltung unseres Intuition-
            *    Treibers das Zeichnen auf 15/16/24 Bit Screens über das OS
            *    verhindert. Nichtsdestotrotz ist es aber sauberer...
            */
           nw.LeftEdge  = 0; /*Das Fenster soll den ganzen Screen abdecken*/
           nw.TopEdge   = 0;
           nw.Width     = s->Width;
           nw.Height    = s->Height;
           nw.FirstGadget = NULL; /* keine Gadgets im Fenster */
           nw.Title     = NULL;
           nw.IDCMPFlags= IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS;
           nw.Flags     = WFLG_BACKDROP | WFLG_BORDERLESS | WFLG_RMBTRAP | WFLG_ACTIVATE;
           nw.Type      = CUSTOMSCREEN;
           nw.Screen    = s;

           if ( w = OpenWindow(&nw) )
           {
             struct Message *msg;
             ULONG           sigs = SIGBREAKF_CTRL_C | (1 << w->UserPort->mp_SigBit);

             /* Die Rechtecke bis zur ersten Eingabe zeichnen */
             Demo24Bit(s, &dm, 0, 1, sigs);
             /*
              *   Als vorsichtige Menschen den UserPort leeren und dann
              *   erst das Fenster schließen.
              */
             while ( msg = GetMsg(w->UserPort) )
               ReplyMsg( msg );
             CloseWindow(w);               /* Fenster wieder schließen    */

           }
           CloseVillageScreen(s);          /* und Screen wieder schließen */

         }
         else
         {  PutStr("Konnte VILLAGE-Screen 24 Bit nicht öffnen\n");
         }
         CloseLibrary(GfxBase);

       }
       CloseLibrary(IntuitionBase);

     }
     CloseLibrary(VilIntuiBase);

   }
   else
   {  PutStr("Konnte \"vilintuisup.library\" nicht öffnen\n");
   }

   return 0;
}


/*
 * Demo24Bit zeichnet ein paar Vierecke in Farbe
 */
BOOL Demo24Bit(struct Screen *s, struct Dimensions *dm, int additive, int loopcount, ULONG sigs)
{
   register WORD xpos = 0, ypos = 0;
   UBYTE *memstart;
   struct VilFillRecord vilfill =
   {  0,
      1024,  /* Width of destination display (480 * 3 = 1024!) */
      256,   /* Width of rectangle box                         */
      256,   /* Height of rectangle box                        */
      0
   };

   /*
    *    Reine Sicherheitsabfrage.
    *    640 * 480 * 3 ~= 1 MB Speicher...
    */
   if ((dm->depth != 24) || (dm->width != 640) || (dm->height != 480))
   {  PutStr("Not a 24 Bit Videomode\n");
      return 0;
   }

   /*******************************************/
   /* This is important for the blitter use ! */
   /*******************************************/
   Forbid();            /* screen must be in displaymem !!! */
   ScreenToFront(s);
   memstart = LockVillageScreen(s);
   Permit();

   while (CheckSignal(sigs) == 0)
   {
     xpos   = rand() % s->Width;
     ypos   = rand() % s->Height;

     /*
      *     Der Blitter arbeitet mit BYTES, keinen Pixeln!
      *     Da in True Color jeder Pixel durch 3 Byte repräsentiert
      *     wird, muß die Breite entsprechend angegeben werden!
      */
     vilfill.DestAdr   = (APTR)(memstart +(xpos*3 + (ypos * s->Width*3)));
     vilfill.DestPitch = s->Width;
     vilfill.Width     = rand() % (s->Width-xpos);
     vilfill.Height    = rand() % (s->Height-ypos);
     vilfill.Color     = rand() % 16777216;

     VillageRectFill(s,&vilfill); /* does it's own WaitVillageBlit() inside */

   } /* while !CheckSignal() */

   UnLockVillageScreen(s);

   return TRUE;
}



