SFML community forums
Help => Window => Topic started by: TheGameSquid on June 11, 2012, 03:35:20 pm
-
Hi!
I'm trying to get my very first mini-engine together using the SFML.NET bindings and C#. I'm having trouble setting up a decent camera system though.
The "engine" (if it's even worth that name) I'm building is an ultra-simplistic tile engine. Using the View object I can implement a scrolling and zooming camera quite easily, I'm just having trouble making sure I only draw that which is in view (In other words, I'm having trouble determining what is actually within view. The drawing is simple). :-[
I'm not asking for a complete solution or anything, but it would be nice if someone could point me in the right direction. I was unable to find examples of such implementations. If you did something similar in your project or know of some good examples , it would be awesome if you would share it. :D
-
Your tiles are arranged in a regular grid. Your view (or its bounding rectangle, if it's rotated) is a rectangle which is aligned on this grid. From there it's not hard to compute the X and Y ranges of tiles that are under the view rect.
-
I'm also working on a similar project. Assuming that you have a player centered on the screen, the render algorithm should go something like this.
define tileResolutionX as the number of tiles per row
define tileResolutionY as the number of tiles per column
define screenResolutionX as the width of the window you are rendering to
define screenResolutionY as the height of the window you are rendering to
xOffset = player.x - floor(tileResolutionX / 2) -> This will center the map around your player's x-position
yOffset = player.y - floor(tileResolutionY / 2) -> This will center the map around your player's y-position
for x = 0 to tileResolutionX -1
for y = 0 to tileResolutionY - 1
render tile(x + xOffset, y + yOffset) at (x * (screenResolutionX / tileResolutionX), y * (screenResolutionY /
tileResolutionY)) -> This keeps all sprite sizes relative to one another and renders them relative to the player
end for
end for
You can replace the resolutions with literals if you don't plan on changing the window's size during the execution of the program.
Hopefully this helps.