// This is a highly configurable example :)

#define AGA         // For AGA
#undef EGCS_WARPUP  // StormC or vbcc-WarpUP
#define REALLIB     // Use Shared Library, not linker-lib
#define RTGM        // Use rtgmaster
#define PPC         // Use rtgmaster PPC
#ifndef AGA
#ifndef RTGM
#define CGX
#endif
#endif

#ifdef RTGM
struct RTGMasterBase *RTGMasterBase=0;
#endif

#ifdef RTGM
#ifdef EGCS_WARPUP
#error RTGMaster includes not (yet) available for EGCS WarpUP !!!
#endif
#endif

#ifdef CGX
#include <clib/cybergraphics_protos.h>
#endif
#include <clib/intuition_protos.h>
#include <intuition/intuition.h>
#include <clib/exec_protos.h>
#ifdef CGX
#include <cybergraphics/cybergraphics.h>
#endif
#include <clib/dos_protos.h>
#ifdef RTGM
#include <clib/rtgmaster_protos.h>
#include <rtgmaster/rtgmaster.h>
#include <rtgmaster/rtgsublibs.h>
#endif

#ifdef REALLIB
#include <clib/chunkyppc_protos.h>
#else
#ifndef EGCS_WARPUP
#include <clib/linkchunkyppc_protos.h>
#else
#error EGCS WARPUP does not support the chunkylib as linkerlibrary !!!
#error Use the shared library version by setting REALLIB !!!
#endif
#endif

#ifdef EGCS_WARPUP
#include <inline/cybergraphics.h>
#include <inline/intuition.h>
#include <inline/exec.h>
#include <inline/dos.h>
#include <inline/chunkyppc.h>
#endif

#ifndef EGCS_WARPUP
#include <clib/powerpc_protos.h>
#else
#include <powerpc/powerpc_protos.h>
#endif

// data is an example data of 8 Bit Data sized 320x256, you would
// of course use your Chunky Buffer for actual code !!!

UBYTE data[320*256];

#ifdef EGCS_WARPUP
struct Library *IntuitionBase;
struct Library *CyberGfxBase;
struct Library *DOSBase;
struct Library *ChunkyPPCBase;
#else
extern struct Library *IntuitionBase;
struct Library *CyberGfxBase;
extern struct Library *DOSBase;
struct Library *ChunkyPPCBase;
#endif

