GLBasic forum

Main forum => GLBasic - en => Topic started by: Poetronic on 2012-Aug-02

Title: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Hi all,

I got two (well, ok, three) more questions which might be very easy to answer for most of you I guess...

1. How can I test if there is an internet connection and is that necessary when using the NETWEBGET command?

2. When I call a PHP-website to save to an online hiscore database, there seems to be a slight delay when jumping back into another game mode. Thereby, the animation that appears when you start the game gets slightly out of sync. What can I do to prevent that from happening?

As always: thank you very much!  :)
Title: Re: 2 Questions NETWEBGET
Post by: MrTAToad on 2012-Aug-02
I use this to determine whether an internet connection is available :

Code (glbasic) Select

LOCAL webAddresses$[]; DIMDATA webAddresses$[],"www.bbc.co.uk","www.microsoft.com","www.glbasic.com"

                self.isInternetAvailable%=FALSE
IF SOCK_INIT()
FOR loop%=0 TO BOUNDS(webAddresses$[],0)-1
socket%=SOCK_TCPCONNECT(webAddresses$[loop%],port%)
IF socket%>=0
SOCK_CLOSE(socket%)
self.isInternetAvailable%=TRUE
BREAK
ENDIF
NEXT

SOCK_SHUTDOWN
ENDIF


2.  You would have to compensate for the time delay and update any animations accordingly unless you use a thread for calculating the animation positions.
Title: 2 Questions NETWEBGET
Post by: Kitty Hello on 2012-Aug-02
2. or use the sock commands instead. See the networking for cowards tutorial.
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Hi thanks for your replies,

I will take a closer look at your comments right away, but I just noticed that when I move the window, the animations stop, and when I re-focus the window, the animations are totally out of sync? How can I keep that from happening? AUTOPAUSE TRUE doesn't seem to make a difference.

Thx in advance!
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Quote2.  You would have to compensate for the time delay and update any animations accordingly unless you use a thread for calculating the animation positions.

How exactly would I know how much time has passed since I used the NETWEBGET command? Isn't it possible to use the command and then check if it has been successful and THEN change the gamemode back to startscreen or something like that? I mean I don't really understand how the animation keeps getting out of sync in the first place - why does it? Explanations?  :)
Title: Re: 2 Questions NETWEBGET
Post by: Wampus on 2012-Aug-02
Quote from: Poetronic on 2012-Aug-02
How exactly would I know how much time has passed since I used the NETWEBGET command? Isn't it possible to use the command and then check if it has been successful and THEN change the gamemode back to startscreen or something like that? I mean I don't really understand how the animation keeps getting out of sync in the first place - why does it? Explanations?  :)

You could use a network timestamp of your own choosing, or probably much better (since I assume the app doesn't require sync with a server), use GETTIMERALL() to measure the time taken and adjust accordingly. However, the problem you're having with animation is a strange one and I can only imagine why animation is getting out of sync based on guesses about how you're handling animation.

If you had a small bit of example code you could post that reproduced the problem it would help a lot.
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Ok, here we go.


Problem is as follows:

The start screen of my game contains a scrolling text made up of an array of letters. The animation is smooth and simple (cf. att. 1). I just call a function inside my game loop in gamestate 0 (which is the startscreen) which animates the letters.

Now when I press the WINDOWS-key in order to change windows (mouse is locked to the window since it is moving the player) and then click on the window and move it a little bit, the animation freezes. But as soon as I release the mouse button and the game is resumed, the text animation is totally broken.

I don't know why that is happening... I thought that a) moving the program window could never cause problems inside the animation loop of the program and b) AUTOPAUSE TRUE would stop the program when it gets out of focus anyway (it does not).

Any form of help is highly appreciated! :)

PS - Perhaps I should have mentioned that the animation speed is based on deltatime.
Title: 2 Questions NETWEBGET
Post by: Kitty Hello on 2012-Aug-02
We need the code that calculates the position of the letters
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Here you go - don't shout at me, I'm just a hobby programmer :)

Code (glbasic) Select
FUNCTION ScrollText:
FOREACH scrl IN ScrList[]

IF scrl.angle > 205 THEN scrl.direction = 1
IF scrl.angle < 155 THEN scrl.direction = 0

IF scrl.x > -ScrlLength AND scrl.x < 641
PRINT scrl.letter$,scrl.x,scrl.y
ENDIF

IF scrl.x < -ScrlLength+640
scrl.x = 641
scrl.y = 380
scrl.angle = 225
ENDIF

IF scrl.x < 641
INC scrl.x, COS(scrl.angle)*scrl.speed*dt
INC scrl.y, SIN(scrl.angle)*scrl.speed/3*dt
IF scrl.direction = 0 THEN INC scrl.angle, 0.035*dt
IF scrl.direction = 1 THEN DEC scrl.angle, 0.035*dt
ENDIF

IF scrl.x >= 641 THEN DEC scrl.x, scrl.speed*dt // Reset x-coordinates = repeat loop

NEXT
ENDFUNCTION
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Somehow I don't think that the code is to blame (although it might look a bit naive to some of you guys).

The problem seems to be the way the program window freezes when I move it and loses sync when I release the mouse button. I read that AUTOPAUSE TRUE should stop all animations in the program when the window is out of focus, but it doesn't. There is no difference between using AUTOPAUSE TRUE or FALSE, the behavior of the pogram window stays exactly the same (also when I use ALT+TAB). The program always keeps running in the background.

Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Ok, I found out why AUTOPAUSE didn't work - I was still in DEBUG mode. But the problem remains the same: losing the animation sync when moving the window...
Title: Re: 2 Questions NETWEBGET
Post by: Wampus on 2012-Aug-02
Hmm. AUTOPAUSE TRUE won't freeze the game timers read with GETTIMERALL(). That might be an issue to resolve if you use that command as part of your deltatime calculations. The other issue with deltatime is the dreaded rounding errors that can occur.

What part of your code sets a new value for variable dt? (if I can be so nosy)
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
No need for excuses, you're not nosy at all! On the contrary, I really appreciate your help!

I use the GETTIMER() command as follows:

Code (glbasic) Select
WHILE TRUE

        // Here is the game loop

        dt = GETTIMER() // time since last SHOWSCREEN

SHOWSCREEN

WEND
Title: Re: 2 Questions NETWEBGET
Post by: Wampus on 2012-Aug-02
I'm almost certain the problem is the way large values for deltatime are interacting with your code, probably in the Sine and Cosine calculations.

Instead of using dt = GETTIMER() try setting dt = 16.6666, dt = 33.3332 and dt = 8.3333. What you'd want to happen is the animation to speed up or slow down but the positions of the letters to be correct. If the positions of the letters are incorrect then a new routine for dealing with the variable deltatime is needed. If it still looks ok, try setting large values for dt, to see how that goes.

I don't like variable deltatime because of the problems large or tiny values can create with code that doesn't anticipate them or accumulative rounding problems. I've used fixed-step deltatime in the past, which has its problems too but is generally easier to code.
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Uhm... wouldn't a fixed dt undermine the very concept of frame independent animation?

EDIT: But yeah, it absolutely solves the issue! What am I supposed to do now?
Title: Re: 2 Questions NETWEBGET
Post by: MrTAToad on 2012-Aug-02
Should be using GETTIMERALL() as GETTIMER is used for the time between SHOWSCREEN commands.
Title: Re: 2 Questions NETWEBGET
Post by: Wampus on 2012-Aug-02
Quote from: Poetronic on 2012-Aug-02
Uhm... wouldn't a fixed dt undermine the very concept of frame independent animation?

Yes, but this is just for testing.
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Ok, will do. I thought GETTIMER() was specifically made for dt calculation, since it returns the exact amount of millisec passed since the last SHOWSCREEN. Guess I was wrong :)
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Ok, just to make sure I am doing it right:

Code (glbasic) Select

WHILE TRUE

        oldtime = timer
       
        // Game Loop

        timer = GETTIMERALL()
        dt = timer - oldtime

WEND


Is that correct? For the speed of the movement I have to use something like 8 now to make the letters move. Does that sound alright? :)
Title: Re: 2 Questions NETWEBGET
Post by: Wampus on 2012-Aug-02
Actually its a little bit more complicated than that. GETTIMER() does measure the time since last SHOWSCREEN call. However, what you probably want is the exact (or near exact) time since the last measurement of GETTIMERALL() in your loop. So, something like this:-

Code (glbasic) Select
olddeltatime = timer

timer = GETTIMERALL()

deltatime = timer - olddeltatime


This should give you roughly 16.6666666 on a monitor running at 60 FPS with no interruptions and fixed vsync. If the window has been moved or something has interrupted the app then the value will be much higher.

EDIT: Ah, yes, you corrected it. Mute point.
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Same problem...
Title: Re: 2 Questions NETWEBGET
Post by: Wampus on 2012-Aug-02
Ok, sure thing. If you want to upload it I'll have a look.
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Hahaha I guess I am a little too tired... same error, still had the dt = 16.6667 in my code... maybe you want to download the whole project and take a look at the weird behavior? I could upload it for you and PM you a link.
Title: Re: 2 Questions NETWEBGET
Post by: Poetronic on 2012-Aug-02
Ok, sent you the link.