Commits

Alex Szpakowski committed f655bcf

Error instead of crash when love.graphics.newScreenshot fails

Comments (0)

Files changed (4)

src/common/Matrix.cpp

 void Matrix::setRotation(float rad)
 {
 	setIdentity();
-	float c = cos(rad), s = sin(rad);
+	float c = cosf(rad), s = sinf(rad);
 	e[0] = c;
 	e[4] = -s;
 	e[1] = s;
 void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
 {
 	memset(e, 0, sizeof(float)*16); // zero out matrix
-	float c = cos(angle), s = sin(angle);
+	float c = cosf(angle), s = sinf(angle);
 	// matrix multiplication carried out on paper:
 	// |1     x| |c -s    | |sx       | | 1 ky    | |1     -ox|
 	// |  1   y| |s  c    | |   sy    | |kx  1    | |  1   -oy|

src/common/Vector.h

 	 * Gets the length of the Vector.
 	 * @return The length of the Vector.
 	 *
-	 * This method requires sqrt() and should be used
+	 * This method requires sqrtf() and should be used
 	 * carefully.
 	 **/
 	float getLength() const;
 
 inline float Vector::getLength() const
 {
-	return sqrt(x*x + y*y);
+	return sqrtf(x*x + y*y);
 }
 
 inline Vector Vector::getNormal() const

src/modules/graphics/opengl/Graphics.cpp

 		glGetFloatv(GL_MODELVIEW_MATRIX, m);
 		float det  = m[0]*m[5]*m[10] + m[4]*m[9]*m[2] + m[8]*m[1]*m[6];
 		det       -= m[2]*m[5]*m[8]  + m[6]*m[9]*m[0] + m[10]*m[1]*m[4];
-		pixel_size = 1.f / sqrt(det);
+		pixel_size = 1.f / sqrtf(det);
 
 		overdraw_factor = pixel_size / halfwidth;
 		halfwidth = std::max(.0f, halfwidth - .25f*pixel_size);
 	float *coords = new float[2 * (points + 1)];
 	for (int i = 0; i < points; ++i, phi += angle_shift)
 	{
-		coords[2*i]   = x + radius * cos(phi);
-		coords[2*i+1] = y + radius * sin(phi);
+		coords[2*i]   = x + radius * cosf(phi);
+		coords[2*i+1] = y + radius * sinf(phi);
 	}
 
 	coords[2*points]   = coords[0];
 
 	for (int i = 0; i <= points; ++i, phi += angle_shift)
 	{
-		coords[2 * (i+1)]     = x + radius * cos(phi);
-		coords[2 * (i+1) + 1] = y + radius * sin(phi);
+		coords[2 * (i+1)]     = x + radius * cosf(phi);
+		coords[2 * (i+1) + 1] = y + radius * sinf(phi);
 	}
 
 	// GL_POLYGON can only fill-draw convex polygons, so we need to do stuff manually here
 
 	int size = row*h;
 
-	GLubyte *pixels = new GLubyte[size];
-	GLubyte *screenshot = new GLubyte[size];
+	GLubyte *pixels = 0;
+	GLubyte *screenshot = 0;
+
+	try
+	{
+		pixels = new GLubyte[size];
+		screenshot = new GLubyte[size];
+	}
+	catch (std::exception &)
+	{
+		delete[] pixels;
+		delete[] screenshot;
+		throw love::Exception("Out of memory.");
+	}
 
 	glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
 
 	for (int i = 0; i < h; ++i)
 		memcpy(dst-=row, src+=row, row);
 
-	love::image::ImageData *img = image->newImageData(w, h, (void *) screenshot);
+	delete[] pixels;
 
-	delete [] pixels;
-	delete [] screenshot;
+	love::image::ImageData *img = 0;
+	try
+	{
+		img = image->newImageData(w, h, (void *) screenshot);
+	}
+	catch (love::Exception &)
+	{
+		delete[] screenshot;
+		throw;
+	}
+
+	delete[] screenshot;
 
 	return img;
 }

src/modules/graphics/opengl/wrap_Graphics.cpp

 {
 	love::image::Image *image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
 	bool copyAlpha = luax_optboolean(L, 1, false);
-	love::image::ImageData *i = instance->newScreenshot(image, copyAlpha);
+	love::image::ImageData *i = 0;
+	try
+	{
+		i = instance->newScreenshot(image, copyAlpha);
+	}
+	catch (love::Exception &e)
+	{
+		return luaL_error(L, "%s", e.what());
+	}
 	luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)i);
 	return 1;
 }
 
 int w_rotate(lua_State *L)
 {
-	float deg = (float)luaL_checknumber(L, 1);
-	instance->rotate(deg);
+	float angle = (float)luaL_checknumber(L, 1);
+	instance->rotate(angle);
 	return 0;
 }