Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.82 KB | None | 0 0
  1. module main;
  2.  
  3. import std.stdio;
  4. import derelict.opengl3.gl;
  5. import derelict.glfw3.glfw3;
  6.  
  7. extern (C) void onError(int error, const (char)* desc) nothrow
  8. {
  9.     scope(failure) assert(0);
  10.     writeln("Houston, we've had a problem: [", error, "] ", desc);
  11. }
  12.  
  13. extern (C) void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) nothrow
  14. {
  15.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  16.         glfwSetWindowShouldClose(window, GL_TRUE);
  17. }
  18.  
  19. extern (C) void onResize(GLFWwindow* window, int width, int height) nothrow
  20. {
  21.     glViewport(0, 0, width, height);
  22. }
  23.  
  24. void main(string[] args)
  25. {
  26.     // start
  27.     writeln("Starting...");
  28.     DerelictGLFW3.load();
  29.     DerelictGL.load();
  30.     glfwSetErrorCallback(&onError);
  31.     if (!glfwInit())
  32.         return;
  33.  
  34.     // init
  35.     GLFWwindow* window = glfwCreateWindow(640, 480, "Snake 4D", null, null);
  36.     if (window == null)
  37.         return;
  38.     glfwSetKeyCallback(window, &onKey);
  39.     glfwSetFramebufferSizeCallback(window, &onResize);
  40.     glfwMakeContextCurrent(window);
  41.  
  42.     // run
  43.     while (!glfwWindowShouldClose(window))
  44.     {
  45.         float ratio;
  46.         int width, height;
  47.         glfwGetFramebufferSize(window, &width, &height);
  48.         ratio = width / cast(float) height;
  49.         glViewport(0, 0, width, height);
  50.         glClear(GL_COLOR_BUFFER_BIT);
  51.         glMatrixMode(GL_PROJECTION);
  52.         glLoadIdentity();
  53.         glOrtho(-ratio, ratio, -1.0f, 1.0f, 1.0f, -1.0f);
  54.         glMatrixMode(GL_MODELVIEW);
  55.         glLoadIdentity();
  56.         glRotatef(cast(float) glfwGetTime() * 50.0f, 0.0f, 0.0f, 1.0f);
  57.         glBegin(GL_TRIANGLES);
  58.         glColor3f(1.0f, 0.0f, 0.0f);
  59.         glVertex3f(-0.6f, -0.4f, 0.0f);
  60.         glColor3f(0.0f, 1.0f, 0.0f);
  61.         glVertex3f(0.6f, -0.4f, 0.0f);
  62.         glColor3f(0.0f, 0.0f, 1.0f);
  63.         glVertex3f(0.0f, 0.6f, 0.0f);
  64.         glEnd();
  65.         glfwSwapBuffers(window);
  66.         glfwPollEvents();
  67.     }
  68.  
  69.     // finish
  70.     if (window != null)
  71.         glfwDestroyWindow(window);
  72.     glfwTerminate();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement