Smooth Scrolling?

BlitzMax Forums/BlitzMax Beginners Area/Smooth Scrolling?

Shambler(Posted 2006) [#1]
I have tried all of the BMax scrolling examples I can find on this site and others but they all stutter or flicker.

Could there be a problem with my PC or is there no way yet to achieve solid looking scrolling in BMax?

I have tried many different graphics card settings/'Graphics' command values but I can not get the screen to smoothly scroll. =/


ImaginaryHuman(Posted 2006) [#2]
See Flip


Gabriel(Posted 2006) [#3]
Do you have VSync disabled in drivers by any chance?


bradford6(Posted 2006) [#4]
hint: make sure ALL of the variables are floats.


Shambler(Posted 2006) [#5]
I've narrowed it down , all variables are ints and I have tried forcing vsync on / off and application preference in the driver.

The scrolling is as smooth as I can get if my mainloop is this
While Not KeyHit(KEY_ESCAPE)
'Automatic scroll
pos=pos+direction

If pos=0 Or pos=256 Then direction=-direction

map.draw(pos,pos)

Flip

Wend

But as soon as I control the scrolling with keypresses the scrolling keeps stopping intermittently for a split second at random intervals...
While Not KeyHit(KEY_ESCAPE)
'Using keys
If KeyDown(KEY_LEFT) Then pos=pos+1
If KeyDown(KEY_RIGHT) Then pos=pos-1

map.draw(pos,pos)

Flip

Wend

Could there be some sort of latency or mis-reading of the keyboard going on here?

[Edit] And I've just noticed that in the automatic scrolling example, if I press keys on the keyboard this too makes the scrolling 'hiccup' for a split second.


Tom Darby(Posted 2006) [#6]
bradford6 has it: you're using ints when you should be using floats. If you use floats, your movement should be fluid.


While Not KeyHit(KEY_ESCAPE)
'Automatic scroll
pos#=pos#+direction#

If pos#=0 Or pos#=256 Then direction#=-direction#

map.draw(pos#,pos#)

Flip

Wend



I strongly recommend using Strict or SuperStrict in order to make sure you don't accidentally cast floats down to ints; that's likely the source of your trouble...


Shambler(Posted 2006) [#7]
Wow thanks to both of you!

To bradford for saying use floats ( which I read as use int's lol ) and Tom for reiterating it.

Why this makes a difference I'm not sure, int made sense to use since I am only using integer positions and 'back when I was a young lad' int's were faster too.

Anyway it is rock stable atm whether I use keys or not.

Cheers ^^


Tom Darby(Posted 2006) [#8]
...it makes the difference becuase in most cases, you're moving objects by only a few pixels (or less than a pixel) each flip. Unless you keep track of those sub-pixel changes, you'll get jerky movement when you try to move things about the screen.

Imagine, for example, a car sprite with a maximum speed of 3 pixels per flip--that is, its "acceleration" value falls between 0 and 3. Just for argument's sake, let's say that's equivalent to 120 mph in this case. If you use ints to track the movement of this car, than it can only ever travel at 0, 40, 80, or 120 miles per hour, since the car's acceleration can only be 0, 1, 2, or 3 pixels per flip. If you use floats, you can have the car travel at 0.25 pixels per flip--10 mph--so that while the sprite only moves 1 pixel every 4 frames, you're tracking the movement on a sub-pixel basis, which lets you know exactly where the car is--even though the position difference is too small to be represented on the screen.


ImaginaryHuman(Posted 2006) [#9]
You only need floats when your object might be moving a non-whole pixel amount per frame.


Shambler(Posted 2006) [#10]
That is what I assumed Angel and I am only moving the tiles with integer multiples.

Using int however means that the scrolling will stall intermittently however using float this does not happen ( unless there is some hard disk access which is understandable).

Incidentally when using float and scrolling by a fractional amount ( less than 1 ) other artifacts become apparent.

The edges of the tiles appear to change slightly and I assume this is caused because Max is using 3D to represent 2D graphics since I have had a similar effect in Blitz3D when using 3D to simulate 2D.

[Edit] This effect is far worse when using 'SetGraphicsDriver GLMax2DDriver()' rather than the default directX driver.

[Edit] Got rid of that edge thing by removing the FILTEREDIMAGE flag and using MASKEDIMAGE only i.e.

Global Images:TImage=LoadAnimImage("block1.png",32,32,0,32,MASKEDIMAGE)