/*

                                   Copper.c

                Dimostrazione utilizzo Copper dal linguaggio C

*/



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

#include <intuition/intuitionbase.h>

#include <graphics/gfxmacros.h>
#include <graphics/copper.h>
#include <hardware/custom.h>

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

struct NewScreen CopperScreen =
       {
	0,0,
	640,256,
	4,
	0,1,
	HIRES,
	CUSTOMSCREEN,
	NULL,
	"640x256 con piu` di 16 colori!",
	NULL,
	NULL
       };

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *Screen;
struct RastPort *RP;
struct ViewPort *VP;

extern struct Custom far custom;

void gameover();

void _main ()
{
struct UCopList *CopperList;
short ypos=0;
short coppcolor=0;

/*

 /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
(    Apertura librerie e impostazioni schermo, RastPort, ViewPort e colori   )
 \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

*/

IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",NULL);
if (!IntuitionBase) gameover();

GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",NULL);
if (!GfxBase) gameover();

if (!(Screen = (struct Screen *) OpenScreen(&CopperScreen)))  gameover();

RP = &Screen -> RastPort;
VP = &Screen -> ViewPort;

SetRGB4(VP,0,0,0,0);

/*

 /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
(                            Creazione CopperList                            )
 \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

*/

CopperList = (struct UCopList *)AllocMem(sizeof(struct UCopList),MEMF_PUBLIC|MEMF_CLEAR);

/*
   Allocazione in memoria di una struttura UCopList, necessaria alla
   creazione di una nuova Copper List.
   UCopList e` un acronimo per UserCopperList
*/

CINIT(CopperList,512);

/*
   Viene indicato al sistema che voglio creare un listato per il Copper
   composto da 256 istruzioni.
   La Copper List viene creata dal ciclo sottostante
*/
   

for(ypos=0; ypos<=256; ++ypos)
   {
    CWAIT(CopperList,ypos,0);
    CMOVE(CopperList,custom.color[0],coppcolor);
    coppcolor+=16;
   }

/*
   A fine ciclo ci sarà in memoria, nella zona puntata da CopperList, una
   sequenza di 256 CWAIT e CMOVE.
   custom.color[0] è l'indirizzo hardware del registro colore 0, valore
   cromatico che viene usato per lo sfondo.
*/

CEND(CopperList);
VP -> UCopIns = CopperList;

/*
    L'assegnazione soprastante indica a intuition che la Copper List da
    usare per lo schermo è quello che ho creato, e che ha come puntatore
    CopperList.
*/

RethinkDisplay();

/*
    Questa funzione fa in modo che il computer ridisegni lo schermo usando
    la nuova Copper List.
    In questo modo viene abbandonata la Copper List che il sistema usava
    precedentemente.
*/

}

/*

 /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
(                                  Funzioni                                  )
 \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

*/

void gameover()
{
if (Screen)  CloseScreen(Screen);

if (GfxBase)        CloseLibrary((struct Library *)GfxBase);
if (IntuitionBase)  CloseLibrary((struct Library *)IntuitionBase);

exit(NULL);
}
