Error instead of crash when love.graphics.newScreenshot fails

This commit is contained in:
Alex Szpakowski
2013-04-28 11:12:19 -07:00
parent b44c083894
commit ad2239aaaf
4 changed files with 47 additions and 17 deletions
+32 -10
View File
@@ -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;
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;
}
+11 -3
View File
@@ -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;
}