/********************************************************************************************\
*																															*
*		How To double-buffer a CyberScreen (Wie man einen Double Buffer CyberScreen benutzt)	*
*																															*
*		(by Gerd Kautzmann,  Email to gerd@ufoo.phase5.de)													*
*		Note: There is only one way to be shure that two screens are simultanously at a grafik *
*				card, let them be one screen, and scroll it.													*
*				Es gibt nur eine Möglichkeit zwei Screen so zu öffnen, daß sich beide auf der 	*
*				Grafikkarte befinden, indem man eine doppelt so hohen (breiten) Screen benutzt,  *
*				und diesen dann hin und herscrollt.																* 
*		LastNode: You will need cybergraphics.library 40.92 and cyberintuition.library 40.33	*
*																															*
\********************************************************************************************/



#include <libraries/configvars.h>
#include <dos/dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <exec/memory.h>

#include <clib/exec_protos.h>
#include <clib/graphics_protos.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>

/*						Includes a la Cybervision			*/

#include <proto/cybergraphics.h>
#include <cybergraphics/cybergraphics.h>
#define	MANYMB 0

#if MANYMB

/* 					Works only if more than 2MB Grafik Ram	*/

#define WINX	640
#define WINY	480

#else

#define WINX	640
#define WINY	400

#endif

/*#define MODE	0x40120052	*/	/* 640 x 480 Mode */
#define MODE	0x40120051	/* 320 x 200 Mode */	
/*#define MODE 0x40120050	*/ /* 800 x 600 Mode */	

#define DEPTH	32

/*				Protos 			*/

void CloseAll(void);
void ToggleScreen(void);

struct Rectangle ScreenRectangle = {
		0,0,
		WINX-1,WINY-1,
		};

struct RastPort 	*rp = NULL;
struct Screen		*Scr= NULL;
struct ViewPort	*vp = NULL;
struct RasInfo		*ri = NULL;

struct IntuitionBase *IntuitionBase = NULL; 
struct GfxBase			*GfxBase 		= NULL;
struct Library			*CyberGfxBase	= NULL;

RyOffset	= WINY;
int	i;

main()
{
	if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L)))
		{ printf ("ERROR: Konnte Intuition Library nicht öffnen\n"); CloseAll(); }
	if (!(CyberGfxBase=(struct Library *)OpenLibrary("cybergraphics.library",40)))
		{ printf ("ERROR: Konnte Cyber Library nicht öffnen\n"); CloseAll();}
  	if (!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L)))
		{ printf ("ERROR: Couldn't open GfX Base\n"); CloseAll(); }


	if (!(Scr=OpenScreenTags(NULL,SA_DisplayID,MODE,
											SA_Width,WINX,
											SA_Height,WINY*2,
											SA_Depth,DEPTH,
											SA_Draggable,FALSE,
											SA_AutoScroll,FALSE,
											SA_Exclusive,TRUE,
											SA_Quiet,TRUE,
											SA_ShowTitle,FALSE,
											SA_DClip,&ScreenRectangle,
											TAG_DONE)))
		{ printf ("ERROR: Couldn't get any CyberScreen\n"); CloseAll(); }

	rp = &Scr->RastPort; 
	vp = &Scr->ViewPort;
	ri	= vp->RasInfo;

	FillPixelArray(rp,0,   0,WINX,  WINY,0x00ff0000);	/* Red Screen 	 */
	FillPixelArray(rp,0,WINY,WINX,WINY*2,0x0000ff00);	/* Green Screen */

	for(i=0;i<10;i++)
	{
		ToggleScreen();
		Delay(30);
	}
	CloseAll();

}
void ToggleScreen()
{
	if ((ri->RyOffset = RyOffset) == WINY)
		RyOffset = 0;
	else
		RyOffset = WINY;
	ScrollVPort(vp);
}
/***********************************************************************************\
*																												*
*		CloseAll																								*
*																												*
\***********************************************************************************/
void CloseAll()
{
	if (Scr) CloseScreen(Scr);
	if (IntuitionBase) CloseLibrary ((struct Library *)IntuitionBase);
	if (GfxBase) CloseLibrary ((struct Library *)GfxBase);
	if (CyberGfxBase) CloseLibrary ((struct Library *)CyberGfxBase);
	exit(0);
}
