mirror of
https://github.com/love2d/love.git
synced 2026-08-20 04:30:09 +02:00
Code cleanup
This commit is contained in:
@@ -342,7 +342,7 @@ Image *Graphics::newImage(love::image::ImageData *data)
|
|||||||
catch(love::Exception &e)
|
catch(love::Exception &e)
|
||||||
{
|
{
|
||||||
image->release();
|
image->release();
|
||||||
throw love::Exception(e.what());
|
throw;
|
||||||
}
|
}
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -143,41 +143,46 @@ void Image::drawq(love::graphics::Quad *quad, float x, float y, float angle, flo
|
|||||||
drawv(t, v);
|
drawv(t, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Image::checkMipmapsCreated() const
|
||||||
|
{
|
||||||
|
if (filter.mipmap != FILTER_NEAREST && filter.mipmap != FILTER_LINEAR)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!hasMipmapSupport())
|
||||||
|
throw love::Exception("Mipmap filtering is not supported on this system!");
|
||||||
|
|
||||||
|
// some old GPUs/systems claim support for NPOT textures, but fail when generating mipmaps
|
||||||
|
// we can't detect which systems will do this, so we fail gracefully for all NPOT images
|
||||||
|
int w = int(width), h = int(height);
|
||||||
|
if (w != next_p2(w) || h != next_p2(h))
|
||||||
|
throw love::Exception("Could not generate mipmaps: image does not have power of two dimensions!");
|
||||||
|
|
||||||
|
bind();
|
||||||
|
|
||||||
|
GLboolean mipmapscreated;
|
||||||
|
glGetTexParameteriv(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, (GLint *)&mipmapscreated);
|
||||||
|
|
||||||
|
// generate mipmaps for this image if we haven't already
|
||||||
|
if (!mipmapscreated)
|
||||||
|
{
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
|
||||||
|
|
||||||
|
if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_object)
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
else if (GLEE_EXT_framebuffer_object)
|
||||||
|
glGenerateMipmapEXT(GL_TEXTURE_2D);
|
||||||
|
else
|
||||||
|
// modify single texel to trigger mipmap chain generation
|
||||||
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data->getData());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Image::setFilter(const Image::Filter &f)
|
void Image::setFilter(const Image::Filter &f)
|
||||||
{
|
{
|
||||||
filter = f;
|
filter = f;
|
||||||
|
|
||||||
bind();
|
bind();
|
||||||
|
checkMipmapsCreated();
|
||||||
if (f.mipmap == FILTER_NEAREST || f.mipmap == FILTER_LINEAR)
|
|
||||||
{
|
|
||||||
if (!hasMipmapSupport())
|
|
||||||
throw love::Exception("Mipmap filtering is not supported on this system!");
|
|
||||||
|
|
||||||
// some old GPUs/systems claim support for NPOT textures, but fail when generating mipmaps for them
|
|
||||||
// we can't detect which systems will do this, so it's better to fail gracefully for all NPOT images
|
|
||||||
int w = int(width), h = int(height);
|
|
||||||
if (w != next_p2(w) || h != next_p2(h))
|
|
||||||
throw love::Exception("Could not generate mipmaps: image does not have power of two dimensions!");
|
|
||||||
|
|
||||||
GLboolean mipmapscreated;
|
|
||||||
glGetTexParameteriv(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, (GLint *)&mipmapscreated);
|
|
||||||
|
|
||||||
// generate mipmaps for this image if we haven't already
|
|
||||||
if (!mipmapscreated)
|
|
||||||
{
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
|
|
||||||
|
|
||||||
if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_object)
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
else if (GLEE_EXT_framebuffer_object)
|
|
||||||
glGenerateMipmapEXT(GL_TEXTURE_2D);
|
|
||||||
else
|
|
||||||
// modify single texel in texture to trigger mipmap chain generation
|
|
||||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data->getData());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setTextureFilter(f);
|
setTextureFilter(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +191,7 @@ const Image::Filter &Image::getFilter() const
|
|||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Image::setWrap(Image::Wrap &w)
|
void Image::setWrap(const Image::Wrap &w)
|
||||||
{
|
{
|
||||||
wrap = w;
|
wrap = w;
|
||||||
|
|
||||||
@@ -201,7 +206,7 @@ const Image::Wrap &Image::getWrap() const
|
|||||||
|
|
||||||
void Image::setMipmapSharpness(float sharpness)
|
void Image::setMipmapSharpness(float sharpness)
|
||||||
{
|
{
|
||||||
if (!(GLEE_VERSION_1_4 || GLEE_EXT_texture_lod_bias))
|
if (!hasMipmapSharpnessSupport())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// LOD bias has the range (-maxbias, maxbias)
|
// LOD bias has the range (-maxbias, maxbias)
|
||||||
@@ -236,7 +241,7 @@ void Image::unload()
|
|||||||
|
|
||||||
bool Image::loadVolatile()
|
bool Image::loadVolatile()
|
||||||
{
|
{
|
||||||
if (GLEE_VERSION_1_4 || GLEE_EXT_texture_lod_bias)
|
if (hasMipmapSharpnessSupport())
|
||||||
glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS, &maxmipmapsharpness);
|
glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS, &maxmipmapsharpness);
|
||||||
|
|
||||||
if (hasNpot())
|
if (hasNpot())
|
||||||
@@ -360,6 +365,11 @@ bool Image::hasMipmapSupport()
|
|||||||
return (GLEE_VERSION_1_4 || GLEE_SGIS_generate_mipmap) != 0;
|
return (GLEE_VERSION_1_4 || GLEE_SGIS_generate_mipmap) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Image::hasMipmapSharpnessSupport()
|
||||||
|
{
|
||||||
|
return (GLEE_VERSION_1_4 || GLEE_EXT_texture_lod_bias) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
} // opengl
|
} // opengl
|
||||||
} // graphics
|
} // graphics
|
||||||
} // love
|
} // love
|
||||||
|
|||||||
@@ -96,14 +96,13 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the filter mode.
|
* Sets the filter mode.
|
||||||
*
|
|
||||||
* @param f The filter mode.
|
* @param f The filter mode.
|
||||||
**/
|
**/
|
||||||
void setFilter(const Image::Filter &f);
|
void setFilter(const Image::Filter &f);
|
||||||
|
|
||||||
const Image::Filter &getFilter() const;
|
const Image::Filter &getFilter() const;
|
||||||
|
|
||||||
void setWrap(Image::Wrap &w);
|
void setWrap(const Image::Wrap &w);
|
||||||
|
|
||||||
const Image::Wrap &getWrap() const;
|
const Image::Wrap &getWrap() const;
|
||||||
|
|
||||||
@@ -122,6 +121,7 @@ public:
|
|||||||
|
|
||||||
static bool hasNpot();
|
static bool hasNpot();
|
||||||
static bool hasMipmapSupport();
|
static bool hasMipmapSupport();
|
||||||
|
static bool hasMipmapSharpnessSupport();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@ private:
|
|||||||
// Mipmap texture LOD bias value
|
// Mipmap texture LOD bias value
|
||||||
float mipmapsharpness;
|
float mipmapsharpness;
|
||||||
|
|
||||||
// Implementation-dependent maximum/minimum sharpness values
|
// Implementation-dependent maximum/minimum mipmap sharpness values
|
||||||
float maxmipmapsharpness;
|
float maxmipmapsharpness;
|
||||||
|
|
||||||
// The image's filter mode
|
// The image's filter mode
|
||||||
@@ -159,6 +159,8 @@ private:
|
|||||||
bool loadVolatilePOT();
|
bool loadVolatilePOT();
|
||||||
bool loadVolatileNPOT();
|
bool loadVolatileNPOT();
|
||||||
|
|
||||||
|
void checkMipmapsCreated() const;
|
||||||
|
|
||||||
}; // Image
|
}; // Image
|
||||||
|
|
||||||
} // opengl
|
} // opengl
|
||||||
|
|||||||
@@ -68,21 +68,16 @@ int w_Canvas_setFilter(lua_State *L)
|
|||||||
{
|
{
|
||||||
Canvas *canvas = luax_checkcanvas(L, 1);
|
Canvas *canvas = luax_checkcanvas(L, 1);
|
||||||
|
|
||||||
Image::FilterMode min;
|
Image::Filter f;
|
||||||
Image::FilterMode mag;
|
|
||||||
|
|
||||||
const char *minstr = luaL_checkstring(L, 2);
|
const char *minstr = luaL_checkstring(L, 2);
|
||||||
const char *magstr = luaL_optstring(L, 3, minstr);
|
const char *magstr = luaL_optstring(L, 3, minstr);
|
||||||
|
|
||||||
if (!Image::getConstant(minstr, min))
|
if (!Image::getConstant(minstr, f.min))
|
||||||
return luaL_error(L, "Invalid filter mode: %s", minstr);
|
return luaL_error(L, "Invalid filter mode: %s", minstr);
|
||||||
if (!Image::getConstant(magstr, mag))
|
if (!Image::getConstant(magstr, f.mag))
|
||||||
return luaL_error(L, "Invalid filter mode: %s", magstr);
|
return luaL_error(L, "Invalid filter mode: %s", magstr);
|
||||||
|
|
||||||
Image::Filter f;
|
|
||||||
f.min = min;
|
|
||||||
f.mag = mag;
|
|
||||||
|
|
||||||
canvas->setFilter(f);
|
canvas->setFilter(f);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -92,7 +87,7 @@ int w_Canvas_setFilter(lua_State *L)
|
|||||||
int w_Canvas_getFilter(lua_State *L)
|
int w_Canvas_getFilter(lua_State *L)
|
||||||
{
|
{
|
||||||
Canvas *canvas = luax_checkcanvas(L, 1);
|
Canvas *canvas = luax_checkcanvas(L, 1);
|
||||||
Image::Filter f = canvas->getFilter();
|
const Image::Filter f = canvas->getFilter();
|
||||||
|
|
||||||
const char *minstr;
|
const char *minstr;
|
||||||
const char *magstr;
|
const char *magstr;
|
||||||
@@ -109,21 +104,16 @@ int w_Canvas_setWrap(lua_State *L)
|
|||||||
{
|
{
|
||||||
Canvas *canvas = luax_checkcanvas(L, 1);
|
Canvas *canvas = luax_checkcanvas(L, 1);
|
||||||
|
|
||||||
Image::WrapMode s;
|
Image::Wrap w;
|
||||||
Image::WrapMode t;
|
|
||||||
|
|
||||||
const char *sstr = luaL_checkstring(L, 2);
|
const char *sstr = luaL_checkstring(L, 2);
|
||||||
const char *tstr = luaL_optstring(L, 3, sstr);
|
const char *tstr = luaL_optstring(L, 3, sstr);
|
||||||
|
|
||||||
if (!Image::getConstant(sstr, s))
|
if (!Image::getConstant(sstr, w.s))
|
||||||
return luaL_error(L, "Invalid wrap mode: %s", sstr);
|
return luaL_error(L, "Invalid wrap mode: %s", sstr);
|
||||||
if (!Image::getConstant(tstr, t))
|
if (!Image::getConstant(tstr, w.t))
|
||||||
return luaL_error(L, "Invalid wrap mode, %s", tstr);
|
return luaL_error(L, "Invalid wrap mode, %s", tstr);
|
||||||
|
|
||||||
Image::Wrap w;
|
|
||||||
w.s = s;
|
|
||||||
w.t = t;
|
|
||||||
|
|
||||||
canvas->setWrap(w);
|
canvas->setWrap(w);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -132,7 +122,7 @@ int w_Canvas_setWrap(lua_State *L)
|
|||||||
int w_Canvas_getWrap(lua_State *L)
|
int w_Canvas_getWrap(lua_State *L)
|
||||||
{
|
{
|
||||||
Canvas *canvas = luax_checkcanvas(L, 1);
|
Canvas *canvas = luax_checkcanvas(L, 1);
|
||||||
Image::Wrap w = canvas->getWrap();
|
const Image::Wrap w = canvas->getWrap();
|
||||||
|
|
||||||
const char *wrap_s;
|
const char *wrap_s;
|
||||||
const char *wrap_t;
|
const char *wrap_t;
|
||||||
|
|||||||
@@ -94,21 +94,16 @@ int w_Font_setFilter(lua_State *L)
|
|||||||
{
|
{
|
||||||
Font *t = luax_checkfont(L, 1);
|
Font *t = luax_checkfont(L, 1);
|
||||||
|
|
||||||
Image::FilterMode min;
|
Image::Filter f;
|
||||||
Image::FilterMode mag;
|
|
||||||
|
|
||||||
const char *minstr = luaL_checkstring(L, 2);
|
const char *minstr = luaL_checkstring(L, 2);
|
||||||
const char *magstr = luaL_optstring(L, 3, minstr);
|
const char *magstr = luaL_optstring(L, 3, minstr);
|
||||||
|
|
||||||
if (!Image::getConstant(minstr, min))
|
if (!Image::getConstant(minstr, f.min))
|
||||||
return luaL_error(L, "Invalid filter mode: %s", minstr);
|
return luaL_error(L, "Invalid filter mode: %s", minstr);
|
||||||
if (!Image::getConstant(magstr, mag))
|
if (!Image::getConstant(magstr, f.mag))
|
||||||
return luaL_error(L, "Invalid filter mode: %s", magstr);
|
return luaL_error(L, "Invalid filter mode: %s", magstr);
|
||||||
|
|
||||||
Image::Filter f;
|
|
||||||
f.min = min;
|
|
||||||
f.mag = mag;
|
|
||||||
|
|
||||||
t->setFilter(f);
|
t->setFilter(f);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -117,13 +112,11 @@ int w_Font_setFilter(lua_State *L)
|
|||||||
int w_Font_getFilter(lua_State *L)
|
int w_Font_getFilter(lua_State *L)
|
||||||
{
|
{
|
||||||
Font *t = luax_checkfont(L, 1);
|
Font *t = luax_checkfont(L, 1);
|
||||||
Image::Filter f = t->getFilter();
|
const Image::Filter f = t->getFilter();
|
||||||
Image::FilterMode min = f.min;
|
|
||||||
Image::FilterMode mag = f.mag;
|
|
||||||
const char *minstr;
|
const char *minstr;
|
||||||
const char *magstr;
|
const char *magstr;
|
||||||
Image::getConstant(min, minstr);
|
Image::getConstant(f.min, minstr);
|
||||||
Image::getConstant(mag, magstr);
|
Image::getConstant(f.mag, magstr);
|
||||||
lua_pushstring(L, minstr);
|
lua_pushstring(L, minstr);
|
||||||
lua_pushstring(L, magstr);
|
lua_pushstring(L, magstr);
|
||||||
return 2;
|
return 2;
|
||||||
|
|||||||
@@ -88,12 +88,12 @@ int w_Image_getFilter(lua_State *L)
|
|||||||
const Image::Filter f = t->getFilter();
|
const Image::Filter f = t->getFilter();
|
||||||
const char *minstr;
|
const char *minstr;
|
||||||
const char *magstr;
|
const char *magstr;
|
||||||
const char *mipmapstr;
|
|
||||||
Image::getConstant(f.min, minstr);
|
Image::getConstant(f.min, minstr);
|
||||||
Image::getConstant(f.mag, magstr);
|
Image::getConstant(f.mag, magstr);
|
||||||
lua_pushstring(L, minstr);
|
lua_pushstring(L, minstr);
|
||||||
lua_pushstring(L, magstr);
|
lua_pushstring(L, magstr);
|
||||||
|
|
||||||
|
const char *mipmapstr;
|
||||||
if (Image::getConstant(f.mipmap, mipmapstr))
|
if (Image::getConstant(f.mipmap, mipmapstr))
|
||||||
lua_pushstring(L, mipmapstr);
|
lua_pushstring(L, mipmapstr);
|
||||||
else
|
else
|
||||||
@@ -147,10 +147,7 @@ int w_Image_setMipmapSharpness(lua_State *L)
|
|||||||
int w_Image_getMipmapSharpness(lua_State *L)
|
int w_Image_getMipmapSharpness(lua_State *L)
|
||||||
{
|
{
|
||||||
Image *i = luax_checkimage(L, 1);
|
Image *i = luax_checkimage(L, 1);
|
||||||
|
lua_pushnumber(L, i->getMipmapSharpness());
|
||||||
float sharpness = i->getMipmapSharpness();
|
|
||||||
lua_pushnumber(L, sharpness);
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user