( graphics DAY 05.04.2001 )

needs ekey os_events

: line ( x1 y1 x2 y2 -- )
   WinDrawLine
;

: plot ( x y )
   2dup WinDrawLine
;

160 value maxx
160 value maxy

api: WinGetDisplayExtent
: InitXY
  rvarw rvarw
  WinGetDisplayExtent
  wr> to maxy wr> to maxx
;

create rect1 0 , 0 ,
variable cx
variable cy
variable dd

: diag ( x y -- x1 y1 )
    1- swap 1+ swap
    2dup - 2* 2+ dd +!
;

: cplot ( x y )
   cy @ + swap cx @ + 
   swap 2dup WinDrawLine
;

: _circle ( rad )
   dup 1 < if drop cx @ cy @ 
              plot exit then
   0 swap dup
   1 - negate 2* dd ! \ x y d
   begin
   dd @
   if
     dd @ dup 0<
     if  
       2 pick - 2* 1- 0>
       if   diag
       else \ hor
         swap 1+ dup
         2* 1+ dd +!
         swap
       then
     else
       over + 2* 1- 0> 
       if   \ vert
         1- dup 2* negate 1+ dd +!
       else diag then
     then
   else diag 
   then
   2dup
   over negate over
   over negate over negate
   over negate over
   cplot cplot cplot cplot
   dup 0=
   until 2drop
;

: circle ( x y rad )
   >r cy ! cx !
   r> _circle
;

api: WinDrawRectangleFrame 

1     constant simpleFrame
0x401 constant roundFrame
0x702 constant boldRoundFrame
0x205 constant popupFrame
0x302 constant dialogFrame

: rect! ( x y w h rect )
    >r
    r@ 6 + w!
    r@ cell+ w!
    r@ 2+ w!
    r> w!
;

: frame ( x y w h type -- )
   >r rect1 rect!
      r> rect1 >abs 
      WinDrawRectangleFrame 
;

: atScreen ( x y )
   CurY ! CurX !
;

create screenRect 0 , 160 w, 160 w,

api: WinCreateOffscreenWindow 

: CreateOffScreen ( w h -- handle )
    0 rvarw 
    WinCreateOffscreenWindow
    wr> throw
;

API: FntAverageCharWidth
api: FntLineHeight

: AT-XY ( x y -- )
   FntLineHeight *
   swap FntAverageCharWidth *
   CurX ! CurY !
;

api: WinDrawBitmap
api: WinScreenMode

: ShowBitmap ( type id ) 
  DmGetResource dup dup
  MemHandleLock
  0 0 WinDrawBitmap 
  MemHandleUnlock throw 
  DmReleaseResource throw
;


variable screenDepth
variable scolor
variable oldDepth
variable oldColor

: OldScreenMode ( mode )
   0 0 
   OldDepth >abs
   OldColor >abs
   WinScreenMode throw
;

: SaveScreenMode
   1 OldScreenMode
;

: RestoreScreenMode
   3 OldScreenMode
;

: ColorScreen? ( -- f )
   5 0 0 0 scolor >abs
   WinScreenMode throw
   scolor w@ 0<>
;

: SetMaxDepth ( sdepth )
\ set up the screen depth <= sdepth
\ sdepth = 16,8,4,2,1
   4 0 0 screenDepth >abs
   0 WinScreenMode throw
   1 swap 0 do 2* loop
   2*
   begin
     2/ dup screenDepth @ and
   until 
   0 >r begin 2/ rp@ 1+! dup 0= until
   drop r>
   screenDepth !
   3 0 0 screenDepth >abs
   scolor >abs WinScreenMode throw
;

: splash ( type id )
   ShowBitmap
   begin
     ekey 1 = ( penDownEvent)
   until
   begin ekey 0= until
;
