    incdir "include:"
    include "rtgmaster/rtgmaster.i"
    include "rtgmaster/rtgsublibs.i"
    include "rtgmaster/rtgmaster_lib.i"
    include "rtgmaster/rtgc2p.i"
    include "exec/memory.i"

    XDEF _InitIt
    XDEF _CloseIt

    mc68020

_InitIt:
    movem.l a3/a6,-(sp)               ; Save a3 and a6 to the stack
    move.l $4,a6                      ; Get ExecBase
    lea InitData,a3                   ; The Data structure for InitIt
    lea RTGMasterName,a1
    moveq #0,d0
    jsr -408(a6)                      ; Open "rtgmaster.library"
    move.l d0,(a3)                    ; Put it to RTGMasterBase
    move.l #$FFFFFFFF,d0
    jsr -330(a6)
    move.l d0,4(a3)                   ; Allocate an Exec Signal for later usage
    move.l (a3),a6                    ; Put RTGMasterBase to a6
    lea SrTags,a0                     ; The Tags for the Screenmode-Requester
    jsr _LVORtgScreenModeReq(a6)      ; Call the Screenmode-Requester
    move.l d0,8(a3)                   ; Save the ScreenModeReq-Structure
    move.l 8(a3),a0                   ; Get the ScreenModeReq-Structure
    lea ScrTags,a1                    ; Get the Screen Tags
    jsr _LVOOpenRtgScreen(a6)         ; Open the Screen
    move.l d0,12(a3)                  ; Save the RtgScreen Structure
    move.l 12(a3),a0                  ; Get the RtgScreen Structure
    lea DescTags,a1                   ; Get the Description Tags
    jsr _LVOGetRtgScreenData(a6)      ; Find out information about this screen
    move.l 12(a3),a0                  ; Get the RtgScreen Structure
    lea ColorTable,a1                 ; Get the ColorTable
    jsr _LVOLoadRGBRtg(a6)            ; Set the Colors to the Screen
    move.l 12(a3),a0                  ; Get the RtgScreen Structure
    jsr _LVOLockRtgScreen(a6)         ; Lock the Screen
    move.l d0,16(a3)                  ; This is Video RAM Base Address of Buffer 0
    move.l 12(a3),a0                  ; Get the RtgScreen Structure
    move.l #1,d0                      ; Now Buffer 1
    jsr _LVOGetBufAdr(a6)             ; Get the Address of Buffer 1 (if it exists)
    move.l d0,20(a3)                  ; And save it
    move.l 12(a3),a0                  ; Get the RtgScreen Structure
    move.l #2,d0                      ; Now Buffer 2
    jsr _LVOGetBufAdr(a6)             ; Get the Address of Buffer 2 (if it exists)
    move.l d0,24(a3)                  ; And save it
    move.l 12(a3),a0                  ; Get the RtgScreen Structure
    jsr _LVORtgInitRDCMP(a6)          ; Init Mouse/Keyboard Functions of rtgmaster
    move.l d0,a0
    move.l 4(a0),28(a3)               ; The Userport Signal
    move.l a3,d0                      ; return the InitData Structure in d0
    movem.l (sp)+,a3/a6               ; return a3/a6 from the stack
    rts



InitData:
    dc.l 0                           ; RTGMasterBase Adress
    dc.l 0                           ; TheSignal
    dc.l 0                           ; The ScreenModeReq Structure
    dc.l 0                           ; The RtgScreen Structure
    dc.l 0                           ; Video RAM Base Address Buffer 0
    dc.l 0                           ; Video RAM Base Address Buffer 1 (if this buffer exists)
    dc.l 0                           ; Video RAM Base Address Buffer 2 (if this buffer exists)
    dc.l 0                           ; The userport signal

DescTags:
    dc.l grd_Width
    dc.l 0                           ; Here the Screen Width will be put
    dc.l grd_Height
    dc.l 0                           ; Here the Screen Height will be put
    dc.l grd_PixelLayout
    dc.l 0                           ; Here the PixelLayout will be put (see below)
    dc.l grd_ColorSpace
    dc.l 0                           ; Here the ColorSpace will be put (see below)
    dc.l grd_Depth
    dc.l 0                           ; Here the Screen Depth will be put
    dc.l grd_PlaneSize
    dc.l 0                           ; If the Screen is Planar, here the PlaneSize will be put, else 0
    dc.l grd_BytesPerRow
    dc.l 0                           ; How many Bytes has a Row of the Screen ?
    dc.l grd_MouseX
    dc.l 0                           ; Current Mouse X position will be found here
    dc.l grd_MouseY
    dc.l 0                           ; Current Mouse Y position will be found here
    dc.l grd_BusSystem
    dc.l 0                           ; Used Bussystem will be found here (see below)
    dc.l 0,0

; Explanation about Pixellayout
;
; Possible values (look in rtgsublibs.i for them) :
;
;grd_PLANAR     EQU 0    ; Non interleaved planar layout [X bitplanes/pixel]
;grd_PLANARI    EQU 1    ; Interleaved planar layout     [X bitplanes/pixel]
;grd_CHUNKY     EQU 2    ; 8-bit Chunky layout           [BYTE/pixel]
;grd_HICOL15    EQU 3    ; 15-bit Chunky layout          [WORD/pixel]
;grd_HICOL16    EQU 4    ; 16-bit Chunky layout          [WORD/pixel]
;grd_TRUECOL24  EQU 5    ; 24-bit Chunky layout          [3 BYTES/pixel]
;grd_TRUECOL24P EQU 6    ; 24-bit Chunky layout          [3 BYTEPLANES/pixel]
;grd_TRUECOL32  EQU 7    ; 24-bit Chunky layout          [LONG/pixel] (RGBx or BGRx)
;grd_GRAFFITI   EQU 8    ; 8-bit Graffiti-type Chunky layout (very special...)
;grd_TRUECOL32B EQU 9    ; 24-bit Chunky layout          [LONG/pixel] (xRGB or xBGR)

; NOTE: There exists a value grd_GRAFFITI, but the Graffiti is not yet supported
; in rtgmaster, maybe in a future version...
;
; Explanation about ColorSpace
;
; Possible values (look in rtgsublibs.i for them) :
;
;grd_Palette    EQU 0    ; Mode uses a Color Look-Up Table (CLUT)
;grd_RGB        EQU 1    ; standard RGB color space
;grd_BGR        EQU 2    ; high-endian RGB color space, BGR
;grd_RGBPC      EQU 3    ; RGB with lowbyte and highbyte swapped
;grd_BGRPC      EQU 4    ; BGR with lowbyte and highbyte swapped
;
; Usually we will get a Pixellayout = grd_CHUNKY (GFX Board) or grd_PLANAR (AGA)
; and a ColorSpace=grd_Palette (8 Bit Colortable).
;
; Explanation of BusSystem
;
; Note: rtgmaster will claim a Merlin or Cybervision/3D being Zorro II, even if
; it is really Zorro III !!! All other boards are found correctly.
;
;grd_Z3 EQU 1 ; Zorro III
;grd_Z2 EQU 2 ; Zorro II
;grd_Custom EQU 3 ; Custom Chipset
;grd_RGBPort EQU 4 ; connected to RGB Port
;grd_GVP EQU 5 ; EGS 110 is connected to "special" bus of GVP Turbo Board
;grd_DDirect EQU 6 ; DraCo Direct Bus


; Following is an example Colortable.

ColorTable:
    dc.w 40,0
    dc.l 0,0,0,$20202020,0,0,$40404040,0,0,$60606060,0,0,$80808080,0,0,$A0A0A0A0,0,0,$C0C0C0C0,0,0,$E0E0E0E0,0,0
    dc.l $F0F0F0F0,0,0,$F0F0F0F0,$10101010,0,$F0F0F0F0,$20202020,0,$F0F0F0F0,$30303030,0,$F0F0F0F0,$40404040,0,$F0F0F0F0,$50505050,0,$F0F0F0F0,$60606060,0,$F0F0F0F0,$70707070,0
    dc.l $F0F0F0F0,$80808080,0,$F0F0F0F0,$90909090,0,$F0F0F0F0,$A0A0A0A0,0,$F0F0F0F0,$B0B0B0B0,0,$F0F0F0F0,$C0C0C0C0,0,$F0F0F0F0,$D0D0D0D0,0,$F0F0F0F0,$E0E0E0E0,0,$F0F0F0F0,$F0F0F0F0,0
    dc.l $F0F0F0F0,$F0F0F0F0,0,$F0F0F0F0,$F0F0F0F0,$10101010,$F0F0F0F0,$F0F0F0F0,$20202020,$F0F0F0F0,$F0F0F0F0,$30303030,$F0F0F0F0,$F0F0F0F0,$40404040,$F0F0F0F0,$F0F0F0F0,$50505050,$F0F0F0F0,$F0F0F0F0,$60606060,$F0F0F0F0,$F0F0F0F0,$70707070
    dc.l $F0F0F0F0,$F0F0F0F0,$80808080,$F0F0F0F0,$F0F0F0F0,$90909090,$F0F0F0F0,$F0F0F0F0,$A0A0A0A0,$F0F0F0F0,$F0F0F0F0,$B0B0B0B0,$F0F0F0F0,$F0F0F0F0,$C0C0C0C0,$F0F0F0F0,$F0F0F0F0,$D0D0D0D0,$F0F0F0F0,$F0F0F0F0,$E0E0E0E0,$F0F0F0F0,$F0F0F0F0,$F0F0F0F0
    dc.w 0

; Following are the Tags for the Screenmode-Requester (Example Values provided)

SrTags:
    dc.l smr_MinWidth,320
    dc.l smr_MaxWidth,640
    dc.l smr_MinHeight,200
    dc.l smr_MaxHeight,480
    dc.l smr_ChunkySupport,-1
    dc.l smr_PlanarSupport,-1
    dc.l 0,0

; Allowed Screen depths are controlled over smr_ChunkySupport (Screendepths for GFX Boards) and
; smr_PlanarSupport (Screendepths for Planar). -1 like in the example allows ANY Screendepth.
; The values are :
; ChunkySupport (one Bit, one value, for example smr_ChunkySupport = 519 (1+2+4+519) would
; meen ARGB32,RGB24, RGB16 and LUT8 (LUT8 is the normal 8 Bit Chunky) would be allowed.

;    ;  0  TrueColor  LONG   RGB   %00000000 rrrrrrrr gggggggg bbbbbbbb  ARGB32
;    ;  1  TrueColor 3 BYTE  RGB   %rrrrrrrr gggggggg bbbbbbbb           RGB24
;    ;  2  TrueColor  WORD   RGB   %rrrrrggg gggbbbbb                    RGB16
;    ;  3  TrueColor  WORD   RGB   %0rrrrrgg gggbbbbb                    RGB15
;    ;  4  TrueColor  LONG   BGR   %00000000 bbbbbbbb gggggggg rrrrrrrr  ABGR32
;    ;  5  TrueColor 3 BYTE  BGR   %bbbbbbbb gggggggg rrrrrrrr           BGR24
;    ;  6  TrueColor  WORD   BGR   %bbbbbggg gggrrrrr                    BGR16
;    ;  7  TrueColor  WORD   BGR   %0bbbbbgg gggrrrrr                    BGR15
;    ;  8  TrueColor  LONG   RGB   %rrrrrrrr gggggggg bbbbbbbb 00000000  RGBA32
;    ;  9  ColorMap   BYTE   -     -                                     LUT8
;    ; 10  Graffiti   BYTE   -     - (Graffiti style chunky, very special)
;    ; 11  TrueColor  WORD   RGB   %gggbbbbb 0rrrrrgg                    RGB15PC
;    ; 12  TrueColor  WORD   BGR   %gggrrrrr 0bbbbbgg                    BGR15PC
;    ; 13  TrueColor  WORD   RGB   %gggbbbbb rrrrrggg                    RGB16PC
;    ; 14  TrueColor  WORD   BGR   %gggrrrrr bbbbbggg                    BGR16PC
;    ; 15  TrueColor  LONG   BGR   %bbbbbbbb gggggggg rrrrrrrr 00000000  BGRA32
;
; PlanarSupport :
;
;    ; Bit 0: Indicates it supports 1 bitplane non-interleaved
;    ; Bit 1: Indicates it supports 2 bitplanes non-interleaved
;    ; (...)
;    ; Bit 7: Indicates it supports 8 bitplanes non-interleaved
;
;    ; Bit 16: Indicates it supports 1 bitplane interleaved
;    ; Bit 17: Indicates it supports 2 bitplanes interleaved
;    ; (...)
;    ; Bit 23: Indicates it supports 8 bitplanes interleaved
;
;    ; Bit 15: Indicates it supports EHB mode (6 bitplanes) non-interleaved
;    ; Bit 31: Indicates it supports EHB mode (6 bitplanes) interleaved
;

; The library name of rtgmaster.library :

RTGMasterName: dc.b 'rtgmaster.library',0
    even

; Following are the Tags for the Screen. This example opens a screen with 3
; Buffers (the Maximum !!!) About the Double/Triple-Buffering system of
; rtgmaster you should be cautious. The problem is, that the two WB Emulations
; CyberGraphX and Picasso96 up to now do not support real good DoubleBuffering-Functions,
; and rtgmaster can't change this. The only possible Doublebuffering under CyberGraphX
; can be achieved using ScrollVPort, that sometimes tends to flicker. As soon as
; CyberGraphX supports REAL Doublebuffering i will change the Doublebuffering function
; of rtgmaster, of course, so that it is used... so you might want to do some tests
; first, if the current DoubleBuffering of CyberGraphX is good enough for a game,
; and if it is not, implement the game with one Buffer. Also check out the Blitting
; functions. They are quite fast.

ScrTags:
   dc.l rtg_Buffers,3
   dc.l 0,0


_CloseIt:
    movem.l a3/a6,-(sp)                  ; Put a3 and a6 on Stack
    move.l a0,a3                         ; Save InitData to a3
    move.l (a3),a6                       ; Put RTGMasterBase to a6
    move.l 12(a3),a0                     ; Get RtgScreen Structure
    jsr _LVOUnlockRtgScreen(a6)          ; Unlock the Screen again
    move.l 12(a3),a0                     ; Get RtgScreen Structure
    jsr _LVOCloseRtgScreen(a6)           ; And Close the RtgScreen
    move.l 8(a3),a0                      ; Get ScreenReq Structure
    jsr _LVOFreeRtgScreenModeReq(a6)     ; Free it again
    move.l a6,a1
    move.l $4,a6
    jsr -414(a6)                         ; Close rtgmaster.library again
    move.l 4(a3),d0                      ; Get the Signal
    jsr -336(a6)                         ; And Free it again
    movem.l (sp)+,a3/a6                  ; And return a3/a6 from stack again
    rts


    END
