// ----------------------------------------------------------------------------
#include "Framework/Config.h"
#include "Framework/Debug.h"
#include "Framework/ExposedAPI.h"

#include "Demo/BankConfig.h"

#include "Demo/Parts/RotoScroller.h"

// ----------------------------------------------------------------------------
IMPLEMENT_BOOTSTRAP( RotoScroller )

// ----------------------------------------------------------------------------
#define TEXTURE_SIZE 32 // must be power 2
#define TEXTURE_PTR 0x7000
#define FX_BANK 0xC5
#define TEX_Y_PTR 0x4000 // size = TEX_Y_SIZE
#define TEX_Y_SIZE ( 2 * TEXTURE_SIZE ) 
#define WIDTH (SCREEN_WIDTH_WORDS / 2)
#define HEIGHT 41

#define FONT_PTR ( 0x4000 + TEX_Y_SIZE )
#define FONT_WIDTH (680/2)
#define FONT_HEIGHT 16
#define FONT_CHAR_WIDTH 12 // 24 // 16
#define ROTO_OPCODES_PTR ( CODESEGMENT8000 )

#define ONDULPTR ( (unsigned char *) ( CODESEGMENT4000 ) )
#define ROTO_BGOPCODES_PTR ( (unsigned char *) ( CODESEGMENT4000 + 0x0F00 ) )

#define SPR1 ( (unsigned char *) ( 0xF800 ) )

#define SCRPEN0 0xFFFF
#define SCRPEN1 0x0000
#define SCRPEN2 0xC0C0
	
// ----------------------------------------------------------------------------
#define FONT_CHARWIDTH 12
#define FONT_CHARHEIGHT 16

// ----------------------------------------------------------------------------
static extern unsigned int tickCount;

static extern unsigned char iX;
static extern unsigned char iY;
static extern unsigned char offsetSinAngle;
static extern unsigned char scrOffset;
static extern unsigned short textureOffset;
static extern char pivotX;
static extern char pivotY;
static extern short u;
static extern short v;
static extern short _u;
static extern short _v;
static extern short xx;
static extern short yy;
static extern unsigned char angle;
static extern unsigned short timeBeginRoto;
static extern unsigned char *texYPtr[];
static extern unsigned char *endFramePtr;
static extern unsigned char *screenPtr;
static extern unsigned char inverse;
static extern unsigned char *bugPtr;
static extern char cosTable[];
static extern char sinTable[];
static extern unsigned char **scrLines;
static extern char scrollText[];
static extern char *scrollTextPtr;
static extern unsigned short fontOffset;
static extern char offsetInChar;
static extern char tempChar;
static extern unsigned char ** asmLinesOpcodes;
static extern unsigned short backgroundJumpAdr;

