Skip to content

Instantly share code, notes, and snippets.

@ErnestasJa
Last active October 7, 2015 09:15
Show Gist options
  • Save ErnestasJa/fde4658b67416b3b54d2 to your computer and use it in GitHub Desktop.
Save ErnestasJa/fde4658b67416b3b54d2 to your computer and use it in GitHub Desktop.
Application::App...{
***
m_fileSystem = new FileSystem(m_appContext, argv);
m_settingsManager = new SettingsManager(m_appContext); // gets FS pointer through app ctx.
***
}
####################################
MyApplication::Init...{
***
m_appContext->fileSystem->AddSearchDirectory("resources");
m_appContext->fileSystem->AddSearchDirectory("config");
***
m_appContext->settingsManager->ReadSettings("config.json");
}
// one way to read.
MyApplication::LoadStuff....{
****
FilePtr file = m_appContext->fileSystem->OpenRead("textures/tex.png");
char * buf;
int len;
len = file->Read(buf, file->GetLength());
****
}
// Another way to read.
template<class T>
using VectorPtr = std::shared_ptr<std::vector<T>>;
MyApplication::LoadStuff....{
****
///Read whole file contents to buffer;
FilePtr file = m_appContext->fileSystem->OpenRead("textures/tex.png");
VectorPtr buffer = file->ReadBuffer<char>();
///read some of the file to buffer, if less is read size will be equal to bytes read.
FilePtr otherFile = m_appContext->fileSystem->OpenRead("textures/voxels.vox");
uint32_t bytesToRead = 1024;
VectorPtr buffer = otherFile->ReadBuffer<char>(bytesToRead);
///Files should be closed when shared pointer goes out of scope or has no more references.
///Same thing goes for buffers.
****
}
///How new BVox reader would look like after changes:
void BVoxLoader::ReadFile(const std::string &fileName)
{
FilePtr file = m_appContext->fileSystem->OpenRead("textures/tex.png");
if(file->IsOpen() == false)
{
//Maybe this log could be inside open function with a parameter to choose (not?) to log it.
//Would remove even more boilerplate code.
m_log->log(LOG_LOG, "Failed to open file '%s'.", "textures/tex.png");
return;
}
VectorPtr buffer = file->ReadBuffer<char>();
m_log->log(LOG_LOG, "Read bytes: %u", buffer->size());
//same old loader code.
uint16_t * data = (uint16_t*)((void*)&buffer[0]);
uint32_t voxel_count = ((uint32_t*)data)[0];
data++;
data++;
m_log->log(LOG_LOG, "Voxel counts: %u", voxel_count);
for(int i = 0; i < voxel_count; i++)
{
uint16_t x = data[0], y = data[1], z = data[2];
m_octree->AddOrphanNode(MNode(x,y,z));
data+=3;
}
delete[] buf;
}
//Basically it would take 2 lines to read contents from file to buffer.
//And "optional" check if file was properly opened.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment