src/glcontext.c

changeset 81
84a546e282b7
parent 80
9f7bfc0a1dc3
child 88
6234b7ea48f3
equal deleted inserted replaced
80:9f7bfc0a1dc3 81:84a546e282b7
46 asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr); 46 asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr);
47 } 47 }
48 cx_strfree(&buf); 48 cx_strfree(&buf);
49 } 49 }
50 50
51 static int asc_primitives_init(AscGLContext *context) {
52 asc_dprintf("Create primitives for the GL context of active window.");
53 asc_mesh_allocate_buffers((AscMesh*)&context->primitives, sizeof(context->primitives) / sizeof(AscMesh));
54
55 asc_primitives_init_plane(&context->primitives.plane);
56 // TODO: more primitives
57
58 return asc_error_catch_all_gl();
59 }
60
61 static void asc_primitives_destroy(AscGLContext *context) {
62 asc_dprintf("Destroy primitives in GL context of active window.");
63 asc_mesh_free_buffers((AscMesh*)&context->primitives, sizeof(context->primitives) / sizeof(AscMesh));
64 }
65
51 static int asc_shader_initialize_predefined(AscGLContext *ctx) { 66 static int asc_shader_initialize_predefined(AscGLContext *ctx) {
52 int ret = 0; 67 int ret = 0;
53 ret |= asc_shader_sprite_init(&ctx->shader.sprite); 68 ret |= asc_shader_sprite_init(&ctx->shader.sprite);
69 ret |= asc_error_catch_all_gl();
54 return ret; 70 return ret;
55 } 71 }
56 72
57 static void asc_shader_destroy_predefined(AscGLContext *ctx) { 73 static void asc_shader_destroy_predefined(AscGLContext *ctx) {
58 asc_shader_program_destroy(&ctx->shader.sprite.program); 74 asc_shader_program_destroy(&ctx->shader.sprite.program);
79 95
80 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 96 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
81 glEnable(GL_DEBUG_OUTPUT); 97 glEnable(GL_DEBUG_OUTPUT);
82 glDebugMessageCallback(asc_gl_debug_callback, NULL); 98 glDebugMessageCallback(asc_gl_debug_callback, NULL);
83 99
84 if (!asc_primitives_init(&ctx->primitives)) { 100 if (asc_primitives_init(ctx)) {
85 asc_error("Creating primitive meshes failed"); 101 asc_error("Creating primitive meshes failed");
86 SDL_GL_DeleteContext(ctx->glctx); 102 SDL_GL_DeleteContext(ctx->glctx);
87 return false; 103 return false;
88 } 104 }
89 105
90 if (asc_shader_initialize_predefined(ctx)) { 106 if (asc_shader_initialize_predefined(ctx)) {
107 asc_error("Initializing predefined shaders failed");
91 SDL_GL_DeleteContext(ctx->glctx); 108 SDL_GL_DeleteContext(ctx->glctx);
92 return false; 109 return false;
93 } 110 }
94 111
95 return true; 112 return true;
103 void asc_gl_context_destroy(AscGLContext *ctx) { 120 void asc_gl_context_destroy(AscGLContext *ctx) {
104 if (ctx->glctx == NULL) return; 121 if (ctx->glctx == NULL) return;
105 SDL_GL_MakeCurrent(ctx->window, ctx->glctx); 122 SDL_GL_MakeCurrent(ctx->window, ctx->glctx);
106 123
107 asc_shader_destroy_predefined(ctx); 124 asc_shader_destroy_predefined(ctx);
108 asc_primitives_destroy(&ctx->primitives); 125 asc_primitives_destroy(ctx);
109 126
110 // destroy the GL context and the window 127 // destroy the GL context and the window
111 SDL_GL_DeleteContext(ctx->glctx); 128 SDL_GL_DeleteContext(ctx->glctx);
112 ctx->glctx = NULL; 129 ctx->glctx = NULL;
113 } 130 }

mercurial