Skip to content

Instantly share code, notes, and snippets.

@oknauta
Created July 11, 2024 14:16
Show Gist options
  • Save oknauta/61172659903ba9a7b38262b62d51838c to your computer and use it in GitHub Desktop.
Save oknauta/61172659903ba9a7b38262b62d51838c to your computer and use it in GitHub Desktop.
A white triangle in C with GLFW
// File: glfw_triangle_white.c
// Date: 2024-07-11 | 2024-07-11
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE "Triangle"
typedef struct
{
double x;
double y;
}vector2;
static void triangle(double base_width, double height, vector2 position) {
glBegin(GL_TRIANGLES);
glVertex2d(position.x, position.y + height);
glVertex2d(position.x + base_width, position.y - height / 2.0);
glVertex2d(position.x - base_width, position.y - height / 2.0);
glEnd();
}
int main()
{
const char *error_description;
GLFWwindow *window;
if(!glfwInit())
{
glfwGetError(&error_description);
printf("Error: %s", error_description);
glfwTerminate();
return -1;
}
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, NULL, NULL);
if(!window)
{
glfwGetError(&error_description);
printf("Error: %s", error_description);
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window))
{
double last_frame = glfwGetTime();
double current_time = glfwGetTime();
double delta_time = current_time - last_frame;
last_frame = current_time;
glClear(GL_COLOR_BUFFER_BIT);
triangle(1.0f, 1.0f, (vector2){0, 0});
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment