瀏覽代碼

Error instead of crash when love.graphics.newScreenshot fails

Alex Szpakowski 12 年之前
父節點
當前提交
ad2239aaaf
共有 4 個文件被更改,包括 47 次插入17 次删除
  1. 2 2
      src/common/Matrix.cpp
  2. 2 2
      src/common/Vector.h
  3. 32 10
      src/modules/graphics/opengl/Graphics.cpp
  4. 11 3
      src/modules/graphics/opengl/wrap_Graphics.cpp

+ 2 - 2
src/common/Matrix.cpp

@@ -104,7 +104,7 @@ void Matrix::setTranslation(float x, float y)
 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;
@@ -128,7 +128,7 @@ void Matrix::setShear(float kx, float ky)
 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|

+ 2 - 2
src/common/Vector.h

@@ -59,7 +59,7 @@ public:
 	 * 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;
@@ -178,7 +178,7 @@ public:
 
 inline float Vector::getLength() const
 {
-	return sqrt(x*x + y*y);
+	return sqrtf(x*x + y*y);
 }
 
 inline Vector Vector::getNormal() const

+ 32 - 10
src/modules/graphics/opengl/Graphics.cpp

@@ -1102,7 +1102,7 @@ void Graphics::polyline(const float *coords, size_t count)
 		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);
@@ -1168,8 +1168,8 @@ void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
 	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];
@@ -1206,8 +1206,8 @@ void Graphics::arc(DrawMode mode, float x, float y, float radius, float angle1,
 
 	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
@@ -1257,8 +1257,20 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
 
 	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);
 
@@ -1276,10 +1288,20 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
 	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;
+
+	love::image::ImageData *img = 0;
+	try
+	{
+		img = image->newImageData(w, h, (void *) screenshot);
+	}
+	catch (love::Exception &)
+	{
+		delete[] screenshot;
+		throw;
+	}
 
-	delete [] pixels;
-	delete [] screenshot;
+	delete[] screenshot;
 
 	return img;
 }

+ 11 - 3
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -1066,7 +1066,15 @@ int w_newScreenshot(lua_State *L)
 {
 	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;
 }
@@ -1570,8 +1578,8 @@ int w_pop(lua_State *L)
 
 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;
 }