.286
.model small
.code
org 100h
start:
	call INIT	
	mov ax, 1
	int 33h
newl:
        mov ah, 06h
        mov dl, 0ffh
        int 21h
        jnz  OVER
		mov ax, 3
		int 33h
		test bx, 2
		jz dal
		call ShowMenu
dal:
		test bx,1
		jz newl

    	mov di, dx
		shl di, 8
		shl dx, 6
		add di, dx
		shr cx, 1
		add di, cx
		mov ax, 2
		int 33h
		mov ah, curcol
		mov es:[di], ah
		mov ax, 1
		int 33h
		jmp newl

over:   
		mov ax, 3h
		int 10h
;---------------------
	int 20h          ; All OVER!
;---------------------
;
;

INIT proc near
	pusha
	mov ax, 13h
	int 10h
	mov ax, 0a000h
	mov es, ax
	mov ax, 0
	int 33h
	popa
	ret
endp

DrawPal	proc 	near
		pusha
		mov		buttonPr, 0
		mov 	ax,	02
		int 	33h
		mov di, 0
		mov ah, 0 
		mov cx, 16
biglp:	push cx
		mov cx, 10
smalp:	push cx
		mov al, ah
		mov cx, 16
ano:	push cx
		mov cx, 10
		repe stosb
		pop cx
		inc al
		loop ano
		pop cx
		add di, 160
		loop SmaLp	
		pop cx
		add ah, 10h
		loop biglp			

		mov 	ax,	01
		int 	33h

;	setMOusePos
		mov ax, 4
        mov cx, 0
		mov dx, 0
		int 33h
;	SetMouseBounds
		mov ax, 7
		mov cx, 0
		mov dx, 140h
		int 33h
		mov ax, 8
		mov cx, 0
		mov dx, 0a0h
		int 33h


colsel:	mov ax, 03
		int 33h
		test bx, 1
		jz		blah3
		mov		buttonPr, 1
		jmp		COLsel
blah3:  cmp		ButtonPr,	1
		jne		COLsel
		
		mov ax, dx
		xor dx, dx
		mov bx, 10
		div bx
		shl ax, 4
		mov di, ax
		mov ax, cx
		mov bx, 20
		xor dx, dx
		div bx
        add di, ax
		mov ax, di
    	mov curcol, al	;	Save new color

		mov ax, 7
		mov cx, 0
		mov dx, 280h
		int 33h
		mov ax, 8
		mov cx, 0
		mov dx, 0c8h
		int 33h
	popa
	ret
endp
; 
SaveBuf	proc near
	pusha
	push es
	push ds
		push 	es
		pop 	ds
		push 	cs
		pop 	es		
		mov 	di, offset PalTabl
		mov 	si, 0
		mov 	cx, 0A0h
lp1:	push 	cx
		mov 	cx, 0a0h
		repe 	movsb
		pop 	cx
		add 	si, 0a0h
		loop 	lp1
	pop ds
	pop es
	popa
	ret
endp
;
; 
LoadBuf	proc near
	pusha
		mov 	di, 0
		push 	cs
		pop 	ds
		mov 	si, offset PalTabl
		mov 	cx, 0A0h
lp2:	push 	cx
		mov 	cx, 0a0h
		repe 	movsb
		pop 	cx
		add 	di, 0a0h
		loop 	lp2
	popa
	ret
endp
;
ShowMenu	proc	near
	pusha
		mov		buttonPr, 0
		mov 	ax,	1130h
		mov 	bh, 06h
		int 	10h
		mov 	FontSeg, es
		mov 	FontOffs, bp

		mov 	ax,	02
		int 	33h
		mov		ax, 0a000h
		mov 	es,	ax
		call	SaveBuf

		mov 	di, 0
		mov 	ax, 20
		mov 	cx, 0a0h

lp3:	push 	cx
		mov 	cx, 0a0h
		repe 	stosb
		pop 	cx
		add 	di, 0a0h
	    loop 	lp3		

	    mov 	di,	320*31
	    mov 	ax, 4
		mov 	cx, 4
lp4:    push 	cx
		mov 	cx, 160
		repe	stosb
		pop 	cx
		add		di, 320*31-160
		loop lp4

		push	0
		push	5
		call	DrawMenuLine
		push	1
		push	3
		call	DrawMenuLine
		push	2
		push	3
		call	DrawMenuLine
		push	3
		push	3
		call	DrawMenuLine
		push	4
		push	3
		call	DrawMenuLine

		mov 	ax,	01
		int 	33h 

;	setMOusePos
		mov ax, 4
        mov cx, 0
		mov dx, 0
		int 33h
;	SetMouseBounds
		mov ax, 7
		mov cx, 0
		mov dx, 140h
		int 33h
		mov ax, 8
		mov cx, 0
		mov dx, 0a0h
		int 33h

