include "inc/cmdsys.plh"
include "inc/lines.plh"
include "dhgr.tk/inc/dhgrlib.plh"
include "dhgr.tk/inc/dhgrutils.plh"

sysflags resxtxt1|reshgr1|resxhgr1
//
// Test sprite
//
const SPR_W = 16
const SPR_H = 16
byte[] sprite   = $55,$55,$55,$05,$50,$55,$55,$55
byte[]          = $55,$55,$00,$00,$00,$00,$55,$55
byte[]          = $55,$00,$00,$AA,$AA,$00,$00,$55
byte[]          = $05,$00,$AA,$AA,$AA,$AA,$00,$50
byte[]          = $00,$A0,$AA,$FF,$FF,$AA,$0A,$00
byte[]          = $00,$AA,$FF,$FF,$FF,$FF,$AA,$00
byte[]          = $00,$AA,$1F,$12,$12,$F2,$AA,$00
byte[]          = $00,$AA,$2F,$21,$21,$F1,$AA,$00
byte[]          = $00,$AA,$1F,$12,$12,$F2,$AA,$00
byte[]          = $00,$AA,$2F,$21,$21,$F1,$AA,$00
byte[]          = $00,$AA,$FF,$FF,$FF,$FF,$AA,$00
byte[]          = $00,$A0,$AA,$FF,$FF,$AA,$0A,$00
byte[]          = $05,$00,$AA,$AA,$AA,$AA,$00,$50
byte[]          = $55,$00,$00,$AA,$AA,$00,$00,$55
byte[]          = $55,$55,$00,$00,$00,$00,$55,$55
byte[]          = $55,$55,$55,$05,$50,$55,$55,$55
byte[] rlesprt  = 7,$55,2,$00,7,$55
byte[]          = 4,$55,8,$00,4,$55
byte[]          = 2,$55,4,$00,4,$AA,4,$00,2,$55
byte[]          = 1,$05,3,$00,8,$AA,3,$00,1,$55
byte[]          = 3,$00,3,$AA,4,$FF,3,$AA,3,$00
byte[]          = 2,$00,2,$AA,8,$FF,2,$AA,2,$00
byte[]          = 2,$00,2,$AA,2,$1F,4,$12,2,$F2,2,$AA,2,$00
byte[]          = 2,$00,2,$AA,2,$2F,4,$21,2,$F1,2,$AA,2,$00
byte[]          = 2,$00,2,$AA,2,$1F,4,$12,2,$F2,2,$AA,2,$00
byte[]          = 2,$00,2,$AA,2,$2F,4,$21,2,$F1,2,$AA,2,$00
byte[]          = 2,$00,2,$AA,8,$FF,2,$AA,2,$00
byte[]          = 3,$00,3,$AA,4,$FF,3,$AA,3,$00
byte[]          = 1,$05,3,$00,8,$AA,3,$00,1,$55
byte[]          = 2,$55,4,$00,4,$AA,4,$00,2,$55
byte[]          = 4,$55,8,$00,4,$55
byte[]          = 7,$55,2,$00,7,$55

word rlesprptr
word sprite7msk[7]
word sprite7[7]
word sprite7span,sprite7w
//
// Dither matrices
//
byte[] dither25 = $55,$55,$00,$00,$AA,$AA,$00,$00
byte[]          = $55,$55,$00,$00,$AA,$AA,$00,$00
byte[]          = $55,$55,$00,$00,$AA,$AA,$00,$00
byte[]          = $55,$55,$00,$00,$AA,$AA,$00,$00
byte[] dither50 = $55,$55,$AA,$AA,$55,$55,$AA,$AA
byte[]          = $55,$55,$AA,$AA,$55,$55,$AA,$AA
byte[]          = $55,$55,$AA,$AA,$55,$55,$AA,$AA
byte[]          = $55,$55,$AA,$AA,$55,$55,$AA,$AA
byte[] dither75 = $55,$55,$FF,$FF,$AA,$AA,$FF,$FF
byte[]          = $55,$55,$FF,$FF,$AA,$AA,$FF,$FF
byte[]          = $55,$55,$FF,$FF,$AA,$AA,$FF,$FF
byte[]          = $55,$55,$FF,$FF,$AA,$AA,$FF,$FF
word[] dither   = @dither25,@dither50,@dither75
//
// Sliding sprite Y coordinate
//
const SLIDE_Y = 170
//
// Enterprise sprites
//
struc t_sprite
  var spr_xorg
  var spr_yorg
  var spr_width
  var spr_height
  var spr_pixmap
  var spr_bl7
