opengl_learn

step-by-step introduction to OpenGL
git clone https://0xdd.org/code/opengl_learn.git
Log | Files | Refs | README | LICENSE

ogl_init.c (1584B)


      1 /*
      2 2018 David DiPaola
      3 licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/)
      4 */
      5 
      6 #include <stdio.h>
      7 
      8 #include <stdlib.h>
      9 
     10 #include <GL/glew.h>
     11 
     12 #include <GLFW/glfw3.h>
     13 
     14 #include "ogl.h"
     15 #include "_ogl.h"
     16 
     17 GLFWwindow *
     18 ogl_init(int window_width, int window_height, int vsync, const char * window_title) {
     19 	GLFWwindow * window;
     20 	int status;
     21 
     22 	glfwSetErrorCallback(&_ogl_glfw_error);
     23 
     24 	status = glfwInit();
     25 	if (!status) {  /* TODO GLFW_TRUE doesn't exist..? */
     26 		fprintf(stderr, "GLFW glfwInit(): ERROR" "\n");
     27 		fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__);
     28 		exit(-1);
     29 	}
     30 
     31 	glfwWindowHint(GLFW_CLIENT_API,            GLFW_OPENGL_ES_API);
     32 	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
     33 	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
     34 
     35 	window = glfwCreateWindow(window_width, window_height, window_title, NULL, NULL);
     36 	if (window == NULL){
     37 		fprintf(stderr, "GLFW glfwCreateWindow(): ERROR" "\n");
     38 		fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__);
     39 		glfwTerminate();
     40 		exit(-1);
     41 	}
     42 
     43 	glfwMakeContextCurrent(window);
     44 
     45 	GLenum glewstatus;
     46 	glewstatus = glewInit();
     47 	if (glewstatus != GLEW_OK) {
     48 		fprintf(stderr, "GLEW glewInit(): ERROR" "\n");
     49 		fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__);
     50 		glfwTerminate();
     51 		exit(-1);
     52 	}
     53 
     54 	glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
     55 
     56 	if (!vsync) {
     57 		glfwSwapInterval(0);
     58 	}
     59 
     60 	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
     61 
     62 	glEnable(GL_DEPTH_TEST);
     63 	glDepthFunc(GL_LESS);
     64 
     65 	_ogl_window_width  = window_width;
     66 	_ogl_window_height = window_height;
     67 	return window;
     68 }
     69