int main()
{

#ifndef RTGM
 struct RastPort *rp;

 // a Rastport for the Screen
#endif

#ifdef CGX
 struct TagItem helpbuf[3];

 // To find out VRAM Base
#endif

#ifndef RTGM
 struct Screen *myscreen;

 // The screen of the example
#else
 struct RtgScreen *MyScreen; 
 struct ScreenReq *ScreenReq=0;
 int wb=0;
 int aga=0;
 struct TagItem ScreenModeTags[]={
    smr_MinWidth,       320,
    smr_MinHeight,      256,
    smr_MaxWidth,       320,
    smr_MaxHeight,      256,
    smr_ChunkySupport,  LUT8,
    smr_PlanarSupport,  Planar8,
    smr_Buffers,        1,
    smr_Workbench,      TRUE,
    smr_PrefsFileName,  (ULONG)"RtgScreenMode.prefs",
    TAG_DONE,           NULL
};

struct TagItem RtgTags[]={
   rtg_Draggable,       TRUE,
   rtg_Buffers,         1,
   rtg_Workbench,       0,
   TAG_DONE,            NULL
};

struct TagItem GetTags[] = {
    grd_Width,          0,
    grd_Height,         0,
    grd_Depth,          0,
    grd_BytesPerRow,    0,
    grd_PixelLayout,    0,
    grd_ColorSpace,     0,
    grd_PlaneSize,      0,
    TAG_DONE,           NULL
};
#endif

 UBYTE *address;

 // VRAM Base

 int bpr;

 // The BytesPerRow (needed for CV64 Support, read above)

 int f,g;
 for (f=0;f<320;f++)
 {
  for (g=0;g<256;g++)
  {
   data[f+320*g]=g;
  }
 }

 // Some example data for this demonstration of GFX Board Support

#ifndef RTGM
 IntuitionBase=OpenLibrary("intuition.library",36);
#endif
#ifdef CGX
 CyberGfxBase=OpenLibrary("cybergraphics.library",0);
#endif
 ChunkyPPCBase=OpenLibrary("chunkyppc.library",0);
#ifdef EGCS_WARPUP
 DOSBase=OpenLibrary("dos.library",0);
#endif
#ifdef RTGM
 RTGMasterBase=(struct Library *)OpenLibrary("rtgmaster.library",34);
#ifdef PPC
    ScreenReq=PPCRtgScreenModeReq(ScreenModeTags);
    if (!ScreenReq)
    {
     exit(0);
    }
    if (ScreenReq->Flags&sq_WORKBENCH)
    {
     RtgTags[2].ti_Data=LUT8;
     wb=1;
     printf("WB Window Mode enabled\n");
    }
    else wb=0;
    MyScreen=PPCOpenRtgScreen(ScreenReq,RtgTags);
    PPCGetRtgScreenData(MyScreen, GetTags);
#else
    ScreenReq=RtgScreenModeReq(ScreenModeTags);
    if (!ScreenReq)
    {
     exit(0);
    }
    if (ScreenReq->Flags&sq_WORKBENCH)
    {
     RtgTags[2].ti_Data=LUT8;
     wb=1;
     printf("WB Window Mode enabled\n");
    }
    else wb=0;
    MyScreen=OpenRtgScreen(ScreenReq,RtgTags);
    GetRtgScreenData(MyScreen, GetTags);
#endif
    bpr=GetTags[3].ti_Data;
    if (GetTags[4].ti_Data==grd_PLANAR) aga=1;
    else aga=0;
#else
 myscreen = OpenScreenTags(0,SA_Width,320,SA_Height,256, SA_Depth,8,TAG_END);
 rp=&(myscreen->RastPort);
#endif

 // Screen and RastPort init for demo program

#ifdef CGX
 helpbuf[0].ti_Tag=LBMI_BASEADDRESS;
 helpbuf[0].ti_Data=(ULONG)&address;
 helpbuf[1].ti_Tag=LBMI_BYTESPERROW;
 helpbuf[1].ti_Data=(ULONG)&bpr;
 helpbuf[2].ti_Tag=TAG_END;
 helpbuf[2].ti_Data=0;
 UnLockBitMap(LockBitMapTagList(rp->BitMap,helpbuf));
#endif
#ifdef RTGM
#ifdef PPC
 address=PPCLockRtgScreen(MyScreen);
#else
 address=LockRtgScreen(MyScreen);
#endif
#endif

#ifndef RTGM
#ifdef AGA
 c2p_1(data,rp->BitMap,320,256);
#else
 if (wb)
 {
#ifdef PPC
  PPCCopyRtgBlit(MyScreen,PPCGetBufAdr(MyScreen,0),data,0,0,0,320,256,320,256,0,0);
#else
  CopyRtgBlit(MyScreen,GetBufAdr(MyScreen,0),data,0,0,0,320,256,320,256,0,0);
#endif
 }
 elseChunkyNoffFastest(address,data,320,256,bpr);
 // Copy the example data
#endif
#else
#ifdef PPC
 if (aga) PPCCopyRtgBlit(MyScreen,PPCGetBufAdr(MyScreen,0),data,0,0,0,320,256,320,256,0,0);
#else
 if (aga) CopyRtgBlit(MyScreen,GetBufAdr(MyScreen,0),data,0,0,0,320,256,320,256,0,0);
#endif
 else ChunkyNoffFastest(address,data,320,256,bpr);
#endif

 Delay(300);

 // Delay for the demo

#ifndef RTGM
 CloseScreen(myscreen);
#else
#ifdef PPC
 if (MyScreen) PPCUnlockRtgScreen(MyScreen);
 if (MyScreen) PPCCloseRtgScreen(MyScreen);
 if (ScreenReq) PPCFreeRtgScreenModeReq(ScreenReq);
#else
 if (MyScreen) UnlockRtgScreen(MyScreen);
 if (MyScreen) CloseRtgScreen(MyScreen);
 if (ScreenReq) FreeRtgScreenModeReq(ScreenReq);
#endif
#endif

 // Close the screen again

 CloseLibrary(ChunkyPPCBase);
#ifdef CGX
 CloseLibrary(CyberGfxBase);
#endif
#ifndef RTGM
 CloseLibrary(IntuitionBase);
#endif
#ifdef EGCS_WARPUP
 CloseLibrary(DOSBase);
#endif
#ifdef RTGM
 CloseLibrary((struct Library *)RTGMasterBase);
#endif

 // Close the libs

}