end
res[t_sprite * 8] ncc1701
char[12] sprFile = "ENT.UP.SPR"
char[12]         = "ENT.UR.SPR"
char[12]         = "ENT.RT.SPR"
char[12]         = "ENT.DR.SPR"
char[12]         = "ENT.DN.SPR"
char[12]         = "ENT.DL.SPR"
char[12]         = "ENT.LT.SPR"
char[12]         = "ENT.UL.SPR"
const ENT_W7 = 5
const ENT_H  = 43
const ENT_SZ = ENT_W7*4*ENT_H

def dcgrCompSprite(offset)#0
  byte i

  for i = 0 to 6
    sprite7[i],sprite7span = dhgrAllocBl7Mem(SPR_W+offset,SPR_H)
    sprite7w = sprite7span >> 2
    dhgrSurfMem(OP_SRC,SPR_H,sprite7[i],sprite7span)
    dcgrColor(CLR_BLACK)
    dhgrClearBl7(0,0,sprite7w,SPR_H)
    //dcgrPixMap(i + offset,0,SPR_W,SPR_H,@sprite)
    //dcgrRleMap(i + offset,0,SPR_W,SPR_H,@rlesprt)
    dcgrRleMap(i + offset,0,SPR_W,SPR_H,rlesprptr)
    dhgrSurfScr(OP_SRC)
    dhgrMemBl7(0,i*SPR_H,sprite7w,SPR_H,sprite7[i],sprite7span)
  next
end
def dcgrCompMask(offset)#0
  byte i

  for i = 0 to 6
    sprite7msk[i],sprite7span = dhgrAllocBl7Mem(SPR_W + offset,SPR_H)
    sprite7w = sprite7span >> 2
    dhgrSurfMem(OP_SRC,SPR_H,sprite7msk[i],sprite7span)
    dcgrColor(CLR_WHITE)
    dhgrClearBl7(0,0,sprite7w,SPR_H)
    //dcgrPixMap(i + offset,0,SPR_W,SPR_H,@sprite)
    dcgrRleMap(i + offset,0,SPR_W,SPR_H,@rlesprt)
    dhgrOp(OP_XOR)
    //dcgrPixMap(i + offset,0,SPR_W,SPR_H,@sprite)
    dcgrRleMap(i + offset,0,SPR_W,SPR_H,@rlesprt)
    dhgrSurfScr(OP_SRC)
    dhgrMemBl7(sprite7w,i * SPR_H,sprite7w,SPR_H,sprite7msk[i],sprite7span)
  next
end
def readEnterprise#0
  var xorg,yorg,width,height,pixptr,t_ptr,f_ptr
  byte i

  t_ptr = @ncc1701
  f_ptr = @sprFile
  for i = 0 to 7
    xorg,yorg,width,height,pixptr = spriteRead(f_ptr)
    t_ptr=>spr_xorg   = xorg
    t_ptr=>spr_yorg   = yorg
    t_ptr=>spr_width  = width
    t_ptr=>spr_height = height
    t_ptr=>spr_pixmap = pixptr
    t_ptr=>spr_bl7  = heapalloc(ENT_SZ)
    dhgrSurfMem(OP_SRC,ENT_H,t_ptr=>spr_bl7,ENT_W7*4)
    dcgrColor(CLR_BLACK)
    dhgrClearBl7(0,0,ENT_W7,ENT_H)
    dcgrPixmap(ENT_W7*7/2 - xorg,ENT_H/2 - yorg,width,height,pixptr)
    t_ptr = t_ptr + t_sprite
    f_ptr = f_ptr + 12
  next
end
//////////////////////////////////////////
//                                      //
//             Test Harness             //
//                                      //
//////////////////////////////////////////
def dhgrTest#0
  word i

  //
  // Double Hires Lines
  //
  dhgrSurfScr(OP_OR)
  setlineplot(@dhgrSet)
  for i = 559 downto 0 step 20
    line(0,0,i,191)
  next
  getc
end
def dcgrTest#0
  word i,j,k,oi,oj,i7,im7,k7,km7,s7w,inci,incj,inck
  word backblk,backspan
  word restrblk,restrspan,restr7w,restrw
  byte mode
  byte[40] tilestr

  //
  // Use back buffer for tile set buffer
  //
  backblk,backspan = dhgrAllocBl7Mem(SCR_WIDTH,SPR_H)
  j = 0
  while ^$C000 < 128
    //
    // Convert compiled sprite into tile set
    //
    dhgrMemBl7Tile(0,0,sprite7[j],sprite7span,backblk)
    dhgrMemBl7Tile(1,0,sprite7[j],sprite7span,backblk + 32)
    dhgrMemBl7Tile(2,0,sprite7[j],sprite7span,backblk + 64)
    dhgrMemBl7Tile(3,0,sprite7[j],sprite7span,backblk + 96)
    dhgrMemBl7Tile(0,8,sprite7[j],sprite7span,backblk + 128)
    dhgrMemBl7Tile(1,8,sprite7[j],sprite7span,backblk + 160)
    dhgrMemBl7Tile(2,8,sprite7[j],sprite7span,backblk + 192)
    dhgrMemBl7Tile(3,8,sprite7[j],sprite7span,backblk + 224)
    //
    // Fill screen wil tiles matching compiled sprite
    //
    for i = 0 to 19
      tilestr[i] = i & $03
    next
    for i = 0 to 19
      tilestr[i + 20] = (i & $03) + 4
    next
    dhgrSurfScr(OP_SRC)
    for i = 0 to 12 step 2
      dhgrTiles(10,i,  @tilestr,   10,backblk)
      dhgrTiles(10,i + 1,@tilestr + 20,10,backblk)
    next
    j++
    if j == 7; j = 0; fin
  loop
  getc
  //
  // Lines
  //
  dhgrSurfScr(OP_SRC)
  setlineplot(@dcgrPixel)
  setlinespans(@dcgrHLin,@dcgrVLin)
  for i = 0 to 191 step 2
    dcgrColor(i)
    linespans(0,0,139,i)
  next
  for i = 139 downto 0 step 2
    dcgrColor(i)
    linespans(0,0,i,191)
  next
  dcgrColor(CLR_BLACK)
  dhgrClearBl7(21/7,30,100/7,132)
  dcgrColor(CLR_WHITE)
  dcgrHLin((21/7)*7-1,(21/7)*7+(100/7)*7,29)
  dcgrHLin((21/7)*7-1,(21/7)*7+(100/7)*7,29+133)
  dcgrVLin(29,29+133,(21/7)*7-1)
  dcgrVLin(29,29+133,(21/7)*7+(100/7)*7)
  dcgrColor(CLR_BLACK) // Outline color
  dcgrBoldStr(CLR_WHITE,48,14,"Hello!")
  dcgrFont(FONT_DEF_THIN)
  dcgrColor(CLR_WHITE) // Outline color
  dcgrBoldStr(CLR_BLACK,42,24,"from DCGRTEST")
  i = 64; j = 64; k = 86
  k7 = k/7
  inci = 1; incj = 1; inck = 2
  dhgrSurfMem(OP_SRC,SPR_H,backblk,backspan)
  dhgrScrBl7(0,SLIDE_Y,backspan >> 2,SPR_H)
  dhgrSurfScr(OP_SRC)
  dcgrColor(CLR_WHITE)
  dcgrHLin(0,139,SLIDE_Y)
  dcgrHLin(0,139,SLIDE_Y+SPR_H-1)
  dcgrVLin(SLIDE_Y,SLIDE_Y+SPR_H-1,0)
  dcgrVLin(SLIDE_Y,SLIDE_Y+SPR_H-1,139)
  getc
  dhgrMemBl7(0,SLIDE_Y,backspan >> 2,SPR_H,backblk,backspan)
  restrblk,restrspan = dhgrAllocBl7Mem(SPR_W,SPR_H)
  restr7w = restrspan >> 2
  restrw  = restr7w * 7
  mode = 1
  while mode
    if ^$C000 >= 128
      mode = (^$C010 & $7F) - '0'
    fin
    k7,km7 = divmod(k,7)
    i7,im7 = divmod(i,7)
    if i7 + sprite7w < (21/7)+(100/7)
      s7w = sprite7w
    else
      s7w = (21/7)+(100/7) - i7
    fin
    when mode
      is 1
        dhgrSurfScr(OP_SRC)
        dhgrMemBl7(i7,j,s7w,SPR_H,sprite7[im7],sprite7span)
      is 2
        if k < 0 and km7  // Handle off left edge special case
          k7--
          km7 = 7 + km7
        fin
        dhgrSurfMem(OP_SRC,SPR_H,restrblk,restrspan)
        dhgrMemBl7(0,0,restr7w,SPR_H,backblk + (k7 << 1),backspan)
        dhgrOp(OP_AND)
        dhgrMemBl7(0,0,sprite7w,SPR_H,sprite7msk[km7],sprite7span)
        dhgrOp(OP_XOR)
        dhgrMemBl7(0,0,sprite7w,SPR_H,sprite7[km7],sprite7span)
        dhgrSurfScr(OP_SRC)
        dhgrMemBl7(k7,SLIDE_Y,restr7w,SPR_H,restrblk,restrspan)
        break
      is 3
        dhgrSurfScr(OP_SRC)
        dcgrPixMap(i,j,SPR_W,SPR_H,@sprite)
      is 4
        dhgrSurfMem(OP_SRC,SPR_H,restrblk,restrspan)
        dhgrMemBl7(0,0,restr7w,SPR_H,backblk + (k7 << 1),backspan)
        dcgrPixMap(km7 + 2,0,SPR_W,SPR_H,@sprite)
        dhgrSurfScr(OP_SRC)
        dhgrMemBl7(k7,SLIDE_Y,restr7w,SPR_H,restrblk,restrspan)
        break
      is 5
        dhgrSurfScr(OP_SRC)
        dcgrPixMap(i,j,SPR_W,SPR_H,@sprite)
      is 6
        dhgrSurfMem(OP_SRC,SPR_H,restrblk,restrspan)
        dhgrMemBl7(0,0,restr7w,SPR_H,backblk + (k7 << 1),backspan)
        dcgrColor(CLR_BLACK)
        dcgrRect(km7 + 2,3,16 + 2,10)
        dcgrColor(CLR_WHITE)
        dcgrStr(km7 + 3,13,"DCGR")
        dhgrSurfScr(OP_SRC)
        dhgrMemBl7(k7,SLIDE_Y,restr7w,SPR_H,restrblk,restrspan)
        break
      is 7
        dhgrSurfScr(OP_SRC)
        dhgrMemBl7(i7,j,s7w,SPR_H,sprite7[im7],sprite7span)
        break
      is 8
        dhgrSurfScr(OP_SRC)
        dcgrPixMap(i,j,SPR_W,SPR_H,@sprite)
        break
      is 9
        dhgrSurfScr(OP_XOR)
        dcgrPixMap(i,j,SPR_W,SPR_H,@sprite)
        dcgrPixMap(oi,oj,SPR_W,SPR_H,@sprite)
        break
    wend
    if k > 136 or k < -16; inck = -inck; fin
    k = k + inck
    oi = i; oj = j
    if i > 102 or i < 22; inci = -inci; fin
    i = i + inci
    if j > 145 or j < 32; incj = -incj; fin
    j = j + incj
  loop
  //
  // Rotate Enterprise
  //
  dhgrSurfScr(OP_SRC)
  dcgrColor(CLR_BLACK)
  dhgrClearBl7(21/7,30,100/7,132)
  dhgrOp(OP_XOR)
  k = 0
  i = @ncc1701
  oi = 73-i=>spr_xorg
  oj = 96-i=>spr_yorg
  dcgrPixMap(oi,oj,i=>spr_width,i=>spr_height,i=>spr_pixmap)
  while ^$C000 < 128
    dcgrPixMap(oi,oj,i=>spr_width,i=>spr_height,i=>spr_pixmap)
    i = i + t_sprite
    k++
    if k > 7
      k = 0
      i = @ncc1701
    fin
    oi = 73-i=>spr_xorg
    oj = 96-i=>spr_yorg
    dcgrPixMap(oi,oj,i=>spr_width,i=>spr_height,i=>spr_pixmap)
  loop
  getc
  dhgrOp(OP_SRC)
  while ^$C000 < 128
    dcgrColor(CLR_BLACK)
    dhgrClearBl7(60/7,75,5,43)
    oi = 73-i=>spr_xorg
    oj = 96-i=>spr_yorg
    dcgrPixMap(oi,oj,i=>spr_width,i=>spr_height,i=>spr_pixmap)
    i = i + t_sprite
    k++
    if k > 7
      k = 0
      i = @ncc1701
    fin
  loop
  getc
  while ^$C000 < 128
    dhgrMemBl7(60/7,75,ENT_W7,ENT_H,i=>spr_bl7,ENT_W7*4)
    i = i + t_sprite
    k++
    if k > 7
      k = 0
      i = @ncc1701
    fin
  loop
  getc
  //
  // Re-use the back buffer for a stipple pattern to mask the screen
  //
  for k = 0 to 2
    dhgrSurfMem(OP_SRC,SPR_H,backblk,backspan)
    dcgrColor(CLR_WHITE)
    dhgrClearBl7(0,0,SCR_WIDTH7,SPR_H)
    dcgrColor(CLR_BLACK)
    for i = 0 to 139 step 16
      dcgrBitMap(i,0,16,16,dither[k])
    next
    dhgrSurfScr(OP_AND)
    for j = 0 to 95 step 16
      dhgrMemBl7(0,j,SCR_WIDTH7,16,backblk,backspan)
      dhgrMemBl7(0,175 - j,SCR_WIDTH7,16,backblk,backspan)
    next
  next
  dhgrSurfScr(OP_SRC)
  dhgrClearBl7(0,0,SCR_WIDTH7,SCR_HEIGHT)
  //
  // Create color patterns
  //
  for i = 0 to 15
    dcgrColor(i)
    dhgrClearBl7(i,0,1,SCR_HEIGHT)
  next
  oj = 0
  for j = 0 to 191 step 12
    dcgrColor(oj)
    oi = 0
    for i = 0 to 111 step 14
      dcgrBitMap(i,j,14,12,@dither25)
      oi++
    next
    oj++
  next
  getc
  for i = 0 to 15
    dcgrColor(i)
    dhgrClearBl7(i,0,1,SCR_HEIGHT)
  next
  oj = 0
  for j = 0 to 191 step 12
    dcgrColor(oj)
    oi = 0
    for i = 0 to 111 step 14
      dcgrBitMap(i,j,14,12,@dither50)
      oi++
    next
    oj++
  next
  getc
  for i = 0 to 15
    dcgrColor(i)
    dhgrClearBl7(i,0,1,SCR_HEIGHT)
  next
  oj = 0
  for j = 0 to 191 step 12
    dcgrColor(oj)
    oi = 0
    for i = 0 to 111 step 14
      dcgrBitMap(i,j,14,12,@dither75)
      oi++
    next
    oj++
  next
  getc
end

rlesprptr = heapmark
heapalloc(dcgrEncPixMap(SPR_W,SPR_H,@sprite,rlesprptr))
dhgrMode(DHGR_COLOR_MODE)
dhgrTest
screenRead("splash.dhgr")
dhgrOp(OP_SRC)
dcgrPixMap(10,10,20,20,dcgrGetPixMap(60,66,20,20))
getc
readEnterprise
dcgrCompSprite(2)
dcgrCompMask(2)
dcgrTest
dhgrMode(DHGR_TEXT_MODE)
done
