Sign in to follow this  
raptorstrike

scrolling in sdl

Recommended Posts

One idea that springs to mind is to create a huge image (much, much larger than the screen) of all the items that are not part of the moving item. This is done once at the start of the program (or as an image through some art package). Then blit to the screen only the area you want, and place your 'moving' item at the same spot on the screen everytime.

This assumes you don't need to place any objects in front of the moving item.

HTH.

Share this post


Link to post
Share on other sites
this COULD work but since alot of my items move around and the sceen is constantintly changing its just not practical.
if all else fails ill just make a seprate map for every section i just wanted to know if there was a way to scroll without moving everything behind it

Share this post


Link to post
Share on other sites
here is a reply i posted to someone who had this question before. you can read the thread here.

if you want the screen to scroll, your going to need scrolling offsets.

this is going to be hard to explain, but ill try my best. in order to have a scrolling screen, you need to start thinking in WORLD coordinates. throw screen coordinates out the window, THERE IS NO MORE SCREEN COORDINATES! understand? only WORLD coordinates.

from now on, ALL of your characters and objects will store WORLD positions. this means their x/y position could be (20,30), or (2102,24320), the point is, their position is no longer limited to the screen. it is only limited by the world. IE, your position can be (9000,9000), as long as the world your in is that big, IE, the map you are on is bigger then (9000,9000).

now, your going to have to define 2 varialbes - Screen_Offset_X and Screen_Offset_Y. make them integers. this represents the offset that the screen is into the world. this is the position of the screen / camera in the world. lets pretend your screen is 800x600 resolution, and lets also pretend your on a map which is 2000x2000 pixels big. heres an image:



Take a look. this is your world. the top left corner of your world is 0,0 and the bottom right corner is 2000,2000. right now, your player is somewhere towards the top left portion of the world.

each frame, simply have the "camera", or the screen, follow the player. to do this, each frame, do this:

Screen_Offset_X = player.xPosition - (400);
Screen_Offset_Y = player.yPosition - (300);

do you see what i mean? you LOCK the screen position to follow the player - make it he players position Minus half the screens dimensions - bingo, the screen now follows the player around, keeping the player in the center of the screen at all times. you can factor in the players dimensions in that equation too, to make it more centered, but that is the gist of it.

now, theres one thing to note. You cant draw your objects at their position anymore. Why? well, the player or object no longer has a screen position, a world position, remember? something that could be huge, like (5000,2000), or (3423,3090). try drawing something at those positions, and what will happen? well, it wont show up, since you can only draw within the bounds of the screen, in our case, only on the x plane of (0,800) and the y plane of (0,600).

so, how do you draw everything then, if you cant draw them at their world position? the answer is simple. you just "cast" the world position into the screen coordinates... but how you ask? easy! just subtract the current screens offset from the objects position! the world position is now converted to a screen position.

Draw_Image(player,player.xPos - Screen_Offset_X,player.yPos - Screen_Offset_Y);

see what happends? your "casting" from world coordinates back to screen coordinates...

theres 2 important things to always remember:

1) YOU MUST ALWAYS STORE EVERYTHING IN WORLD COORDINATES!!!
2) YOU MUST ALWAYS DRAW EVERYTHING IN SCREEN COORDINATES!

remember, to convert from World to Screen, just do Position - Offset

to convert from Screen to World, just do Position + Offset. You will probably need to convert from screen to world in certain situations, like when using the mouse and stuff.

i hope some of this made sence.

[Edited by - graveyard filla on December 5, 2004 12:13:33 AM]

Share this post


Link to post
Share on other sites
Are you trying to say scroll around the map to view whats going on without having to move your player around? If this is the case you might want to make 2 of the camera objects like graveyardfilla explained. One to follow the player, then if you want, one to switch to that is independent of any of the game objects. Or, you could just detatch the camera from the player and just have it move based on the arrow keys. Then when you want it to go back to the player, just re attatch it to the players coordinates.

This can sound a bit confusing I guess but really, all a camera in a 2d tile game is is an offset value of where to start drawing the game view. Lets expand a little on to what filla was saying about your game objects only knowing their position in world coordinates. You will eventually need to do 2 things in your games. Convert from world to screen coordinates in order to draw things where they should be, and convert from screen to world coordinates to pick out items you might click on.

screen_to_world:
world_x = screen_x+camera_x
world_y = screen_y+camera_y

world_to_screen:
screen_x = world_x-camera_x;
screen_y = world_y-camera_y;

When we talk about attatching the camera to a game object, we mean using the objects world coordinates to place the camera. Usually we want the object to be in the middle of the screen so the camera is usually offset from the object by 1/2 the screen width and 1/2 the screen height. If you remove the game object from this equation, then you have a camera that resembles that of a RTS game where you can view different parts of the map by moving the camera and you are not focused on any one particular object.

I hope that helps answer your question, if not then I don't think I really understand the question.


Share this post


Link to post
Share on other sites
ok i think i get how to do it but i dont know were people are comming up with the idea that i DONT want my player move. I stated in the begining and on out that i ONLY want my player to move not the background to.

Share this post


Link to post
Share on other sites
I just started working on a 2d side scroller, and scrolling is one of things I'm currently learning how to do. To everyone who made informative posts, thank you very much!

*Bookmarks Thread*

Share this post


Link to post
Share on other sites
If you want it to scroll one screen in another direction then it would be like above except:

You would break your map into several screens. Then you would constantly detect for the edge of the viewable screen, like collision detection. When the player hit the edge of the viewable screen you would simply shift the POV to the next viewable screen, adding an accelerated scroll to it would make it look better as well.

You would need to keep a map of where every screen is relative to one another. And I haven't thought far enough ahead to accomodate that >.<

That isn't a very technical description, I hope it's right though ;)

BTW: Hi, everybody :D

Share this post


Link to post
Share on other sites
In short I would have to say 'no', simply because its 2D. You can try a whole bunch of stuff, but let me tell you from experience that I don't think its possible. I too tried a similar approach, when making a simple rts map demo. THe main problem was that when I scrolled over, it "scrolled" but not in a camera sense to where the new image made sense. IE. I had a isometrix map with jaggeded edges, when you "scroll" over, they should disappear since you are no longer at the edge of the map, but they didn't, b/c of the 2D drawing code. So the only way to get it done is the way you don't want it to. If you need any more clarifications or explantations feel free to ask. Goodluck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this