src/Graphics/Texture.cpp in gosu-0.8.7.2 vs src/Graphics/Texture.cpp in gosu-0.9.0.pre1
- old
+ new
@@ -11,10 +11,12 @@
}
Gosu::Texture::Texture(unsigned size)
: allocator(size, size)
{
+ ensureCurrentContext();
+
// Create texture name.
glGenTextures(1, &name);
if (name == static_cast<GLuint>(-1))
throw std::runtime_error("Couldn't create OpenGL texture");
@@ -48,10 +50,12 @@
#endif
}
Gosu::Texture::~Texture()
{
+ ensureCurrentContext();
+
glDeleteTextures(1, &name);
}
unsigned Gosu::Texture::size() const
{
@@ -62,22 +66,23 @@
{
return name;
}
GOSU_UNIQUE_PTR<Gosu::TexChunk>
- Gosu::Texture::tryAlloc(Graphics& graphics, DrawOpQueueStack& queues,
- std::tr1::shared_ptr<Texture> ptr, const Bitmap& bmp, unsigned padding)
+ Gosu::Texture::tryAlloc(std::tr1::shared_ptr<Texture> ptr, const Bitmap& bmp, unsigned padding)
{
GOSU_UNIQUE_PTR<Gosu::TexChunk> result;
BlockAllocator::Block block;
if (!allocator.alloc(bmp.width(), bmp.height(), block))
return result;
- result.reset(new TexChunk(graphics, queues, ptr, block.left + padding, block.top + padding,
+ result.reset(new TexChunk(ptr, block.left + padding, block.top + padding,
block.width - 2 * padding, block.height - 2 * padding, padding));
+ ensureCurrentContext();
+
glBindTexture(GL_TEXTURE_2D, name);
glTexSubImage2D(GL_TEXTURE_2D, 0, block.left, block.top, block.width, block.height,
Color::GL_FORMAT, GL_UNSIGNED_BYTE, bmp.data());
return GOSU_MOVE_UNIQUE_PTR(result);
@@ -96,9 +101,11 @@
Gosu::Bitmap Gosu::Texture::toBitmap(unsigned x, unsigned y, unsigned width, unsigned height) const
{
#ifdef GOSU_IS_OPENGLES
throw std::logic_error("Texture::toBitmap not supported on iOS");
#else
+ ensureCurrentContext();
+
Gosu::Bitmap fullTexture(size(), size());
glBindTexture(GL_TEXTURE_2D, name);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, fullTexture.data());
Gosu::Bitmap bitmap(width, height);
bitmap.insert(fullTexture, -int(x), -int(y));