Parallax scrolling in less than 100 lines

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Parallax scrolling in less than 100 lines

Post by angros47 »

I got the idea from here:
http://www.youtube.com/watch?v=r_GeVKAZT4I

(it was an old demo for dos, called "Mario and Luigi")

Parallax scrolling in the second level is made by color cycling: the same effect can be achieved in freebasic:

Code: Select all

dim shared as integer p(255, 2)

sub Scroll()
	dim as ubyte ptr target, source
	screenLock 
	target = screenPtr
	source = target + 1
	for i as integer = 0 to 800*600-2
		*target = *source
		target +=1: source +=1
	next i
	screenUnlock
end sub

sub cycle (lowest as integer, highest as integer) 
	dim as integer a, b, c 
	a = p(highest, 0)
	b = p(highest, 1)
	c = p(highest, 2)
	for i as integer = highest to lowest +1 step -1
		p(i, 0) = p(i-1, 0)
		p(i, 1) = p(i-1, 1)
		p(i, 2) = p(i-1, 2)
		palette i, p(i, 0), p(i, 1), p(i, 2)
	next i
	p(lowest, 0) = a
	p(lowest, 1) = b
	p(lowest, 2) = c
	palette lowest, a, b, c
end sub



screenres 800,600, 8


'Prepare a tile
Dim image As Any Ptr = ImageCreate( 64, 64, 0,8)
line image,(1,1)-(63,63),6,bf
line image,(1,1)-(63,3),15,bf
line image,(1,1)-(3,63),15,bf

line image,(60,3)-(63,63),4,bf
line image,(3,60)-(63,63),4,bf

'prepare palette
for i as integer = 129 to 144
	p(i, 0) = 128
	p(i, 1) = 64
	p(i, 2) = 64
	palette i, p(i, 0), p(i, 1), p(i, 2)
next i
p(130, 0) = 64
p(130, 1) = 0
p(130, 2) = 0
palette 130, p(130, 0), p(130, 1), p(130, 2)



'main cycle

dim as integer i, y

do
	i+=1
	'background
	line (799,0)-(799,600),129+(i mod 16),,254
	line (799,0)-(799,600),129+((i+8) mod 16),,254*256
	line (799,0)-(799,600),128,,257

	
	'tile
	if (i mod 64)=0 then y=rnd(1)*8
	put (799,y*64),image, (i mod 64,0)-step(2,64),pset

	Scroll

	if i mod 2 then cycle 130,144

	sleep 5
loop until multikey(1)
No extra layers are required, and only a line of each tile is drawn at every frame (so, this trick can work even on slow systems)
VANYA
Posts: 1834
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Parallax scrolling in less than 100 lines

Post by VANYA »

Thank you, very helpful
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Re: Parallax scrolling in less than 100 lines

Post by kiyotewolf »

Wow, clever trick.



~Kiyote!
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Parallax scrolling in less than 100 lines

Post by fxm »

IMHO, we can simplify the two procedures:

Code: Select all

sub Scroll()
   dim as ubyte ptr target = screenPtr
   screenLock
   for i as integer = 0 to 800*600-2
      *(target + i) = *(target + i + 1)
   next i
   screenUnlock
end sub

sub cycle (lowest as integer, highest as integer)
   for i as integer = highest to lowest +1 step -1
      swap p(i, 0), p(i-1, 0)
      swap p(i, 1), p(i-1, 1)
      swap p(i, 2), p(i-1, 2)
      palette i, p(i, 0), p(i, 1), p(i, 2)
   next i
   palette lowest, p(lowest, 0), p(lowest, 1), p(lowest, 2)
end sub
Roland Chastain
Posts: 993
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Parallax scrolling in less than 100 lines

Post by Roland Chastain »

@angros47

Very nice !

@fxm

I've just tried your code. It doesn't give a good result. The scrolling isn't as smooth as with angros47's code.

:-)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Parallax scrolling in less than 100 lines

Post by fxm »

Roland Chastain wrote:@fxm
I've just tried your code. It doesn't give a good result. The scrolling isn't as smooth as with angros47's code.
IMHO, the two modified procedures produce exactly the same result, but 'Scroll()' is more fast and 'cycle()' is a very little more slow.
Post Reply