src/glcontext.c

changeset 88
6234b7ea48f3
parent 81
84a546e282b7
child 89
e1f682a8a145
equal deleted inserted replaced
87:874a02a683c5 88:6234b7ea48f3
72 72
73 static void asc_shader_destroy_predefined(AscGLContext *ctx) { 73 static void asc_shader_destroy_predefined(AscGLContext *ctx) {
74 asc_shader_program_destroy(&ctx->shader.sprite.program); 74 asc_shader_program_destroy(&ctx->shader.sprite.program);
75 } 75 }
76 76
77 static int asc_texture_initialize_predefined(AscGLContext *ctx) {
78 asc_texture_init_rectangle(&ctx->textures.empty_1x1_rect);
79 asc_texture_init_2d(&ctx->textures.empty_1x1_2d);
80
81 // Create a 1x1 surface with 32-bit RGBA format
82 SDL_Surface* white1x1 = SDL_CreateRGBSurface(
83 0, 1, 1, 32,
84 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
85 if (white1x1 == NULL) {
86 // TODO: merge error messages once asc_error allows formatting
87 asc_error("Failed to create surface");
88 asc_error(SDL_GetError());
89 return 1;
90 }
91 SDL_FillRect(white1x1, NULL, 0xFFFFFFFF);
92
93 // Initialize the empty textures with the white surface
94 asc_texture_from_surface(&ctx->textures.empty_1x1_2d, white1x1);
95 asc_texture_from_surface(&ctx->textures.empty_1x1_rect, white1x1);
96
97 return asc_error_catch_all_gl();
98 }
99
100 static void asc_texture_destroy_predefined(AscGLContext *ctx) {
101 asc_texture_destroy(&ctx->textures.empty_1x1_2d);
102 asc_texture_destroy(&ctx->textures.empty_1x1_rect);
103 }
104
77 bool asc_gl_context_initialize( 105 bool asc_gl_context_initialize(
78 AscGLContext *ctx, 106 AscGLContext *ctx,
79 SDL_Window *window, 107 SDL_Window *window,
80 AscGLContextSettings const *settings 108 AscGLContextSettings const *settings
81 ) { 109 ) {
107 asc_error("Initializing predefined shaders failed"); 135 asc_error("Initializing predefined shaders failed");
108 SDL_GL_DeleteContext(ctx->glctx); 136 SDL_GL_DeleteContext(ctx->glctx);
109 return false; 137 return false;
110 } 138 }
111 139
140 if (asc_texture_initialize_predefined(ctx)) {
141 asc_error("Initializing predefined textures failed");
142 return false;
143 }
144
112 return true; 145 return true;
113 } else { 146 } else {
114 asc_error(glewGetErrorString(err)); 147 asc_error(glewGetErrorString(err));
115 SDL_GL_DeleteContext(ctx->glctx); 148 SDL_GL_DeleteContext(ctx->glctx);
116 return false; 149 return false;
119 152
120 void asc_gl_context_destroy(AscGLContext *ctx) { 153 void asc_gl_context_destroy(AscGLContext *ctx) {
121 if (ctx->glctx == NULL) return; 154 if (ctx->glctx == NULL) return;
122 SDL_GL_MakeCurrent(ctx->window, ctx->glctx); 155 SDL_GL_MakeCurrent(ctx->window, ctx->glctx);
123 156
157 asc_texture_destroy_predefined(ctx);
124 asc_shader_destroy_predefined(ctx); 158 asc_shader_destroy_predefined(ctx);
125 asc_primitives_destroy(ctx); 159 asc_primitives_destroy(ctx);
126 160
127 // destroy the GL context and the window 161 // destroy the GL context and the window
128 SDL_GL_DeleteContext(ctx->glctx); 162 SDL_GL_DeleteContext(ctx->glctx);

mercurial