base gameoflife graph
This commit is contained in:
		
							
								
								
									
										6
									
								
								build.sh
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								build.sh
									
									
									
									
									
								
							@ -2,4 +2,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
set -xe
 | 
					set -xe
 | 
				
			||||||
 | 
					
 | 
				
			||||||
gcc -Wall -Wextra -o main main.c
 | 
					CFLAGS="-Wall -Wextra $(pkg-config --cflags sdl2 glew)"
 | 
				
			||||||
 | 
					LIBS="$(pkg-config --libs sdl2 glew)"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# gcc -Wall -Wextra -o gameoflife_term gameoflife_term.c
 | 
				
			||||||
 | 
					gcc ${CFLAGS} -o gameoflife_graph gameoflife_graph.c ${LIBS}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										131
									
								
								gameoflife_graph.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										131
									
								
								gameoflife_graph.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,131 @@
 | 
				
			|||||||
 | 
					#include <SDL2/SDL_events.h>
 | 
				
			||||||
 | 
					#include <GL/glew.h>
 | 
				
			||||||
 | 
					#include <SDL2/SDL.h>
 | 
				
			||||||
 | 
					#include <SDL2/SDL_opengl.h>
 | 
				
			||||||
 | 
					#include <SDL2/SDL_opengl_glext.h>
 | 
				
			||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										7
									
								
								shader.fs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								shader.fs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					#version 330 core
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					out vec4 outColor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void main(){
 | 
				
			||||||
 | 
						outColor = vec4(1.0, 1.0, 1.0, 1.0);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user