scrolling in sdl

Started by
14 comments, last by Drew_Benton 19 years, 4 months ago
is there a way to scroll images in sdl WITHOUT having to move everything but the "moving" item(to give the illusion of movement) so that you can get sort of a camera effect?
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
Are you saying you don't want to move the background stuff when your scrolling?
yes that is exactly it
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Yes it is Possible to do so. since each object has their own x and y coord, all you have to do is change the x and y coord of the objects you want to move that's it.
yeah but what happens when i get to the end of the screen? (i already knew that) thanks any way[smile]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
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.
Cheers,Jeroen
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
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
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]
FTA, my 2D futuristic action MMORPG
how would one go about moving the screen like a camera, in SDL?
thanks alot for your reply
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
re-read my post. this is exactly what i explain... if your still having trouble, read through the thread i posted. also, the API has no effect on this type of stuff. this will work using any graphics API.
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement