/**************************************************
* The C Version of the Example of the Generic     *
* rtgmaster game videodriver. The Generic         *
* rtgmaster videodriver is a linkable object file *
* that works together with any C or ASM code and  *
* should work okay as video driver for most       *
* games. It already opens all needed libraries    *
* and all that stuff. It is coded in C, but       *
* internally calls a lot of ASM functions. It is  *
* in fact used by some of the games mentioned     *
* in the rtgmaster docs. This example is a simple *
* example about how to use this driver.           *
* The driver does not give you all features of    *
* rtgmaster, but it is more easy to use for       *
* people who never used libraries before.         *
* You have to install the rtgmaster includes      *
* to use it.                                      *
**************************************************/

#include "vid_rtgm.h"
#include <stdio.h>

/**************************************************
* viddef_t describes the Screenmode structure of  *
* the Display that is opened by the driver        *
* (width, height, depth,...)                      *
**************************************************/

viddef_t lvid;

/**************************************************
* vmode_t contains data about ALL available modes.*
**************************************************/

vmode_t *modes;

/**************************************************
* vrect_t defines the rectangle for the CopyBuffer*
**************************************************/

vrect_t myrect;

/**************************************************
* This example uses the rtgmaster Screenmode      *
* Requester. If you want to have an example of    *
* how to use this driver WITHOUT a Screenmode     *
* Requester, look at the ASM example. The ASM     *
* example does not use a Screenmode-Requester.    *
**************************************************/

/**************************************************
* We want Screens listed in the requester from    *
* 320x200 to 640x480, we want only Chunky8 (this  *
* is 512 in the rtgmaster.library, look at        *
* vid_rtgm.h or vid_rtgm.i to see a list of all   *
* values.                                         *
**************************************************/

struct MyMode mymode={320,200,640,480,512};

/**************************************************
* 3*256 colorvalues of type 8 Bit R-value, 8 Bit  *
* G value, 8 Bit B value, after them 1 Byte with  *
* 0 to indicate end of structure.                 *
**************************************************/

UBYTE pal[769];

/**************************************************
* For Input Handling                              *
**************************************************/

struct MyEvent *myevent;

int quit=0;

/**************************************************
* Some variables to initialize the Backbuffer to  *
* have something to play around with in the       *
* example.                                        *
**************************************************/

int f,g;
UBYTE *test=0;
UBYTE tester[9]={1,1,1,1,1,1,1,1,1};

