summaryrefslogtreecommitdiff
path: root/main.c
blob: 32aff4edcfa54983b0601748b19571a862294aca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <KHR/khrplatform.h>
#define GLFW_INCLUDE_NONE
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <stdio.h>

// struct and function declaration
// ----------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
// ----------------------------------------------------------------------------
// END

int main(void) {

    // GLFW settings init and which version of opengl we should use
    // and which subset of opengl profiles
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
    

    // Window settings
    // create window
    int w_height = 1280;
    int w_width = 820;
    GLFWwindow* window = glfwCreateWindow(w_height, w_width, "Test", NULL, NULL);
    if (window == NULL) {
	printf("Failed to create GLFW window\n");
	glfwTerminate();
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
	return -1;
    }
    // init window
    glfwMakeContextCurrent(window);

    // init GLAD
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
	printf("Failed to initialize GLAD\n");
	return -1;
    }

    // opengl viewport
    framebuffer_size_callback(window, w_width, w_height);

    // -------------------------------------------------------------------------
    // render loop
    // -------------------------------------------------------------------------
    while (!glfwWindowShouldClose(window)) {
	// input
	processInput(window);

	// rendering commands here
	// -----------------------
	glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	
	// check and call events and swap the buffers
	glfwSwapBuffers(window);
	glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

// END render loop
// -----------------------------------------------------------------------------

// Functions
// -----------------------------------------------------------------------------
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, 1);
    if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) glfwSetWindowShouldClose(window, 1);
}