// ----------------------------------------------------------------------------
static void codeVars( )
{
_asm

_tempChar::
	.db 0

_fontOffset::
	.dw 0

_scrollText::
	.db "W"
	.db "H"
	.db "A"
	.db "T"
	.db " "
	.db "A"
	.db " "
	.db "B"
	.db "I"
	.db "G"
	.db " "
	.db "S"
	.db "C"
	.db "R"
	.db "O"
	.db "L"
	.db "L"
	.db " "
	.db " "
	.db " "
	.db "E"
	.db "V"
	.db "E"
	.db "N"
	.db " "
	.db "B"
	.db "E"
	.db "T"
	.db "T"
	.db "E"
	.db "R"
	.db " "
	.db "W"
	.db "I"
	.db "T"
	.db "H"
	.db " "
	.db "R"
	.db "O"
	.db "T"
	.db "A"
	.db "T"
	.db "I"
	.db "O"
	.db "N"
	.db " "
	.db "a"
	.db " "
	.db " "
	.db " "
	.db " "
	.db " "
	.db " "
	.db " "
	.db " "
	.db " "
	.db " "
	.db #0
	
_scrollTextPtr::
	.dw #0
	
_offsetInChar::
	.db #FONT_CHAR_WIDTH-1

_scrLines::
	.dw #0

_textureOffset::
	.dw #0

_bugPtr::
	.dw #0
	
_tickCount::
	.dw #0
	
_inverse::
	.db #0
	
_offsetSinAngle::
	.db #0
	
_scrOffset::
	.db #0
	
_pivotX::
	.db #0
	
_pivotY::
	.db #0
	
_angle::
	.db #0
	
_timeBeginRoto::
	.dw #0
	
_iX::
	.db #0
	
_iY::
	.db #0
	
_u::
	.dw #0
	
_v::
	.dw #0
	
__u::
	.dw #0
	
__v::
	.dw #0
	
_xx::
	.dw #0
	
_yy::
	.dw #0
	
_endFramePtr::
	.dw #0
							
_screenPtr::
	.dw #0
	
_cosTable::	
	.db #127, #126, #126, #126, #126, #126, #125, #125, #124, #123, #123, #122
	.db #121, #120, #119, #118, #117, #115, #114, #113, #111, #110, #108, #106
	.db #105, #103, #101, #99, #97, #95, #93, #91, #88, #86, #84, #82
	.db #79, #77, #74, #71, #69, #66, #64, #61, #58, #55, #52, #49
	.db #47, #44, #41, #38, #35, #32, #29, #25, #22, #19, #16, #13
	.db #10, #7, #4, #0, #-2, #-5, #-8, #-11, #-14, #-17, #-21, #-24
	.db #-27, #-30, #-33, #-36, #-39, #-42, #-45, #-48, #-51, #-54, #-56, #-59
	.db #-62, #-65, #-67, #-70, #-73, #-75, #-78, #-80, #-83, #-85, #-87, #-89
	.db #-92, #-94, #-96, #-98, #-100, #-102, #-104, #-105, #-107, #-109, #-110, #-112
	.db #-113, #-115, #-116, #-117, #-118, #-119, #-120, #-121, #-122, #-123, #-124, #-124
	.db #-125, #-125, #-126, #-126, #-126, #-126, #-126, #-126, #-126, #-126, #-126, #-126
	.db #-125, #-125, #-124, #-124, #-123, #-122, #-121, #-120, #-120, #-118, #-117, #-116
	.db #-115, #-113, #-112, #-111, #-109, #-107, #-106, #-104, #-102, #-100, #-98, #-96
	.db #-94, #-92, #-90, #-88, #-85, #-83, #-80, #-78, #-75, #-73, #-70, #-68
	.db #-65, #-62, #-60, #-57, #-54, #-51, #-48, #-45, #-42, #-39, #-36, #-33
	.db #-30, #-27, #-24, #-21, #-18, #-15, #-12, #-8, #-5, #-2, #0, #3
	.db #6, #9, #13, #16, #19, #22, #25, #28, #31, #34, #37, #40
	.db #43, #46, #49, #52, #55, #58, #60, #63, #66, #69, #71, #74
	.db #76, #79, #81, #84, #86, #88, #90, #93, #95, #97, #99, #101
	.db #103, #104, #106, #108, #109, #111, #112, #114, #115, #116, #118, #119
	.db #120, #121, #122, #123, #123, #124, #125, #125, #125, #126, #126, #126
	.db #126, #126

_sinTable::					
	.db #0, #3, #6, #9, #12, #15, #18, #21, #25, #28, #31, #34
	.db #37, #40, #43, #46, #49, #52, #54, #57, #60, #63, #65, #68
	.db #71, #73, #76, #78, #81, #83, #86, #88, #90, #92, #94, #96
	.db #98, #100, #102, #104, #106, #108, #109, #111, #112, #114, #115, #116
	.db #117, #119, #120, #121, #122, #122, #123, #124, #124, #125, #125, #126
	.db #126, #126, #126, #126, #126, #126, #126, #126, #126, #125, #125, #124
	.db #124, #123, #122, #121, #120, #119, #118, #117, #116, #114, #113, #112
	.db #110, #108, #107, #105, #103, #101, #100, #98, #96, #93, #91, #89
	.db #87, #85, #82, #80, #77, #75, #72, #70, #67, #64, #62, #59
	.db #56, #53, #50, #47, #44, #41, #38, #35, #32, #29, #26, #23
	.db #20, #17, #14, #11, #8, #4, #1, #-1, #-4, #-7, #-10, #-13
	.db #-17, #-20, #-23, #-26, #-29, #-32, #-35, #-38, #-41, #-44, #-47, #-50
	.db #-53, #-56, #-58, #-61, #-64, #-67, #-69, #-72, #-74, #-77, #-79, #-82
	.db #-84, #-87, #-89, #-91, #-93, #-95, #-97, #-99, #-101, #-103, #-105, #-107
	.db #-108, #-110, #-111, #-113, #-114, #-116, #-117, #-118, #-119, #-120, #-121, #-122
	.db #-123, #-123, #-124, #-125, #-125, #-126, #-126, #-126, #-126, #-126, #-126, #-126
	.db #-126, #-126, #-126, #-125, #-125, #-125, #-124, #-123, #-122, #-122, #-121, #-120
	.db #-119, #-118, #-116, #-115, #-114, #-112, #-111, #-109, #-108, #-106, #-104, #-103
	.db #-101, #-99, #-97, #-95, #-93, #-90, #-88, #-86, #-84, #-81, #-79, #-76
	.db #-74, #-71, #-68, #-66, #-63, #-60, #-58, #-55, #-52, #-49, #-46, #-43
	.db #-40, #-37, #-34, #-31, #-28, #-25, #-22, #-19, #-16, #-12, #-9, #-6
	.db #-3, #0

_endasm;
}
		
