/* * SDL.createRGBSurfaceFrom(pixels, * width, * height, * depth, * pitch, * rmask, * gmask, * bmask, * amask) * * Create a surface object and return it. * * Returns: * The surface or nil * The error message */ static int l_surface_createRGBFrom(lua_State *L) { char *pixels = strdup(luaL_checkstring(L, 1)); int width = luaL_checkinteger(L, 2); int height = luaL_checkinteger(L, 3); int depth = luaL_checkinteger(L, 4); int pitch = luaL_checkinteger(L, 5); int rmask = luaL_checkinteger(L, 6); int gmask = luaL_checkinteger(L, 7); int bmask = luaL_checkinteger(L, 8); int amask = luaL_checkinteger(L, 9); /* * TODO: we need to store the pixels into the metatable and free * them after the surface. */ SDL_Surface *s = SDL_CreateRGBSurfaceFrom(pixels, width, height, depth, pitch, rmask, gmask, bmask, amask); if (s == NULL) return commonPushSDLError(L, 1); return commonPush(L, "p", SurfaceName, s); }
/* * SDL.numJoysticks() * * Returns: * The number of joysticks or nil on failure * The error message */ static int l_numJoysticks(lua_State *L) { int ret = SDL_NumJoysticks(); if (ret < 0) return commonPushSDLError(L, 1); return commonPush(L, "i", ret); }
/* * SDL.registerEvents(num) * * Arguments: * num the number of events to register * * Returns: * The beginning number */ static int l_event_registerEvents(lua_State *L) { int num = luaL_checkinteger(L, 1); Uint32 ret; if ((ret = SDL_RegisterEvents(num)) == (Uint32)-1) return commonPushSDLError(L, 1); return commonPush(L, "i", ret); }
/* * SDL.loadBMP(path) * * Load a BMP surface and return it * * Params: * path the path to the file * * Returns: * The surface or nil * The error message */ static int l_surface_loadBMP(lua_State *L) { const char *path = luaL_checkstring(L, 1); SDL_Surface *s; s = SDL_LoadBMP(path); if (s == NULL) return commonPushSDLError(L, 1); return commonPush(L, "p", SurfaceName, s); }
/* * SDL.glGetAttribute(attribute) * * Arguments: * attribute the attribute to get * * Returns: * The value or nil on failure * The error message */ static int l_glGetAttribute(lua_State *L) { int attr = luaL_checkinteger(L, 1); int value; if (SDL_GL_GetAttribute(attr, &value) < 0) return commonPushSDLError(L, 1); return commonPush(L, "i", value); }
/* * SDL.glSetAttribute(attribute, value) * * Arguments: * attribute the attribute to set * value the value * * Returns: * True on success or nil on error * The error message */ static int l_glSetAttribute(lua_State *L) { int attr = luaL_checkinteger(L, 1); int value = luaL_checkinteger(L, 2); if (SDL_GL_SetAttribute(attr, value) < 0) return commonPushSDLError(L, 1); return commonPush(L, "b", 1); }
/* * SDL.gameControllerAddMapping(name) * * Arguments: * name the mapping string * * Returns: * 1, 0 or -1 * The error message */ static int l_gameControllerAddMapping(lua_State *L) { const char *mapping = luaL_checkstring(L, 1); int ret; ret = SDL_GameControllerAddMapping(mapping); if (ret < 0) return commonPushSDLError(L, 1); return commonPush(L, "i", ret); }
static int init(lua_State *L, InitFunc func) { Uint32 flags = 0; if (lua_gettop(L) >= 1 && lua_type(L, 1) == LUA_TTABLE) flags = commonGetEnum(L, 1); if (func(flags) == -1) return commonPushSDLError(L, 1); return commonPush(L, "b", 1); }
/* * SDL.gameControllerNameForIndex(index) * * Arguments: * index the controller index * * Returns: * The name or nil on failure * The error message */ static int l_gameControllerNameForIndex(lua_State *L) { int index = luaL_checkinteger(L, 1); const char *name; name = SDL_GameControllerNameForIndex(index); if (name == NULL) return commonPushSDLError(L, 1); return commonPush(L, "s", name); }
/* * SDL.joystickOpen(index) * * Arguments: * index the joystick index * * Returns: * The joystick object or nil on failure * The error message */ static int l_joystickOpen(lua_State *L) { int index = luaL_checkinteger(L, 1); SDL_Joystick *j; j = SDL_JoystickOpen(index); if (j == NULL) return commonPushSDLError(L, 1); return commonPush(L, "p", JoystickName, j); }
/* * SDL.joystickEventState(index) * * Arguments: * state the state * * Returns: * The status or nil on failure * The error message */ static int l_joystickEventState(lua_State *L) { int state = luaL_checkinteger(L, 1); int ret; ret = SDL_JoystickEventState(state); if (ret < 0) return commonPushSDLError(L, 1); return commonPush(L, "i", ret); }
/* * SDL.gameControllerOpen(index) * * Arguments: * index the controller index * * Returns: * The controller object or nil on failure * The error message */ static int l_gameControllerOpen(lua_State *L) { int index = luaL_checkinteger(L, 1); SDL_GameController *c; c = SDL_GameControllerOpen(index); if (c == NULL) return commonPushSDLError(L, 1); return commonPush(L, "p", GameCtlName, c); }
/* * Image.load(path) * * Arguments: * path the path to the image * * Returns: * The surface or nil on failure * The error message */ static int l_image_load(lua_State *L) { const char *path = luaL_checkstring(L, 1); SDL_Surface *surf; surf = IMG_Load(path); if (surf == NULL) return commonPushSDLError(L, 1); return commonPush(L, "p", SurfaceName, surf); }
/* * SDL.createRGBSurfaceWithFormat( * width, * height, * depth = 32, * format = SDL.pixelFormat.RGBA32) * * Create a surface object with the specified format and return it. * * Returns: * The surface or nil * The error message */ static int l_surface_createRGBWithFormat(lua_State *L) { int width = luaL_checkinteger(L, 1); int height = luaL_checkinteger(L, 2); int depth = luaL_optinteger(L, 3, 32); int format = luaL_optinteger(L, 4, SDL_PIXELFORMAT_RGBA32); SDL_Surface *s; s = SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, format); if (s == NULL) return commonPushSDLError(L, 1); return commonPush(L, "p", SurfaceName, s); }
/* * SDL.createThread(name, source) * * Create a separate thread of execution. It creates a new Lua state that * does not share any data frmo the parent process. * * Arguments: * name, the thread name * source, a path to a Lua file or a function to call * * Returns: * The thread object or nil * The error message */ static int l_thread_create(lua_State *L) { const char *name = luaL_checkstring(L, 1); int ret, iv; LuaThread *thread; if ((thread = calloc(1, sizeof (LuaThread))) == NULL) return commonPushErrno(L, 1); thread->L = luaL_newstate(); luaL_openlibs(thread->L); ret = threadDump(L, thread->L, 2); /* If return number is 2, it is nil and the error */ if (ret == 2) goto failure; /* Iterate over the arguments to pass to the callback */ for (iv = 3; iv <= lua_gettop(L); ++iv) { Variant *v = variantGet(L, iv); if (v == NULL) { commonPushErrno(L, 1); goto failure; } variantPush(thread->L, v); variantFree(v); } thread->ptr = SDL_CreateThread((SDL_ThreadFunction)callback, name, thread); if (thread->ptr == NULL) { commonPushSDLError(L, 1); goto failure; } SDL_AtomicIncRef(&thread->ref); return commonPush(L, "p", ThreadName, thread); failure: lua_close(thread->L); free(thread); return 2; }
/* * SDL.waitEvent(timeout) * * Arguments: * timeout (optional) the timeout * * Returns: * The event or nil on failure * The error message */ static int l_event_waitEvent(lua_State *L) { SDL_Event ev; int timeout, ret; if (lua_gettop(L) >= 1) { timeout = luaL_checkinteger(L, 1); ret = SDL_WaitEventTimeout(&ev, timeout); } else { ret = SDL_WaitEvent(&ev); } if (!ret) return commonPushSDLError(L, 1); eventPush(L, &ev); return 1; }
/* * SDL.peepEvents(count, action, first, last) * * Arguments: * count number of events to process * action the action to execute * min the minimal event * max the maximal event * * Returns: * A sequence table of events or nil on failure * The error message */ static int l_event_peepEvents(lua_State *L) { int count = luaL_checkinteger(L, 1); int action = luaL_checkinteger(L, 2); int first = SDL_FIRSTEVENT; int last = SDL_LASTEVENT; int ret, toreturn; SDL_Event *events; /* * First and last are defaulted to SDL_FIRSTEVENT and SDL_LASTEVENT. */ if (lua_gettop(L) >= 3) first = luaL_checkinteger(L, 3); if (lua_gettop(L) >= 4) last = luaL_checkinteger(L, 4); if ((events = calloc(sizeof (SDL_Event), count)) == NULL) return commonPushErrno(L, 1); ret = SDL_PeepEvents(events, count, action, first, last); if (ret >= 0) { int i; lua_createtable(L, ret, ret); for (i = 0; i < ret; ++i) { eventPush(L, &events[i]); lua_rawseti(L, -2, i + 1); } toreturn = 1; } else toreturn = commonPushSDLError(L, 1); free(events); return toreturn; }
/* * SDL.createRGBSurface(width, * height, * depth = 32, * rmask = endian-dependant, * gmask = endian-dependant, * bmask = endian-dependant, * amask = endian-dependant) * * Create a surface object and return it. * * Returns: * The surface or nil * The error message */ static int l_surface_createRGB(lua_State *L) { int width = luaL_checkinteger(L, 1); int height = luaL_checkinteger(L, 2); int depth = 32; Uint32 rmask, gmask, bmask, amask; SDL_Surface *s; #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif if (lua_gettop(L) >= 3) depth = luaL_checkinteger(L, 3); if (lua_gettop(L) >= 4) rmask = luaL_checkinteger(L, 4); if (lua_gettop(L) >= 5) gmask = luaL_checkinteger(L, 5); if (lua_gettop(L) >= 6) bmask = luaL_checkinteger(L, 6); if (lua_gettop(L) >= 7) amask = luaL_checkinteger(L, 7); s = SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask); if (s == NULL) return commonPushSDLError(L, 1); return commonPush(L, "p", SurfaceName, s); }