summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main.c36
1 files changed, 27 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index c99bf1c..8c77270 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,6 +3,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+void on_resize(GLFWwindow *win, int width, int height) {
+  glViewport(0, 0, width, height);
+}
+
 int main() {
   // Initialize GLFW
   if (!glfwInit()) {
@@ -14,16 +18,14 @@ int main() {
   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
   glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 
-  // Create a windowed mode window and its OpenGL context
-  GLFWmonitor *glfwMonitor = glfwGetPrimaryMonitor();
-  const GLFWvidmode *mode = glfwGetVideoMode(glfwMonitor);
-  GLFWwindow *window = glfwCreateWindow(mode->width, mode->height,
-                                        "Blue screen", glfwMonitor, NULL);
+  GLFWwindow *window = glfwCreateWindow(320, 240, "Blue screen", NULL, NULL);
   if (!window) {
     glfwTerminate();
     return -1;
   }
 
+  glfwSetFramebufferSizeCallback(window, (void *)on_resize);
+
   glfwMakeContextCurrent(window);
 
   // Initialize GLEW
@@ -33,12 +35,22 @@ int main() {
     return -1;
   }
 
-  float vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f};
+  float vertices[] = {
+      0.5f,  0.5f,  0.0f, // top right
+      0.5f,  -0.5f, 0.0f, // bottom right
+      -0.5f, -0.5f, 0.0f, // bottom left
+      -0.5f, 0.5f,  0.0f  // top left
+  };
+  unsigned int indices[] = {
+      0, 1, 3, // first triangle
+      1, 2, 3  // second triangle
+  };
 
   unsigned int VBO;
   glGenBuffers(1, &VBO);
   glBindBuffer(GL_ARRAY_BUFFER, VBO);
-  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+  glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 3, vertices,
+               GL_STATIC_DRAW);
 
   const char *vertex_shader_src =
       "#version 330 core\n"
@@ -97,18 +109,24 @@ int main() {
   glGenVertexArrays(1, &VAO);
   glBindVertexArray(VAO);
 
+  unsigned int EBO;
+  glGenBuffers(1, &EBO);
+  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(float) * 2 * 3, indices,
+               GL_STATIC_DRAW);
+
   glBindBuffer(GL_ARRAY_BUFFER, VBO);
   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
   glEnableVertexAttribArray(0);
 
-  // Main loop
   while (!glfwWindowShouldClose(window)) {
     glClearColor(0.1f, 0.1f, 0.25f, 1.0f);
     glClear(GL_COLOR_BUFFER_BIT);
 
     glUseProgram(shader_program);
     glBindVertexArray(VAO);
-    glDrawArrays(GL_TRIANGLES, 0, 3);
+    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
+    glBindVertexArray(0);
 
     glfwSwapBuffers(window);
     glfwPollEvents();