scrolling in sdl

Started by
14 comments, last by Drew_Benton 19 years, 4 months ago
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.


Evillive2
Advertisement
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.
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Oh, I think I get what you want now. You want something similar to zelda for the NES. Basicly your character moves around on the screen but when he gets to an edge, the map scrolls one screen in that direction?
Evillive2
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*
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
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!

This topic is closed to new replies.

Advertisement