// ----------------------------------------------------------------------------
static void InitPalette( )
{
	unsigned char palette[] =
	{
		COLOR0,
		COLOR25,
		COLOR2,
		COLOR13,

		COLOR6,
		COLOR14,
		COLOR1,
		COLOR20,

		COLOR17,
		COLOR0, // NOT USED
		COLOR16,
		COLOR26,

		COLOR3,
		COLOR5,
		COLOR11,
		COLOR4
	};

	SetPalette( palette );
}

// ----------------------------------------------------------------------------
static extern unsigned char ** asmLines;
static extern unsigned short asmX;
static extern unsigned short jumpAdr;

// ----------------------------------------------------------------------------
void ASMDrawSprite( )
{
	_asm
		di
		
		.db #0x21 ; ld hl, lines+y
_asmLines::
		.dw #0x00
		
		.db #0x01 ; ld bc, startx
_asmX::
		.dw #0x00
		
		.db #0xCD ; call
_jumpAdr::
		.dw #0x00

		ei
		
	_endasm;
}

// ----------------------------------------------------------------------------
void RotoScroller( )
{
	Init( );
	
	do
	{
		DrawFrame( );
		
		FlipScreen( );
	//	WaitVBL( );
		
		tickCount++;
		
		if ( tickCount == 2 )
		{	
			ASM_DisableInterrupts();
			InitPalette( );	
			ASM_EnableInterrupts();
		}
	} while( tickCount != 267 );
	
	SetBorderAndColors( COLOR0 );
} 

