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?