Ch3. Basic IO
Important : To view this page correctly you require Verdana font. Also I dont accept any liability/responsibility for any damage this page may cause you directly or indirectly.
In this chapter we will cover some basic input and output (IO) routines, but more input than output.
Below are the subroutines we will require to use the mouse and keyboard through FL:
'***
Mouse - commands related to non SVGA mouse ***
(Basically you
can use these commands in normal QB's Text/CGA/EGA/VGA modes)
DECLARE SUB MouseShow ()
DECLARE SUB MouseDriver (ax%, bx%, CX%, dx%)
DECLARE SUB MouseHide ()
DECLARE SUB MousePut (X%, Y%)
DECLARE SUB MouseRange (X1%, Y1%, x2%, y2%)
DECLARE SUB MouseStatus (Lb%, Rb%, XMouse%, Ymouse%)
DECLARE FUNCTION MouseInit% ()
'***
Mouse - commands related to mouse in SVGA modes ***
(You can use
these commands only in FL's SVGA modes)
DECLARE SUB Future.UpdateMouse ALIAS "SVGAupdatemouse" ()
DECLARE SUB SetMouseRange (BYVAL X1%, BYVAL Y1%, BYVAL x2%, BYVAL y2%)
DECLARE SUB SetLocation (BYVAL X%, BYVAL Y%)
DECLARE SUB SetMouseMaskColor ALIAS "SVGAmousemask" (BYVAL C&)
DECLARE SUB Future.MouseShape ALIAS "SVGAmouseShape" (BYVAL Offset%, BYVAL Segment%)
DECLARE SUB Future.ResetMouseShape ALIAS "MouseCursorGenerator" ()
DECLARE SUB Future.MouseOff ALIAS "SVGAmouseoff" ()
DECLARE SUB Future.MouseOn ALIAS "SVGAmouseon" ()
DECLARE FUNCTION Future.MouseB% ALIAS "SVGAmouseB" ()
DECLARE FUNCTION Future.MouseY% ALIAS "SVGAmouseY" ()
DECLARE FUNCTION Future.MouseX% ALIAS "SVGAmouseX" ()
DECLARE SUB SetMouseSensitivity (BYVAL H%, BYVAL V%)
'***
Key handling - keyboard and joystick input ***
DECLARE SUB StickStatus (JoyX%, JoyY%, But1%, But2%, But3%)
DECLARE SUB LockKeys (Nr%, State%)
DECLARE SUB DisableBreak
DECLARE SUB ClearKeyBuffer
DECLARE FUNCTION GetKey% (BYVAL KeyCode%)
DECLARE SUB KBHon ()
DECLARE SUB KBHoff ()
The above routines contain 3 different sets of definations:
The first set of routines are used to make use of the mouse in normal QB modes(Screen 1...12).
The
second set of
routines are used to make use of the mouse in SVGA modes provided by FL.
Note: Dont
interchange the commands for SVGA modes and QB modes i.e. you cant use
SVGA mouse functions in normal QB modes and vice versa. If you accidentally do
then you will get a error message and windows will shut your program.
The last set of routines are used to make use of the keyboard. These are special routines which will allow you to find out the status of CAPS Lock, SCROLL lock etc... and 'clear your keyboard buffer' etc... These routines are independent of the screen mode in which you are working.
Keeping the above notes in mind we start with our first program with the mouse as an input device!
Mouse
This program will deal with using the mouse inside the normal QB modes:
'$INCLUDE:
'future.bi' 'Include the future.bi file
'$INCLUDE: 'future.bi'
mi% = MouseInit 'Initializes the mouse
IF mi% = 0 THEN END
CLS
XRange% = 79 'Horizontal range
YRange% = 24 'Vertical range
MouseShow 'Shows the mouse
MouseRange 1, 1, 8 * XRange%, 8 * YRange% 'Sets the range
MousePut 8*40, 8*10 'Positions the mouse cursor on the
screen
DO
MouseStatus LBtn%, RBtn%, Xpos%, Ypos% 'Checks
the mouse status
COLOR 14, 0
LOCATE 1, 1: PRINT "Mouse Status"
COLOR 15
LOCATE 2, 1: PRINT "L:" + STR$(LBtn%) + " R:" + STR$(RBtn%)
LOCATE 3, 1: PRINT " "
LOCATE 3, 1: PRINT "X:" + STR$(Xpos% / 8) + " Y:" + STR$(Ypos% / 8)
LOOP UNTIL RBtn% = -1
MouseHide 'Hides the mouse
End
The above program will exit on depressing the right button.
The commands and their usage:
MouseInit: Initializes the mouse. Returns 0 if mouse initialization was unsuccessful.
MouseShow: Displays the mouse cursor on the screen.
MouseRange: Sets the mouse range. Takes parameters x1%, y1%, x2%, y2%(in pixels).
MousePut: Sets the position of the mouse cursor on the screen. Takes parameters x%, y%(in pixels).
MouseStatus: Returns the status of left button, right button, and x-y position in variables.
MouseHide: Hides the mouse.
SVGA Mouse
The following program will deal with SVGA mouse.
Warning: These routines should be used ONLY IN SVGA MODES. Using them in non-SVGA modes WILL crash your program!
'$INCLUDE:
'future.bi'
'***The following subroutines are hardly required for basic mouse programming***
'DECLARE SUB SetMouseMaskColor ALIAS "SVGAmousemask" (BYVAL C&)
'DECLARE SUB Future.MouseShape ALIAS "SVGAmouseShape" (BYVAL Offset%, BYVAL Segment%)
'DECLARE SUB Future.ResetMouseShape ALIAS "MouseCursorGenerator" ()
'DECLARE SUB SetMouseSensitivity (BYVAL H%, BYVAL V%)
Set800x600 16 'Set the screen mode
Future.MouseON 'Turn ON the mouse
XRange% = 790 'Horizontal range
YRange% = 590 'Vertical range
Future.Print 10, 10, " Mouse Status in SVGA! ", RGB2Color&(255, 255, 0), RGB2Color&(0, 0, 255)
Future.Box 10, 10, 790, 590, RGB2Color&(255, 255, 255)
SetMouseRange 10, 10, XRange%, YRange% 'Sets the range
SetLocation 10, 10 'Sets the location of mouse pointer on the screen
DO
Future.UpdateMouse 'Updates the mouse - without this statement the mouse will not move in the SVGA mode
Xpos% = Future.MouseX 'Gets the X position
Ypos% = Future.MouseY 'Gets the Y position
Btn% = Future.MouseB 'Gets the button status : 1 = Left btn 2 = Right btn
Future.Print 20, 30, "X:" + STR$(Xpos%) + " Y:" + STR$(Ypos%) + " ", RGB2Color&(255, 255, 255), 0
Future.Print 20, 50, "B:" + STR$(Btn%), RGB2Color&(255, 255, 255), 0
LOOP UNTIL Btn% = 2
Future.MouseOFF 'Turns OFF the mouse
ResetScreen 'Resets the screen and safely ends the program
The above program will exit on depressing the right button.
The commands and their usage: I hope that the reader knows about the basic SVGA drawing and output commands discussed in Chapter 2.
MouseON: 'Shows' the mouse pointer on the screen. Should be used only after entering the required SVGA screen mode.
MouseOFF: 'Hides' the mouse pointer. Should be used only after entering the required SVGA screen mode.
SetMouseRange: Sets the mouse range. It accepts x1%, y1%, x2%, y2%(in pixels) to set the range.
SetLocation: Sets the location of the pointer mouse on the screen. Takes parameters x%, y%(in pixels).
Future.UpdateMouse: This statement is required to be called constantly. The mouse pointer's position on the screen will be updated only if it is called. The moment you stop calling this function the mouse pointer will 'freeze'. It also updates the status of left button, right button, and x-y position. Which can be obtained from the following three commands.
Future.MouseX: A function which gets the x position of the mouse pointer.
Future.MouseY: A function which gets the y position of the mouse pointer.
Future.MouseB: A function which gets the status of the mouse buttons. It returns the following values depending on the button(s) depressed:
Button |
Value |
None |
0 |
Left |
1 |
Right |
2 |
Left + Right |
3 |
A small note on the following commands:
SetMouseMaskColor
Future.MouseShape
Future.ResetMouseShape
SetMouseSensitivity
These commands are hardly used by newbies. Infact even intermidiate users avoid these commands since there is very little documentation available about them. But there are programs included with future library which demonstrate their usage. May be not all commands are used. See Mouse.BAS included in the EXAMPLES folder of FL.
Keyboard Handler
The following program will deal with the keyboard handler of FL.
'$INCLUDE: 'future.bi'
KBHOn
DO
KeyP% = GetKey%(1)
ClearKeyBuffer
LOOP UNTIL KeyP% = 1
KBHOff
This program demonstrates how to detect the escape key.
KBHOn: Turns on the keyboard handler. Its mandatory to call this function before executing any of the other keyboard handling functions. In the absence of this statement all other statements may cause the program to crash.
GetKey%(KeyScanCode%): A function which gets the status of the key whose scancode is passed. It returns 1 if the key is depressed else 0.
ClearKeyBuffer: A statment which clears the keyboard buffer. This prevents the BEEPs when the user keeps any key depressed for a long time. This is not possible to prevent in QB. Thus this keyboard handler is required for game programmers.
KBHOff: A VERY VERY VERY important command! If you fail to include this command at the end of your program then you will not be able to quit your program! Because when KBHOn is called it disables (Ctrl + Break) function of QB.
Note: Since I have moderate experience in developing games and using this keyboard handler so please refer to Future.Library's TESTKBH.BAS for a 'bigger' program and a better example.