summaryrefslogtreecommitdiff
path: root/main.c
blob: e5e0f51b74e3ba21317b68d4aa6d912e1250e71e (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <math.h>
#include <SDL2/SDL.h>
#include <glad/glad.h>

int main(int argc, char *argv[])
{
	int width = 1920;
	int height = 1080;
	
	if (SDL_Init(SDL_INIT_VIDEO) != 0)
	{
		printf("Error initialising SDL2: %s\n", SDL_GetError());
		return 0;
	};

	// initialise window with opengl flag
	SDL_Window *window = SDL_CreateWindow("SDL Test", 
		SDL_WINDOWPOS_UNDEFINED, 
		SDL_WINDOWPOS_UNDEFINED, 
		width, 
		height, 
		SDL_WINDOW_OPENGL);
	
	// create an opengl context
	SDL_GLContext context = SDL_GL_CreateContext(window);
	if (!context)
	{
		printf("OpenGL context creation failed: %s\n", SDL_GetError());
		return -1;
	}

	// load glad
	if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) {
	    printf("Failed to initialize Glad\n");
	    return 1;
	}

	// opengl rendering and shader stuff here
	// @todo: 
	// have to learn how to setup shaders and other things with modern opengl (4.5+)
  const char* vertexSource =
      "#version 330 core\n"
      "layout(location = 0) in vec3 position;"
      "void main() {"
      "    gl_Position = vec4(position, 1.0);"
      "}";

  const char* fragmentSource =
      "#version 330 core\n"
      "out vec4 fragColor;"
      "void main() {"
      "    fragColor = vec4(1.0, 0.0, 0.0, 1.0);"
      "}";

  GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  glShaderSource(vertexShader, 1, &vertexSource, NULL);
  glCompileShader(vertexShader);

  GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  glCompileShader(fragmentShader);

  GLuint shaderProgram = glCreateProgram();
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glLinkProgram(shaderProgram);
  glUseProgram(shaderProgram);

  glDeleteShader(vertexShader);
  glDeleteShader(fragmentShader);

  GLfloat vertices[] = {
      -0.5f, -0.5f, 0.0f,
       0.5f, -0.5f, 0.0f,
       0.0f,  0.5f, 0.0f
  };

  GLuint VBO, VAO;
  glGenVertexArrays(1, &VAO);
  glGenBuffers(1, &VBO);

  glBindVertexArray(VAO);

  glBindBuffer(GL_ARRAY_BUFFER, VBO);
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
  glEnableVertexAttribArray(0);

  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glBindVertexArray(0);
	for(;;)
	{
		SDL_Event ev;
		while(SDL_PollEvent(&ev))
		{
			if (ev.type == SDL_QUIT)
			{
				return 0;
			}
		}

		// opengl rendering code here
		// @note: this part is more or less the same
		// as with my learn opengl experiments
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glUseProgram(shaderProgram);
    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(0);

    SDL_GL_SwapWindow(window);
	}
  // opengl free calls
  glDeleteVertexArrays(1, &VAO);
  glDeleteBuffers(1, &VBO);
  glDeleteProgram(shaderProgram);
  // sdl free calls
	SDL_GL_DeleteContext(context);
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}