Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Created March 31, 2014 04:53
Show Gist options
  • Save alphaKAI/9885513 to your computer and use it in GitHub Desktop.
Save alphaKAI/9885513 to your computer and use it in GitHub Desktop.
The sample of GLFW3 and OpenGL. requirements: Derelict3, libglfw(3) : $ dmd test_glfw.d lib/libDerelictGL3.a lib/libDerelictUtil.a lib/libDerelictGLFW3.a -L/usr/lib/libdl.so -L/usr/lib/libglfw.so
import derelict.opengl3.gl,
derelict.opengl3.gl3,
derelict.glfw3.glfw3;
class TestGlfw{
static int width = 800;
static int height = 600;
static GLFWwindow* window;
this(){
DerelictGL.load();
DerelictGL3.load();
DerelictGLFW3.load();
if (!glfwInit())
throw new Error("GLFW Initialize failed.");
window = glfwCreateWindow(width, height, "GLFW3 and OpenGL Test", null, null);
if (!window){
glfwTerminate();
throw new Error("Falied to Create GLFW Window.");
}
glfwMakeContextCurrent(window);
}
void mainLoop(){
while(!glfwWindowShouldClose(window)){
float ratio;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / cast(float)height;
glViewport(0,0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.0f, 1.0f, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(cast(float)glfwGetTime() * 50.0f, 0.0f, 0.0f, 1.0f);
{
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.6f, -0.4f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.6f, -0.4f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.6f, 0.0f);
glEnd();
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return;
}
}
void main(){
TestGlfw tw = new TestGlfw();
with(tw){
mainLoop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment