BethesdaLove said:
Its very nice linking to an image but HOW ABOUT THE SCROLLING CODE?
I place the dude randomly on map and want the camera to center on him. But SDL always shows the [0][0] corner in the upper left and what else fitts on the camera.
Opps sorry, I kind of missed your reply.
I'm compiling the source code for release soon, but here's how I did it:
There's a global offset in the scene, which is the player's current pixel location. The camera follows the player (on every update loop, update the camera position to match the player position).
When you render any renderable entity, use this formula:
x = ex + ( gx - cx )
y = ey + ( gy - cy )
ex, ey = the current renderable entity's pixel position
gx, gy = the global offset (player pixel position)
cx, cy = the camera pixel position (top left corner of the camera)
Ideally, you will need to do this instead:
x = ex + ( gx - cx ) - ( player entity sprite width / 2 )
y = ey + ( gy - cy ) - ( player entity sprite height / 2 )
to get a perfectly centered player character.
This works for me.
I'm not sure what you meant by SDL showing [0][0] for your camera code. Maybe you're getting the SCREEN pixel location rather than the WORLD pixel location?
How do you store your entities? On a 2D tile array? BSP Tree? Scene graph?