#include #include #include #include #include #include #include #define FACTOR 80 #define WIDTH ((FACTOR) * 16) #define HEIGHT ((FACTOR) * 9) SDL_Event e; float vertices[] = { 0.0f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f }; char * read_file(const char* path){ FILE* file = fopen(path, "r"); fseek(file, 0, SEEK_END); int eof = ftell(file); fseek(file, 0, SEEK_SET); char* file_content = malloc(eof * sizeof(char)); fread(file_content, sizeof(char), eof, file); fclose(file); return file_content; } GLuint CompileShader(const char* data, GLenum type){ GLuint shader = glCreateShader(type); glShaderSource(shader, 1, &data, NULL); glCompileShader(shader); GLint status; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); char buffer[512]; glGetShaderInfoLog(shader, 512, NULL, buffer); printf("%s", buffer); return shader; } GLuint LinkShader(GLuint vertexShader, GLuint fragmentShader){ GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); GLint status; glGetProgramiv(shaderProgram, GL_LINK_STATUS, &status); char buffer[512]; glGetProgramInfoLog(shaderProgram, 512, NULL, buffer); printf("%s", buffer); return shaderProgram; } int main(void) { const char* vs = read_file("./shader.vs"); const char* fs = read_file("./shader.fs"); if(SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO)){ SDL_Log("Unable to initialize SDL: %s", SDL_GetError()); } SDL_Window *win; win = SDL_CreateWindow("Game of life", 0, 0, WIDTH, HEIGHT, SDL_WINDOW_OPENGL); if(!win){ SDL_Log("Unable to create Window: %s", SDL_GetError()); } SDL_GLContext con = SDL_GL_CreateContext(win); if(!con){ SDL_Log("Unable to create OpenGL Context: %s", SDL_GetError()); } if(glewInit()){ SDL_Log("SDL_INIT error"); }; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); GLuint vertexBuffer; glGenBuffers(1, &vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); GLuint vertexShader = CompileShader(vs, GL_VERTEX_SHADER); GLuint fragmentShader = CompileShader(fs, GL_FRAGMENT_SHADER); GLuint shaderProgram = LinkShader(vertexShader, fragmentShader); GLint posAttrib = glGetAttribLocation(shaderProgram, "positions"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); bool should_close = false; while(!should_close){ glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, 3); SDL_GL_SwapWindow(win); while(SDL_PollEvent(&e)){ if(e.type == SDL_QUIT) should_close = true; if(e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_a) should_close = true; } } glDeleteProgram(shaderProgram); glDeleteShader(fragmentShader); glDeleteShader(vertexShader); glDeleteBuffers(1, &vertexBuffer); SDL_GL_DeleteContext(con); SDL_DestroyWindow(win); SDL_Quit(); return 0; }