MENUsel:mov 	ax, 03
		int 	33h
		shr	    dx,	5
		cmp		dx,	curline
		je		blah
		push	dx
		mov		ax, 02
		int 	33h
		push	curline
		push	3
		call	DrawMenuLine
		pop		dx
		mov		curline,	dx
		push	curline
		push	5
		call	DrawMenuLine
		mov		ax, 01
		int 	33h
		jmp		menusel

blah:	test 	bx, 1
		jz		blah2
		mov		buttonPr, 1
		jmp		MENUsel
blah2:  cmp		ButtonPr,	1
		jne		MENUsel
		
		cmp		curline,	0
		jne		menu1

		mov 	ax,	02
		int 	33h
		push	offset	RodName
		call	Load
		mov 	ax, 7
		mov 	cx, 0
		mov 	dx, 280h
		int 	33h
		mov 	ax, 8
		mov 	cx, 0
		mov 	dx, 0c8h
		int 	33h
		mov 	ax,	01
		int 	33h
		popa
		ret
menu1:	cmp		curline,	1
		jne		menu2
		mov 	ax,	02
		int 	33h
		call	LoadBuf
		push	offset	RodName
		call	save
		mov 	ax, 7
		mov 	cx, 0
		mov 	dx, 280h
		int 	33h
		mov 	ax, 8
		mov 	cx, 0
		mov 	dx, 0c8h
		int 	33h
		mov 	ax,	01
		int 	33h
		popa
		ret
menu2:	cmp		curline,	2
		jne		menu3
		call	DrawPal
		jmp		menuend
menu3:	cmp		curline,	3
		jne		menuend
		jmp		over
;tablend
MENuend:	
		mov 	ax,	02
		int 	33h  
		call	LoadBuf
		mov 	ax, 7
		mov 	cx, 0
		mov 	dx, 280h
		int 	33h
		mov 	ax, 8
		mov 	cx, 0
		mov 	dx, 0c8h
		int 	33h
		mov 	ax,	01
		int 	33h

	popa
	ret
endp
;
;---------------------------------------------
OutCharXY proc near 		  ;OutCharXY(X,Y,Char,Color,bg,TableOffs,TableSeg)
	push bp
	mov bp, sp
TableSeg  	equ word ptr [bp+4]
TableOffs 	equ word ptr [bp+6]
Color		equ byte ptr [bp+10]
Bg	 		equ byte ptr [bp+8]
Char 		equ byte ptr [bp+12]
posY		equ	word ptr [bp+14]
posX		equ	word ptr [bp+16]
    pusha
    push es
;----------------------------
;  Character Creation
    	mov di, TableOffs
    	mov es,	TableSeg
    	mov ah, 0
    	mov al, Char
        shl ax, 4
		add di,	ax
        mov cx,	16
		mov si,	offset face
MainLoop:
		mov bh, es:di
		push cx
		mov cx, 8
LineLoop:
		test bh, 80h
        jz	BackGround
        mov ah, Color
        mov byte ptr ds:[si], ah
		jmp LoopEnd
BackGround:
        mov ah, Bg
	    mov byte ptr ds:[si], ah
LoopEnd:
		shl bh, 1
		inc si
		loop LineLoop
		pop cx
		inc di
		loop MainLoop
; End of Character creation
;=======================================
	mov ax,	posX
	push ax				; X position

	mov ax,	posY
	push ax 			; Y position

	push offset face    ; Character bitmap
	push 8				; SizeX
	push 16				; SizeY
    call WriteSprite	; WriteChar

    pop es
    popa
	pop bp
	ret 14
endp
;-------------------------------------------
WriteSprite proc near         ;writesprite(x,y,char *sprt,razmx,razmy)
    push bp
    mov bp,sp
razmy equ word ptr [bp+4]
razmx equ word ptr [bp+6]
sprt  equ word ptr [bp+8]
posy  equ word ptr [bp+10]
posx  equ word ptr [bp+12]
    pusha
    push es

    mov ax, 0A000h
    mov es, ax
    mov ax, posy
    mov bx, ax
    shl ax, 8
    shl bx, 6
    add ax, bx
    add ax, posx
    mov di, ax
    mov ax, sprt
    mov si, ax
    mov cx, razmy
lup:
    push cx
    mov cx, razmx
    repe movsb
    add di, 320
    sub di,razmx
    pop cx
    loop lup

    pop es
    popa
    pop bp
ret 10
endp

DrawMenuLine	proc	near	;DrawMenuLine(int Linenum,int color);
	push 	bp
	mov 	bp,	sp
