30 #include "ascension/texture.h" |
30 #include "ascension/texture.h" |
31 #include "ascension/filesystem.h" |
31 #include "ascension/filesystem.h" |
32 |
32 |
33 #include <assert.h> |
33 #include <assert.h> |
34 #include <GL/glew.h> |
34 #include <GL/glew.h> |
35 #include <SDL2/SDL_image.h> |
35 #include <SDL3_image/SDL_image.h> |
36 |
36 |
37 void asc_texture_bind(const AscTexture *tex, int uniform_location, int unit) { |
37 void asc_texture_bind(const AscTexture *tex, int uniform_location, int unit) { |
38 glActiveTexture(GL_TEXTURE0 + unit); |
38 glActiveTexture(GL_TEXTURE0 + unit); |
39 GLenum error = glGetError(); |
39 GLenum error = glGetError(); |
40 if (error == GL_INVALID_ENUM) { |
40 if (error == GL_INVALID_ENUM) { |
54 } |
54 } |
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 / SDL_BYTESPERPIXEL(surface->format)); |
60 if (asc_error_catch_gl("Binding texture object")) return; |
60 if (asc_error_catch_gl("Binding texture object")) return; |
61 |
61 |
62 // 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 |
63 GLint internal_format; |
63 GLint internal_format; |
64 GLenum format; |
64 GLenum format; |
65 switch (surface->format->format) { |
65 switch (surface->format) { |
66 case SDL_PIXELFORMAT_RGB24: |
66 case SDL_PIXELFORMAT_RGB24: |
67 internal_format = GL_RGB8; |
67 internal_format = GL_RGB8; |
68 format = GL_RGB; |
68 format = GL_RGB; |
69 break; |
69 break; |
70 case SDL_PIXELFORMAT_BGR24: |
70 case SDL_PIXELFORMAT_BGR24: |
80 case SDL_PIXELFORMAT_BGRA32: |
80 case SDL_PIXELFORMAT_BGRA32: |
81 internal_format = GL_RGBA8; |
81 internal_format = GL_RGBA8; |
82 format = GL_BGRA; |
82 format = GL_BGRA; |
83 break; |
83 break; |
84 default: |
84 default: |
85 asc_error("Unsupported pixel format: %x", surface->format->format); |
85 asc_error("Unsupported pixel format: %x", surface->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, |
100 cxmutstr filepath = asc_filesystem_combine_paths(cx_strcast(asc_context.texture_path), cx_str(name)); |
100 cxmutstr filepath = asc_filesystem_combine_paths(cx_strcast(asc_context.texture_path), cx_str(name)); |
101 asc_dprintf("Load texture from %" CX_PRIstr, CX_SFMT(filepath)); |
101 asc_dprintf("Load texture from %" CX_PRIstr, CX_SFMT(filepath)); |
102 SDL_Surface *image = IMG_Load(filepath.ptr); |
102 SDL_Surface *image = IMG_Load(filepath.ptr); |
103 cx_strfree(&filepath); |
103 cx_strfree(&filepath); |
104 if (image == NULL) { |
104 if (image == NULL) { |
105 asc_error("Failed to load texture: %s", IMG_GetError()); |
105 asc_error("Failed to load texture: %s", SDL_GetError()); |
106 return; |
106 return; |
107 } |
107 } |
108 asc_texture_from_surface(tex, image); |
108 asc_texture_from_surface(tex, image); |
109 asc_dprintf("Free temporary surface %"PRIxPTR, (uintptr_t) image); |
109 asc_dprintf("Free temporary surface %"PRIxPTR, (uintptr_t) image); |
110 SDL_FreeSurface(image); |
110 SDL_DestroySurface(image); |
111 } |
111 } |
112 |
112 |
113 void asc_texture_init( |
113 void asc_texture_init( |
114 AscTexture *tex, |
114 AscTexture *tex, |
115 unsigned count, |
115 unsigned count, |