package speiger.src.coreengine.platform.graphics.opengl.utils;

import org.lwjgl.opengl.GL11;

import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.utils.Stack;
import speiger.src.coreengine.math.vector.ints.Vec4i;

public class ViewPortStack {
	Stack<Vec4i> stack = new ObjectArrayList<>();
	Vec4i defaultPort = Vec4i.ZERO;
	
	public void setDefault(int x, int y, int w, int h) {
		if(defaultPort.x() == x && defaultPort.y() == y && defaultPort.z() == w && defaultPort.w() == h) return;
		defaultPort.set(x, y, w, h);
		if(stack.isEmpty()) GL11.glViewport(x, y, w, h);
	}
	
	public void push(int x, int y, int w, int h) {
		Vec4i current = top();
		if(current.x() == x && current.y() == y && current.z() == w && current.w() == h) return;
		stack.push(Vec4i.of(x, y, w, h));
		GL11.glViewport(x, y, w, h);
	}
	
	public void pop() {
		if(stack.isEmpty()) return;
		stack.pop();
		Vec4i current = top();
		GL11.glViewport(current.x(), current.y(), current.z(), current.w());
	}
	
	public Vec4i top() {
		return stack.isEmpty() ? defaultPort : stack.top();
	}
}
