Author Topic: Log In and Error Message Screens  (Read 3718 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Log In and Error Message Screens
« on: November 28, 2019, 02:05:13 pm »
I'm working on making a couple of games which run on the internet; but I wanted to make them a bit more professional looking than just the little demo which I posted with the "Click It!" game...   One feature which I've incorporated is a simple little LOG IN/ REGISTER screen, which I thought I'd share independently, in case anyone would ever want to include it into one of their programs in the future.  Test it out and see how it works for you, and let me know if there's anything missing which I need to add to the little function.

Code: [Select]
$COLOR:32
SCREEN _NEWIMAGE(1024, 720, 32)

CLS
result = LogIn(Detail$)
PRINT Detail$


FUNCTION LogIn (Detail$)
    CONST True = -1, False = 0
    DIM DC AS _UNSIGNED LONG, BGC AS _UNSIGNED LONG
    D = _DEST: Disp = _AUTODISPLAY
    DC = _DEFAULTCOLOR: BGC = _BACKGROUNDCOLOR
    BG = _COPYIMAGE(D)


    'DO
    '    count = count + 1
    '    Client = _OPENCLIENT("TCP/IP:7993:172.93.60.23") 'attempt to join as client
    '    _LIMIT 10
    'LOOP UNTIL Client <> 0 OR count > 100
    'OK$ = Verify$
    'IF LEFT$(OK$, 4) <> "[OK]" THEN LogIn = 0: EXIT FUNCTION

    LogInScreen = _NEWIMAGE(200, 240, 32)

    _DEST LogInScreen
    _KEYCLEAR 'clear the buffer before we start accepting stray input as being part of our login
    Xoffset = (_WIDTH(D) - _WIDTH(LogInScreen)) / 2
    Yoffset = (_HEIGHT(D) - _HEIGHT(LogInScreen)) / 2
    DO
        CLS , DarkGray
        LINE (3, 3)-(_WIDTH - 4, _HEIGHT - 4), LightGray, BF
        LINE (20, 40)-(180, 200), DarkGray, BF
        LINE (23, 68)-(177, 92), DarkGray, BF
        LINE (23, 128)-(172, 152), DarkGray, BF
        LINE (25, 70)-(175, 90), Black, BF
        LINE (25, 130)-(175, 150), Black, BF
        LINE (20, 210)-(100, 230), DarkGray, BF
        LINE (22, 212)-(98, 228), Green, BF
        LINE (101, 210)-(180, 230), DarkGray, BF
        LINE (103, 212)-(178, 228), Red, BF

        COLOR DarkBlue, 0
        _PRINTSTRING (100 - _PRINTWIDTH("CHECKERS") / 2, 15), "CHECKERS"
        _PRINTSTRING (100 - _PRINTWIDTH("User Name") / 2, 95), "User Name"
        _PRINTSTRING (100 - _PRINTWIDTH("Password") / 2, 155), "Password"
        COLOR Yellow
        _PRINTSTRING (60 - _PRINTWIDTH("LOG IN") / 2, 212), "LOG IN"
        _PRINTSTRING (141 - _PRINTWIDTH("REGISTER") / 2, 212), "REGISTER"
        count = (count + 1) MOD 30
        IF count MOD 15 = 0 THEN blink = NOT blink
        k = _KEYHIT
        SELECT CASE k
            CASE 9, 13 'tab or enter to toggle between the two fields.
                S = NOT S
            CASE 8 'backspace
                IF S THEN p$ = LEFT$(p$, LEN(p$) - 1) ELSE n$ = LEFT$(n$, LEN(n$) - 1)
            CASE 27 'Escape quits before we login
                'Detail$ = "[ERROR]User Exited Log In Manually"
                GOTO function_exit
            CASE 32 TO 255 'visable characters
                IF S THEN p$ = p$ + CHR$(k) ELSE n$ = n$ + CHR$(k)
        END SELECT
        COLOR White

        _PRINTSTRING (27, 72), RIGHT$(n$, 18): _PRINTSTRING (27, 132), RIGHT$(p$, 18)
        IF blink THEN
            IF S THEN l = _PRINTWIDTH(p$) ELSE l = _PRINTWIDTH(n$)
            _PRINTSTRING (27 + l, 72 - 60 * S), "_"
        END IF
        _PUTIMAGE (Xoffset, Yoffset), LogInScreen, D
        WHILE _MOUSEINPUT: WEND

        mb = _MOUSEBUTTON(1): mx = _MOUSEX - Xoffset: my = _MOUSEY - Yoffset
        IF mb = True AND oldmouse = False THEN
            IF my >= 210 AND my <= 230 THEN 'we're in the right X/Y position
                IF mx >= 20 AND mx <= 100 THEN
                    'Run log in code
                    'Send Client, "[LOG IN]" + n$ + "," + p$ 'Register a new account
                    'OK$ = Verify$
                    'Detail$ = OK$
                    'IF LEFT$(OK$, 4) = "[OK]" THEN LogIn = -1: GOTO function_exit 'Success
                    Detail$ = "User: " + n$ + CHR$(13) + "Password: " + p$
                    LogIn = 0
                    GOTO function_exit 'Failure
                END IF
                IF mx >= 101 AND mx <= 180 THEN
                    'Run register code
                    'Send Client, "[REGISTER]" + n$ + "," + p$ 'Login from an existing account
                    'OK$ = Verify$
                    'Detail$ = OK$
                    'IF LEFT$(OK$, 4) = "[OK]" THEN LogIn = -1: GOTO function_exit
                    Detail$ = "User: " + n$ + CHR$(13) + "Password: " + p$
                    LogIn = 0
                    GOTO function_exit
                END IF
            ELSEIF my >= 70 AND my <= 90 AND mx >= 20 AND mx <= 180 THEN
                S = 0
            ELSEIF my >= 130 AND my <= 150 AND mx >= 20 AND mx <= 180 THEN
                S = -1
            END IF
        END IF
        '_DISPLAY
        _LIMIT 30
        oldmouse = mb
    LOOP

    function_exit: 'Safely restore our settings.

    _DEST D
    IF Disp THEN _AUTODISPLAY

    _PUTIMAGE , BG, D 'restore the background
    COLOR DC, BGC
    _FREEIMAGE BG
END FUNCTION

PS:  If you're not using the latest Development Builds, the $COLOR:32 version isn't going to work for you.  Just replace the colors inside with whatever color scheme you'd like for the pop up to have for you.  ;)
« Last Edit: November 29, 2019, 05:55:28 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Log In Screen
« Reply #1 on: November 28, 2019, 02:31:09 pm »
This is exciting to be able to code games everyone can play, wow!

Also maybe run demo visual programs without having to download or workup gifs?
but I guess they would have to be compatible versions of QB64.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Log In Screen
« Reply #2 on: November 28, 2019, 02:47:10 pm »
This is exciting to be able to code games everyone can play, wow!

Also maybe run demo visual programs without having to download or workup gifs?
but I guess they would have to be compatible versions of QB64.

Nearly any version of QB64 should work... The server is simply asking for text input and then returning text back to the client.  I'm just in the habit of being lazy and making use of things like the $COLOR:32 command so I can use color names instead of needing _RGB or &HFFFFFFFF values.  Except for using it, I imagine any version of QB64 all the way back to the SDL versions should run the code without any real issues.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Log In and Error Message Screens
« Reply #3 on: November 29, 2019, 05:54:04 am »
Added an Error Message Screen as well, which I thought I'd share in case anyone might ever want to make use of it independently for something.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. 'draw some junk for the background
  3.  
  4. FOR x = 1 TO 100
  5.     LINE (RND * 400, RND * 300)-(RND * _WIDTH, RND * _HEIGHT), _RGB32(RND * 256, RND * 256, RND * 256), BF
  6.  
  7. E$ = "You ate too much macaronni and chesse, turkey, and pumpkin pie for Thanksgiving!  Try not to die!"
  8. DisplayError E$
  9.  
  10.  
  11.  
  12.  
  13.  
  14. SUB DisplayError (ErrorMessage$)
  15.     D = _DEST: Disp = _AUTODISPLAY
  16.     BG = _COPYIMAGE(D)
  17.     E$ = ErrorMessage$ 'back up the string so we don't corrupt it any way.
  18.  
  19.     LogInScreen = _NEWIMAGE(200, 240, 32)
  20.     _DEST LogInScreen
  21.     _KEYCLEAR 'clear the buffer before we start accepting stray input as being part of our login
  22.     Xoffset = (_WIDTH(D) - _WIDTH(LogInScreen)) / 2
  23.     Yoffset = (_HEIGHT(D) - _HEIGHT(LogInScreen)) / 2
  24.  
  25.     CLS , DarkGray
  26.     LINE (3, 3)-(_WIDTH - 4, _HEIGHT - 4), LightGray, BF
  27.     LINE (5, 5)-(_WIDTH - 6, 27), DarkGray, BF
  28.     LINE (6, 6)-(_WIDTH - 7, 26), White, BF
  29.     LINE (5, 30)-(_WIDTH - 6, 180), DarkGray, BF
  30.     LINE (6, 31)-(_WIDTH - 7, 179), Black, BF
  31.     LINE (75, 190)-(125, 225), DarkGray, BF
  32.     LINE (78, 193)-(122, 222), Green, BF
  33.     ViewWidth = _WIDTH - 13 - _FONTWIDTH '(_WIDTH -7, -6 from above for our print box area)
  34.     ViewHeight = 179 - 31 '(again, from the 179, 31 above)
  35.  
  36.     U = ViewHeight \ _FONTHEIGHT - 1
  37.     IF U < 0 THEN U = 0 'we can't have negative lines to print to!
  38.     DIM A(U) AS STRING 'the most lines we can print too
  39.  
  40.     COLOR Red, 0
  41.  
  42.     i = 0: out$ = ""
  43.     FOR j = 1 TO LEN(E$)
  44.         out$ = out$ + MID$(E$, j, 1)
  45.         IF _PRINTWIDTH(out$) >= ViewWidth THEN 'we're printing off the screen
  46.             j1 = 0 'back up to our next valid breakpoint.
  47.             DO
  48.                 j = j - 1: j1 = j1 + 1
  49.                 SELECT CASE MID$(out$, LEN(out$) - j1, 1)
  50.                     CASE " ", ".", ";", "-", "!", "?": EXIT DO 'valid end of line breaks.
  51.                 END SELECT
  52.             LOOP UNTIL j1 >= ViewWidth
  53.             IF j1 >= ViewWidth THEN j = j + j1: j1 = 0
  54.             'if we have such a long word that we can't break it
  55.             '(perhaps like a html link), then print as much of it on one line as we can and carry on
  56.             'with business like usual.
  57.             A(i) = _TRIM$(LEFT$(out$, LEN(out$) - j1))
  58.             out$ = ""
  59.             i = i + 1
  60.             IF i > U THEN EXIT FOR
  61.         END IF
  62.     NEXT
  63.     IF i < U AND out$ <> "" THEN A(i) = out$
  64.  
  65.     p1 = (U - i) / 2
  66.     FOR p = 0 TO U 'center the text as much as possible inside our error box
  67.         _PRINTSTRING (100 - _PRINTWIDTH(A(p)) / 2, 33 + _FONTHEIGHT * (p + p1)), A(p)
  68.     NEXT
  69.  
  70.     COLOR Black, 0
  71.     _PRINTSTRING (100 - _PRINTWIDTH("OK") / 2, 200), "OK"
  72.  
  73.     DO
  74.         count = (count + 1) MOD 2
  75.         IF count MOD 2 = 0 THEN blink = NOT blink
  76.         IF blink THEN COLOR Red, 0 ELSE COLOR Black, 0
  77.         _PRINTSTRING (100 - _PRINTWIDTH("WARNING: ERROR") / 2, 10), "WARNING: ERROR"
  78.         k = _KEYHIT
  79.         SELECT CASE k
  80.             CASE 13, 27, 32: EXIT DO 'Space,Enter, Escape will click "OK" and close the window
  81.         END SELECT
  82.  
  83.         WHILE _MOUSEINPUT: WEND
  84.         mb = _MOUSEBUTTON(1): mx = _MOUSEX - Xoffset: my = _MOUSEY - Yoffset
  85.         IF mb = -1 AND oldmouse = 0 THEN
  86.             IF mx >= 75 AND mx <= 125 AND my >= 190 AND my <= 225 THEN
  87.                 EXIT DO 'we clicked our "OK"
  88.             END IF
  89.         END IF
  90.         oldmouse = mb
  91.         _PUTIMAGE (Xoffset, Yoffset), LogInScreen, D
  92.         _DISPLAY
  93.         _LIMIT 10
  94.     LOOP
  95.  
  96.     _DEST D
  97.     IF Disp THEN _AUTODISPLAY
  98.     COLOR DC, BGC
  99.     _PUTIMAGE , BG, D 'restore the background
  100.     _FREEIMAGE BG

Once again, if you're not using the latest development build, you'll need to replace the color names with values which you prefer instead.  ;)
« Last Edit: November 29, 2019, 05:55:52 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Log In and Error Message Screens
« Reply #4 on: December 01, 2019, 10:05:45 am »
Great Steve.
One day I can follow you in this project.
Quote
I'm just in the habit of being lazy
But why lazyness? I imagine that game on server must reduce to minimum the amount of data to exchange on the connection so that the server must be not so hardly overloaded. So in this my imagination in a game online the client part works for sound, music and graphics on the local machine and send string message events to the server that must cohordinate the several clients connected... with a strong structure based on timedconnection and a strong structured map of the game. In a wide world I think it is spending / wasting resources and time  to search a collision with standard method used for a single player local game. Think just 100 connected players to a Minecraft server, Unreal arena, Age of Empire arena, or modern FortNite! Or a chess server  to play online!
So  server or limits itself to group and share data on the world  and client detects events byself or send a data plus a probability of event to check in the local machines.
But this is only what I can think in general about gaming online.

Sorry I's forgetting the core of my post:
please change code here:
from this
Code: QB64: [Select]
  1. E$ = "You ate too much macaronni and chesse, turkey, and pumpkin pie for Thanksgiving!  Try not to die!"

to this
Code: QB64: [Select]
  1. E$ = "You ate too much maccaroni and parmiggiano, turkey, and pumpkin pie for Thanksgiving!  Try not to die!"
Programming isn't difficult, only it's  consuming time and coffee