initialize_zoom:
	ld hl,terrain_buffer
	ld de,terrain_buffer+1
	ld (hl),0
	ld bc,34
	ldir
	ret

	
	
	
#define scaling_step 4
	
#macro scaling_spread( val )
 #if val < 24*scaling_step
   .db (val%24)+(val/24)
   scaling_spread(eval(val+scaling_step))
 #endif
#endmacro

zoom_table:
 .db 23
 .db 15
 .db 19
 .db 11
 .db 3
 .db 7
 .db 23-1
 .db 15-1
 .db 19-1
 .db 11-1
 .db 3-1
 .db 7-1
 .db 23-1-1
 .db 15-1-1
 .db 19-1-1
 .db 11-1-1
 .db 3-1-1
 .db 7-1-1
 .db 23-1-1-1
 .db 15-1-1-1
 .db 19-1-1-1
 .db 11-1-1-1
 .db 3-1-1-1
 .db 7-1-1-1
 
 

;scaling_spread(0)
	
;a - number to zoom (24 is zoom ni all the way)
zoom_screen_base:
	set f_text,(iy+flags)
	ld a,24
	call do_zoom
	
	ld b,55
_	
	push bc
	bcall(_grbufcpy)
	pop bc
	djnz -_
	res f_text,(iy+flags)
	
	call sub_sync
	
	ld b,5
	ld hl,tile_table+(5*768)-1
zoom_out_loop:
	push bc
	ld de,sprite_buffer+768-1
	ld bc,768
	lddr
	push hl
	bcall(_grbufcpy)
	call sub_sync
	pop hl
	pop bc
	djnz zoom_out_loop

	ld a,1
	ld (p_com_ctr),a		;next one will cause a switch
	
	PCALL(restore_current_tileset)
	jp great_update
	
	
do_zoom:
	ld de,tile_table
	exx
	ld (p_com_ctr),a
	ld b,a
	push bc
	call initialize_zoom
	pop bc
	sra b
zoom_loop:
	push bc
	call widen_row
	call widen_row
	bcall(_grbufcpy)
	pop bc
	ld a,b
	exx
	dec a
	jr z,_
	and %1
	jr nz,_
	ld hl,sprite_buffer
	ld bc,768
	ldir
_
	exx
	djnz zoom_loop

	ret
	
widen_row:
	ld a,(p_com_ctr)
	dec a
	ld (p_com_ctr),a
	ld b,0
	ld c,a
	ld hl,zoom_table
	add hl,bc
	ld a,(hl)
	
	ld hl,terrain_buffer
	ld d,0
	ld e,a
	add hl,de
	
	inc a
	cp (hl)
	jr nz,got_a_row
_
	inc a
	inc hl
	cp 25
	jr c,_
	ld a,1
	ld hl,terrain_buffer
_
	cp (hl)
	jr nz,got_a_row
	jp --_
got_a_row:
	ld d,a
	ld (hl),a
widen_row_up:
	dec a		;0 to 23
	cp 8
	push af
	add a,24	;24-47
	
	ld c,a
	;for each one that's in front of you, you'll move one less
	ld a,48
	sub c
	ld b,a
	dec b\ inc hl
	jr z,no_comps
_
	xor a
	or (hl+)
	jr z,+_
	dec c
	jr z,no_comps
_	djnz --_
no_comps:
	pop af
	ld a,c
	jr c,widen_column
	
	sub 16		;16 to 32
	
	ld h,0
	ld l,a
	
	mul hl,12

	ld b,h
	ld c,l

	ld hl,sprite_buffer+12
	ld de,sprite_buffer
	push bc
	ldir
	pop bc

	ld hl,sprite_buffer+767-12
	ld de,sprite_buffer+767
	lddr

	add a,16
	
widen_column:
	ld hl,sprite_buffer
	ld b,64
widen_column_loop:
	push bc
	
	push af
	push hl
	call rotate_row_bytes_left
	pop hl
	pop af
	ld de,12
	add hl,de
	
	pop bc
	djnz widen_column_loop
	
	ld hl,sprite_buffer+11
	ld b,64
widen_column_r_loop:
	push bc
	
	push af
	push hl
	call rotate_row_bytes_right
	pop hl
	pop af
	ld de,12
	add hl,de
	
	pop bc
	djnz widen_column_r_loop
	
	ret


	
rotate_row_bytes_right:
	ld c,a
	sra a\ sra a\ sra a
	ld b,a
	push bc
	cpl
	ld e,a
	ld d,$FF
	inc de
	add hl,de
	call rotate_edge_byte_right
	pop bc
	ex af,af'
	ld a,b
	or a
	ret z
	ex af,af'
	inc hl
_
	rr (hl+)
	djnz -_
	ret
	
	
	
rotate_row_bytes_left:
;requires a reference in a
	ld c,a
	sra a\ sra a\ sra a		;byte address of bit to copy
	ld e,a
	ld d,0
	push de
	add hl,de
	call rotate_edge_byte_left
	pop de
	ld b,e
	ex af,af'
	ld a,b
	or a
	ret z
	ex af,af'
	dec hl
finish_row_loop:
	rl (hl-)
	djnz finish_row_loop
	ret
	
rotate_edge_byte_left:
	;hl points to the byte
	;c is column reference
	ld b,(hl)	;back up the data
	ld a,(hl)
	push hl
	rl b		;b has whole byte rotate left
rebfinish:
	push af		;save carry and backup data
	ld a,c
	and %111	;remainder
	ld hl,msk_tab
	ld d,0
	ld e,a
	add hl,de
	ld e,(hl)
	pop af
	push af
	and e		;how much of the old we want ot keep
	ld d,a		;base
	ld a,e
	cpl
	and b		;and the new mask with the rotate part
	or d		;final byte
	ld b,a
	pop af
	pop hl
	ld (hl),b
	ret
	
rotate_edge_byte_right:
	;hl points to the byte
	;c is column reference
	ld b,(hl)	;back up the data
	ld a,(hl)
	push hl
	rr b		;b has whole byte rotate left
	push af		;save carry and backup data
	ld a,c
	and %111	;remainder
	cpl
	ld hl,msk_tab+8
	ld d,$FF
	ld e,a
	inc de
	add hl,de
	ld a,(hl)
	cpl
	ld e,a
	pop af
	push af
	and e		;how much of the old we want ot keep
	ld d,a		;base
	ld a,e
	cpl
	and b		;and the new mask with the rotate part
	or d		;final byte
	ld b,a
	pop af
	pop hl
	ld (hl),b
	ret
	
	
msk_tab:
	.db %11111111
	.db %01111111
	.db %00111111
	.db %00011111
	.db %00001111
	.db %00000111
	.db %00000011
	.db %00000001
	.db %00000000