Source

love-openzdk / src / modules / graphics / opengl2 / Image.cpp

/**
* Copyright (c) 2006-2010 LOVE Development Team
* 
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* 
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 
* 1. The origin of this software must not be misrepresented; you must not
*    claim that you wrote the original software. If you use this software
*    in a product, an acknowledgment in the product documentation would be
*    appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
*    misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/

// C++
#include <cassert>

// OpenGL
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

// LOVE
#include <image/ImageData.h>
#include <graphics/Image.h>
#include "GLUtilities.h"
#include "Image.h"

namespace love
{
namespace graphics
{
namespace opengl2
{
	Image::Image(love::image::ImageData * source)
		: source(source), textureId(0)
	{
		// Cache the dimensions and check if we're NPOT
		assert(source);
		width = source->getWidth();
		height = source->getHeight();
		npot = (width & -width) != width ||
		       (height & -height) != height;
	}

	void Image::draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const
	{
		// FIXME: Implement draw
		assert(0);
	}

	Image::Filter Image::getFilter() const
	{
		return filter;
	}

	int Image::getHeight() const
	{
		return height;
	}

	int Image::getWidth() const
	{
		return width;
	}

	Image::Wrap Image::getWrap() const
	{
		return wrap;
	}

	bool Image::loadVolatile()
	{
		// We can short-circuit this if we're already loaded
		if (textureId)
			return true;

		// Textures are always R8G8B8A8 for now
		GLenum format = GL_RGBA;
		GLenum type = GL_UNSIGNED_BYTE;

		// Create and load an OpenGL texture object
		LGLErrorResetR();
		glGenTextures(1, &textureId);
		glBindTexture(GL_TEXTURE_2D, textureId);
		glTexImage2D(GL_TEXTURE_2D, 0, format, width, height,
				0, format, type, source->getData());

		// Sync up the texture parameters
		setFilter(filter);
		setWrap(wrap);
		if (LGLErrorTestR())
		{
			glDeleteTextures(1, &textureId);
			return false;
		}
		return true;
	}

	void Image::setFilter(Image::Filter f)
	{
		// Store the value for later retrieval
		filter = f;

		// Update the relevant OpenGL parameters
		if (textureId)
		{
			GLenum minification = GL_LINEAR;
			GLenum magnification = GL_LINEAR;
			if (f.min == FILTER_NEAREST)
				minification = GL_NEAREST;
			else if (f.min != FILTER_LINEAR)
				assert(0);
			if (f.mag == FILTER_NEAREST)
				magnification = GL_NEAREST;
			else if (f.mag != FILTER_LINEAR)
				assert(0);
			LGLErrorResetD();
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minification);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magnification);
			LGLErrorCheckD();
		}
	}

	void Image::setWrap(Image::Wrap r)
	{
		// Store the value for later retrieval
		wrap = r;

		// Update the relevant OpenGL parameters
		if (textureId)
		{
			GLenum swrap = GL_CLAMP_TO_EDGE;
			GLenum twrap = GL_CLAMP_TO_EDGE;
			if (!npot)
			{
				if (r.s == WRAP_REPEAT)
					swrap = GL_REPEAT;
				else if (r.s != WRAP_CLAMP)
					assert(0);
				if (r.t == WRAP_REPEAT)
					twrap = GL_REPEAT;
				else if (r.t != WRAP_CLAMP)
					assert(0);
			}
			LGLErrorResetD();
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, swrap);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, twrap);
			LGLErrorCheckD();
		}
	}

	void Image::unloadVolatile()
	{
		// Delete the OpenGL texture object
		if (textureId)
		{
			LGLErrorResetD();
			glDeleteTextures(1, &textureId);
			LGLErrorCheckD();
			textureId = 0;
		}
	}

	Image::~Image()
	{
		// Unload the texture if we haven't already done so
		unloadVolatile();
	}

} // opengl2
} // graphics
} // love