Scrolling Question

Started by
4 comments, last by Nobles 24 years, 4 months ago
I would actually create a system in where you center the view around a certain coordinate (most likely the character or a camera).

So if you are working in 800x600, then:

camera_x = 1024; // whatever;
camera_y = 1240; // whatever;

view_x = camera_x - 400; // half of 800
view_y = camera_y - 300; // half of 600
blit(view_x, view_y, 800, 600);

The reason it might be slow is you are only scrolling by one pixel at a time. You can actually go up to 1-32 pixels at a time and still make it 'smooth' enough.


Jim Adams

Advertisement
Ahh, ok I didn't even think of increasing the amount of pixels scrolling. That helped quite a bit, thanks!
I'm interested in what you were saying about setting the view scrolling around a camera or the character but I'm not sure if I understand how to implement it. I want the player to be able to move around the map w/ out having to move the character (Like Command & Conquer or Baldur's Gate) so I guess I would have to move a "camera." But I also need for the player to be able to move the mouse pointer freely in viewable area w/ out the screen moving... that is until he/she moves the cursor to the edge of the viewable area, then the map would move. Would I still be able to use the "camera" method you described?
I really want to use the cleanest method possible for this. So I'm game for any suggestions.
Thanks again!
-Nobles-
For the camera idea, you want to keep the location of the camera, and when the user bumps the side of the screen increment or decrement the location of the camera accordingly. Then blit your background in relation to the camera. i.e. if your camera is at 400, 300 blit the surface between 400,300 and 1200, 900.

Just as an aside, on x86 platforms assigning more than 2 variables to be register variables doesn't help, as there are only 4 general purpose registers and you need at least two of them for your arithmetic and store/load operations. It's arguable with the more recent (Pentium and newer) processors using more than one register variable is appropriate.

You can either have the camera represent a section of the map (ie 0,0 to 799,599), or as a single coordinate that you center on when rendering the view - it's up to you, but I would go with the single coordinate idea - tends to port better if you are going to change resolutions at any time.

So, you have a camera that you track the coordinates on. Keep track of the mouse coordinates on screen, and you can add the camera coordinates to it to get world coordinates if they are needed.

Use a timer so that when the mouse reaches the edges of the screen, after 1/2 second (or so many frames), the camera moves in that direction.

Let's say the camera coordinates are:
camerax = 1024; // whatever
cameray = 1234; // whatever

the mouse screen coordinates are:
screenmousex = 535; // whatever
screenmousey = 202; // whatever

Then, since the screen is 800x600, you would render the map with the following coordinates:

topx = camerax - 400; // screenwidth / 2
topy = cameray - 300; // screenheight / 2

And you would get the mouse world coordinates like:

worldmousex = screenmousex + topx;
worldmousey = screenmousey + topy;


Now, you check the mouse screen coordinates against the edges (an 8 pixel edge for example):

if(screenmousex < 8)
left_edge_count++;
else left_edge_count=0;

if(screenmousex > 791)
right_edge_count++;
else right_edge_count=0;

if(screenmousey < 8)
top_edge_count++;
else top_edge_count=0;

if(screenmousey > 591)
bottom_edge_count++;
else bottom_edge_count=0;

Check for scrolling - say if the mouse stays in spot for 15 frames (1/2sec at 30fps):

if(top_edge_count >= 15)
cameray -= camera_scrollspeed;
if(bottom_edge_count >= 15)
cameray += camera_scrollspeed;
if(left_edge_count >= 15)
camerax -= camera_scrollspeed;
if(right_edge_count >= 15)
camerax += camera_scrollspeed;


Make sure to get the mouse world coordinates after scrolling the field to ensure the camera coordinates haven't changing during that last part.


Jim Adams

Hello,
I've just started programming in 'C' recently using DJGPP w/ Allegro. I need some advice with scrolling a large map i.e. Baldur's Gate.
From what I've read everyone seems to prefer tiles. But for this project this is the method agreed on by the artist and I.
I've kinda' got it working but the scrolling is SO slow... and the horizontal scrolling is choppy. I've read something about this but am uncertain on what to do about it. I'm sure the way I'm going about the scrolling is probably misguided... so any suggestions on a better method would be greatly appreciated! Also would a Window's compiler offer any speed advantages over DJGPP?
The map is 1600x1200 w/ an 800x600 viewable area. Here is what I've been doing:

register int xl = 799;
register int xs = 1;
register int yl = 599;
register int ys = 1;

...blah-blah-code...

do {
x = mouse_x - 0;
y = mouse_y - 0;

if (x > xl){
++xl;
++xs;
}
else if (x < xs){
--xl;
--xs;
}
if (y > yl){
++yl;
++ys;
}
else if (y < ys){
--yl;
--ys;
}
request_scroll(xs, ys);

blit(background, screen, 0, 0, 0, 0, w, h);

} while (!keypressed());

Just to beat a dead horse...

The method i use is quite simple, and makes use of the POINT and RECT structs.

for those not familiar with POINT or RECT, POINT has two ints: x,y; and RECT has four ints: left,top,right,bottom.

any position can be represented by a POINT, any rectangular area can be represented by a RECT.

so, your world may be 1600x1600 pixels, and your resolution of choice is 800x600. (for the sake of discussion, i am assuming that the ENTIRE display is used)

your main character (or camera, or whatever) is represented by a POINT structure that describes a location in worldspace (described by the RECT l=0,t=0,r=1600,b=1600) (This is called an anchor point)

and of course, you want the camera to look at the center of the screen(400,300).

so, a RECT describing the extent of the camera is l=-400,t=-300,r=400,b=300

so, adding x to left and right, and y to top and bottom, you have the RECT in worldspace that describes what is seen in screen space.

but, we dont want our RECT to be outside of worldspace, so we clip our anchor to a RECT that ensures that the RECT will not contain any points not in worldspace.

we take the anchor extent RECT (-400,-300,400,300) and subtract it from the coordinates of worldspace (0,0,1600,1600), to get anchorspace (400,300,1200,1300). now, as long as the anchor is within anchorspace, we can plot whatever is in worldspace into screenspace without fear.

how does this relate to scrolling?

well, when scrolling, all you need to do is move the anchor, and clip it to anchorspace.

[This message has been edited by TANSTAAFL (edited December 16, 1999).]

Get off my lawn!

This topic is closed to new replies.

Advertisement