///////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 The 8-Bit Bunch. Licensed under the Apache License, Version 1.1
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-1.1>.
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, either express or implied. See the License for the specific language
// governing permissions and limitations under the License.
///////////////////////////////////////////////////////////////////////////////////////////////////

include "gamelib.plh"
include "globalDefs.plh"
include "playtype.plh"

// Exported functions go here. First a predef for each one, then a table with function pointers
// in the same order as the constants are defined in the the header.
predef _intimate_setMode(enable)#1, _intimate_displayStr(str)#1, _intimate_swipeImg()#1
word[] funcTbl = @_intimate_setMode, @_intimate_displayStr, @_intimate_swipeImg

byte spaceWidth

///////////////////////////////////////////////////////////////////////////////////////////////////
// Definitions used by assembly code
asm __defs

; Use hi-bit ASCII for Apple II
!convtab "../../include/hiBitAscii.ct"

; Headers
!source "../../include/global.i"
!source "../../include/plasma.i"
!source "../../include/mem.i"

; General use
tmp         = $2
pTmp        = $4

end

///////////////////////////////////////////////////////////////////////////////////////////////////
// Special effect: "swipe"
asm swipe(onLines, offLines, iterations)#0
  +asmPlasmRet 3
  sta pTmp          ; iterations
  lda evalStkL+1,x
  sta tmp+1         ; offLines
  lda evalStkL+2,x
  sta tmp           ; onLines
  clc               ; doesn't change; for convenient branching
.lup1:
  lda $C057         ; hi-res mode
  ldy tmp           ; onLines
.lup2:
  ldx #10           ; 1 -> 6 cyc. 2 -> 11 cyc. n -> n*5+1 cyc. 10 -> 51 cyc
- dex
  bne -
  dey
  beq +
  pha
  pla
  bcc .lup2 ; always taken
+ lda $C056         ; lo-res mode
  ldy tmp+1         ; offLines
  nop
.lup3:
  ldx #9            ; 1 -> 5 cyc. 2 -> 11 cyc. n -> n*5+1 cyc. 9 -> 46 cyc
- dex
  bne -
  dey
  bne +
  dec pTmp          ; next iteration
  bne .lup1
  rts
+ pha
  pla
  nop
  nop
  bcc .lup3         ; always taken
end

///////////////////////////////////////////////////////////////////////////////////////////////////
// Set up (or clear) intimate mode
def _intimate_setMode(enable)#1
  if enable
    loadFrameImg(NULL)  // flip to page 1, unload textures, etc.
    displayChar('N'-$40) // Set normal mode - clear all special modes (like underline, etc.)
    setWindow(0, 192, 0, 280) // Top, Bottom, Left, Right
    clearWindow()
    rawDisplayStr("\$1270")  // Set character rate to somewhat slow
    spaceWidth = calcWidth(" ") // used to calc # of spaces to erase with
  else
    loadMainFrameImg()
    rawDisplayStr("\$1200")  // Set character rate - 0=fastest
  fin
  return 0
end

///////////////////////////////////////////////////////////////////////////////////////////////////
// Display string on a blank screen, with blanking follow-up
def _intimate_displayStr(str)#1
  word width, i
  width = calcWidth(str)
  rawDisplayf1("^T020^V050%s", str) // ctrl-T = horizontal position, ctrl-V = vertical position
  rawDisplayStr("^T020^V050\$014") // ctrl-A = foreground color, 4=hi-bit-black
  for i = (width/spaceWidth)+1 downto 0
    displayChar(' ')
  next
  rawDisplayStr("\$017") // ctrl-A = foreground color, 7=hi-bit-white
  return 0
end

def _intimate_swipeImg()#1 // FIXME
  return 0
end

///////////////////////////////////////////////////////////////////////////////////////////////////
// Boilerplate module initialization code
return @funcTbl
done
