src/glcontext.c

changeset 311
87ff4c57955d
parent 290
2eb3813562e7
equal deleted inserted replaced
310:9212be32d7a2 311:87ff4c57955d
27 27
28 #include "ascension/glcontext.h" 28 #include "ascension/glcontext.h"
29 #include "ascension/shader.h" 29 #include "ascension/shader.h"
30 #include "ascension/error.h" 30 #include "ascension/error.h"
31 31
32 #include <GL/glew.h> 32 #include "glad.h"
33 #include <cx/array_list.h> 33 #include <cx/array_list.h>
34 34
35 static void asc_gl_debug_callback( 35 static void asc_gl_debug_callback(
36 GLenum source, GLenum type, GLuint id, GLenum severity, 36 GLenum source, GLenum type, GLuint id, GLenum severity,
37 GLsizei length, const GLchar *message, 37 GLsizei length, const GLchar *message,
72 data->f3(data->memory, data->additional_data); 72 data->f3(data->memory, data->additional_data);
73 break; 73 break;
74 } 74 }
75 } 75 }
76 76
77 AscGLContextSettings asc_gl_context_settings_default(int gl_major_version, int gl_minor_version) { 77 bool asc_gl_context_initialize(AscGLContext *ctx, SDL_Window *window) {
78 return (AscGLContextSettings) { 78 const int gl_major_version = 4;
79 .gl_major_version = gl_major_version, 79 const int gl_minor_version = 3;
80 .gl_minor_version = gl_minor_version,
81 .depth_size = 24,
82 .vsync = true,
83 .fullscreen = false,
84 };
85 }
86 80
87 bool asc_gl_context_initialize(
88 AscGLContext *ctx,
89 SDL_Window *window,
90 const AscGLContextSettings *settings
91 ) {
92 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); 81 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
93 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); 82 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, gl_major_version);
94 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); 83 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, gl_minor_version);
95 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); 84 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
96 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 85 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
97 ctx->glctx = SDL_GL_CreateContext(window); 86 ctx->glctx = SDL_GL_CreateContext(window);
98 if (ctx->glctx == NULL) return false; 87 if (ctx->glctx == NULL) return false;
99 ctx->window = window; 88 ctx->window = window;
100 89
101 glewExperimental = GL_TRUE; 90 int version = gladLoadGL(SDL_GL_GetProcAddress);
102 GLenum err = glewInit(); 91 asc_dprintf("Loaded OpenGL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
103 if (err == GLEW_OK) { 92 if (version == 0) {
104 SDL_GL_SetSwapInterval(settings->vsync ? 1 : 0); 93 asc_error("Loading OpenGL failed.");
94 SDL_GL_DestroyContext(ctx->glctx);
95 return false;
96 } else {
97 // Enable vsync by default (can be changed by the user later)
98 SDL_GL_SetSwapInterval(1);
105 99
106 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 100 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
107 glEnable(GL_DEBUG_OUTPUT); 101 glEnable(GL_DEBUG_OUTPUT);
108 glDebugMessageCallback(asc_gl_debug_callback, NULL); 102 glDebugMessageCallback(asc_gl_debug_callback, NULL);
109 103
115 ctx->shaders = cxArrayListCreate(NULL, CX_STORE_POINTERS, 32); 109 ctx->shaders = cxArrayListCreate(NULL, CX_STORE_POINTERS, 32);
116 cxSetDestructor(ctx->shaders, asc_shader_free); 110 cxSetDestructor(ctx->shaders, asc_shader_free);
117 ctx->active_program = 0; 111 ctx->active_program = 0;
118 112
119 return true; 113 return true;
120 } else {
121 asc_error("glewInit failed: %s", glewGetErrorString(err));
122 SDL_GL_DestroyContext(ctx->glctx);
123 return false;
124 } 114 }
125 } 115 }
126 116
127 void asc_gl_context_destroy(AscGLContext *ctx) { 117 void asc_gl_context_destroy(AscGLContext *ctx) {
128 if (ctx->glctx == NULL) return; 118 if (ctx->glctx == NULL) return;

mercurial