Color4 	EQU	word ptr	[bp+4]
linenum	EQU	byte ptr	[bp+6]
	pusha
		cmp		linenum,	0
		jne		tst2
		push	64
		push	8
		push	offset	line1
		push	color4
		push	20
		push	FontOffs
		push	FontSeg
		call	OutTextXY
		jmp		fend
tst2:	cmp		linenum,	1
		jne		tst3
		push	64
		push	40
		push	offset	line2
		push	color4
		push	20
		push	FontOffs
		push	FontSeg
		call	OutTextXY
		jmp		fend
tst3:	cmp		linenum,	2
		jne		tst4
		push	60
		push	72
		push	offset	line3
		push	color4
		push	20
		push	FontOffs
		push	FontSeg
		call	OutTextXY
		jmp		fend
tst4:	cmp		linenum,	3
		jne		tst5
		push	64
		push	104
		push	offset	line4
		push	color4
		push	20
		push	FontOffs
		push	FontSeg
		call	OutTextXY
		jmp		fend

tst5:	push	64
		push	136
		push	offset	line5
		push	color4
		push	20
		push	FontOffs
		push	FontSeg
		call	OutTextXY
fend:
	popa
	pop		bp
	ret 4
endp

OutTextXY	proc	near
	push bp
	mov bp, sp
TableSeg2  	equ word ptr [bp+4]
TableOffs2 	equ word ptr [bp+6]
Color2		equ word ptr [bp+10]
Bg2	 		equ word ptr [bp+8]
String		equ word ptr [bp+12]
posY2		equ	word ptr [bp+14]
posX2		equ	word ptr [bp+16]
    pusha
    push	es
    		mov ax,	posx2
    		mov	di,	String
drlp:		cmp byte ptr cs:[di],	0
			je	drend
			push 	ax
			push	posy2
			mov 	bl,	cs:[di]
			mov 	bh,	0
			push 	bx
			push	color2
			push	bg2
			push	tableoffs2
			push	tableseg2
			call	outcharxy
			add		ax,	8
			inc		di
			jmp		drlp
drend:		
    pop		es
    popa
	pop bp
	ret 14
endp

Save	proc	near
	push	bp
	mov		bp,	sp
SFN	EQU	word ptr [bp+4]
	pusha
	push	es
		mov		ah,	3ch
		mov		cx,	0
		mov		dx,	SFN
		int		21h
		jc		mend
		mov		bx,	ax
		mov		ah,	40h
		mov		dx,	offset	buf
		mov		cx,	6
		int		21h

		mov		cx,	100h
		mov		dx,	3c7h
		mov		al,		0
		out		dx, al
		mov		di,	offset buf
pallp:  push	cx
		mov		dx,	3c9h
		in		al,	dx
		mov		di[0],	al
		in		al,	dx
		mov		di[1],	al
		in		al,	dx
		mov		di[2],	al

		mov		dx,	di
		mov		ah,	40h
		mov		cx,	3
		int		21h
		pop		cx
		loop	pallp

		push	ds
		push	es
		pop		ds
		mov		ah,	40h
		mov		dx,	0
		mov		cx,	0fA00h
		int		21h
		pop		ds

		mov		ah, 3eh
		int		21h		
mend:
	pop		es
	popa
	pop		bp
	ret		2
buf	db	'RODGfx'
endp

Load	proc	near
	push	bp
	mov		bp,	sp
LFN	EQU	word ptr [bp+4]
	pusha
	push	es
		mov		ax,	3d00h
		mov		dx,	LFN
		int		21h
		jc		mend2
		mov		bx,	ax
		mov		ah,	3fh
		mov		dx,	offset	buf
		mov		cx,	6
		int		21h

		mov		cx,	100h
		mov		dx,	3c8h
		mov		al,		0
		out		dx, al
		mov		di,	offset buf2

pallp2: push	cx
		mov		dx,	di
		mov		ah,	3fh
		mov		cx,	3
		int		21h

		mov		dx,	3c9h
		mov		al,	di[0]
		out		dx, al
		mov		al,	di[1]
		out		dx, al
		mov		al,	di[2]
		out		dx, al 
		pop		cx
		loop	pallp2

		push	ds
		push	es
		pop		ds
		mov		ah,	3fh
		mov		dx,	0
		mov		cx,	0fA00h
		int		21h
		pop		ds

		mov		ah, 3eh
		int		21h		
mend2:
	pop		es
	popa
	pop		bp
	ret		2
buf2	db	6 dup (?)
endp


face		db 128 dup (?)
FontOffs	dw ?
FontSeg		dw ?
curcol		db 	4
Line1		db	'Load',0
Line2		db	'Save',0
Line3		db  'Color',0
Line4		db	'Quit',0
Line5		db	'Back',0
curline		dw	0
buttonPr	db	0
RodName		db	'rod.pic',0
PalTabl		db	6400h dup (?)
end start