Browse Source

Cleaned up some graphics code.

Alex Szpakowski 10 years ago
parent
commit
d31168c0e0

+ 45 - 91
src/modules/graphics/opengl/Graphics.cpp

@@ -555,10 +555,11 @@ void Graphics::setScissor(int x, int y, int width, int height)
 {
 {
 	OpenGL::Viewport box = {x, y, width, height};
 	OpenGL::Viewport box = {x, y, width, height};
 
 
-	states.back().scissor = true;
 	glEnable(GL_SCISSOR_TEST);
 	glEnable(GL_SCISSOR_TEST);
 	// OpenGL's reversed y-coordinate is compensated for in OpenGL::setScissor.
 	// OpenGL's reversed y-coordinate is compensated for in OpenGL::setScissor.
 	gl.setScissor(box);
 	gl.setScissor(box);
+
+	states.back().scissor = true;
 	states.back().scissorBox = box;
 	states.back().scissorBox = box;
 }
 }
 
 
@@ -570,14 +571,14 @@ void Graphics::setScissor()
 
 
 bool Graphics::getScissor(int &x, int &y, int &width, int &height) const
 bool Graphics::getScissor(int &x, int &y, int &width, int &height) const
 {
 {
-	OpenGL::Viewport scissor = gl.getScissor();
+	const DisplayState &state = states.back();
 
 
-	x = scissor.x;
-	y = scissor.y;
-	width = scissor.w;
-	height = scissor.h;
+	x = state.scissorBox.x;
+	y = state.scissorBox.y;
+	width = state.scissorBox.w;
+	height = state.scissorBox.h;
 
 
-	return states.back().scissor;
+	return state.scissor;
 }
 }
 
 
 void Graphics::drawToStencilBuffer(bool enable)
 void Graphics::drawToStencilBuffer(bool enable)
@@ -651,56 +652,12 @@ void Graphics::clearStencil()
 
 
 Image *Graphics::newImage(love::image::ImageData *data, const Image::Flags &flags)
 Image *Graphics::newImage(love::image::ImageData *data, const Image::Flags &flags)
 {
 {
-	// Create the image.
-	Image *image = new Image(data, flags);
-
-	if (!isCreated())
-		return image;
-
-	bool success = false;
-	try
-	{
-		success = image->load();
-	}
-	catch(love::Exception &)
-	{
-		image->release();
-		throw;
-	}
-	if (!success)
-	{
-		image->release();
-		return nullptr;
-	}
-
-	return image;
+	return new Image(data, flags);
 }
 }
 
 
 Image *Graphics::newImage(love::image::CompressedData *cdata, const Image::Flags &flags)
 Image *Graphics::newImage(love::image::CompressedData *cdata, const Image::Flags &flags)
 {
 {
-	// Create the image.
-	Image *image = new Image(cdata, flags);
-
-	if (!isCreated())
-		return image;
-
-	bool success = false;
-	try
-	{
-		success = image->load();
-	}
-	catch(love::Exception &)
-	{
-		image->release();
-		throw;
-	}
-	if (!success)
-	{
-		image->release();
-		return nullptr;
-	}
-
-	return image;
+	return new Image(cdata, flags);
 }
 }
 
 
 Quad *Graphics::newQuad(Quad::Viewport v, float sw, float sh)
 Quad *Graphics::newQuad(Quad::Viewport v, float sw, float sh)
