package com.gamepatriot.framework2d.classes;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import com.gamepatriot.framework2d.implementation.Main;

public class FPS {
	
	//Flags
	private int second;
	private int counter;
	private int lastFPS;
	
	//Containers
	private final Rect drawRect;
	private final Paint paint;
	private final Paint paintText;
	
	public FPS(){
		
		int $width = 80;
		int $height = 30;
		drawRect = new Rect(Main.CANVAS_WIDTH-$width, 0, Main.CANVAS_WIDTH, $height);
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paintText = new Paint();
		paintText.setColor(Color.WHITE);
		paintText.setTextSize(20);
		
		counter = 1;
		second = (int)(System.currentTimeMillis()/1000);
		lastFPS = 0;
	}
	
	public void tick(Canvas $canvas){
		int $second = (int)(System.currentTimeMillis()/1000);
		
		if ($second == second) counter++;
		else {
			
			lastFPS = counter;
			counter = 1;
			second = $second;
		}
		
		render($canvas);
	}
	
	public void render(Canvas $canvas){
		$canvas.drawRect(drawRect, paint);
		$canvas.drawText("FPS:" + lastFPS, drawRect.left + 8, drawRect.bottom/2 + 5, paintText);
	}
	
}