// ----------------------------------------------------------------------------
void InitBug()
{
	scrLines = GetBackLines( );
	bugPtr = scrLines[99] + 94;	
	
	PushBank( BANKC0 );
	
	_asm
	push hl
	push de
	ld hl, ( #_bugPtr )
	ld a, ( hl )
	ld de, #bug1 + 1
	ld ( de ), a
	inc hl
	ld a, ( hl )
	ld de, #bug2 + 1
	ld ( de ), a
	inc hl
	ld a, ( hl )
	ld de, #bug3 + 1
	ld ( de ), a
	inc hl
	ld a, ( hl )
	ld de, #bug4 + 1
	ld ( de ), a
	inc hl
	pop de
	pop hl
	_endasm;
	
	PopBank( );
}

// ----------------------------------------------------------------------------
void ClearBug()
{
	PushBank( BANKC0 );
	
	_asm
	push hl
	ld hl, ( #_bugPtr )
bug1::
	ld ( hl ), #255
	inc hl
bug2::
	ld ( hl ), #255
	inc hl
bug3::
	ld ( hl ), #255
	inc hl
bug4::
	ld ( hl ), #255
	pop hl
	_endasm;
	
	PopBank( );
}

// ----------------------------------------------------------------------------
static void ScrollText( )
{
	unsigned char *texture = (unsigned char *) TEXTURE_PTR;
	unsigned char *font = (unsigned char *) FONT_PTR;
	//char iY;
	unsigned short offsetTex = 0;
	unsigned short offsetFont = 0;
	unsigned char currentChar;
	
	offsetInChar++;
	if ( offsetInChar == FONT_CHAR_WIDTH )
	{
		offsetInChar = 0;
		scrollTextPtr++;
		
		currentChar = *scrollTextPtr;
		if ( currentChar == 0 )
		{
			scrollTextPtr = scrollText;
			currentChar = *scrollTextPtr;
		}
		
		if ( currentChar == ' ' )
		{
			currentChar = 'Z' + 2;
			offsetInChar = FONT_CHAR_WIDTH / 2;
		}	
		else if ( currentChar == 'a' ) // !
		{
			currentChar = 'Z' + 1;
			offsetInChar = FONT_CHAR_WIDTH / 2;
		}
		
		currentChar -= 65;
		
		fontOffset = currentChar * FONT_CHAR_WIDTH;
	}
	
	
	PushBank( FX_BANK );	
	
//	Break;

	texture += scrOffset;
	//font += textureOffset;
	font += fontOffset;
	
	scrOffset++;
	if ( scrOffset >= TEXTURE_SIZE )
	{
		scrOffset = 0;
	}

	// center font in texture
	offsetTex += (TEXTURE_SIZE*2) * ( ( TEXTURE_SIZE - FONT_HEIGHT ) / 2 );
		
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	texture[ offsetTex ] = texture[ offsetTex + TEXTURE_SIZE ] = font[ offsetFont ]; offsetFont += FONT_WIDTH; offsetTex += TEXTURE_SIZE * 2;
	
	PopBank( );	
	
	fontOffset++;
	/*textureOffset++;
	if ( textureOffset >= ( FONT_WIDTH - 1 ) )
	{
		textureOffset = 0;
	}*/
}

// ----------------------------------------------------------------------------
void Init( )
{
	unsigned char iX = 0;
	unsigned char iY = 0;
	unsigned char *texture = (unsigned char *) TEXTURE_PTR;
	unsigned char **texYPtr = (unsigned char **) TEX_Y_PTR;
	
	scrollTextPtr = scrollText - 1;
	
	ClearScreen( );

	PushBank( ROTOSCROLLER_BOTTOMBINPCK_BANK );		
	MemCpy( CODESEGMENT8000, ROTOSCROLLER_BOTTOMBINPCK_PTRINBANK, ROTOSCROLLER_BOTTOMBINPCK_SIZE );
	PushBank( BANKC0 );		
	Unpack( CODESEGMENT8000, 0x4000 );
	Unpack( CODESEGMENT8000, 0xC000 );
	PopBank( );
	PopBank( );

	PushBank( ROTOSCROLLER_TOPBINPCK_BANK );		
	MemCpy( CODESEGMENT8000, ROTOSCROLLER_TOPBINPCK_PTRINBANK, ROTOSCROLLER_TOPBINPCK_SIZE );
	PushBank( BANKC0 );		
	Unpack( CODESEGMENT8000, 0x8080 );
	Unpack( CODESEGMENT8000, 0x0080 );
	PopBank( );
	PopBank( );

	InitBug( );
	
	PushBank( ROTOSCROLLER_BGSTARSBINPCK_BANK );	
	MemCpy( CODESEGMENT8000, ROTOSCROLLER_BGSTARSBINPCK_PTRINBANK, ROTOSCROLLER_BGSTARSBINPCK_SIZE );	
	PopBank( );
	PushBank( BANKC0 );
	Unpack( CODESEGMENT8000, ROTO_BGOPCODES_PTR );
	PopBank( );	
	backgroundJumpAdr = (unsigned short) ROTO_BGOPCODES_PTR;
	
	PushBank( ROTOSCROLLER_ONDULBINPCK_BANK );	
	MemCpy( CODESEGMENT8000, ROTOSCROLLER_ONDULBINPCK_PTRINBANK, ROTOSCROLLER_ONDULBINPCK_SIZE );	
	PopBank( );
	PushBank( BANKC0 );
	Unpack( CODESEGMENT8000, ONDULPTR );
	PopBank( );

	PushBank( FONTPUSHBCBINPCK_BANK );	
	MemCpy( CODESEGMENT8000, FONTPUSHBCBINPCK_PTRINBANK, FONTPUSHBCBINPCK_SIZE );	
	PopBank( );
	PushBank( FX_BANK );
	Unpack( CODESEGMENT8000, FONT_PTR );
	PopBank( );
	
	PushBank( FX_BANK );

	for ( iY = 0; iY < TEXTURE_SIZE; ++iY )
	{
		texYPtr[ iY ] = texture;

		texture += TEXTURE_SIZE*2;
	}

	texture = (unsigned char *) TEXTURE_PTR;

	for ( iY = 0; iY < TEXTURE_SIZE; iY++ )
	{
		for ( iX = 0; iX < TEXTURE_SIZE * 2; iX++ )
		{
			*texture = 0xC5; // ( iY & (TEXTURE_SIZE-1) ) >= (TEXTURE_SIZE/2) ? ( ( iX & (TEXTURE_SIZE-1) ) < (TEXTURE_SIZE/2) ? 0xC5 : 0xE5 ) : ( ( iX & (TEXTURE_SIZE-1) ) < (TEXTURE_SIZE/2) ? 0xE5 : 0xC5 );

			texture++;
		}
	}

	PopBank( );

	PushBank( BHMASK15_SPRITEBINPCK_BANK );	
	Unpack( BHMASK15_SPRITEBINPCK_PTRINBANK, SPR1 );
	PopBank( );	
}	

// ----------------------------------------------------------------------------
static void DrawBackgroundStars( )
{	
	_asm
		push hl
		push bc
		push de
		push af
		
		.db #0x21 ; ld hl, lines
_asmLinesOpcodes::
		.dw #0x00
		
		.db #0xCD ; call
_backgroundJumpAdr::
		.dw #0x00		
		
		pop af
		pop de
		pop bc
		pop hl
	_endasm;
}

// ----------------------------------------------------------------------------
static void CreateRotoBitmap( )
{
_asm		
	push af
	push bc
	push de
	push hl
	exx
	ex af, af'
	push af
	push bc
	push de
	push hl
	
	ld h, #0
	ld a, ( #_scrOffset )
	ld l, a
	ld ( #offsetX1 + #1 ), hl
	ld ( #offsetX2 + #1 ), hl
	
	ld ( #createRotoSP + #1 ), sp
	di
	ld sp, #ROTO_OPCODES_PTR+WIDTH*HEIGHT+(HEIGHT*4) // (HEIGHT*4) = jp/jp+jp/jp

loop::
	
	ld bc, #endDrawLine	
	ld h, #0
	ld l, b
	push hl // make jump at end of line!
	ld h, c
	ld l, #0xC3
	push hl // make jump at end of line!
	
	ld hl, ( #_v ) // _global!!
	ld bc, ( #_yy ) // _global!!
	
	exx
	ld hl, ( #_u ) // _global!!
	ld bc, ( #_xx ) // _global!!
	exx
	
startLoopX::
	
	ld a, h
	add hl, bc // _v += yy;
	ld ( #vSave + #1 ), hl
	//and #31
	sla a // *2
	ld h, #0x40 // table ptr tex
	ld l, a
	ld a, ( hl )
	inc l
	ld h, ( hl )
	ld l, a
	
	exx
	ld a, h
	//and #31	
	add hl, bc // _u += xx; 
	exx
	
	ld d, #0
	ld e, a
	add hl, de	
	
offsetX1::
	ld de, #0x0000// texture offset (scroll)
	add hl, de
	
	ld a, ( hl ) // texel
	ex af, af'
	
vSave::
	ld hl, #0
	
	ld a, h
	add hl, bc // _v += yy;
	ld ( #vSave2 + #1 ), hl
	//and #31
	sla a  // *2
	ld h, #0x40 // table ptr tex
	ld l, a
	ld a, ( hl )
	inc l
	ld h, ( hl )
	ld l, a
	
	exx
	ld a, h
	//and #31	
	add hl, bc // _u += xx; 
	exx
	
	ld d, #0
	ld e, a
	add hl, de	
	
offsetX2::
	ld de, #0x0000 // texture offset (scroll)
	add hl, de
	
	ld e, ( hl ) // texel
	ex af, af'
	ld d, a
	
	push de
	
vSave2::
	ld hl, #0

loopX::
	ld a, #WIDTH/2
	dec a
	ld ( #loopX + #1 ), a
	jp nz, startLoopX
	ld a, #WIDTH/2
	ld ( #loopX + #1 ), a

	ld ( #saveSP2 + 1 ), sp
	ld sp, ( #createRotoSP + 1 )
	ei
	
	//ld hl, #0xF3FB // DI/EI
	//push hl
	
	ld hl, ( #_u ) // _global!!
	ld bc, ( #_yy ) // _global!!
	add hl, bc // u += yy;
	ld ( #_u ), hl // _global!!
	
	ld hl, ( #_v ) // _global!!
	ld bc, ( #_xx ) // _global!!
	or a // reset C flag
	sbc hl, bc // v -= xx;
	ld ( #_v ), hl // _global!!

	di
saveSP2::
	ld sp, #0
	
loopY::
	ld a, #HEIGHT
	dec a
	ld ( #loopY + #1 ), a
	jp nz, loop
	ld a, #HEIGHT
	ld ( #loopY + #1 ), a
	
	//ld ( #_endFramePtr ), ix
createRotoSP::
	ld sp, #0
	ei
	
	pop hl
	pop de
	pop bc
	pop af
	ex af, af'
	exx
	pop hl
	pop de
	pop bc
	pop af
	
_endasm;
}

// ----------------------------------------------------------------------------
static void DrawRotoBitmap( )
{
_asm		
	push af
	push bc
	push de
	push hl
	exx
	ex af, af'
	push af
	push bc
	push de
	push hl
	
	ld a, #1
	ld ( #drawYChar + #1 ), a
	
	ld hl, #ROTO_OPCODES_PTR-4-(WIDTH) // +4 = jp end of line
	ld ( #currCodePtr + #1 ), hl
	
	exx
	ld de, ( #_scrLines )
	ld bc, #WIDTH*2 // *2 because words to byte
	exx
	
	ld hl, #SCRPEN0
	ld bc, #SCRPEN1
	ld de, #SCRPEN2
	
	ld a, #205
drawLines::
	ex af, af'
	
drawYChar::
	ld a, #1
	dec a	
	or a
	jp nz, #drawYCharEnd
	
	
	ld hl, ( #currCodePtr + #1 )
	
	ld bc, #4+(WIDTH) // +4 = jp end of line
	add hl, bc
	ld ( #currCodePtr + #1 ), hl
	
	ld hl, #SCRPEN0
	ld bc, #SCRPEN1
	
	ld a, #5
drawYCharEnd::
	ld ( #drawYChar + #1 ), a
	
	exx 
	ld a, ( de )
	ld l, a
	inc de
	ld a, ( de )
	ld h, a
	inc de
	
	add hl, bc // add width
	
	di
	ld ( #drawRotoSP + #1 ), sp	
	
	ld sp, hl
	exx
	
currCodePtr::
	jp endDrawLine
	
endDrawLine::

drawRotoSP::
	ld sp, #0
	
	ei
	
	ex af, af'
	dec a
	jp nz, drawLines	
	
	pop hl
	pop de
	pop bc
	pop af
	ex af, af'
	exx
	pop hl
	pop de
	pop bc
	pop af
	
_endasm;
}

// ----------------------------------------------------------------------------
void DrawSprite( unsigned char x, unsigned char y )
{
	asmLines = GetBackLines( ) + y;	
	asmX = x >> 1;
	
	if ( x & 1 )
	{
		// jumpAdr = (unsigned short) BALL816_UNPAIRED;
		Break;
	}
	else
	{
		jumpAdr = (unsigned short) ONDULPTR;
	}
	
	ASMDrawSprite( );
}

// ----------------------------------------------------------------------------
void DrawFrame( )
{	
	ScrollText( );
	ScrollText( );
	
	/*for ( iY = 0; iY < HEIGHT; iY++ )
	{
		_u = u;
		_v = v;
		
		for ( iX = 0; iX < WIDTH; iX++ )
		{
			texPtr = TEXTURE_PTR; 
			texPtr += ( ( ( _v >> 8 ) & 31 ) << 6 ) + ( ( _u >> 8 ) & 31 );
			
			*( screenPtr++ ) = *( texPtr ); 
			_u += xx; 
			_v += yy;
		}
		
		v -= xx;
		u += yy;
	}*/
	
	if ( inverse == 0 )
	{
		xx = cosTable[ angle ] << 1;
		yy = sinTable[ angle ] << 1;
	}
	else
	{
		xx = -cosTable[ angle ] << 1;
		yy = -sinTable[ angle ] << 1;
	}
	
	// zoom
	xx -= ( xx >> 1 );
	yy -= ( yy >> 1 );
	
	// x
	u = -xx * ( WIDTH >> 1 );
	v = -yy * ( WIDTH >> 1 );
	
	// y
	u -= yy * ( HEIGHT >> 1 );
	v += xx * ( HEIGHT >> 1 );		
	
	// center texture
	u += ( TEXTURE_SIZE >> 1 ) << 8;
	v += ( TEXTURE_SIZE >> 1 ) << 8;
	
	PushBank( FX_BANK );
	screenPtr = ROTO_OPCODES_PTR + WIDTH*HEIGHT + (HEIGHT*4);	// (HEIGHT*4) = jp/jp+jp/jp
	CreateRotoBitmap( );	
	PopBank( );	
	
	//Break;
	
	scrLines = GetBackLines( );
	bugPtr = scrLines[99] + 94;
	DrawRotoBitmap( );
	
	PushBank( BANKC0 );	
	DrawSprite( 88, 0 );
	PopBank( );
	
	asmLinesOpcodes = GetBackLines( );
	DrawBackgroundStars( );

	PushBank( BANKC0 );	
	
	asmLinesOpcodes = GetBackLines( );
	DrawBackgroundStars( );	
	
	asmLines = GetBackLines( ) + 170;	
	asmX = 3;
	jumpAdr = (unsigned short) SPR1;	
	ASMDrawSprite( );
	
	PopBank( );
	
	//Break;
	scrLines = GetFrontLines( );
	bugPtr = scrLines[99] + 94;
	ClearBug();
	
#define ROTOSTART 129

	timeBeginRoto++;
	if ( ( timeBeginRoto >= ROTOSTART ) ) // && ( timeBeginRoto <= (ROTOSTART+127) ) )
	{
		angle+=2;
		if ( angle > 127 )
		{		
			angle = 0;
			
			if ( inverse == 0 )
			{
				inverse = 1;
			}
			else
			{
				inverse = 0;
			}
		}
	}	
}