@@ -752,21 +709,20 @@ Canvas *Graphics::newCanvas(int width, int height, Canvas::Format format, int ms
 	error_string << "Cannot create canvas: ";
 	error_string << "Cannot create canvas: ";
 	switch (err)
 	switch (err)
 	{
 	{
-
 	case GL_FRAMEBUFFER_UNSUPPORTED:
 	case GL_FRAMEBUFFER_UNSUPPORTED:
 		error_string << "Not supported by your OpenGL implementation.";
 		error_string << "Not supported by your OpenGL implementation.";
 		break;
 		break;
-
+	case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
+		error_string << "Texture format cannot be rendered to on this system.";
+		break;
 		// remaining error codes are highly unlikely:
 		// remaining error codes are highly unlikely:
 	case GL_FRAMEBUFFER_UNDEFINED:
 	case GL_FRAMEBUFFER_UNDEFINED:
-	case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
 	case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
 	case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
 	case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
 	case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
 	case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
 	case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
 	case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
 	case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
-		error_string << "Error in implementation. Possible fix: Make canvas width and height powers of two.";
+		error_string << "Error in implementation.";
 		break;
 		break;
-
 	default:
 	default:
 		// my intel hda card wrongly returns 0 to glCheckFramebufferStatus() but sets
 		// my intel hda card wrongly returns 0 to glCheckFramebufferStatus() but sets
 		// no error flag. I think it meant to return GL_FRAMEBUFFER_UNSUPPORTED, but who
 		// no error flag. I think it meant to return GL_FRAMEBUFFER_UNSUPPORTED, but who
@@ -949,41 +905,47 @@ Graphics::ColorMask Graphics::getColorMask() const
 
 
 void Graphics::setBlendMode(Graphics::BlendMode mode)
 void Graphics::setBlendMode(Graphics::BlendMode mode)
 {
 {
-	OpenGL::BlendState blend = {GL_ONE, GL_ONE, GL_ZERO, GL_ZERO, GL_FUNC_ADD};
+	GLenum func   = GL_FUNC_ADD;
+	GLenum srcRGB = GL_ONE;
+	GLenum srcA   = GL_ONE;
+	GLenum dstRGB = GL_ZERO;
+	GLenum dstA   = GL_ZERO;
 
 
 	switch (mode)
 	switch (mode)
 	{
 	{
 	case BLEND_ALPHA:
 	case BLEND_ALPHA:
-		blend.srcRGB = GL_SRC_ALPHA;
-		blend.srcA = GL_ONE;
-		blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_ALPHA;
+		srcRGB = GL_SRC_ALPHA;
+		srcA = GL_ONE;
+		dstRGB = dstA = GL_ONE_MINUS_SRC_ALPHA;
 		break;
 		break;
 	case BLEND_MULTIPLY:
 	case BLEND_MULTIPLY:
-		blend.srcRGB = blend.srcA = GL_DST_COLOR;
-		blend.dstRGB = blend.dstA = GL_ZERO;
+		srcRGB = srcA = GL_DST_COLOR;
+		dstRGB = dstA = GL_ZERO;
 		break;
 		break;
 	case BLEND_PREMULTIPLIED:
 	case BLEND_PREMULTIPLIED:
-		blend.srcRGB = blend.srcA = GL_ONE;
-		blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_ALPHA;
+		srcRGB = srcA = GL_ONE;
+		dstRGB = dstA = GL_ONE_MINUS_SRC_ALPHA;
 		break;
 		break;
 	case BLEND_SUBTRACT:
 	case BLEND_SUBTRACT:
-		blend.func = GL_FUNC_REVERSE_SUBTRACT;
+		func = GL_FUNC_REVERSE_SUBTRACT;
 	case BLEND_ADD:
 	case BLEND_ADD:
-		blend.srcRGB = blend.srcA = GL_SRC_ALPHA;
-		blend.dstRGB = blend.dstA = GL_ONE;
+		srcRGB = srcA = GL_SRC_ALPHA;
+		dstRGB = dstA = GL_ONE;
 		break;
 		break;
 	case BLEND_SCREEN:
 	case BLEND_SCREEN:
-		blend.srcRGB = blend.srcA = GL_ONE;
-		blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_COLOR;
+		srcRGB = srcA = GL_ONE;
+		dstRGB = dstA = GL_ONE_MINUS_SRC_COLOR;
 		break;
 		break;
 	case BLEND_REPLACE:
 	case BLEND_REPLACE:
 	default:
 	default:
-		blend.srcRGB = blend.srcA = GL_ONE;
-		blend.dstRGB = blend.dstA = GL_ZERO;
+		srcRGB = srcA = GL_ONE;
+		dstRGB = dstA = GL_ZERO;
 		break;
 		break;
 	}
 	}
 
 
-	gl.setBlendState(blend);
+	glBlendEquation(func);
+	glBlendFuncSeparate(srcRGB, dstRGB, srcA, dstA);
+
 	states.back().blendMode = mode;
 	states.back().blendMode = mode;
 }
 }
 
 
@@ -1114,13 +1076,13 @@ void Graphics::point(float x, float y)
 
 
 void Graphics::polyline(const float *coords, size_t count)
 void Graphics::polyline(const float *coords, size_t count)
 {
 {
-	DisplayState &state = states.back();
+	const DisplayState &state = states.back();
 
 
 	if (state.lineJoin == LINE_JOIN_NONE)
 	if (state.lineJoin == LINE_JOIN_NONE)
 	{
 	{
-			NoneJoinPolyline line;
-			line.render(coords, count, state.lineWidth * .5f, float(pixel_size_stack.back()), state.lineStyle == LINE_SMOOTH);
-			line.draw();
+		NoneJoinPolyline line;
+		line.render(coords, count, state.lineWidth * .5f, float(pixel_size_stack.back()), state.lineStyle == LINE_SMOOTH);
+		line.draw();
 	}
 	}
 	else if (state.lineJoin == LINE_JOIN_BEVEL)
 	else if (state.lineJoin == LINE_JOIN_BEVEL)
 	{
 	{
@@ -1351,31 +1313,23 @@ Graphics::Stats Graphics::getStats() const
 
 
 double Graphics::getSystemLimit(SystemLimit limittype) const
 double Graphics::getSystemLimit(SystemLimit limittype) const
 {
 {
-	double limit = 0.0;
-
 	switch (limittype)
 	switch (limittype)
 	{
 	{
 	case Graphics::LIMIT_POINT_SIZE:
 	case Graphics::LIMIT_POINT_SIZE:
 		{
 		{
 			GLfloat limits[2];
 			GLfloat limits[2];
 			glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, limits);
 			glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, limits);
-			limit = limits[1];
+			return (double) limits[1];
 		}
 		}
-		break;
 	case Graphics::LIMIT_TEXTURE_SIZE:
 	case Graphics::LIMIT_TEXTURE_SIZE:
-		limit = (double) gl.getMaxTextureSize();
-		break;
+		return (double) gl.getMaxTextureSize();
 	case Graphics::LIMIT_MULTI_CANVAS:
 	case Graphics::LIMIT_MULTI_CANVAS:
-		limit = (double) gl.getMaxRenderTargets();
-		break;
+		return (double) gl.getMaxRenderTargets();
 	case Graphics::LIMIT_CANVAS_MSAA:
 	case Graphics::LIMIT_CANVAS_MSAA:
-		limit = (double) gl.getMaxRenderbufferSamples();
-		break;
+		return (double) gl.getMaxRenderbufferSamples();
 	default:
 	default:
-		break;
+		return 0.0;
 	}
 	}
-
-	return limit;
 }
 }
 
 
 bool Graphics::isSupported(Support feature) const
 bool Graphics::isSupported(Support feature) const

+ 3 - 5
src/modules/graphics/opengl/Image.cpp

@@ -52,7 +52,9 @@ Image::Image(love::image::ImageData *data, const Flags &flags)
 {
 {
 	width = data->getWidth();
 	width = data->getWidth();
 	height = data->getHeight();
 	height = data->getHeight();
+
 	preload();
 	preload();
+	loadVolatile();
 
 
 	++imageCount;
 	++imageCount;
 }
 }
@@ -81,6 +83,7 @@ Image::Image(love::image::CompressedData *cdata, const Flags &flags)
 	}
 	}
 
 
 	preload();
 	preload();
+	loadVolatile();
 
 
 	++imageCount;
 	++imageCount;
 }
 }
@@ -123,11 +126,6 @@ void Image::preload()
 		filter.mipmap = defaultMipmapFilter;
 		filter.mipmap = defaultMipmapFilter;
 }
 }
 
 
-bool Image::load()
-{
-	return loadVolatile();
-}
-
 void Image::generateMipmaps()
 void Image::generateMipmaps()
 {
 {
 	// The GL_GENERATE_MIPMAP texparameter is set in loadVolatile if we don't
 	// The GL_GENERATE_MIPMAP texparameter is set in loadVolatile if we don't

+ 0 - 2
src/modules/graphics/opengl/Image.h

@@ -82,8 +82,6 @@ public:
 
 
 	virtual ~Image();
 	virtual ~Image();
 
 
-	bool load();
-
 	// Implements Volatile.
 	// Implements Volatile.
 	bool loadVolatile();
 	bool loadVolatile();
 	void unloadVolatile();
 	void unloadVolatile();

+ 2 - 18
src/modules/graphics/opengl/OpenGL.cpp

@@ -121,9 +121,6 @@ void OpenGL::setupContext()
 
 
 	glActiveTexture(curgltextureunit);
 	glActiveTexture(curgltextureunit);
 
 
-	BlendState blend = {GL_ONE, GL_ONE, GL_ZERO, GL_ZERO, GL_FUNC_ADD};
-	setBlendState(blend);
-
 	createDefaultTexture();
 	createDefaultTexture();
 
 
 	// Invalidate the cached matrices by setting some elements to NaN.
 	// Invalidate the cached matrices by setting some elements to NaN.
@@ -286,8 +283,8 @@ void OpenGL::createDefaultTexture()
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
 
-	GLubyte pix = 255;
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 1, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &pix);
+	GLubyte pix[] = {255, 255, 255, 255};
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix);
 
 
 	bindTexture(curtexture);
 	bindTexture(curtexture);
 }
 }
@@ -417,19 +414,6 @@ OpenGL::Viewport OpenGL::getScissor() const
 	return state.scissor;
 	return state.scissor;
 }
 }
 
 
-void OpenGL::setBlendState(const BlendState &blend)
-{
-	glBlendEquation(blend.func);
-	glBlendFuncSeparate(blend.srcRGB, blend.dstRGB, blend.srcA, blend.dstA);
-
-	state.blend = blend;
-}
-
-OpenGL::BlendState OpenGL::getBlendState() const
-{
-	return state.blend;
-}
-
 void OpenGL::setPointSize(float size)
 void OpenGL::setPointSize(float size)
 {
 {
 	if (GLAD_VERSION_1_0)
 	if (GLAD_VERSION_1_0)

+ 0 - 20
src/modules/graphics/opengl/OpenGL.h

@@ -99,13 +99,6 @@ public:
 		}
 		}
 	};
 	};
 
 
-	struct BlendState
-	{
-		GLenum srcRGB, srcA;
-		GLenum dstRGB, dstA;
-		GLenum func;
-	};
-
 	struct
 	struct
 	{
 	{
 		std::vector<Matrix> transform;
 		std::vector<Matrix> transform;
@@ -235,17 +228,6 @@ public:
 	 **/
 	 **/
 	Viewport getScissor() const;
 	Viewport getScissor() const;
 
 
-	/**
-	 * Sets blending functionality.
-	 * Note: This does not globally enable or disable blending.
-	 **/
-	void setBlendState(const BlendState &blend);
-
-	/**
-	 * Gets the currently set blending functionality.
-	 **/
-	BlendState getBlendState() const;
-
 	/**
 	/**
 	 * Sets the global point size.
 	 * Sets the global point size.
 	 **/
 	 **/
@@ -380,8 +362,6 @@ private:
 
 
 		GLuint defaultTexture;
 		GLuint defaultTexture;
 
 
-		BlendState blend;
-
 		Matrix lastProjectionMatrix;
 		Matrix lastProjectionMatrix;
 		Matrix lastTransformMatrix;
 		Matrix lastTransformMatrix;
 
 

+ 1 - 0
src/modules/image/magpie/STBHandler.cpp

@@ -29,6 +29,7 @@ static void loveSTBAssert(bool test, const char *teststr)
 
 
 // stb_image
 // stb_image
 // #define STBI_ONLY_JPEG
 // #define STBI_ONLY_JPEG
+// #define STBI_ONLY_PNG
 #define STBI_ONLY_BMP
 #define STBI_ONLY_BMP
 #define STBI_ONLY_TGA
 #define STBI_ONLY_TGA
 #define STBI_NO_STDIO
 #define STBI_NO_STDIO