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 return 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 return NLUA_ERROR( L, _(
"Spritesheet dimensions must be positive") );
222 if (ld->
type != LUADATA_NUMBER)
223 return NLUA_ERROR( L, _(
"Data has invalid type for texture") );
225 return NLUA_ERROR( L, _(
"Texture dimensions don't match data size!") );
226 SDL_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 return NLUA_ERROR( L, _(
"Spritesheet dimensions must be positive") );
248 rw = PHYSFSRWOPS_openRead( lf->
path );
250 return 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;
321 s = luaL_checkstring(L,1);
322 rw = PHYSFSRWOPS_openRead( s );
324 return NLUA_ERROR(L, _(
"problem opening file '%s' for reading"), s );
327 surface = IMG_Load_RW( rw, 1 );
329 return NLUA_ERROR(L, _(
"problem opening image for reading") );
332 SDL_LockSurface( surface );
333 size = surface->w * surface->h;
334 ld.
elem =
sizeof(float);
336 ld.
data = calloc( ld.
elem*4, size );
337 ld.
type = LUADATA_NUMBER;
338 data = (
float*)ld.
data;
339 for (
int i=0; i<surface->h; i++) {
340 for (
int j=0; j<surface->w; j++) {
341 pix = get_pixel( surface, j, i );
342 SDL_GetRGBA( pix, surface->format, &r, &g, &b, &a );
343 size_t pos = 4*((surface->h-i-1)*surface->w+j);
344 data[ pos+0 ] = ((float)r)/255.;
345 data[ pos+1 ] = ((float)g)/255.;
346 data[ pos+2 ] = ((float)b)/255.;
347 data[ pos+3 ] = ((float)a)/255.;
350 SDL_UnlockSurface( surface );
354 lua_pushinteger(L, surface->w);
355 lua_pushinteger(L, surface->h);
358 SDL_FreeSurface( surface );
373 const char *filename = luaL_checkstring(L,2);
377 SDL_Surface *surface;
382 len = w * h * 4 *
sizeof(GLubyte);
383 data = malloc( len );
386 glBindTexture( GL_TEXTURE_2D, tex->
texture );
387 glGetTexImage( GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
391 surface = SDL_CreateRGBSurface( 0, w, h, 32, RGBAMASK );
392 for (
int i=0; i<h; i++)
393 memcpy( (GLubyte*)surface->pixels + i * (4*w), &data[ (h - i - 1) * (4*w) ], 4*w );
399 if (!(rw = PHYSFSRWOPS_openWrite( filename )))
400 return NLUA_ERROR(L,_(
"Unable to open '%s' for writing!"),filename);
402 IMG_SavePNG_RW( surface, rw, 1 );
404 SDL_FreeSurface( surface );
406 lua_pushboolean(L,1);
427 lua_pushnumber( L, tex->
w );
428 lua_pushnumber( L, tex->
h );
429 lua_pushnumber( L, tex->
sw );
430 lua_pushnumber( L, tex->
sh );
450 lua_pushnumber( L, tex->
sx * tex->
sy );
451 lua_pushnumber( L, tex->
sx );
452 lua_pushnumber( L, tex->
sy );
472 double a = luaL_checknumber( L, 2 );
475 if ((a >= 2.*M_PI) || (a < 0.)) {
476 a = fmod( a, 2.*M_PI );
483 lua_pushinteger( L, sx+1 );
484 lua_pushinteger( L, sy+1 );
499 const char *smin = luaL_checkstring(L,2);
500 const char *smag = luaL_optstring(L,3,smin);
506 if (min==0 || mag==0)
507 NLUA_INVALID_PARAMETER(L,2);
509 glBindTexture( GL_TEXTURE_2D, tex->
texture );
510 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag );
511 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min );
529 const char *shoriz = luaL_checkstring(L,2);
530 const char *svert = luaL_optstring(L,3,shoriz);
531 const char *sdepth = luaL_optstring(L,4,shoriz);
532 GLint horiz, vert, depth;
538 if (horiz==0 || vert==0 || depth==0)
539 NLUA_INVALID_PARAMETER(L,2);
541 glBindTexture( GL_TEXTURE_2D, tex->
texture );
542 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, horiz );
543 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, vert );
544 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.
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.