src/texture.c

changeset 221
14eddd43b3f7
parent 168
f70569c49c24
equal deleted inserted replaced
220:6b266e907f89 221:14eddd43b3f7
42 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_units); 42 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_units);
43 asc_error("Tried to use more texture units than available (%u/%u).", unit, max_units); 43 asc_error("Tried to use more texture units than available (%u/%u).", unit, max_units);
44 } 44 }
45 glBindTexture(tex->target, tex->tex_id); 45 glBindTexture(tex->target, tex->tex_id);
46 glUniform1i(uniform_location, unit); 46 glUniform1i(uniform_location, unit);
47 asc_error_catch_all_gl(); 47 asc_error_catch_gl("Binding texture to uniform location");
48 } 48 }
49 49
50 void asc_texture_from_surface(AscTexture *tex, const SDL_Surface *surface) { 50 void asc_texture_from_surface(AscTexture *tex, const SDL_Surface *surface) {
51 if (tex->tex_id == 0) { 51 if (tex->tex_id == 0) {
52 asc_error("Tried to use uninitialized texture (%"PRIxPTR ").", (uintptr_t) tex); 52 asc_error("Tried to use uninitialized texture (%"PRIxPTR ").", (uintptr_t) tex);
55 tex->width = surface->w; 55 tex->width = surface->w;
56 tex->height = surface->h; 56 tex->height = surface->h;
57 glBindTexture(tex->target,tex->tex_id); 57 glBindTexture(tex->target,tex->tex_id);
58 glPixelStorei(GL_UNPACK_ROW_LENGTH, 58 glPixelStorei(GL_UNPACK_ROW_LENGTH,
59 surface->pitch / surface->format->BytesPerPixel); 59 surface->pitch / surface->format->BytesPerPixel);
60 if (asc_error_catch_gl("Binding texture object")) return;
60 61
61 // Determine the format and internal format based on the SDL surface format 62 // Determine the format and internal format based on the SDL surface format
62 GLint internal_format; 63 GLint internal_format;
63 GLenum format; 64 GLenum format;
64 switch (surface->format->format) { 65 switch (surface->format->format) {
86 } 87 }
87 glTexImage2D(tex->target, 0, internal_format, 88 glTexImage2D(tex->target, 0, internal_format,
88 surface->w, surface->h, 89 surface->w, surface->h,
89 0, format, 90 0, format,
90 GL_UNSIGNED_BYTE, surface->pixels); 91 GL_UNSIGNED_BYTE, surface->pixels);
91 // TODO: replace catch all with proper error handling for this single call 92 asc_error_catch_gl("Writing texture data");
92 asc_error_catch_all_gl();
93 } 93 }
94 94
95 void asc_texture_from_file(AscTexture *tex, const char *name) { 95 void asc_texture_from_file(AscTexture *tex, const char *name) {
96 cxmutstr filepath = asc_filesystem_combine_paths(cx_strcast(asc_context.texture_path), cx_str(name)); 96 cxmutstr filepath = asc_filesystem_combine_paths(cx_strcast(asc_context.texture_path), cx_str(name));
97 asc_dprintf("Load texture from %" CX_PRIstr, CX_SFMT(filepath)); 97 asc_dprintf("Load texture from %" CX_PRIstr, CX_SFMT(filepath));
129 assert(min_filter < sizeof(texture_filters) / sizeof(GLint)); 129 assert(min_filter < sizeof(texture_filters) / sizeof(GLint));
130 assert(mag_filter < 2); // mag filter only supports nearest/linear 130 assert(mag_filter < 2); // mag filter only supports nearest/linear
131 131
132 GLuint textures[count]; 132 GLuint textures[count];
133 glGenTextures(count, textures); 133 glGenTextures(count, textures);
134 if (asc_error_catch_gl("Creating texture objects")) return;
134 135
135 for (unsigned i = 0; i < count; ++i) { 136 for (unsigned i = 0; i < count; ++i) {
136 memset(&tex[i], 0, sizeof(AscTexture)); 137 memset(&tex[i], 0, sizeof(AscTexture));
137 tex[i].tex_id = textures[i]; 138 tex[i].tex_id = textures[i];
138 tex[i].target = texture_targets[target]; 139 tex[i].target = texture_targets[target];
141 texture_filters[min_filter]); 142 texture_filters[min_filter]);
142 glTexParameteri(tex[i].target, GL_TEXTURE_MAG_FILTER, 143 glTexParameteri(tex[i].target, GL_TEXTURE_MAG_FILTER,
143 texture_filters[mag_filter]); 144 texture_filters[mag_filter]);
144 asc_dprintf("Initialized texture: %u", tex[i].tex_id); 145 asc_dprintf("Initialized texture: %u", tex[i].tex_id);
145 } 146 }
146 147 asc_error_catch_gl("Initializing texture objects");
147 // TODO: proper error handling for each gl call
148 asc_error_catch_all_gl();
149 } 148 }
150 149
151 void asc_texture_destroy(AscTexture *tex, unsigned count) { 150 void asc_texture_destroy(AscTexture *tex, unsigned count) {
152 GLuint textures[count]; 151 GLuint textures[count];
153 for (unsigned i = 0; i < count; ++i) { 152 for (unsigned i = 0; i < count; ++i) {

mercurial