' example of DXImageList handling from a DXG file.
' first file is always begin with 0. this could be
' a good example making User Interface for DirectX
' game. [zaidon - gp500m@yahoo.com]

$TYPECHECK ON
$INCLUDE "RAPIDQ.INC"

DEFSTR Rider$(1 TO 29) = {"V Rossi", "T Ukawa", "S Itoh",_
						 "K Robert Jr", "S Gibernau", "A Ryo",_
						 "M Biaggi", "C Checa", "J Kocinski",_
						 "N Abe", "P Riba", "A Criville",_
						 "A Barros", "L Capirossi", "O Jacque",_
						 "S Nakano", "D Kato", "G McCoy",_
						 "J Hopkins", "R Laconi", "JVD Goorbergh",_
						 "J McWilliams", "N Aoki", "T Harada",_
						 "A Yanagawa", "M Doohan", "Shinichi",_
						 "A Catchcart","Year 2002"}

SUB DummyProc:END SUB

DIM Form AS QForm
    Form.BorderStyle = bsSingle
    Form.DelBorderIcons(2)
    Form.ClientHeight = 94
    Form.ClientWidth = 183
    Form.Center
    Form.WndProc = DummyProc
DIM xScreen AS QDXScreen
    xScreen.Parent = Form
    xScreen.Init(94, 183)
    xScreen.Align = alClient
DIM DXImageList AS QDXImageList
DIM UiImgList AS QDXImageList
DIM DXTimer AS QDXTimer
DIM UsrTimer AS QTimer
DIM Font AS QFont
    Font.Name = "Verdana"
    Font.Size = 8
    'Font.AddStyles(fsBold)
DIM X AS DOUBLE, bX AS DOUBLE, ls AS DOUBLE, rs AS DOUBLE, pcH AS Integer
DIM BikeX AS DOUBLE, BikeY AS DOUBLE, CurPnt AS DOUBLE, ReX AS DOUBLE
    BikeX = 0: BikeY = 0: ls=1: rs=3: CurPnt=0: bX=0: ReX=73: pcH=0

SUB DXTimerExpired
  'xScreen.Fill(0)

  ' control screensaver
  IF pcH >= 119 THEN
 	IF pcH >= 310 THEN
		pcH = 120
		BikeX = -183: BikeY = -94
	END IF

	BikeX=BikeX+1: BikeY=BikeY+1
  	
  	IF BikeX<=0 AND BikeY<=0 THEN
  		X = 29
  	ELSEIF BikeY=1 AND BikeX<=0 THEN
  		BikeY = 0: X = 29
  	ELSE
	  	X = ROUND(RND(28))
		BikeX = 0: BikeY = 0
		'UsrTimer.Enabled = 0
		SLEEP 1
		'UsrTimer.Enabled = 1
    END IF
  	pcH = pcH + 1
  END IF
  
  DXImageList.Draw(0, 0,  0,    0)
   'xScreen.Color = -2147483647 

  'draw animated background from UserInterface imglist
  'UiImgList.Draw(0, bX,  0,    0)
                  '|  |-X |-Y   |- Mask
                  '|- Image Index

  ' special BikeX and BikeY re-positioning on img_index=28
  IF X = 28 THEN
    BikeX = 10: BikeY = 10: ReX = 50
  ELSEIF X<28 THEN
    BikeX = 0: BikeY = 0: ReX=73
  END IF

  ' Display next or previous bike img from X value.
  DXImageList.Draw(X, BikeX, BikeY, 0)
  
  ' Painting a text
  xScreen.Font = Font
  xScreen.TextOut(ReX, 82, Rider$(X), &H0000FF, -1)
  
  ' debug timer
  xScreen.TextOut(82, 2, STR$(pcH), &H06AA06, -1)
  
  'draw navigate buttons
  UiImgList.Draw(ls, 2,  2,    0)
  UiImgList.Draw(rs, 167,  2,  0)

  'bX = bX - 1
  'IF bX = -137 THEN   ' Reset position
  '  bX = 0
  'END IF
  ' Always remember to call this, or else nothing will be shown
  xScreen.Flip
END SUB

SUB UserCome
	pcH = 0
	IF X=29 THEN
		X=1
	END IF
	BikeX = 0: BikeY = 0
END SUB

SUB UserIdle
	IF pcH<120 THEN
		pcH=pcH+1
		BikeX = -183: BikeY = -94
	ELSE
		' nothing
	END IF
END Sub

SUB MouseXOver (posX, posY)
	IF PosX>2 AND PosY>2 AND PosX<18 AND PosY<18 THEN
		xScreen.Cursor = crHandPoint
		CurPnt=-1
		ls=0
	ELSEIF PosX>167 AND PosX<181 AND PosY>2 AND PosY<18 THEN
		xScreen.Cursor = crHandPoint
		CurPnt=1
		rs=2
	ELSE
		xScreen.Cursor = crDefault
		CurPnt=0
		ls=1: rs=3
	END IF
	UserCome
END SUB

SUB MouseXClick
	Select CASE CurPnt
		CASE -1
		  X = X - 1
		  IF X <= 0 THEN
		    X = 28
		  END IF
		  ls=1
		CASE 1
		  X = X + 1
		  IF X >= 29 THEN
		    X = 1
		  END IF
		  rs=3
		CASE ELSE
		  ls=1: rs=3
	END Select
	xScreen.Flip
    UserCome
END SUB

SUB KeyDown(Key AS BYTE, Shift AS INTEGER)
  'key trap, 40=down, 39=right, 38=up, 37=left, 27=esc
  SELECT CASE Key
    CASE 39  ' Right
      Screen.Cursor = crAppStart
      X = X + 1
      IF X>=29 THEN
      	X=1
      END IF
      rs=2
    CASE 37  ' Left
      Screen.Cursor = crAppStart
      X = X - 1
      IF X<=0 THEN
      	X=28
      END IF
      ls=0
    CASE 27  ' ESC
      Form.Close 
  END SELECT
  UserCome
END SUB

SUB KeyUp(Key AS BYTE, Shift AS INTEGER)
	'Select CASE Key
	'	CASE 39
		  rs=3
	'	CASE 37
		  ls=1
	'END Select
	Screen.Cursor = crDefault
END SUB


'' Remember, you can't do any drawing if the form is not visible
X=1
DXImageList.Parent = xScreen
UiImgList.Parent = xScreen
DXImageList.LoadFromFile("MOTOGP2002.DXG")
UiImgList.LoadFromFile("UserInterface.DXG")

DXTimer.Enabled = True
DXTimer.Interval = 0               '' Let DirectX handle FPS
'DXTimer.ActiveOnly = 0
DXTimer.OnTimer = DXTimerExpired

' user movement detection. if idle more than specific
' time, the program will initiate screensaver...
UsrTimer.Enabled = True
UsrTimer.Interval = 100
UsrTimer.OnTimer = UserIdle

xScreen.OnMouseMove=MouseXOver
xScreen.OnClick=MouseXClick
Form.OnKeyDown = KeyDown
Form.OnKeyUp = KeyUp
Form.Caption = "MotoGP 2002 Bikes"
Form.ShowModal
