Parallax scrolling in less than 100 lines

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

Parallax scrolling in less than 100 lines

Postby angros47 » Jun 13, 2012 17:27

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: 1323
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Parallax scrolling in less than 100 lines

Postby VANYA » Jun 14, 2012 2:33

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

Postby kiyotewolf » Jun 16, 2012 7:45

Wow, clever trick.



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

Re: Parallax scrolling in less than 100 lines

Postby fxm » Jun 16, 2012 8:18

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: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: Parallax scrolling in less than 100 lines

Postby Roland Chastain » Jun 16, 2012 13:35

@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
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Parallax scrolling in less than 100 lines

Postby fxm » Jun 16, 2012 14:13

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.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 27 guests