void main()
{
 int x;

/**************************************************
* We want to copy a 320x200 rectangle, later.     *
**************************************************/

 myrect.x=192;
 myrect.y=100;
 myrect.width=64;
 myrect.height=100;

/**************************************************
* Init the modes. We want only LUT8 modes (8 Bit  *
* modes. If we would like for example LUT8 or     *
* RGB16 or RGB15 modes, we would provide          *
* LUT8+RGB16+RGB15 as parameter. Look at          *
* vid_rtgm.h for the possible constants. These    *
* constants show you all colortypes available     *
* on Amiga GFX Boards. Only 8 Bit is the same     *
* on all boards. We want to use an invisible      *
* pointer. We don't want to use an extra vram     *
* buffer to be allocated (if we would want that,  *
* the extra buffer would be available with the    *
* variable extravram.                             *
**************************************************/

/**************************************************
* types :                                         *
* MOVE16 = use Move16 for 8 Bit, rtgmaster        *
* CopyRtgPixelArray for 15/16/24 Bit.             *
* RTGMASTER = use rtgmaster CopyRtgPixelArray for *
* all depths. (Move16 does not run on all hard-   *
* ware, so be sure to allow this option. rtgmaster*
* function is not much slower than the other      *
* possible functions.                             *
* MOVEM = use COPY_MOVEM_MOVEM                    *
* BLITTER = Use the GFX Board Blitter to fake     *
* Doublebuffering. This is quite slow.            *
* DOUBLEBUFFER = Use Doublebuffering. This does   *
* flicker on some machines.                       *
* MOVE16 and MOVEM use RTGMASTER for 15/16/24 Bit,*
* as MOVE16 and MOVEM do not yet support >8 Bit.  *
**************************************************/

/**************************************************
* Speed :                                         *
* RTGMASTER = 1.0 (higher value = faster)         *
* COPY_MOVE_MOVE without Src/Destination Offset   *
* = 1.002                                         *
* COPY_MOVEM_MOVEM = 1.215                        *
* FCOPY = 1.227                                   *
* Source of this driver is provided, so if you    *
* want to change the copy function to be used,    *
* simply modify the vid_rtgm.c and fcopy.s        *
* files.                                          *
**************************************************/

/**************************************************
* 0 is no extra vram buffer, 1 is use extra vram  *
* buffer. This buffer is free to do everything    *
* with it you want to do with it.                 *
**************************************************/

/**************************************************
* Move16 and Rtgmaster use a Fastcopy Buffer for  *
* lvid.buffer, the other two methods use a slower *
* Videoram Buffer.                                *
**************************************************/

/**************************************************
* This function returns a list of all Screenmodes.*
* The first screenmode will be overwritten by the *
* data of the Screenmode chosen by the user. So   *
* you can just use this data. If you give 0       *
* instead of &mymode, no Screenmoderequester will *
* be used and you get a list of ALL available     *
* Screenmodes instead.                            *
**************************************************/

 modes=RTGM_Init(LUT8,MOVEM,NOPOINTER,0,&mymode);

/**************************************************
* Init the example Palette.                       *
**************************************************/


 for (x=0;x<=769;x++) pal[x]=0;
 pal[3]=255;

/**************************************************
* This driver only uses GFX Board Screens. If it  *
* returns 0, probably no GFX Board Screenmode     *
* was found (Pure AGA System ?)                   *
**************************************************/

 if (!modes) return;
 else
    {

     lvid.width=modes->width;
     lvid.height=modes->height;
     lvid.rowbytes=modes->rowbytes;
     printf("%i %i\n",lvid.width,lvid.height);

/**************************************************
* Set the Mode, open the Screen, set the Palette. *
**************************************************/

     modes->setmode(&lvid,modes);

     modes->setpalette(&lvid,modes,pal);

/**************************************************
* The Mainloop of this example consists of a      *
* Buffer-Swapping (MOVE16 and RTGMASTER use       *
* CPU-Copy, BLITTER uses Blitter-Copy,            *
* DOUBLEBUFFER uses Doublebuffering (which does   *
* not run flickerfree on some GFX Boards, support *
* at least RTGMASTER !!!) To render the offscreen *
* Buffer, render to lvid.buffer !!!               *
* If you provide the parameter VID_WAIT_NONE, no  *
* synching will be done. Synching is only needed  *
* for DOUBLEBUFFERING, MOVE16, RTGMASTER and      *
* BLITTER do not need it.                         *
* I paint a red screen to use it in this example. *
**************************************************/

 for (f=0;f<320;f++)
 {
  for (g=0;g<200;g++)
  {
   test=lvid.buffer+f+g*320;
   *test=1;
  }
 }

/**************************************************
* BeginDirectRect and EndDirectRect are used for  *
* "Loading..." symbols and such stuff. They       *
* backup the Display before writing to it. Max.   *
* resolution for those two functions is 24x24.    *
* They use simple Longword-Copy, so do not use    *
* them for speed-relevant things.                 *
* In the example a 3x3 symbol is painted.         *
* They work in 8/15/16/24 Bit.                    *
**************************************************/

 modes->begindirectrect(&lvid,modes,160,100,tester,3,3);
 Delay(300);
 modes->enddirectrect(&lvid,modes,160,100,3,3);

 while (quit==0)
 {
  modes->swapbuffers(&lvid,modes,&myrect,VID_WAIT_VSYNC);

/**************************************************
* Sys_SendKeyEvent only reacts on Keypresses. If  *
* you also want Mouseevents, you have to modify   *
* the vid_rtgm.c or use rtgmaster functions to do *
* so. Sys_SendKeyEvent returns RawKey codes or 0. *
* It includes the KeyUp/KeyDown information.      *
**************************************************/

  myevent=modes->sendkeyevents();

/***************************************************
* Quit, if user presses CTRL q                     *
***************************************************/

  if ((myevent->rawkey==16)&&(myevent->qualifier&CONTROL)) quit=1;
  if (myevent->mouse==MENUDOWN) printf("You pressed the Menukey!!!\n");
  printf("X %i Y %i\n",myevent->x,myevent->y);

 }

/**************************************************
* Before quitting, free all allocated resources.  *
**************************************************/

  modes->closemode(&lvid,modes);

 }


}

/**************************************************
* NOTE:                                           *
* This example is for 8 Bit. The 15/16/24 Bit     *
* SwapBuffer work in the same way though.         *
* There also are two more sets of functions.      *
* BeginDirectRect copies a small rectangle to the *
* Screen and saves the overwritten stuff to a     *
* Backingbuffer. EndDirectRect writes the saved   *
* rectangle back. This is for displaying a loading*
* symbol (max. 24x24 pixel). These two functions  *
* use a simple Longword-Copy.                     *
* Look at the source for more information.        *
**************************************************/

