Пример #1
0
Gosu::Bitmap Gosu::Texture::toBitmap(unsigned x, unsigned y, unsigned width, unsigned height) const
{
#ifdef GOSU_IS_IPHONE
    throw std::logic_error("Texture::toBitmap not supported on iOS");
#else
    Gosu::Bitmap fullTexture;
    fullTexture.resize(size(), size());
    glBindTexture(GL_TEXTURE_2D, name);
    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, fullTexture.data());
    Gosu::Bitmap bitmap;
    bitmap.resize(width, height);
    bitmap.insert(fullTexture, -int(x), -int(y));
    
    return bitmap;
#endif
}
Пример #2
0
void Gosu::loadImageFile(Gosu::Bitmap& bitmap, Reader input)
{
    stbi_io_callbacks callbacks;
    callbacks.read = readCallback;
    callbacks.skip = skipCallback;
    callbacks.eof = eofCallback;
    int x, y, n;
    stbi_uc* bytes = stbi_load_from_callbacks(&callbacks, &input, &x, &y, &n, STBI_rgb_alpha);

    if (bytes == 0) {
        // TODO - stbi_failure_reason is not thread safe. Everything here should be wrapped in a mutex.
        throw std::runtime_error("Cannot load image: " + std::string(stbi_failure_reason()));
    }

    bitmap.resize(x, y);
    std::memcpy(bitmap.data(), bytes, x * y * sizeof(Gosu::Color));

    stbi_image_free(bytes);
}