Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Only draw tiles that are within view  (Read 2216 times)

0 Members and 1 Guest are viewing this topic.

TheGameSquid

  • Newbie
  • *
  • Posts: 3
    • View Profile
Only draw tiles that are within view
« 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Only draw tiles that are within view
« Reply #1 on: June 11, 2012, 03:38:16 pm »
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.
Laurent Gomila - SFML developer

massive_potato

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Only draw tiles that are within view
« Reply #2 on: June 21, 2012, 10:13:44 pm »
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.

 

anything