; BGI services: Make BGI functions metafilable and Gr-clippable
; Copr. 1994 Matthias K”ppe

	.286

		INCLUDE	grserv.asm
		LOCALS	@@

DATA		SEGMENT WORD PUBLIC

		ASSUME	DS: DATA

		EXTRN	MetaState: WORD, BgiClipState: WORD, \
			NeedToDraw: BYTE, ExtSave: DWORD, \
			BgiX: WORD, BgiY: WORD

RetAddr		DD	?
BgiMapping	DW	?

DATA		ENDS

CODE		SEGMENT BYTE PUBLIC

		ASSUME	CS: CODE

		EXTRN	Clip: NEAR

	Def_Mapping
	Def_MapSync

CODE		ENDS

ms_Draw		EQU	1
ms_Record	EQU	2

	; BgiMeta macro

BgiMeta		MACRO	DoSave, AddX, AddY
		push	bp
		mov	bp, sp
	IFNB <DoSave>
		TEST	MetaState, ms_Record
		JZ	SHORT @@NR
		PUSH	0
		PUSH	ArgSize
		PUSH	0
		MOV	AX, 3
		CALL	[ExtSave]
		MOV	BYTE PTR BgiClipState[1], 1
@@NR:		TEST	MetaState, ms_Draw
		jz	SHORT @@ND
		call	Clip
		cmp	NeedToDraw, 0
		jnz	SHORT @@D
@@ND:		leave
		retf	ArgSize
	ENDIF
@@D:
	IRP XArg, AddX
		add	XArg, ax
	ENDM
	IRP YArg, AddY
		add	YArg, dx
	ENDM
		leave
		ENDM

GetMapping	MACRO
		call	GetWindowNum
		mov	BgiMapping, dx
		mov	WindowNum, dx
		ENDM

SetMapping	MACRO
		mov	dx, BgiMapping
		call	CallMapAddr
		ENDM

Entry           MACRO   Name
	PUBLIC	Name
	EXTRN	Graph_&Name:FAR
Name		PROC FAR
		ENDM

	;
	; replacing call
	;

rCall		MACRO	Address
		pop	RetAddr
		call	Address
		push	RetAddr
		ENDM

	;
	; replacing call with mapping
	;

rCallm		MACRO	Address
		SetMapping
		rCall	Address
		GetMapping
		ENDM

Exit		MACRO
		ret
		ENDP
		ENDM
