Qbasicnews.com

Full Version: pixel * pixel scrolling
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have recently found a pixel by pixel scrolling tutorial in QBasic.

pixel by pixel scrolling in Qbasic, by Aaron Severn

It's been around for a while I bet, so I tried it out.
It works very well, except that the original tutorial works with 10 * 10 pixel sprites.

can anyone tell me which numbers I have to change to make it scroll 16 * 12 tiles?
He didn't remark that portion very well.
Well, you could always rewrite it using your own code. Pixel*pixel scrolling is really easy, actually, I don't see what the big deal is...

Okay, here's how you do it (the tutorial gives you this, too, just in a longer form, and I think he mentions clipping, too):

1) You have two variables (offset variables)that track how much the map has been scrolled (x and y-values).

2) When you draw the tiles as usual, simply do the following:
a) Compute where you would draw the tile normally (you can use an existing tile engine for this, just something that can draw tilemaps)
b) Add the offsets to the final x and y coordinates of the tile BEFORE you draw the tile.

See how simple it is? Now, go off and write yourself a nice, flexible tile engine. You'll learn more that way than copying and pasting and modifying somebody else's code. Then, in the future, you'll always be able to inplement a smooth-scrolling, pixel*pixel tile engine no matter what platform you're on.

This technique also works for isometric tile engines, by the way, and so if you ever decide to implement an isometric game, you will be able to do it easily just by modifying the way you draw the tiles.
ok, I've built my own pixel by pixel scrolling engine.
it works alright, but I was wondering one thing.
is there any way to get a faster command than "MOD"?
(returning the remander of a division)
I realized how slow it is, probably because it uses floating point opperations.
If it's stuff MOD 2 you could replace it with stuff AND 1
Quote:If it's stuff MOD 2 you could replace it with stuff AND 1

Let me clarify that.

X MOD Y will evaluate to the same result as X AND Y if Y+1 is an integer power of 2.

Anonymous

and X \ Y evaluates to X shr Z, where Y = 2^Z
(y HAS to be a power of 2)


to math geeks out there:

how would you factor Z from this: Y = 2^Z :-?
Quote:ok, I've built my own pixel by pixel scrolling engine.
it works alright, but I was wondering one thing.
is there any way to get a faster command than "MOD"?
(returning the remander of a division)
I realized how slow it is, probably because it uses floating point opperations.

Use tile dimmensions that are a power of two. That way you won't have to multiply/divide/modulize ever.

If you do 16*16 tiles, QB will change /16 and *16 by "SHR 4" and "SHL 4" which are binary shifts (right and left, respectively) - so much faster. The module can be calculated as mentioned: A AND 15 is the same as A MOD 16. This only works with powers of two (i.e. A MOD 2 = A AND 1, A MOD 4 = A AND 3, etcétera).
Quote:and X \ Y evaluates to X shr Z, where Y = 2^Z
(y HAS to be a power of 2)


to math geeks out there:

how would you factor Z from this: Y = 2^Z :-?

Rational Exponents

Exponential Equation:
a^x = a^y

So Y = 2^Z
You would turn Y into a base of 2 then solve x = y

Let's say Y = 8 then:
2^3 = 2^Z <- 2^3 = 8 as a base of 2
x = y
3 = Z
Therefore Z = 3

Let's say Y = 16 then:
2^4 = 2^Z <- 2^4 = 8 as a base of 2
x = y
4 = Z
Therefore Z = 4

...

That's Exponential Equations in a nutshell, if you need more help just ask Big Grin

Anonymous

that didnt make any sense, you just replaced the "8" with 2^3, of course i know how to do that, i wanted the math to figure that out ;p

could you write a function that takes an integer as an arg, and returns the power of two that the number is? like:

Code:
? getexponent( 16 )
'' prints 4

? getexponent( 256 )
'' prints 8

ya know?
Just use Logs Wink
Pages: 1 2