Thanks 71M/Shazz p_texture is either an existing texture (in case it must be of the correct dimensions and format) else a new texture is created and returned.
| 440 | // correct dimensions and format) else a new texture is created and returned. |
| 441 | //***************************************************************************** |
| 442 | CRefPtr<CNativeTexture> LoadPng( const char * p_filename, ETextureFormat texture_format ) |
| 443 | { |
| 444 | const size_t SIGNATURE_SIZE = 8; |
| 445 | u8 signature[ SIGNATURE_SIZE ]; |
| 446 | |
| 447 | FILE * fh( fopen( p_filename,"rb" ) ); |
| 448 | if(fh == nullptr) |
| 449 | { |
| 450 | return nullptr; |
| 451 | } |
| 452 | |
| 453 | if (fread( signature, sizeof(u8), SIGNATURE_SIZE, fh ) != SIGNATURE_SIZE) |
| 454 | { |
| 455 | fclose(fh); |
| 456 | return nullptr; |
| 457 | } |
| 458 | |
| 459 | if ( !png_check_sig( signature, SIGNATURE_SIZE ) ) |
| 460 | { |
| 461 | return nullptr; |
| 462 | } |
| 463 | |
| 464 | png_struct * p_png_struct( png_create_read_struct( PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr ) ); |
| 465 | if ( p_png_struct == nullptr) |
| 466 | { |
| 467 | return nullptr; |
| 468 | } |
| 469 | |
| 470 | png_info * p_png_info( png_create_info_struct( p_png_struct ) ); |
| 471 | if ( p_png_info == nullptr ) |
| 472 | { |
| 473 | png_destroy_read_struct( &p_png_struct, nullptr, nullptr ); |
| 474 | return nullptr; |
| 475 | } |
| 476 | |
| 477 | if ( setjmp( png_jmpbuf(p_png_struct) ) != 0 ) |
| 478 | { |
| 479 | png_destroy_read_struct( &p_png_struct, nullptr, nullptr ); |
| 480 | return nullptr; |
| 481 | } |
| 482 | |
| 483 | png_init_io( p_png_struct, fh ); |
| 484 | png_set_sig_bytes( p_png_struct, SIGNATURE_SIZE ); |
| 485 | png_read_png( p_png_struct, p_png_info, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_BGR, nullptr ); |
| 486 | |
| 487 | png_uint_32 width = png_get_image_width(p_png_struct, p_png_info);//p_png_info->width; |
| 488 | png_uint_32 height = png_get_image_height(p_png_struct, p_png_info);//p_png_info->height; |
| 489 | |
| 490 | |
| 491 | CRefPtr<CNativeTexture> texture = CNativeTexture::Create( width, height, texture_format ); |
| 492 | |
| 493 | #ifdef DAEDALUS_ENABLE_ASSERTS |
| 494 | DAEDALUS_ASSERT( texture->GetWidth() >= width, "Width is unexpectedly small" ); |
| 495 | DAEDALUS_ASSERT( texture->GetHeight() >= height, "Height is unexpectedly small" ); |
| 496 | DAEDALUS_ASSERT( texture_format == texture->GetFormat(), "Texture format doesn't match" ); |
| 497 | #endif |
| 498 | u8 * p_dest( new u8[ texture->GetBytesRequired() ] ); |
| 499 | if( !p_dest ) |
no test coverage detected