Difference between revisions of "GW-Basic/Mouse"

From GPWiki
Jump to: navigation, search
m (Formatting tweaks)
Line 13: Line 13:
 
<B>Demonstration Program:</B>
 
<B>Demonstration Program:</B>
  
 +
<syntaxhighlight lang='gwbasic'>
 
  1 '''''''''''''  GW-Calculator By: Daryl Dubbs, Jan. 2014  '''''''''''''''''
 
  1 '''''''''''''  GW-Calculator By: Daryl Dubbs, Jan. 2014  '''''''''''''''''
 
  2 SCREEN 9: COLOR 14, 1
 
  2 SCREEN 9: COLOR 14, 1
Line 134: Line 135:
 
  910 WHILE INKEY$ = "": WEND
 
  910 WHILE INKEY$ = "": WEND
 
  911 SYSTEM
 
  911 SYSTEM
 +
</syntaxhighlight>
  
 
<B>The Mouse Routine:</B>
 
<B>The Mouse Routine:</B>
Line 144: Line 146:
 
Upon return from function 3 ( Read status ), the variables "H" and "V" will have values corresponding to the horizontal and vertical position of the mouse cursor ( respectively ), while "B" will have a value of 0 to 3 ( for a 2-button mouse ), indicating which mouse button(s) is/are pressed as follows;
 
Upon return from function 3 ( Read status ), the variables "H" and "V" will have values corresponding to the horizontal and vertical position of the mouse cursor ( respectively ), while "B" will have a value of 0 to 3 ( for a 2-button mouse ), indicating which mouse button(s) is/are pressed as follows;
  
Value Of "B":      Button(s) Pressed:
+
{| class='wikitable' style='text-align: center;'
    0                   None
+
!Value Of "B" !! Button(s) Pressed
    1                   Left
+
|-
    2                   Right
+
| 0 || None
    3                   Both
+
|-
 +
| 1 || Left
 +
|-
 +
| 2 || Right
 +
|-
 +
| 3 || Both
 +
|}
  
 
<B>Interfacing With The Mouse:</B>
 
<B>Interfacing With The Mouse:</B>
Line 157: Line 165:
 
<B>A few notes:</B>
 
<B>A few notes:</B>
 
   
 
   
* Make sure to hide the mouse cursor before drawing any graphics to the screen, or else they may be garbled when the mouse is moved.   
+
* Make sure to hide the mouse cursor before drawing any graphics to the screen, or else they may be garbled when the mouse is moved.   
* Do not hide the cursor via function 2 more than once before calling function 1 to display it again, or it may not show up.
+
* Do not hide the cursor via function 2 more than once before calling function 1 to display it again, or it may not show up.
* To ensure that the values of "B", "H" and "V" are accurate and current, call function 3 IMMEDIATELY before reading these variables.   
+
* To ensure that the values of "B", "H" and "V" are accurate and current, call function 3 IMMEDIATELY before reading these variables.   
  I find that calling function 3 at the beginning of the main program loop is usually sufficient to keep the variables updated, unless
+
 
  you wish to call the mouse from a sub-routine, in which case you'd want to include a call to function 3 in that routine.
+
I find that calling function 3 at the beginning of the main program loop is usually sufficient to keep the variables updated, unless you wish to call the mouse from a sub-routine, in which case you'd want to include a call to function 3 in that routine.
  
 
<B>Conclusion:</B>
 
<B>Conclusion:</B>
Line 167: Line 175:
 
That's about all there is to it; just play around with the program a bit, until you get the feel for what's going on there.  To create
 
That's about all there is to it; just play around with the program a bit, until you get the feel for what's going on there.  To create
 
your own programs using the mouse, you might want to strip out everything except the mouse routine ( lines 200 - 211 ), then save that file as a sort of bare-bones "template", which you can copy as often as you like to give you a starting point for editing your next masterpiece.  Hope you found this topic interesting, and Enjoy!
 
