; ===========================================================================
; Toolbox.asm contains video routines
; This is just a place holder for CVS, NXDOS specific graphics routines
; and API calls can be stored here.
; ===========================================================================

;can't change es, di, dx, or ah.
;al and bx will get trashed, but they need not be preserved.

GX          dw 00h
GY          dw 00h
GColor      db 00h

vidtype         db 0

vmode_640x480 proc far
        mov  ax, 12h
        int  10h     ; 640x480
        mov  ax, 0a000h
        mov  es, ax
        ret
vmode_640x480 endp

vmode_text proc far
        mov  ax, 0003     
        int  10h
        ret
vmode_text endp

g_putpixel   proc far
; byte offset = Y * (horz_res / 8) + int(X / 8)

           mov  ax,GY                    ; calculate offset
           mov  dx,80                   ;
           mul  dx                      ; ax = y * 80
           mov  bx,GX                    ;
           mov  cl,bl                   ; save low byte for below
           shr  bx,03                   ; div by 8
           add  bx,ax                   ; bx = offset this group of 8 pixels

           mov  dx,03CEh                ; set to video hardware controller

           and  cl,07h                  ; Compute bit mask from X-coordinates
           xor  cl,07h                  ;  and put in ah
           mov  ah,01h                  ;
           shl  ah,cl                   ;
           mov  al,08h                  ; bit mask register
           out  dx,ax                   ;

           mov  ax,0205h                ; read mode 0, write mode 2
           out  dx,ax                   ;
           
           mov  al,es:[bx]              ; load to latch register
           mov  al,GColor
           mov  es:[bx],al              ; write to register

           ret
g_putpixel   endp

DrawLine proc far
; mov  word ptr GX,0001h       
; mov  word ptr GY,0001h            ;
; mov  byte ptr GColor,00h  
; mov  cx, 480           
         call g_putpixel
         inc  word ptr GX          
         inc  word ptr GY                  ;
         inc  byte ptr GColor  
         and  byte ptr GColor,0Fh       ; 00h - 0Fh only
         loop DrawLine                ; do it
         ret
drawline endp



; misc. 

pal_save proc far
;Save existing palette
	mov	ax,1017h		;get all palette registers.
	mov	bx,0			;first palette register.
	mov	cx,256			;read all palette registers.
	push	ds
	pop	es
	mov	dx,offset save_dacs
	int	10h

	ret
pal_save endp

pal_setgrey proc far
	mov	di,offset gray_dacs
	mov	al,0
pal_setgrey_1:
	mov	cx,3 * 256 / 64		;3 colors, 256 grays, 64 slots.
	rep	stosb

	inc	al
	cmp	al,64
	jb	pal_setgrey_1

	mov	dx,offset gray_dacs
	jmp	short pal_set

pal_restore:
	mov	dx,offset save_dacs
pal_set:
	mov	ax,1012h		;set all palette registers.
	mov	bx,0			;first palette register.
	mov	cx,256			;read all palette registers.
	push	ds
	pop	es
	int	10h

	ret
pal_setgrey endp

vga_test proc far
        mov  ax,1a00h
        int  10h
        cmp  al,1ah
        je   have_vga
have_ega:
        mov  al, 2
        jmp  short vgatestx
have_vga:
        mov  al, 1
vgatestx:
        ret
vga_test endp

init_vid proc far
        call vga_test
        mov  vidtype, al
        cmp  al, 1
        je   initvga
initega:
	mov	ax,10h			;put the screen into ega mode.
	int	10h

	mov	ax,1000h		;change the palette entry for 7
	mov	bx,77Q*256 + 7		;  to 77 octal.
	int	10h

	mov	ax,1000h
	mov	bx,007Q*256 + 08h
	int	10h

	mov	ax,1000h
	mov	bx,010Q*256 + 09h
	int	10h

	mov	ax,1000h
	mov	bx,020Q*256 + 0ah
	int	10h

	mov	ax,1000h
	mov	bx,030Q*256 + 0bh
	int	10h

	mov	ax,1000h
	mov	bx,040Q*256 + 0ch
	int	10h

	mov	ax,1000h
	mov	bx,050Q*256 + 0dh
	int	10h

	mov	ax,1000h
	mov	bx,060Q*256 + 0eh
	int	10h

	mov	ax,1000h
	mov	bx,070Q*256 + 0fh
	int	10h

        jmp  init_vid_x
initvga:
	mov	ax,13h			;put the screen into vga mode.
	int	10h

	call	pal_save		;save the palette
	call	pal_setgrey		;set to a gray palette.
init_vid_x:
	ret
init_vid endp

uninit_vid proc far
        mov  al, vidtype
        cmp  al, 1
        je   uninit_vga
uninit_ega:

        jmp  uninit2tm
uninit_vga:
	call	pal_restore

uninit2tm:
	mov	ax,3h			;put the screen into text mode.
	int	10h

	ret
uninit_vid endp

