Jump to content
  • Advertisement
CodeBro1

I do not understand side-scrolling implementation in SDL

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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.    

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!