[DarkBASIC] Making an Instant Messenger menu

User Tag List

Results 1 to 3 of 3
  1. #1
    ReidE96's Avatar Archer Authenticator enabled
    Reputation
    470
    Join Date
    Dec 2006
    Posts
    1,625
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [DarkBASIC] Making an Instant Messenger

    Spinny cubes are fun and all, but of limited practical value. So here's an Instant Messenger, coded in DarkBASIC. The comments (REM or ` for each comment line, REMSTART for the beginning of a block of comments (no REM needed in the block) and REMEND for the end of a block), should help you understand how it all works.

    Code:
    REMSTART
    Project: Simplechat
    Rem Created: 05/05/2006 17:09:03
    
    ***** Main Source File *****
    REMEND
    
    ` Set the resolution to 800 by 600, 32 bit
    SET DISPLAY MODE 800, 600, 32
    
    ` Set the screen refresh rate to 30 Hz
    SYNC ON
    SYNC RATE 30
    
    ` Keep track of what was said and who said it (declaring a few arrays)
    DIM ChatText$(32)
    DIM PlayersName$(1)
    DIM NumberOfPlayers(1)
    DIM LastNumberOfPlayers(1)
    
    ` Find the TCP/IP connection number with a function we'll write later
    TcpIpNumber = FindTCPIPConnectionNumber()
    
    ` Print the text in the "" to the screen, then a blank line
    PRINT "Welcome to SimpleChat! Press the Escape key to exit."
    PRINT
    
    ` Make the screen reload
    SYNC
    
    ` Create a GOTO point in case they don't enter a name
    :name
    
    ` Get the user's name and store it in the MyName variable
    INPUT "Please enter your name. ", MyName$
    
    ` Refresh the screen a couple of times
    SYNC
    SYNC
    
    ` Check they entered a name
    If MyName$ = ""
       ` If they didn't, tell them they need to
       PRINT "You need to enter a name!"
    
       ` Wait for the user to press a key
       WAIT KEY
    
       ` Get them to enter a name again
       GOTO name
    
    ` Close our IF
    ENDIF
    
    ` Store the name in the array
    PlayersName$(1) = MyName$
    
    ` Find out whether they're the host or client
    
    ` Throw in a blank line
    PRINT
    
    ` Ask ze question
    PRINT "Are you the host or client? (Press 1 or 2 to choose)"
    
    ` Display the possible answers
    PRINT "(1) I'm the Host"
    PRINT "(2) I'm the Client"
    
    ` Refresh the screen a couple of times
    SYNC
    SYNC
    
    ` Set a variable for what key they press
    A$ = ""
    
    ` Set a variable to get their answer
    Answer = 0
    
    ` Get their answer
    
    ` Loop till they choose something
    WHILE Answer = 0
       ` Make the key variable whatever key they press
       A$ = INKEY$()
    
       ` If they press 1, set the answer to 1   
       IF A$ = "1" THEN Answer = 1
    
       ` If they press 2, set the answer to 2
       IF A$ = "2" THEN Answer = 2
    
    ` Close the loop
    ENDWHILE
    
    ` Show a new line
    PRINT
    
    ` Tell them how to exit
    PRINT "Press the Escape key to quit."
    
    ` If they're the host, do the hosty stuff
    IF Answer = 1
    
       ` Tell them we're setting up the session
       PRINT "Creating chat session. Please wait"
    
       ` Refresh the screen
       SYNC
    
       ` Wait 0.2 seconds
       SLEEP 200
    
       ` Create a new connection on the port we picked up earlier
       SET NET CONNECTION TcpIpNumber, " "
    
       ` Create a net "game", as DarkBASIC calls it (t is made for games, after all). We're calling the game "Chat" and their player name will be the name they chose earlier. We're allowing 16 people to be in the room at once, and the flag is 1, making this peer-to-peer (P2P)
       CREATE NET GAME "Chat", MyName$, 16, 1
    
    ` Close our If statement
    ENDIF
    
    ` If they're the client, do the clienty stuff
    IF Answer = 2
       ` Create a GOTO point, just in case they screw up
       :client
    
       ` Ask for an IP to connect to and store it in the AddressData variable
       INPUT "Please enter the host's IP address. ", AddressData$
    
       ` Tell them we're connecting
       PRINT "Connecting to chat session. Please wait"
    
       ` Update the screen
       SYNC
    
       ` Create a net connection using the number we picked up earlier to the IP they just provided
       SET NET CONNECTION TcpIpNumber, AddressData$
    
       ` Ask the host how many "games" it has open
       PERFORM CHECKLIST FOR NET SESSIONS
    
       ` Store the number of games in a variable
       NumberOfGames = CHECKLIST QUANTITY()
    
       ` If there's no "games"
       IF NumberOfGames = 0
    
          ` Tell the user there's no games
          PRINT "No chat session found at that address"
    
          ` Update the screen
          SYNC
    
          ` Wait for the user to press a key
          WAIT KEY
    
          ` Go to the :client GOTO point
          GOTO client
    
       ` Close our IF
       ENDIF
    
       ` If we got past the IF, we've found a game, so connect to it. There's only ever going to be 1 instance of this on the host, so it's game number will be 1. Our player name will be the one they chose earlier
       JOIN NET GAME 1, MyName$
    
       ` Tell them we've connected
       PRINT "Connected to session!"
    
       ` Update the screen
       SYNC
    
    ` Close our IF
    ENDIF
    
    ` The prep is complete, so we'll run our main chat client, which is a function. Yay!
    ChatClient()
    
    ` End the program (because the chat client function will keep going until they leave)
    END
    
    ` This function will determine which NET CONNECTION number is TCP/IP
    FUNCTION FindTCPIPConnectionNumber()
       ` Create a variable to hold the number
       Flag = 0
    
       ` Clear the screen
       CLS
    
       ` Get a list of current net connections
       PERFORM CHECKLIST FOR NET CONNECTIONS
    
       ` Start a FOR loop for every item in the checklist
       FOR x = 1 TO CHECKLIST QUANTITY()
    
          ` Get the name of the current item in the checklist
          Service$ = CHECKLIST STRING$(x)
    
          ` See if the first 15 letters of the name are "Internet TCP/IP"
          IF LEFT$(Service$,15) = "Internet TCP/IP"
             ` If they are, set the flag to the current number
             Flag = x
    
          ` End the IF
          ENDIF
    
       ` Move on to the next item in the checklist
       NEXT x
    
    ` This ends the function, and returns the value of flag
    ENDFUNCTION Flag
    
    `This function has all the chat functionality
    FUNCTION ChatClient()
    
       ` Clear the chat text from the array (another function!)
       ClearChatText()
    
       ` Display the initial chatters in the room
       
       ` Get a list of people in the "game"
       PERFORM CHECKLIST FOR NET PLAYERS
    
       ` Store the number of players in a variable
       NumberOfPlayers(1) = CHECKLIST QUANTITY()
    
       ` Start a FOR loop for every entry on the checklist
       FOR x = 1 TO NumberOfPlayers(1)
          ` Use ANOTHER function to show a list of who is here, for each entry on the list
          AddUserMessage(CHECKLIST STRING$(x))
    
       ` Move onto the next person in the list
       NEXT x
    
       `Send an entry message
    
       ` Set the variable C to their name and "has joined"
       C$ = PlayersName$(1)+" has joined."
    
       ` Send the message to the entire room (0 means everyone, a number would mean an individual in the room)
       SEND NET MESSAGE STRING 0,C$
    
       ` Displays the chat text with yet ANOTHER function
       DisplayChatText()
    
       ` Set the buffers for text entry
       A$ = ""
       B$ = ""
       C$ = ""
    
       ` Clear whatever key was last pressed from the buffer so it's ready
       CLEAR ENTRY BUFFER
    
       ` Capture Text Input and process it accordingly
    
       ` Start a WHILE loop that runs until Esc is pressed
       WHILE ESCAPEKEY() = 0
          ` Do the CheckIncomingMessages function
          CheckIncomingMessages()
    
          ` Set variable A to the key pressed
          A$ = INKEY$()
    
          ` If the ASCII value of the key pressed is 8 (the backspace key)
          IF ASC(A$) = 8
             ` Set the C variable to whatever it currently is and whatever is in the ENTRY buffer
             C$ = C$ + ENTRY$()
    
             ` Take the left x letter of C, x being one less than the current length of C
             C$ = LEFT$(C$,LEN(C$)-1)
    
             ` Clear the entry buffer
             CLEAR ENTRY BUFFER
    
             ` Clear the screen (to remove any traces of the backspaced letter)
             CLS
    
             ` Show the current chat text (yup, it's a function :D)
             DisplayChatText()
    
          ` Close our IF statement
          ENDIF
    
          ` Set the B variable to the C variable, plus whatever's currently in the ENTRY buffer
          B$ = C$ + ENTRY$()
    
          ` Show whatever's in B at x coordinate 10, y coordinate 460
          TEXT 10,460,B$
    
          ` If enter is pressed and the B variable isn't empty
          IF RETURNKEY()=1 AND B$ <> ""
    
             ` Wait for 0.25 seconds
             SLEEP 250
    
             ` Send the message
    
             ` Set variable D to the current user's name, a :, a space, and whatever's in B
             D$ = PlayersName$(1) + ": " + B$
    
             ` Send D to everyone in the room
             SEND NET MESSAGE STRING 0,D$
    
             ` Add the message to our own screen with ANOTHER FUNCTION!
             AddStringToChat(D$)
    
             ` Clear the D, B and C variables, along with the ENTRY buffer
             D$ = ""
             B$ = ""
             C$ = ""
             CLEAR ENTRY BUFFER
    
             ` Show the current chat text
             DisplayChatText()
    
          ` Close our IF
          ENDIF
    
          ` Update the screen
          SYNC
    
       ` Close the while
       ENDWHILE
    
    `End the function, not returning a variable
    ENDFUNCTION
    
    ` This function scans the incoming packets for strings of text and displays them
    FUNCTION CheckIncomingMessages()
       ` This command gets the oldest message from the incoming message queue (any messages DarkBASIC sends are stored in a queue) and makes it the current message
       GET NET MESSAGE
    
       ` If there's no incoming packets, end the function
       IF NET MESSAGE EXISTS() = 0 THEN EXITFUNCTION
    
       ` While there IS a message
       WHILE NET MESSAGE EXISTS() <> 0
          ` Store the type of message (integer, float, string, memblock, image, bitmap, sound, or mesh) in the MsgType variable
          MsgType = NET MESSAGE TYPE()
    
          ` If the message is of type 3 (a string)
          IF MsgType = 3
             ` Set the Msg variable to the text from the message
             Msg$ = NET MESSAGE STRING$()
    
             ` Use the AddStringToChat function to add Msg to the chat
             AddStringToChat(Msg$)
    
             ` Run the DisplayChatText function
             DisplayChatText()
    
          ` End the IF
          ENDIF
    
          ` Check for a new net message (once processed, net messages are automagically deleted)
          GET NET MESSAGE
    
       ` End the WHILE loop
       ENDWHILE
    
    ` End the function, returning no variable
    ENDFUNCTION
    
    ` This function displays messages saying who's in the room when you join, taking the Name variable from when it's called
    FUNCTION AddUserMessage(Name$)
       ` Set the NewString variable to the Name variable and the phrase "is here"
       NewString$ = Name$ + " is here."
    
       ` Use the AddStringToChat function to add NewString to the chat
       AddStringToChat(NewString$)
    
    ` End the function
    ENDFUNCTION
    
    ` This function adds a string to the chat, taking the variable a when it is called
    FUNCTION AddStringToChat(a$)
       ` Start a FOR loop going from 1 to 32
       FOR x = 1 TO 32
          ` If the ChatText array at value x is blank
          IF ChatText$(x) = ""
             ` Set the text of the ChatText array at value x to a
             ChatText$(x) = a$
             ` Exit the function
             EXITFUNCTION
    
          ` End the IF statement
          ENDIF
    
       ` End the FOR loop
       NEXT x
    
       ` Start a FOR loop running from 32 to 2
       FOR x = 32 TO 2
          ` Set the variable y to 1 less then the current value of x
          y = x-1
    
          ` Set the value of the ChatText array at y to the value of the ChatText array at x (has the effect of moving everything up a line and scrolling the top line off the screen)
          ChatText$(y) = ChatText$(x)
    
       ` End the FOR loop
       NEXT x
    
       ` Set the 32nd value of the ChatText array to a (if we got to here, the ChatText array was full as the first function would have noticed a blank line and exited the function)
       ChatText$(32) = a$
    
    ` This marks the end of the function
    ENDFUNCTION
    
    ` This function clears the ChatText$ array
    FUNCTION ClearChatText()
       ` Starts a FOR loop from 1 to 32
       FOR x = 1 TO 32
          ` Clear the value of the ChatText array at the value of x
          ChatText$(x) = ""
    
       ` End the FOR loop
       NEXT x
    
    ` This marks the end of the funciton
    ENDFUNCTION
    
    ` This function displays the chat text on the screen
    FUNCTION DisplayChatText()
       ` Clear the screen
       CLS
    
       ` Sets the bitmap we're sending the text to to bitmap 0 (the screen)
       SET CURRENT BITMAP 0
    
       ` Centres the text "SimpleChat - Copyright(c) Euan Reid, 2006" at x coordinate 400 and y coordinate 10. This is from when I released it in 2006, you can change it if you want. This entire thing is just the source code from an app I made, with added comments to make things REALLY clear
       CENTER TEXT 400, 10, "SimpleChat - Copyright(c) Euan Reid, 2006"
    
       ` Starts a FOR loop from 1 to 32
       FOR x = 1 TO 32
          ` Show the value of ChatText at the value of x at x coordinate 10 and y coordinate 10 plus the current value of x times 15
          TEXT 10, 10+(x*15), ChatText$(x)
    
       ` Ends the FOR loop
       NEXT x
    
    ` This marks the end of the function
    ENDFUNCTION
    With any luck, this will help you learn some DarkBASIC. I coded this using DarkBASIC Professional v1.0, and it worked then. It also works in DarkBASIC Professional v1.03, as I just tested it. I don't know if it will work in DarkBASIC v1.x (the not-professional version) as it is far more limited as regards networking commands.

    Happy coding!

    [DarkBASIC] Making an Instant Messenger
  2. #2
    warsheep's Avatar Contributor
    Reputation
    184
    Join Date
    Sep 2006
    Posts
    1,216
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So.. This is like a client for a instant messenger?
    Gotta download DarkBasic to test it out.

    Edit:
    Read through the well commented code... It looks great! Installing DarkBasic now, this sounds like something I'll have to check out!
    Last edited by warsheep; 03-27-2008 at 06:32 PM.
    FOR A MOMENT, NOTHING HAPPENED. THEN, AFTER A SECOND OR SO, NOTHING CONTINUED TO HAPPEN.

  3. #3
    Kissy's Avatar Active Member
    Reputation
    60
    Join Date
    Jun 2006
    Posts
    332
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you might wanna check line 32
    Code:
    :name

Similar Threads

  1. [Misc] Making an instant 85 welcomer NPC/Quest?
    By Blimey in forum WoW EMU Questions & Requests
    Replies: 4
    Last Post: 06-19-2011, 02:05 PM
  2. LEGIT Guide to making gold instantly!
    By Atoslayer2 in forum World of Warcraft General
    Replies: 1
    Last Post: 07-14-2009, 12:42 PM
  3. Sweet Addon I stumbled upon today...WoW Instant Messenger
    By Stans Dad in forum WoW UI, Macros and Talent Specs
    Replies: 38
    Last Post: 09-06-2008, 05:52 PM
  4. [AIM] New AOL Instant Messenger Themes
    By Mellexx in forum Community Chat
    Replies: 0
    Last Post: 08-06-2008, 08:58 AM
  5. The best Instant messenger! and free download
    By Zore. in forum Community Chat
    Replies: 9
    Last Post: 03-09-2008, 01:53 PM
All times are GMT -5. The time now is 04:37 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search