src/glcontext.c

changeset 139
5d655459db85
parent 137
f8e6e0ae61a8
equal deleted inserted replaced
138:2ceb0368b02d 139:5d655459db85
43 source, id, type, severity, length, message); 43 source, id, type, severity, length, message);
44 } else { 44 } else {
45 asc_dprintf("OpenGL source = %d, id = %u, type = %d, severity= %d, message = %.*s", 45 asc_dprintf("OpenGL source = %d, id = %u, type = %d, severity= %d, message = %.*s",
46 source, id, type, severity, length, message); 46 source, id, type, severity, length, message);
47 } 47 }
48 }
49
50 static int asc_shader_initialize_predefined(AscGLContext *ctx) {
51 // TODO: check if we can replace that by lazy loading shaders just like fonts
52 int ret = 0;
53 ret |= asc_shader_sprite_init(&ctx->shader.sprite_uv, false);
54 ret |= asc_shader_sprite_init(&ctx->shader.sprite_rect, true);
55 ret |= asc_error_catch_all_gl();
56 return ret;
57 }
58
59 static void asc_shader_destroy_predefined(AscGLContext *ctx) {
60 asc_shader_program_destroy(&ctx->shader.sprite_uv.program);
61 asc_shader_program_destroy(&ctx->shader.sprite_rect.program);
62 } 48 }
63 49
64 struct asc_gl_context_cleanup_data { 50 struct asc_gl_context_cleanup_data {
65 int type; 51 int type;
66 union { 52 union {
107 93
108 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 94 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
109 glEnable(GL_DEBUG_OUTPUT); 95 glEnable(GL_DEBUG_OUTPUT);
110 glDebugMessageCallback(asc_gl_debug_callback, NULL); 96 glDebugMessageCallback(asc_gl_debug_callback, NULL);
111 97
112 if (asc_shader_initialize_predefined(ctx)) { 98 // Create the cleanup functions array
113 asc_error("Initializing predefined shaders failed");
114 SDL_GL_DeleteContext(ctx->glctx);
115 return false;
116 }
117
118 ctx->cleanup_funcs = cxArrayListCreateSimple(sizeof(struct asc_gl_context_cleanup_data), 8); 99 ctx->cleanup_funcs = cxArrayListCreateSimple(sizeof(struct asc_gl_context_cleanup_data), 8);
119 cxDefineDestructor(ctx->cleanup_funcs, asc_gl_context_cleanup); 100 cxDefineDestructor(ctx->cleanup_funcs, asc_gl_context_cleanup);
101
102 // Create the shaders array
103 ctx->shaders = cxArrayListCreateSimple(CX_STORE_POINTERS, 32);
104 cxDefineDestructor(ctx->shaders, asc_shader_free);
120 105
121 return true; 106 return true;
122 } else { 107 } else {
123 asc_error("glewInit failed: %s", glewGetErrorString(err)); 108 asc_error("glewInit failed: %s", glewGetErrorString(err));
124 SDL_GL_DeleteContext(ctx->glctx); 109 SDL_GL_DeleteContext(ctx->glctx);
128 113
129 void asc_gl_context_destroy(AscGLContext *ctx) { 114 void asc_gl_context_destroy(AscGLContext *ctx) {
130 if (ctx->glctx == NULL) return; 115 if (ctx->glctx == NULL) return;
131 SDL_GL_MakeCurrent(ctx->window, ctx->glctx); 116 SDL_GL_MakeCurrent(ctx->window, ctx->glctx);
132 117
118 cxListFree(ctx->shaders);
133 cxListFree(ctx->cleanup_funcs); 119 cxListFree(ctx->cleanup_funcs);
134 asc_shader_destroy_predefined(ctx);
135 120
136 // destroy the GL context and the window 121 // destroy the GL context and the window
137 SDL_GL_DeleteContext(ctx->glctx); 122 SDL_GL_DeleteContext(ctx->glctx);
138 ctx->glctx = NULL; 123 ctx->glctx = NULL;
139 } 124 }

mercurial