Public Group

# I do not understand side-scrolling implementation in SDL

This topic is 796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've seen some SDL tutorials on implementing side-scrolling, and I don't understand how they do it. All of the tutorials I've seen do it like this:(taken from LazyFoo's code)

	camera.x = ( dot.getPosX() + Dot::DOT_WIDTH / 2 ) - SCREEN_WIDTH / 2;
camera.y = ( dot.getPosY() + Dot::DOT_HEIGHT / 2 ) - SCREEN_HEIGHT / 2;


This part, I at least understand. The camera's position is the position of the dot's origin minus the screen's width or height, because the camera should start moving should the dot's x or y coordinates exceed the middle of the screen. The part which I absolutely don't understand is this:

	void Dot::render( int camX, int camY ) {
gDotTexture.render( mPosX - camX, mPosY - camY );
}


In the above code, where the dot is drawn on the screen is the dot's x and y coordinates subtracted by the camera's x and y coordinates. I don't understand how or why one would come to the conclusion that the dot is to be rendered at the coordinates. I don't understand how it works. Can someone please explain?

##### Share on other sites

Look up translation algebra. One position is world coords other is camera coords. Make a drawing of two rects one inside the other. The inside one is the camera or viewport, outside is world. Do the math. The resulting pos is located around origin such that you can refer to pixels, and draw stuff.

##### Share on other sites
19 hours ago, h8CplusplusGuru said:

Look up translation algebra. One position is world coords other is camera coords. Make a drawing of two rects one inside the other. The inside one is the camera or viewport, outside is world. Do the math. The resulting pos is located around origin such that you can refer to pixels, and draw stuff.

I made the diagram and I think I see what you're saying. The calculations of the camera's x and y coordinates keep the dot in the middle of the camera rectangle, which but the dot's "world coords", as you said, aren't necessarily in the middle of the camera coords, so the position the dot is drawn at is the dot's world coords subtracted by the extra distance the camera has moved from (0,0), which would be the camera's position. Does that sound like a reasonable explanation? Do you think I've understood?

##### Share on other sites

Yes I think your description sounds reasonable. To further elaborate:

So the goal is to draw stuff on the screen. The screen has pixel coords, say, from (0,0) to (800,600). You have your camera position on the world map, and you have your dot, or thing to draw, position on the world map. You do the subtraction of camera from dot, that moves the dot into the cameras view about origin (0,0), which is where your pixels are. You can then draw the sprite on the screen. So doing this for every object that is visible in the camera, you will be able to see somewhere in the world.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 19
• 36
• 9
• 16
• 75