| 37 |
37 |
| 38 void asc_texture_bind(AscTexture const *tex, int uniform_location, int unit) { |
38 void asc_texture_bind(AscTexture const *tex, int uniform_location, int unit) { |
| 39 glActiveTexture(GL_TEXTURE0 + unit); |
39 glActiveTexture(GL_TEXTURE0 + unit); |
| 40 GLenum error = glGetError(); |
40 GLenum error = glGetError(); |
| 41 if (error == GL_INVALID_ENUM) { |
41 if (error == GL_INVALID_ENUM) { |
| 42 asc_error("Tried to use more texture units than available."); |
42 GLint max_units; |
| |
43 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_units); |
| |
44 asc_error("Tried to use more texture units than available (%u/%u).", unit, max_units); |
| 43 } |
45 } |
| 44 glBindTexture(tex->target, tex->tex_id); |
46 glBindTexture(tex->target, tex->tex_id); |
| 45 glUniform1i(uniform_location, unit); |
47 glUniform1i(uniform_location, unit); |
| 46 asc_error_catch_all_gl(); |
48 asc_error_catch_all_gl(); |
| 47 } |
49 } |
| 48 |
50 |
| 49 void asc_texture_from_surface(AscTexture *tex, SDL_Surface const *surface) { |
51 void asc_texture_from_surface(AscTexture *tex, SDL_Surface const *surface) { |
| 50 if (tex->tex_id == 0) { |
52 if (tex->tex_id == 0) { |
| 51 asc_error("Tried to use uninitialized texture."); |
53 asc_error("Tried to use uninitialized texture (%"PRIxPTR ").", (uintptr_t) tex); |
| 52 asc_dprintf("Texture address: %"PRIxPTR, (uintptr_t) tex); |
|
| 53 return; |
54 return; |
| 54 } |
55 } |
| 55 tex->width = surface->w; |
56 tex->width = surface->w; |
| 56 tex->height = surface->h; |
57 tex->height = surface->h; |
| 57 glBindTexture(tex->target,tex->tex_id); |
58 glBindTexture(tex->target,tex->tex_id); |
| 79 case SDL_PIXELFORMAT_BGRA32: |
80 case SDL_PIXELFORMAT_BGRA32: |
| 80 internal_format = GL_RGBA8; |
81 internal_format = GL_RGBA8; |
| 81 format = GL_BGRA; |
82 format = GL_BGRA; |
| 82 break; |
83 break; |
| 83 default: |
84 default: |
| 84 // TODO: add more output once asc_error allows format strings |
85 asc_error("Unsupported pixel format: %x", surface->format->format); |
| 85 asc_error("Unsupported pixel format."); |
|
| 86 return; |
86 return; |
| 87 } |
87 } |
| 88 glTexImage2D(tex->target, 0, internal_format, |
88 glTexImage2D(tex->target, 0, internal_format, |
| 89 surface->w, surface->h, |
89 surface->w, surface->h, |
| 90 0, format, |
90 0, format, |
| 151 |
151 |
| 152 void asc_texture_destroy(AscTexture *tex, unsigned count) { |
152 void asc_texture_destroy(AscTexture *tex, unsigned count) { |
| 153 GLuint textures[count]; |
153 GLuint textures[count]; |
| 154 for (unsigned i = 0; i < count; ++i) { |
154 for (unsigned i = 0; i < count; ++i) { |
| 155 if (tex[i].refcount > 0) { |
155 if (tex[i].refcount > 0) { |
| 156 // TODO: asc_wprintf() for warnings |
156 asc_wprintf("Texture %u still in use by %u objects.", |
| 157 asc_dprintf("Texture %u still in use by %u objects.", |
|
| 158 tex[i].tex_id, tex[i].refcount); |
157 tex[i].tex_id, tex[i].refcount); |
| 159 } |
158 } |
| 160 asc_dprintf("Destroy texture: %u", tex[i].tex_id); |
159 asc_dprintf("Destroy texture: %u", tex[i].tex_id); |
| 161 textures[i] = tex[i].tex_id; |
160 textures[i] = tex[i].tex_id; |
| 162 memset(&tex[i], 0, sizeof(AscTexture)); |
161 memset(&tex[i], 0, sizeof(AscTexture)); |