Jump to content

  • Log In with Google      Sign In   
  • Create Account


Scrolling buffer in a tile engine


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 fatjaba   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 October 2004 - 03:48 AM

Hi, I have a question that hopefully someone can help me with. I have been reading about tile engines for a while now and all the articles I seem to read say blit the tiles straight to the screen, but using offsets to allow for smooth scrolling. This I understand. However, on a limited device say a pda/mobile phone this is not possible due to the limited amount of draws you can do per refresh. So what I heard you can do is make an offscreen buffer and then only draw this, so you would scroll the buffer and draw only the buffer to the screen making it much faster. However, my problem is this, say the buffer is only one tile width larger than the screen of the device, what happens when you have scrolled one tile width and you run out of buffer to update? I think i may be being stupid, but some help would really be appreciated! ------------------------- | | | |-----------------| | | | screen | | | | | | | | | | | |-----------------| | | buffer | |------------------------

Sponsor:

#2 overflowed_   Members   -  Reputation: 194

Like
0Likes
Like

Posted 19 October 2004 - 09:11 AM

I think you're just trying to implement double buffering, in which case you wouldn't scroll the buffer, you'd scroll whatever you're drawing to the buffer.

The back-buffer should be the same size of the screen, whatever that may be. You draw everything to it (to limit the number of screen-draws) and when the frame is done, you blit the entire back-buffer to the screen, thus resulting in a single screen-draw. Makes things much smoother on computers (no flickering).

GameTutorials.com has some Win32 tutorials using .Net that demonstrate double-buffering. Haven't looked through it myself, but it may be of some use. Basically, just treat the buffer like you would have were it the screen, and when finish, swap the buffers (blit it to the screen).

Have fun!
-----BEGIN GEEK CODE BLOCK-----Version: 3.12GCS/M/S d->+(++) s+: a19? C++++ UL++ P+++ L+ !E W+++ N+ o++ K? w!O M-- V? !PS PE Y+ PGP t++ 5+++ X R tv+> b+(++)>+++ DI+++>+++++ D++G e>++++ h! r y?------END GEEK CODE BLOCK------

#3 The Rug   Members   -  Reputation: 628

Like
0Likes
Like

Posted 19 October 2004 - 09:28 AM

Doesn't that problem exist if you have drawn your tiles directly to the screen, only in that case you'd run out of tiles instead of buffer? You'd just have to either make sure you don't scroll the buffer too far, or tile the buffer, depending on whether you want the world to be limited to within a certain bounds or youd want it to wrap around.

#4 doynax   Members   -  Reputation: 850

Like
0Likes
Like

Posted 19 October 2004 - 09:30 AM

Treat the backbuffer like it's circular (i.e. a two dimensional ring buffer), when scrolling you just wrap around the edges and blit any new tiles on the other side. Finally you copy the buffer to screen in four separate pieces.

And the result is unlimited four-directional scrolling without using any extra copying!
Am I the only 386 coder left around here? ;)

#5 fatjaba   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 October 2004 - 10:48 AM

doynax, thats exactly the kinda thing I wanted to hear, can you explain that to me in a bit more detail, or point me in the direction of a tutorial/article on it?

Thanks for the replies guys!

#6 doynax   Members   -  Reputation: 850

Like
0Likes
Like

Posted 19 October 2004 - 11:11 AM

Quote:
Original post by fatjaba
doynax, thats exactly the kinda thing I wanted to hear, can you explain that to me in a bit more detail, or point me in the direction of a tutorial/article on it?
Okay.. here's an example.

This is the original screen that you want to display (with tiles A through D)
0123
----
ABCD
ABCD
ABCD
ABCD
The slow way of left scrolling this buffer would be to copy the whole thing one step to the left and then bring in a new column tiles (E).

0123
----
BCDE
BCDE
BCDE
BCDE
The fast way is instead of actually copying all of the data we only replace the column that scrolled out (A) with the new one (E).
0123
----
EBCD
EBCD
EBCD
EBCD
The idea is that you view the buffer as if it extends infinitely to the right but repeating the same data every fourth column.

0|1230|1230123...
-+----+----------
E|BCDE|BCDEBCE...
E|BCDE|BCDEBCE...
E|BCDE|BCDEBCE...
E|BCDE|BCDEBCE...
When displaying this buffer we have to blit it to screen in two parts. Map buffer column 1-3 onto screen column 0-2 and buffer column 0 to screen column 3.

Things get a bit more complicated when scrolling multi-directionally, but it's really not that hard.
If you need more help I could probably post some sample code.

#7 overflowed_   Members   -  Reputation: 194

Like
0Likes
Like

Posted 19 October 2004 - 05:28 PM

That's a great way to do it doynax, and I did actually think about that, I was just trying to present a simple double-buffering method.

It should be noted though, that if you want to have the actual world larger than a single buffer (you mentioned it's tile-based, so I'm assuming it is), you'll need to make the back-buffer 1 column and 1 row larger than the screen, or you'll get black spaces on the screen when you do smooth scrolling.

With 2-dimensional scrolling, you'll also need to do as many as 4 blits to the screen.

Edit:
Sorry, you'll either get black spaces or you'll get part of the column from the other side of the screen, and assuming it's not a donut world, that's not what you want.
-----BEGIN GEEK CODE BLOCK-----Version: 3.12GCS/M/S d->+(++) s+: a19? C++++ UL++ P+++ L+ !E W+++ N+ o++ K? w!O M-- V? !PS PE Y+ PGP t++ 5+++ X R tv+> b+(++)>+++ DI+++>+++++ D++G e>++++ h! r y?------END GEEK CODE BLOCK------

#8 fatjaba   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 October 2004 - 09:35 PM

I think I get it this time, but some sample code would be very much appreciated as well.

Thanks again!

#9 doynax   Members   -  Reputation: 850

Like
0Likes
Like

Posted 20 October 2004 - 12:14 AM

Quote:
Original post by fatjaba
I think I get it this time, but some sample code would be very much appreciated as well.

I wrote some sample code, didn't get much sleep tonight though so there's probably a few dozen bugs in it..

Everything is specified in whole tiles and it only handles horizontal scrolling. If you need fine scrolling just make the buffer one tile wider to offset the screen blit within (like overflowed_ described).

// The screen's width
enum { Size = 40 };

// Current position. Initially offscreen so that the first call draws the whole buffer
int CurPos = -Size;

// Draw a column in the tile map at MapPos onto BufPos in the scroll buffer
void DrawColumn(int MapPos)
{
// calculate buffer position
int BufPos = MapPos % Size;

// draw the individual tiles
...
}

void ScrollTo(int NewPos)
{
// Only draw the columns once when scrolling more than a screen at a time
if(abs(NewPos - CurPos) > Size)
{
CurPos = NewPos - Size;
}

// Draw the new columns
if(CurPos < NewPos) {
// scroll left
while(CurPos < NewPos) {
DrawColumn(CurPos++ + Size);
}
} else {
// scroll right
while(CurPos > NewPos) {
DrawColumn(--CurPos);
}
}
}

void DisplayBuffer() {
int RSize = CurPos % Size; // Calculate the left buffer block's size
int LSize = Size - RSize; // The remainder is the right block's size

// Blit both blocks to screen
BlitToScreen(RSize, 0, LSize);
BlitToScreen(0, LSize, RSize);
}

// Blit a part of the buffer to screen
void BlitToScreen(int BufPos, int ScreenPos, int Size);





[Edited by - doynax on October 20, 2004 6:14:49 AM]

#10 fatjaba   Members   -  Reputation: 122

Like
0Likes
Like

Posted 20 October 2004 - 01:02 AM

Im still not entirely sure I understand that code, with the drawcolumn part are you not going to end up drawing a lot of columns to the buffer? or are the columns tiles of width 1?

#11 doynax   Members   -  Reputation: 850

Like
0Likes
Like

Posted 20 October 2004 - 01:23 AM

Quote:
Original post by fatjaba
Im still not entirely sure I understand that code, with the drawcolumn part are you not going to end up drawing a lot of columns to the buffer? or are the columns tiles of width 1?

All coodinates are specified in whole tiles, not pixels. Infact the code doesn't deal with pixels at all.
So DrawColumn(3) with a tile width of 16 draws a column of tiles at pixel 48.

#12 fatjaba   Members   -  Reputation: 122

Like
0Likes
Like

Posted 20 October 2004 - 02:35 AM

Ahh gotcha, in that case you are my new hero. :)

#13 Ryan Clark   Members   -  Reputation: 212

Like
0Likes
Like

Posted 21 October 2004 - 04:27 AM

I think that this article on Scrolling with Framing Buffers might be what you're looking for!


Ryan




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS