Esempio n. 1
0
FontID FontContext::addFont(const std::string& _family, const std::string& _weight,
                            const std::string& _style, bool _tryFallback) {

    unsigned int dataSize = 0;
    unsigned char* data = nullptr;
    int font = FONS_INVALID;

    std::string fontKey = _family + "_" + _weight + "_" + _style;

    auto it = m_fonts.find(fontKey);
    if (it != m_fonts.end()) {
        if (it->second < 0) {
            goto fallback;
        }
        return it->second;
    }

    {
        // Assuming bundled ttf file follows this convention
        auto bundledFontPath = "fonts/" + _family + "-" + _weight + _style + ".ttf";
        if (!(data = bytesFromFile(bundledFontPath.c_str(), PathType::resource, &dataSize)) &&
            !(data = bytesFromFile(bundledFontPath.c_str(), PathType::internal, &dataSize))) {
            const std::string sysFontPath = systemFontPath(_family, _weight, _style);
            if ( !(data = bytesFromFile(sysFontPath.c_str(), PathType::absolute, &dataSize)) ) {

                LOGE("Could not load font file %s", fontKey.c_str());
                m_fonts.emplace(std::move(fontKey), INVALID_FONT);
                goto fallback;
            }
        }
    }

    font = fonsAddFont(m_fsContext, fontKey.c_str(), data, dataSize);

    if (font == FONS_INVALID) {
        LOGE("Could not load font %s", fontKey.c_str());
        m_fonts.emplace(std::move(fontKey), INVALID_FONT);
        goto fallback;
    }

    m_fonts.emplace(std::move(fontKey), font);

    return font;

fallback:
    if (_tryFallback && m_fonts.size() > 0) {
        return 0;
    }
    return INVALID_FONT;
}
std::string stringFromFile(const char* _path) {
    size_t length = 0;
    unsigned char* bytes = bytesFromFile(_path, length);

    std::string out(reinterpret_cast<char*>(bytes), length);
    free(bytes);

    return out;
}
Esempio n. 3
0
std::string stringFromFile(const char* _path, PathType _type) {

    unsigned int length = 0;
    unsigned char* bytes = bytesFromFile(_path, _type, &length);
    std::string out(reinterpret_cast<char*>(bytes), length);
    free(bytes);

    return out;

}
Esempio n. 4
0
Texture::Texture(const std::string& _file, TextureOptions _options, bool _generateMipmaps)
    : Texture(0u, 0u, _options, _generateMipmaps) {

    unsigned int size;
    unsigned char* data;

    data = bytesFromFile(_file.c_str(), PathType::resource, &size);

    loadPNG(data, size);

    free(data);
}
Esempio n. 5
0
void TextureCube::load(const std::string& _file) {
    unsigned int size;
    unsigned char* data = bytesFromFile(_file.c_str(), PathType::resource, &size);
    unsigned char* pixels;
    int width, height, comp;

    if (data == nullptr || size == 0) {
        LOGE("Texture not found! '%s'", _file.c_str());
        free(data);
        return;
    }

    pixels = stbi_load_from_memory(data, size, &width, &height, &comp, STBI_rgb_alpha);

    size = width * height;

    m_width = width / 4;
    m_height = height / 3;

    for (int i = 0; i < 6; ++i) {
        GLenum face = CubeMapFace[i];
        m_faces.push_back({face, {}, 0});
        m_faces.back().m_data.resize(m_width * m_height);
    }

    for (int y = 0; y < height; ++y) {
        int jFace = (y - (y % m_height)) / m_height;

        for (int iFace = 0; iFace < 4; ++iFace) {
            Face* face = nullptr;

            if (iFace == 2 && jFace == 1) { face = &m_faces[0]; } // POS_X
            if (iFace == 0 && jFace == 1) { face = &m_faces[1]; } // NEG_X
            if (iFace == 1 && jFace == 0) { face = &m_faces[2]; } // POS_Y
            if (iFace == 1 && jFace == 2) { face = &m_faces[3]; } // NEG_Y
            if (iFace == 1 && jFace == 1) { face = &m_faces[4]; } // POS_Z
            if (iFace == 3 && jFace == 1) { face = &m_faces[5]; } // NEG_Z

            if (!face) { continue; }

            int offset = (m_width * iFace + y * width) * sizeof(unsigned int);
            std::memcpy(face->m_data.data() + face->m_offset, pixels + offset, m_width * sizeof(unsigned int));
            face->m_offset += m_width;
        }
    }

    free(data);
    stbi_image_free(pixels);

}