14#include "physfsrwops.h"
24static int nlua_tex_counter = 0;
27static inline uint32_t get_pixel( SDL_Surface *surface,
int x,
int y );
88 return *( (
glTexture **)lua_touserdata( L, ind ) );
101 luaL_typerror( L, ind, TEX_METATABLE );
119 luaL_checkstring( L, ind ) );
133 luaL_getmetatable( L, TEX_METATABLE );
134 lua_setmetatable( L, -2 );
148 if ( lua_getmetatable( L, ind ) == 0 )
150 lua_getfield( L, LUA_REGISTRYINDEX, TEX_METATABLE );
153 if ( lua_rawequal( L, -1, -2 ) )
170 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
217 w = luaL_checkinteger( L, 2 );
218 h = luaL_checkinteger( L, 3 );
219 if ( ( w < 0 ) || ( h < 0 ) )
220 return NLUA_ERROR( L, _(
"Texture dimensions must be positive" ) );
221 sx = luaL_optinteger( L, 4, 1 );
222 sy = luaL_optinteger( L, 5, 1 );
223 if ( ( sx < 0 ) || ( sy < 0 ) )
224 return NLUA_ERROR( L, _(
"Spritesheet dimensions must be positive" ) );
225 if ( ld->
type != LUADATA_NUMBER )
226 return NLUA_ERROR( L, _(
"Data has invalid type for texture" ) );
227 if ( w * h * ld->
elem * 4 != ld->
size )
228 return NLUA_ERROR( L,
229 _(
"Texture dimensions don't match data size!" ) );
230 SDL_asprintf( &name,
"nlua_texture_%03d", ++nlua_tex_counter );
231 tex = gl_loadImageData( (
void *)ld->
data, w, h, sx, sy, name );
240 path = luaL_checkstring( L, 1 );
242 sx = luaL_optinteger( L, 2, 1 );
243 sy = luaL_optinteger( L, 3, 1 );
244 if ( ( sx < 0 ) || ( sy < 0 ) )
245 return NLUA_ERROR( L, _(
"Spritesheet dimensions must be positive" ) );
251 rw = PHYSFSRWOPS_openRead( lf->
path );
253 return NLUA_ERROR( L,
"Unable to open '%s'", lf->
path );
266static inline uint32_t get_pixel( SDL_Surface *surface,
int x,
int y )
268 int bpp = surface->format->BytesPerPixel;
270 uint8_t *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
282 if ( SDL_BYTEORDER == SDL_BIG_ENDIAN )
283 return p[0] << 16 | p[1] << 8 | p[2];
285 return p[0] | p[1] << 8 | p[2] << 16;
311 SDL_Surface *surface;
324 s = luaL_checkstring( L, 1 );
325 rw = PHYSFSRWOPS_openRead( s );
327 return NLUA_ERROR( L, _(
"problem opening file '%s' for reading" ), s );
330 surface = IMG_Load_RW( rw, 1 );
331 if ( surface == NULL )
332 return NLUA_ERROR( L, _(
"problem opening image for reading" ) );
335 SDL_LockSurface( surface );
336 size = surface->w * surface->h;
337 ld.
elem =
sizeof( float );
339 ld.
data = calloc( ld.
elem * 4, size );
340 ld.
type = LUADATA_NUMBER;
341 data = (
float *)ld.
data;
342 for (
int i = 0; i < surface->h; i++ ) {
343 for (
int j = 0; j < surface->w; j++ ) {
344 pix = get_pixel( surface, j, i );
345 SDL_GetRGBA( pix, surface->format, &r, &g, &b, &a );
346 size_t pos = 4 * ( ( surface->h - i - 1 ) * surface->w + j );
347 data[pos + 0] = ( (float)r ) / 255.;
348 data[pos + 1] = ( (float)g ) / 255.;
349 data[pos + 2] = ( (float)b ) / 255.;
350 data[pos + 3] = ( (float)a ) / 255.;
353 SDL_UnlockSurface( surface );
357 lua_pushinteger( L, surface->w );
358 lua_pushinteger( L, surface->h );
361 SDL_FreeSurface( surface );
376 const char *filename = luaL_checkstring( L, 2 );
380 SDL_Surface *surface;
385 len = w * h * 4 *
sizeof( GLubyte );
386 data = malloc( len );
389 glBindTexture( GL_TEXTURE_2D, tex->
texture );
390 glGetTexImage( GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
392 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
395 surface = SDL_CreateRGBSurface( 0, w, h, 32, RGBAMASK );
396 for (
int i = 0; i < h; i++ )
397 memcpy( (GLubyte *)surface->pixels + i * ( 4 * w ),
398 &data[( h - i - 1 ) * ( 4 * w )], 4 * w );
404 if ( !( rw = PHYSFSRWOPS_openWrite( filename ) ) )
405 return NLUA_ERROR( L, _(
"Unable to open '%s' for writing!" ), filename );
407 IMG_SavePNG_RW( surface, rw, 1 );
409 SDL_FreeSurface( surface );
411 lua_pushboolean( L, 1 );
431 lua_pushnumber( L, tex->
w );
432 lua_pushnumber( L, tex->
h );
433 lua_pushnumber( L, tex->
sw );
434 lua_pushnumber( L, tex->
sh );
453 lua_pushnumber( L, tex->
sx * tex->
sy );
454 lua_pushnumber( L, tex->
sx );
455 lua_pushnumber( L, tex->
sy );
475 double a = luaL_checknumber( L, 2 );
481 lua_pushinteger( L, sx + 1 );
482 lua_pushinteger( L, sy + 1 );
498 const char *smin = luaL_checkstring( L, 2 );
499 const char *smag = luaL_optstring( L, 3, smin );
505 if ( min == 0 || mag == 0 )
506 NLUA_INVALID_PARAMETER( L, 2 );
508 glBindTexture( GL_TEXTURE_2D, tex->
texture );
509 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag );
510 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min );
512 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
532 const char *shoriz = luaL_checkstring( L, 2 );
533 const char *svert = luaL_optstring( L, 3, shoriz );
534 const char *sdepth = luaL_optstring( L, 4, shoriz );
535 GLint horiz, vert, depth;
541 if ( horiz == 0 || vert == 0 || depth == 0 )
542 NLUA_INVALID_PARAMETER( L, 2 );
544 glBindTexture( GL_TEXTURE_2D, tex->
texture );
545 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, horiz );
546 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, vert );
547 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, depth );
549 NLUA_ERROR( L, _(
"OpenGL Error!" ) );
int ndata_getPathDefault(char *path, int len, const char *default_path, const char *filename)
Tries to see if a file is in a default path before seeing if it is an absolute path.
LuaData_t * luaL_checkdata(lua_State *L, int ind)
Gets data at index or raises error if there is no data at index.
int lua_isdata(lua_State *L, int ind)
Checks to see if ind is a data.
LuaData_t * lua_pushdata(lua_State *L, LuaData_t data)
Pushes a data on the stack.
int lua_isfile(lua_State *L, int ind)
Checks to see if ind is a file.
LuaFile_t * luaL_checkfile(lua_State *L, int ind)
Gets file at index or raises error if there is no file at index.
static int texL_dim(lua_State *L)
Gets the dimensions of the texture.
glTexture * lua_totex(lua_State *L, int ind)
Lua bindings to interact with OpenGL textures.
static int texL_setWrap(lua_State *L)
Sets the texture wrapping.
static int texL_readData(lua_State *L)
Reads image data from a file.
int lua_istex(lua_State *L, int ind)
Checks to see if ind is a texture.
static int texL_spriteFromDir(lua_State *L)
Gets the sprite that corresponds to a direction.
glTexture ** lua_pushtex(lua_State *L, glTexture *texture)
Pushes a texture on the stack.
static int texL_new(lua_State *L)
Opens a texture.
glTexture * luaL_checktex(lua_State *L, int ind)
Gets texture at index or raises error if there is no texture at index.
static int texL_writeData(lua_State *L)
Saves texture data as a png.
static int texL_sprites(lua_State *L)
Gets the number of sprites in the texture.
glTexture * luaL_validtex(lua_State *L, int ind, const char *searchpath)
Gets texture directly or from a filename (string) at index or raises error if there is no texture at ...
static int texL_close(lua_State *L)
Frees the texture.
static const luaL_Reg texL_methods[]
static int texL_setFilter(lua_State *L)
Sets the texture minification and magnification filters.
int nlua_loadTex(nlua_env env)
Loads the texture library.
GLenum gl_stringToFilter(const char *s)
Gets the associated min/mag filter from a string.
GLenum gl_stringToClamp(const char *s)
Gets the associated min/mag filter from a string.
glTexture * gl_dupTexture(const glTexture *texture)
Duplicates a texture.
glTexture * gl_newSprite(const char *path, const int sx, const int sy, const unsigned int flags)
Loads the texture immediately, but also sets it as a sprite.
glTexture * gl_newSpriteRWops(const char *path, SDL_RWops *rw, const int sx, const int sy, const unsigned int flags)
Loads the texture immediately, but also sets it as a sprite.
glTexture * gl_newImage(const char *path, const unsigned int flags)
Loads an image as a texture.
void gl_getSpriteFromDir(int *x, int *y, int sx, int sy, double dir)
Sets x and y to be the appropriate sprite for glTexture using dir.
void gl_freeTexture(glTexture *texture)
Frees a texture.
Abstraction for rendering sprite sheets.