#include once "glfw3.bi"
#Include "crt.bi"

Sub error_callback CALLBACK (Error_ As Integer, description As ZString Ptr)
	fputs(description, stderr)
End Sub

sub key_callback CALLBACK (Window_ As GLFWwindow Ptr ,key as integer ,scancode as integer ,action as integer , mods as integer )
	if (key = GLFW_KEY_ESCAPE and action = GLFW_PRESS) Then
		glfwSetWindowShouldClose(window_, GL_TRUE)
	EndIf
End Sub


Dim As GLFWwindow ptr window_

glfwSetErrorCallback(@error_callback)
if glfwInit() = 0 Then
	Print "Error glfwInit!"
	End
EndIf

Window_ = glfwCreateWindow(640, 480, "Simple example", NULL, NULL)
if window_ = 0 Then
	glfwTerminate()
	Print "Error creating window!"
	End
EndIf

glfwMakeContextCurrent(window_)
glfwSetKeyCallback(window_, @key_callback)
while glfwWindowShouldClose(Window_) = 0
	Dim As Single ratio
	Dim As integer width_, height
	glfwGetFramebufferSize(window_, width_, height)
	ratio = width_ / height
	glViewport(0, 0, width_, height)
	glClear(GL_COLOR_BUFFER_BIT)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f)
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
	glRotatef(glfwGetTime() * 50.f, 0.f, 0.f, 1.f)
	glBegin(GL_TRIANGLES)
	glColor3f(1.f, 0.f, 0.f)
	glVertex3f(-0.6f, -0.4f, 0.f)
	glColor3f(0.f, 1.f, 0.f)
	glVertex3f(0.6f, -0.4f, 0.f)
	glColor3f(0.f, 0.f, 1.f)
	glVertex3f(0.f, 0.6f, 0.f)
	glEnd()
	glfwSwapBuffers(window_)
	glfwPollEvents()
Wend
glfwDestroyWindow(window_)
glfwTerminate()
