Skip to content

Instantly share code, notes, and snippets.

@thetooth
Created January 30, 2014 10:27
Show Gist options
  • Save thetooth/8705957 to your computer and use it in GitHub Desktop.
Save thetooth/8705957 to your computer and use it in GitHub Desktop.
// Before -- What the fuck tier
int Texture::LoadTexture(const char *fname){
cl("Loading Texture: %s ", fname);
unique_ptr<FILE, int(*)(FILE*)> filePtr(fopen(fname, "rb"), fclose);
if(filePtr == nullptr){
cl("[FAILED]\n");
throw KLGLException("[KLGLTexture::LoadTexture][%d:%s] \nResource \"%s\" could not be opened.", __LINE__, __FILE__, fname);
return 1;
}
// Pass a big file and we could be sitting here for a long time
fseek(filePtr.get(), 0, SEEK_END);
size_t size = ftell(filePtr.get());
rewind(filePtr.get());
unsigned char *data = new unsigned char[size+8];
fread((unsigned char*)data, 1, size, filePtr.get());
filePtr.release();
InitTexture(data, size);
data[size+1] = '\0';
delete [] data;
cl("[OK]\n");
return 0;
}
// After -- Kinda acceptable tier
int Texture::LoadTexture(const char *fname){
cl("Loading Texture: %s ", fname);
ifstream file(fname, ios::in | ios::binary);
if(file.bad()){
cl("[FAILED]\n");
throw KLGLException("[KLGLTexture::LoadTexture][%d:%s] \nResource \"%s\" could not be opened.", __LINE__, __FILE__, fname);
return 1;
}
std::vector<unsigned char>data;
std::copy(
std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>( ),
std::back_inserter(data));
InitTexture(data.data(), data.size());
cl("[OK]\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment