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

import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;

import speiger.src.coreengine.platform.graphics.api.core.GraphicsSurface;
import speiger.src.coreengine.platform.input.window.Window;

public class GLSurface implements GraphicsSurface {
	GLGraphicsDevice device;
	Window window;
	
	public GLSurface(Window window, GLGraphicsDevice device) {
		this.window = window;
		this.device = device;
	}

	@Override
	public void setVsync(boolean value) {
		window.vsync(value);
	}

	@Override
	public void beginFrame() {
		GL11.glClearColor(0.2F, 0.55F, 0.66F, 1F);
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		window.beginFrame();
		GL.setCapabilities(device.capabilities);
		if(window.isPrimaryWindow()) window.manager().joinOnWindowThread(GLFW::glfwPollEvents);
		Thread.onSpinWait();
		window.handleInput();
	}

	@Override
	public void finishFrame() {
		window.finishFrame();
	}
	
}
