package com.fundynamic.d2tm.game.map.renderer;

import com.fundynamic.d2tm.game.math.Vector2D;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/**
 * Responsible for selecting cells in view and calling the renderer for those drawing positions.
 *
 */
public class MapRenderer {

    private final int tileHeight;
    private final int tileWidth;
    private int cellsThatFitHorizontally;
    private int cellsThatFitVertically;

    public MapRenderer(int tileHeight, int tileWidth, Vector2D windowDimensions) {
        this.tileHeight = tileHeight;
        this.tileWidth = tileWidth;
        cellsThatFitHorizontally = (windowDimensions.getX() / tileWidth) + 1;
        cellsThatFitVertically = (windowDimensions.getY() / tileHeight) + 1;
    }

    public void render(Image imageToDrawOn, Vector2D viewingVector, Renderer renderer) throws SlickException {
        int startCellX = (viewingVector.getX() / tileWidth);
        int startCellY = (viewingVector.getY() / tileHeight);

        int endCellX = startCellX + cellsThatFitHorizontally;
        int endCellY = startCellY + cellsThatFitVertically;

        for (int x = startCellX; x <= endCellX; x++) {
            for (int y = startCellY; y <= endCellY; y++) {
                int drawX = ((x - startCellX) * tileWidth) - (viewingVector.getX() % tileWidth);
                int drawY = ((y - startCellY) * tileHeight) - (viewingVector.getY() % tileHeight);

                renderer.draw(imageToDrawOn.getGraphics(), x, y, drawX, drawY);
            }
        }
    }

}
@@ -369,32 +369,38 @@
 th;%0A    private 
+final 
 int cellsThatFit
@@ -425,16 +425,22 @@
 private 
+final 
 int cell

make fields final