your own programs using the mouse, you might want to strip out everything except the mouse routine ( lines 200 - 211 ), then save that file as a sort of bare-bones "template", which you can copy as often as you like to give you a starting point for editing your next masterpiece.  Hope you found this topic interesting, and Enjoy!
 +
 +
[[Category:GWBasic]]

Revision as of 09:43, 2 February 2014

(Stub: Article in progress.)

Using A Mouse In GW-Basic Programs

Introduction:

What can be said about good ol' faithful GW-Basic? Well, it should be quite obvious how utterly obsolete the language is by today's standards, and that about the only ones still interested in working with it, are those last three MS-Dos die-hards ( myself included ), still lurking in their pre-war fallout shelters, still clacking away on their text-only IBM bit-boxes. But did you know that, with a little un-orthodox thinking and creative coding, GW-Basic can actually interface with a mouse, and even a soundcard?!

In fact, with a little practice, you'd find that it's not nearly as difficult as you might think to slap together a simple mouse-driven menu program, for performing such tasks as selecting a character's gender, class ( Ex: Warrior, Wizard, Healer, etc... ) and icon, as well as various other game options, such as sound effects on/off, music on/off, and so forth.

Since the best way to understand a particular programming concept is to see it in action, I have included below a simple calculator program for the purpose of demonstration. While the program really has nothing in particular to do with game programming, it does illustrate how to create and interact with Windows-style "buttons", such as the "Quit" button, the "Clear" button and, of course all of the number keys and function keys.

Demonstration Program:

 1 '''''''''''''  GW-Calculator By: Daryl Dubbs, Jan. 2014  '''''''''''''''''
 2 SCREEN 9: COLOR 14, 1
 3 LINE (0, 0)-(640, 400), 12, BF
 4 LINE (246, 100)-(356, 270), 4, BF
 5 LINE (240, 86)-(350, 263), 1, BF
 6 LINE (242, 90)-(348, 112), 14, B
 7 LINE (242, 116)-(348, 260), 14, B
 8 LOCATE 10, 32: PRINT 7; 8; 9; ; " "; CHR$(246)'" /"
 9 LOCATE 12, 32: PRINT 4; 5; 6; " X"
 10 LOCATE 14, 32: PRINT 1; 2; 3; " -"
 11 LOCATE 16, 32: PRINT " . "; 0; " % "; " +"
 12 LOCATE 18, 32: PRINT "  Clear   "; "="
 13 LINE (249, 232)-(317, 256), 9, B
 14 LINE (321, 232)-(341, 256), 9, B
 15 FOR Y = 0 TO 110 STEP 28
 16 FOR X = 0 TO 72 STEP 24
 17 LINE (X + 249, Y + 120)-(X + 269, Y + 144), 9, B
 18 NEXT X
 19 NEXT Y
 20 LINE (549, 10)-(593, 40), 4, BF: LINE (546, 6)-(590, 35), 1, BF
 21 LINE (548, 9)-(588, 32), 14, B
 22 LOCATE 2, 70: PRINT "Quit"
 23 LINE (110, 10)-(503, 40), 4, BF: LINE (107, 6)-(500, 35), 1, BF
 24 LINE (110, 9)-(497, 32), 14, B
 25 LOCATE 2, 16: PRINT "GW-Basic Calculator By: Daryl Dubbs, Jan. 2014"
 100 ' Start of Main Program  ''''''''''''''''''''''''''''''''''''''''''''''''
 101 FACTOR1 = 0: FACTOR2 = 0: FACTOR$ = " ": K$ = " "
 102 FUNK = 1: GOSUB 200 'Mouse Sub-Routine
 103 WHILE INKEY$ = ""
 104 GOSUB 300
 105 IF B = 1 THEN GOSUB 400
 106 WEND
 107 FUNK = 2: GOSUB 200 'Mouse Sub-Routine
 108 END
 200 ' Mouse Sub-Routine  ''''''''''''''''''''''''''''''''''''''''''''''''''''
 201 POKE 0, 184: POKE 1, FUNK: POKE 2, 0
 202 POKE 3, 205: POKE 4, 51: POKE 5, 137
 203 POKE 6, 30: POKE 7, 170: POKE 8, 10
 204 POKE 9, 137: POKE 10, 14: POKE 11, 187
 205 POKE 12, 11: POKE 13, 137: POKE 14, 22
 206 POKE 15, 204: POKE 16, 12: POKE 17, 203
 207 AB = 0: CALL AB
 208 B = PEEK(&HAAA)
 209 H = PEEK(&HBBB) + PEEK(&HBBC) * 256
 210 V = PEEK(&HCCC) + PEEK(&HCCD) * 256
 211 RETURN
 300 ' Monitor Keypad  '''''''''''''''''''''''''''''''''''''''''''''''''''''''
 301 FUNK = 3: GOSUB 200 ' Mouse Sub-Routine
 302 R = 0: C = 0
 303 IF V > 148 THEN R = 1
 304 IF V > 177 THEN R = 2
 305 IF V > 204 THEN R = 3
 306 IF V > 232 THEN R = 4
 307 IF H > 249 THEN C = 1
 308 IF H > 272 THEN C = 2
 309 IF H > 297 THEN C = 3
 310 IF H > 321 THEN C = 4
 311 RC = R * 4 + C
 312 RETURN
 400 ' Button Pressed  '''''''''''''''''''''''''''''''''''''''''''''''''''''''
 401 IF EQ = 1 THEN : EQ = 0: FACTOR$ = " ": LOCATE 8, 32: PRINT "          "
 402 IF H > 540 THEN GOSUB 900 'Quit
 403 IF RC = 1 THEN K$ = "7"
 404 IF RC = 2 THEN K$ = "8"
 405 IF RC = 3 THEN K$ = "9"
 406 IF RC = 4 THEN OPERATION$ = CHR$(246): GOSUB 600
 407 IF RC = 5 THEN K$ = "4"
 408 IF RC = 6 THEN K$ = "5"
 409 IF RC = 7 THEN K$ = "6"
 410 IF RC = 8 THEN OPERATION$ = "X":  GOSUB 600
 411 IF RC = 9 THEN K$ = "1"
 412 IF RC = 10 THEN K$ = "2"
 413 IF RC = 11 THEN K$ = "3"
 414 IF RC = 12 THEN OPERATION$ = "-":  GOSUB 600
 415 IF RC = 13 THEN K$ = "."
 416 IF RC = 14 THEN K$ = "0"
 417 IF RC = 15 THEN OPERATION$ = "%":  GOSUB 600
 418 IF RC = 16 THEN OPERATION$ = "+":  GOSUB 600
 419 IF RC = 17 THEN K$ = " ": GOSUB 700
 420 IF RC = 18 THEN K$ = " ": GOSUB 700
 421 IF RC = 19 THEN K$ = " ": GOSUB 700
 422 IF RC = 20 THEN K$ = " ": GOSUB 600: GOSUB 800
 423 IF H > 540 THEN SYSTEM
 424 GOSUB 500
 425 RETURN
 500 ' Build Next Factor  ''''''''''''''''''''''''''''''''''''''''''''''''''''
 501 FACTOR$ = LEFT$(FACTOR$ + K$, 11)
 502 LOCATE 8, 34: PRINT USING "\        \"; FACTOR$
 503 WHILE B > 0: FUNK = 3: GOSUB 200: WEND
 504 RETURN
 600 ' Assign Value To Factors  ''''''''''''''''''''''''''''''''''''''''''''''
 601 IF WHICHFACTOR = 0 THEN FACTOR1 = VAL(FACTOR$)
 602 IF WHICHFACTOR = 1 THEN FACTOR2 = VAL(FACTOR$)
 603 WHICHFACTOR = 1 - WHICHFACTOR
 604 FACTOR$ = " ": K$ = " "
 606 LOCATE 8, 34: PRINT VAL(FACTOR$)
 607 LOCATE 8, 32: COLOR 11: PRINT OPERATION$: COLOR 14
 608 RETURN
 700 ' Clear  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 701 FACTOR$ = " ": K$ = " "
 702 LOCATE 8, 32: PRINT " "
 703 RETURN
 800 ' Equals  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 801 EQ = 1'equals
 802 IF OPERATION$ = "+" THEN ANSWER = FACTOR1 + FACTOR2
 803 IF OPERATION$ = "-" THEN ANSWER = FACTOR1 - FACTOR2
 804 IF OPERATION$ = "X" THEN ANSWER = FACTOR1 * FACTOR2
 805 IF OPERATION$ = CHR$(246) THEN ANSWER = FACTOR1 / FACTOR2
 806 IF OPERATION$ = "%" THEN ANSWER = FACTOR1 * FACTOR2 * .01
 807 LOCATE 8, 32: COLOR 11: PRINT "=": COLOR 14
 808 LOCATE 8, 34: FACTOR$ = STR$(ANSWER): PRINT FACTOR$
 809 RETURN
 900 ' Quit  '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 901 FUNK = 2: GOSUB 200
 902 CLS
 903 LOCATE 10, 22: PRINT "Thanks for using GW-Basic Calculator!"
 904 LOCATE 11, 28: PRINT "Have an INCREDIBLE life!"
 905 LOCATE 14, 34: PRINT "B-Bye, now!"
 906 LOCATE 18, 27: PRINT "Press any key to continue..."
 910 WHILE INKEY$ = "": WEND
 911 SYSTEM

