Files
panopainter/engine/asset.cpp
2017-05-07 23:49:46 +01:00

75 lines
1.5 KiB
C++

#include "pch.h"
#include "log.h"
#include "asset.h"
#ifdef __IOS__
#include <Foundation/Foundation.h>
#endif
#ifdef __ANDROID__
AAssetManager* Asset::m_am;
#endif
bool Asset::open(const char* path)
{
LOG("Asset::open %s", path);
m_current_path = path;
std::string file_path = path;
#ifdef __ANDROID__
if (!(m_asset = AAssetManager_open(m_am, path, AASSET_MODE_RANDOM)))
{
LOG("AAssetManager_open failed");
return false;
}
m_len = (int)AAsset_getLength(m_asset);
m_data = (uint8_t*)AAsset_getBuffer(m_asset);
#else
#ifdef __IOS__
NSString* bundle_path = [[NSBundle mainBundle] bundlePath];
std::string base = [bundle_path cStringUsingEncoding:1];
file_path = base + "/" + path;
#endif
if (!(m_fp = fopen(file_path.c_str(), "rb")))
{
LOG("errno = %d", errno);
return false;
}
fseek(m_fp, 0, SEEK_END);
m_len = (int)ftell(m_fp);
fseek(m_fp, 0, SEEK_SET);
#endif
return true;
}
glm::uint8_t* Asset::read_all()
{
#ifdef __ANDROID__
return m_data;
#else
if (!m_data)
{
m_data = new uint8_t[m_len];
if (m_len != fread(m_data, 1, m_len, m_fp))
{
LOG("ASSET READ FAILED for %s", m_current_path.c_str());
delete m_data;
m_data = nullptr;
}
}
return m_data;
#endif
}
void Asset::close()
{
#ifdef __ANDROID__
AAsset_close(m_asset);
#else
if (m_fp)
fclose(m_fp);
if (m_data)
delete m_data;
m_data = nullptr;
#endif
}