17#include "physfsrwops.h"
27static int nlua_tex_counter = 0;
30static inline uint32_t get_pixel(SDL_Surface *surface,
int x,
int y);
91 return *((
glTexture**) lua_touserdata(L,ind));
104 luaL_typerror(L, ind, TEX_METATABLE);
134 luaL_getmetatable(L, TEX_METATABLE);
135 lua_setmetatable(L, -2);
149 if (lua_getmetatable(L,ind)==0)
151 lua_getfield(L, LUA_REGISTRYINDEX, TEX_METATABLE);
154 if (lua_rawequal(L, -1, -2))
214 w = luaL_checkinteger(L,2);
215 h = luaL_checkinteger(L,3);
216 if ((w < 0 ) || (h < 0))
217 NLUA_ERROR( L, _(
"Texture dimensions must be positive") );
218 sx = luaL_optinteger(L,4,1);
219 sy = luaL_optinteger(L,5,1);
220 if ((sx < 0 ) || (sy < 0))
221 NLUA_ERROR( L, _(
"Spritesheet dimensions must be positive") );
222 if (ld->
type != LUADATA_NUMBER)
223 NLUA_ERROR( L, _(
"Data has invalid type for texture") );
225 NLUA_ERROR( L, _(
"Texture dimensions don't match data size!") );
226 asprintf( &name,
"nlua_texture_%03d", ++nlua_tex_counter );
227 tex = gl_loadImageData( (
void*)ld->
data, w, h, sx, sy, name );
237 path = luaL_checkstring(L, 1);
239 sx = luaL_optinteger(L,2,1);
240 sy = luaL_optinteger(L,3,1);
241 if ((sx < 0 ) || (sy < 0))
242 NLUA_ERROR( L, _(
"Spritesheet dimensions must be positive") );
248 rw = PHYSFSRWOPS_openRead( lf->
path );
250 NLUA_ERROR(L,
"Unable to open '%s'", lf->
path );
263static inline uint32_t get_pixel(SDL_Surface *surface,
int x,
int y)
265 int bpp = surface->format->BytesPerPixel;
267 uint8_t *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
279 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
280 return p[0] << 16 | p[1] << 8 | p[2];
282 return p[0] | p[1] << 8 | p[2] << 16;
307 SDL_Surface *surface;
322 s = luaL_checkstring(L,1);
323 rw = PHYSFSRWOPS_openRead( s );
325 NLUA_ERROR(L, _(
"problem opening file '%s' for reading"), s );
328 surface = IMG_Load_RW( rw, 1 );
330 NLUA_ERROR(L, _(
"problem opening image for reading") );
333 SDL_LockSurface( surface );
334 size = surface->w * surface->h;
335 ld.
elem =
sizeof(float);
337 ld.
data = calloc( ld.
elem*4, size );
338 ld.
type = LUADATA_NUMBER;
339 data = (
float*)ld.
data;
340 for (i=0; i<surface->h; i++) {
341 for (j=0; j<surface->w; j++) {
342 pix = get_pixel( surface, j, i );
343 SDL_GetRGBA( pix, surface->format, &r, &g, &b, &a );
344 size_t pos = 4*((surface->h-i-1)*surface->w+j);
345 data[ pos+0 ] = ((float)r)/255.;
346 data[ pos+1 ] = ((float)g)/255.;
347 data[ pos+2 ] = ((float)b)/255.;
348 data[ pos+3 ] = ((float)a)/255.;
351 SDL_UnlockSurface( surface );
355 lua_pushinteger(L, surface->w);
356 lua_pushinteger(L, surface->h);
359 SDL_FreeSurface( surface );
374 const char *filename = luaL_checkstring(L,2);
378 SDL_Surface *surface;
383 len = w * h * 4 *
sizeof(GLubyte);
384 data = malloc( len );
387 glBindTexture( GL_TEXTURE_2D, tex->
texture );
388 glGetTexImage( GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
392 surface = SDL_CreateRGBSurface( 0, w, h, 32, RGBAMASK );
393 for (
int i=0; i<h; i++)
394 memcpy( (GLubyte*)surface->pixels + i * (4*w), &data[ (h - i - 1) * (4*w) ], 4*w );
400 if (!(rw = PHYSFSRWOPS_openWrite( filename )))
401 NLUA_ERROR(L,_(
"Unable to open '%s' for writing!"),filename);
403 IMG_SavePNG_RW( surface, rw, 1 );
405 SDL_FreeSurface( surface );
407 lua_pushboolean(L,1);
428 lua_pushnumber( L, tex->
w );
429 lua_pushnumber( L, tex->
h );
430 lua_pushnumber( L, tex->
sw );
431 lua_pushnumber( L, tex->
sh );
451 lua_pushnumber( L, tex->
sx * tex->
sy );
452 lua_pushnumber( L, tex->
sx );
453 lua_pushnumber( L, tex->
sy );
474 double a = luaL_checknumber( L, 2 );
477 if ((a >= 2.*M_PI) || (a < 0.)) {
478 a = fmod( a, 2.*M_PI );
485 lua_pushinteger( L, sx+1 );
486 lua_pushinteger( L, sy+1 );
501 const char *smin = luaL_checkstring(L,2);
502 const char *smag = luaL_optstring(L,3,smin);
508 if (min==0 || mag==0)
509 NLUA_INVALID_PARAMETER(L);
511 glBindTexture( GL_TEXTURE_2D, tex->
texture );
512 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag );
513 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min );
531 const char *shoriz = luaL_checkstring(L,2);
532 const char *svert = luaL_optstring(L,3,shoriz);
533 const char *sdepth = luaL_optstring(L,4,shoriz);
534 GLint horiz, vert, depth;
540 if (horiz==0 || vert==0 || depth==0)
541 NLUA_INVALID_PARAMETER(L);
543 glBindTexture( GL_TEXTURE_2D, tex->
texture );
544 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, horiz );
545 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, vert );
546 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, depth );
Header file with generic functions and naev-specifics.
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.
int asprintf(char **strp, const char *fmt,...)
Like sprintf(), but it allocates a large-enough string and returns the pointer in the first argument....
GLint gl_stringToFilter(const char *s)
Gets the associated min/mag filter from a string.
GLint 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, const glTexture *t, const 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.