The Mouse Routine:

The lines between 200 and 211 make up the sub-routine that accesses the mouse, which it does by storing the necessary assembly code commands into specific memory locations, calling the assembly routine as a unit by way of the "Call" statement, then storing the appropriate information into the variables "B" ( Button status ), "H" ( Horizontal position ) and "V" ( Vertical position ). The sub-routine requires a single argument, which corresponds to the value stored in the variable "Funk". Valid values for "Funk" are: "1" ( Show the mouse cursor ), "2" ( Hide the cursor ) or "3" ( Read button status, horizontal and vertical positions ). Upon return from function 3 ( Read status ), the variables "H" and "V" will have values corresponding to the horizontal and vertical position of the mouse cursor ( respectively ), while "B" will have a value of 0 to 3 ( for a 2-button mouse ), indicating which mouse button(s) is/are pressed as follows;

Value Of "B" Button(s) Pressed
0 None
1 Left
2 Right
3 Both

Interfacing With The Mouse:

It is a simple matter to wait for the user to press a particular mouse button, usually the left button ("1"), then test whether the mouse cursor is over any on-screen button ( for example the "Quit" button ), in which case the function that button represents is performed. This is done with a series of "If" statements, as illustrated in lines 303 to 310 above, among others.

A few notes:

  • Make sure to hide the mouse cursor before drawing any graphics to the screen, or else they may be garbled when the mouse is moved.
  • Do not hide the cursor via function 2 more than once before calling function 1 to display it again, or it may not show up.
  • To ensure that the values of "B", "H" and "V" are accurate and current, call function 3 IMMEDIATELY before reading these variables.

I find that calling function 3 at the beginning of the main program loop is usually sufficient to keep the variables updated, unless you wish to call the mouse from a sub-routine, in which case you'd want to include a call to function 3 in that routine.

Conclusion:

That's about all there is to it; just play around with the program a bit, until you get the feel for what's going on there. To create your own programs using the mouse, you might want to strip out everything except the mouse routine ( lines 200 - 211 ), then save that file as a sort of bare-bones "template", which you can copy as often as you like to give you a starting point for editing your next masterpiece. Hope you found this topic interesting, and Enjoy!