bool TGAImage::read_tga_file(const char *filename) { if (data) delete[] data; data = NULL; std::ifstream in; in.open(filename, std::ios::binary); if (!in.is_open()) { std::cerr << "can't open file " << filename << "\n"; in.close(); return false; } TGA_Header header; in.read((char *)&header, sizeof(header)); if (!in.good()) { in.close(); std::cerr << "an error occured while reading the header\n"; return false; } width = header.width; height = header.height; bytespp = header.bitsperpixel >> 3; if (width <= 0 || height <= 0 || (bytespp != GRAYSCALE && bytespp != RGB && bytespp != RGBA)) { in.close(); std::cerr << "bad bpp (or width/height) value\n"; return false; } unsigned long nbytes = bytespp*width*height; data = new unsigned char[nbytes]; if (3 == header.datatypecode || 2 == header.datatypecode) { in.read((char *)data, nbytes); if (!in.good()) { in.close(); std::cerr << "an error occured while reading the data\n"; return false; } } else if (10 == header.datatypecode || 11 == header.datatypecode) { if (!load_rle_data(in)) { in.close(); std::cerr << "an error occured while reading the data\n"; return false; } } else { in.close(); std::cerr << "unknown file format " << (int)header.datatypecode << "\n"; return false; } if (!(header.imagedescriptor & 0x20)) { flip_vertically(); } if (header.imagedescriptor & 0x10) { flip_horizontally(); } std::cerr << width << "x" << height << "/" << bytespp * 8 << "\n"; in.close(); return true; }
TextureID ResourceManagerImpl::new_texture_from_file(const unicode& path, TextureFlags flags, bool garbage_collect) { //Load the texture auto tex = texture(new_texture(garbage_collect)); window->loader_for("texture", path.encode())->into(tex); if((flags & TEXTURE_OPTION_FLIP_VERTICALLY) == TEXTURE_OPTION_FLIP_VERTICALLY) { tex->flip_vertically(); } tex->upload( false, (flags & TEXTURE_OPTION_DISABLE_MIPMAPS) != TEXTURE_OPTION_DISABLE_MIPMAPS, (flags & TEXTURE_OPTION_CLAMP_TO_EDGE) != TEXTURE_OPTION_CLAMP_TO_EDGE, (flags & TEXTURE_OPTION_NEAREST_FILTER) != TEXTURE_OPTION_NEAREST_FILTER ); mark_texture_as_uncollected(tex->id()); return tex->id(); }