open_vid_vga:
;enter with cx,dx = point on screen.
;exit with es:di,ah -> byte/bit on screen.
	mov	ax,0a000h
	mov	es,ax
	mov	ax,320			;width of screen.
	mul	dx
	add	ax,cx			;add the offset in.
	mov	di,ax			;remember our pointer.
	ret


move_right_vga:
	inc	di
	ret

move_left_vga:
	dec	di
	ret

move_up_vga:
	sub	di,320			;width of screen.
	ret

move_down_vga:
	add	di,320			;width of screen.
	ret

set_bit_vga:
	mov	es:[di],al
	ret

close_vid_vga:
	ret


open_vid_ega:
;enter with cx,dx = point on screen.
;exit with es:di,ah -> byte/bit on screen.
	mov	ax,0a000h
	mov	es,ax
	mov	ax,640/8*2		;bytes per scan line * scan lines per pixel
	mul	dx
	mov	di,ax
	mov	ax,cx			;/8*2
	shr	ax,1
	shr	ax,1
	add	di,ax

	mov	dx,03ceh		;graphics controller
	mov	ax,0205h		;write mode 2.
	out	dx,al
	inc	dx
	mov	al,ah
	out	dx,al
	dec	dx
	mov	al,08h			;select bitmap register.
	out	dx,al
	inc	dx

	shl	cx,1			;compute the bit.
	and	cl,7
	mov	ah,80h
	shr	ah,cl
	ret


move_right_ega:
	shr	ah,1			;move right by a bit.
	ror	ah,1			;move right by another bit,
	adc	di,0			;  and handle byte increment.
	ret


move_left_ega:
	rol	ah,1			;move left by a bit,
	adc	di,0			;  and handle byte increment.
	shl	ah,1			;move left by another bit.
	ret


move_up_ega:
	sub	di,80*2			;width of screen.
	ret


move_down_ega:
	add	di,80*2			;width of screen.
	ret


set_bit_ega:
	mov	bl,al
	xor	bh,bh

	mov	al,ah
	out	dx,al
	mov	al,ulpal[bx]
	xchg	es:[di],al		;store the palette value.
	mov	al,llpal[bx]
	xchg	es:[di+80],al		;store the palette value.

	shr	ah,1			;move right by a bit.

	mov	al,ah
	out	dx,al
	mov	al,urpal[bx]
	xchg	es:[di],al		;store the palette value.
	mov	al,lrpal[bx]
	xchg	es:[di+80],al		;store the palette value.

	shl	ah,1			;restore the bit.

	ret


close_vid_ega:
	mov	al,0ffh			;mask = all ones.
	out	dx,al
	dec	dx
	mov	ax,0005h		;write mode 0
	out	dx,al
	inc	dx
	mov	al,ah
	out	dx,al

	ret

ulpal	db	20 dup(00h)
	db	20 dup(09h)
	db	20 dup(0bh)
	db	20 dup(0fh)
	db	20 dup(0fh)
	db	20 dup(0fh)
	db	20 dup(0fh)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(07h)
	db	20 dup(07h)
	db	20 dup(07h)
	db	20 dup(07h)
urpal	db	20 dup(00h)
	db	20 dup(00h)
	db	20 dup(0ch)
	db	20 dup(0eh)
	db	20 dup(0fh)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(07h)
llpal	db	20 dup(00h)
	db	20 dup(0ch)
	db	20 dup(0ch)
	db	20 dup(0dh)
	db	20 dup(0fh)
	db	20 dup(0fh)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(07h)
	db	20 dup(07h)
lrpal	db	20 dup(00h)
	db	20 dup(0ah)
	db	20 dup(0bh)
	db	20 dup(0bh)
	db	20 dup(0fh)
	db	20 dup(0fh)
	db	20 dup(0fh)
	db	20 dup(0fh)
	db	20 dup(08h)
	db	20 dup(08h)
	db	20 dup(07h)
	db	20 dup(07h)
	db	20 dup(07h)

save_dacs	db	256 * 3 dup(?)
gray_dacs	db	256 * 3 dup(?)



; ===========================================================================
; OpenGL Sample Implementation    URL: http://oss.sgi.com/projects/ogl-sample
; ===========================================================================

; ===========================================================================
ogl_initialize proc far
; Input : DS:SI = bdigrapgicsinit, passing null will use defaults
; Initializes the NXDOS Runtime API for OpenGL environment
; Should only be called once per program execution. Call ogl_getdefualts to
; fill the init structure before changing entries.
; ===========================================================================

         ret
ogl_initialize endp

; ===========================================================================
ogl_getdefaults proc far
; Input : DS:SI = bdigrapgicsinit
; ===========================================================================

         ret
ogl_getdefaults endp

; ===========================================================================
ogl_generate_character_texture_list proc far
; Scenario* scenario, int character_index
; ===========================================================================

         ret
ogl_generate_character_texture_list endp

;if texture count > 0 {
;   int texture_index
;   for texture index = 0 texture index < texture count texture index++
;       texture_object = ogl_get_character_texture_list_texture_object_at_index(scenario
