与动态数组类似的,站在巨人的肩膀上,我们需要引用这个解码png的工程 https://github.com/elanthis/upng
最初上的贴图颜色并没有对应上 查看解码贴图的工程说明,支持的是RGBA格式
//Creating a SDL texture that is used to display the color buffer
color_buffer_texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
window_width,
window_height
);
而我们写的颜色缓冲问题支持的是ARGB格式
通过文档查看sdl支持的RGBA格式纹理,选择RGBA32作为颜色缓冲纹理的格式。 根据作者提供的用法,我们得先声明一个upng_t类型指针,读取文件存到这个指针的内存中,在通过库中的方法去读取贴图存到uint32_t类型的数组缓冲区,再获得贴图的宽和高。
void load_png_texture_data(char* filename){
png_texture = upng_new_from_file(filename);
if(png_texture != NULL){
upng_decode(png_texture);
if (upng_get_error(png_texture) == UPNG_EOK)
{
mesh_texture = (uint32_t*)upng_get_buffer(png_texture);
texture_width = upng_get_width(png_texture);
texture_height = upng_get_height(png_texture);
}
}
}
释放贴图内存
void free_resources(void){
......
upng_free(png_texture);
......
}