Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Clyde on July 13, 2006

Title: Simple Bank To Dim Conversion
Post by: Clyde on July 13, 2006
I thought this might be of benefit to some of you, especially people like me who are just getting accustomed to using banked arrays from along time of using the good old Dim(x,y) ways of storing and manipulating info. Especially usefull with imagary antics.

Code: [Select]
For Y=0 to Height-1
        For X=0 to Width-1
           
            DimData(X,Y)=BankData( X + ( Y * Width ))
       
      Next
Next

Hope it's of some use,
Cheers Clyde.
Title: Re: Simple Bank To Dim Conversion
Post by: Shockwave on July 14, 2006
Mmmm.. It might be a little tiny bit unclear what you are referring to there clyde so for less experienced coders, basically Clyde is showing a technique used for array access, usually this technique will apply to a screen.

You may be used to doing;

Code: [Select]
Dim SCREEN_ARRAY ( XRES , YRES )

In something like Blitz.

Let's suppose that you wanted to put a dot into screen pos 100,100;

Code: [Select]
SCREEN_ARRAY ( 100 , 100 ) = 1

Or whatever value you want to write into there.

you could then update your screen like this;
Code: [Select]
for y=0 to yres-1
for x=0 to xres-1

plot x,y , SCREEN_ARRAY ( X , Y )

next
next

Nice and simple.
However, Banks are faster and also in Freebasic the tinyptc library updates it's screen from a sequential buffer (one dimensional array), this is how you'd do it;
Code: [Select]
CONST WIDTH  = 640
CONST HEIGHT = 480
DIM SHARED AS INTEGER SCREEN_ARRAY ( WIDTH * HEIGHT )

And to access position 100,100 on the screen you need to do this;

Code: [Select]
X=100
Y=100
POSITION = ( X + ( Y * WIDTH) )
SCREEN_ARRAY ( POSITION ) = 1

PTC_UPDATE@SCREEN_ARRAY(0)  :P

So you can use a 1 dimensional array to simulate a 2 dimensional array like that.
Thanks for the snippet Clyde, most useful and I hope you don't mind me explaining it a little more in depth.
Title: Re: Simple Bank To Dim Conversion
Post by: Clyde on July 14, 2006
No worries dude, and thank you kindly for explaining it more.

Cheers,
Clyde.