Пример #1
0
/*
 * SDL.glResetAttributes()
 *
 * Returns:
 *	true on success; false if the function is not available
 */
static int
l_glResetAttributes(lua_State *L)
{
	if (!SDL_VERSION_ATLEAST(2, 0, 2))
		return commonPush(L, "b", 0);

	SDL_GL_ResetAttributes();
	return commonPush(L, "b", 1);
}
Пример #2
0
/*
 * Image.init(flags)
 *
 * Arguments:
 *	flags the formats requested
 *
 * Returns:
 *	The initialized formats
 *	Nil if some have not been supported
 *	The error message
 */
static int
l_image_init(lua_State *L)
{
	int flags = commonGetEnum(L, 1);
	int ret;

	ret = IMG_Init(flags);
	commonPushEnum(L, ret, ImageFlags);

	if ((ret & flags) != flags)
		return commonPush(L, "n s", IMG_GetError()) + 1;

	return commonPush(L, "b", 1) + 1;
}
Пример #3
0
/*
 * 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);
}
Пример #4
0
/*
 * SDL.hasEvent(type)
 *
 * Arguments:
 *	type the event type
 *
 * Returns:
 *	True if has event
 */
static int
l_event_hasEvent(lua_State *L)
{
	int type = luaL_checkinteger(L, 1);

	return commonPush(L, "b", SDL_HasEvent(type));
}
Пример #5
0
/*
 * SDL.isGameController(index)
 *
 * Arguments:
 *	index the controller index
 *
 * Returns:
 *	True if is a game controller
 */
static int
l_isGameController(lua_State *L)
{
	int index = luaL_checkinteger(L, 1);

	return commonPush(L, "b", SDL_IsGameController(index));
}
Пример #6
0
/*
 * SDL.getHintBoolean(name[, default])
 *
 * Get a hint as a boolean value.
 * If the hint does not exist, returns the 'truthyness'
 * of the 'default' argument.
 *
 * Arguments:
 *	The name of the hint to query.
 *	The default value to return if the hint does not exist.
 *
 * Returns:
 *	The hint value as a boolean.
 */
static int
l_getHintBoolean(lua_State *L)
{
	const char *name	= luaL_checkstring(L, 1);
	int val			= lua_toboolean(L, 2);

	return commonPush(L, "b", SDL_GetHintBoolean(name, val));
}
Пример #7
0
/*
 * SDL.eventState(type, state)
 *
 * Arguments:
 *	type the type of event
 *	state the state -1, 0 or 1
 *
 * Returns:
 *	The current state or the new state
 */
static int
l_event_eventState(lua_State *L)
{
	int type	= luaL_checkinteger(L, 1);
	int state	= luaL_checkinteger(L, 2);

	return commonPush(L, "i", SDL_EventState(type, state));
}
Пример #8
0
/*
 * SDL.hasEvents(min, max)
 *
 * Arguments:
 *	min the minimum event type
 *	max the maximum event type
 *
 * Returns:
 *	True if has events
 */
static int
l_event_hasEvents(lua_State *L)
{
	int min = luaL_checkinteger(L, 1);
	int max = luaL_checkinteger(L, 2);

	return commonPush(L, "b", SDL_HasEvents(min, max));
}
Пример #9
0
/*
 * SDL.setHint(name, value)
 *
 * Arguments:
 *	name the hint name
 *	value the hint value
 *
 * Returns:
 *	True on success or false
 *	The error message
 */
static int
l_setHint(lua_State *L)
{
	const char *name	= luaL_checkstring(L, 1);
	const char *value	= luaL_checkstring(L, 2);

	return commonPush(L, "b", SDL_SetHint(name, value));
}
Пример #10
0
/*
 * SDL.rectEmpty(rect)
 *
 * Arguments:
 *	rect the rectangle
 *
 * Returns:
 *	True if empty
 */
static int
l_rectEmpty(lua_State *L)
{
	SDL_Rect rect;

	videoGetRect(L, 1, &rect);

	return commonPush(L, "b", SDL_RectEmpty(&rect));
}
Пример #11
0
/*
 * SDL.setHint(name, value, priority)
 *
 * Arguments:
 *	name the hint name
 *	value the hint value
 *	priority the hint priority - defaults to SDL.hintPriority.Normal.
 *
 * Returns:
 *	True on success or false
 *	The error message
 */
static int
l_setHint(lua_State *L)
{
	const char *name		= luaL_checkstring(L, 1);
	const char *value		= luaL_checkstring(L, 2);
	const SDL_HintPriority priority	= luaL_optinteger(L, 3, SDL_HINT_DEFAULT);

	return commonPush(L, "b", SDL_SetHintWithPriority(name, value, priority));
}
Пример #12
0
/*
 * SDL.hasIntersection(r1, r2)
 *
 * Arguments:
 *	r1 the first rectangle
 *	r2 the second rectangle
 *
 * Returns:
 *	True on intersections
 */
static int
l_hasIntersection(lua_State *L)
{
	SDL_Rect a, b;

	videoGetRect(L, 1, &a);
	videoGetRect(L, 2, &b);

	return commonPush(L, "b", SDL_HasIntersection(&a, &b));
}
Пример #13
0
/*
 * 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);
}
Пример #14
0
/*
 * SDL.rectEquals(r1, r2)
 *
 * Arguments:
 *	r1 the first rectangle
 *	r2 the second rectangle
 *
 * Returns:
 *	True if equals
 */
static int
l_rectEquals(lua_State *L)
{
	SDL_Rect a, b;

	videoGetRect(L, 1, &a);
	videoGetRect(L, 2, &a);

	return commonPush(L, "b", SDL_RectEquals(&a, &b));
}
Пример #15
0
/*
 * 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);
}
Пример #16
0
/*
 * 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);
}
Пример #17
0
/*
 * 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);
}
Пример #18
0
/*
 * 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);
}
Пример #19
0
/*
 * 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);
}
Пример #20
0
/*
 * 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);
}
Пример #21
0
/*
 * 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);
}
Пример #22
0
/*
 * 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);
}
Пример #23
0
/*
 * 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);
}
Пример #24
0
/*
 * 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);
}
Пример #25
0
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);
}
Пример #26
0
/*
 * 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);
}
Пример #27
0
/*
 * 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;
}
Пример #28
0
/*
 * SDL.intersectRectAndLine(rect, x1, y1, x2, y2)
 *
 * Arguments:
 *	rect the rectangle
 *	x1 the starting x
 *	y1 the starting y
 *	x2 the ending x
 *	y2 the ending y
 *
 * Returns:
 *	The rectangle
 *	The updated x1,
 *	The updated y1,
 *	The updated x2,
 *	The updated y2
 */
static int
l_intersectRectAndLine(lua_State *L)
{
	SDL_Rect rect;
	int x1, y1, x2, y2, rvalue;

	videoGetRect(L, 1, &rect);
	x1 = luaL_checkinteger(L, 2);
	y1 = luaL_checkinteger(L, 3);
	x2 = luaL_checkinteger(L, 4);
	y2 = luaL_checkinteger(L, 5);

	rvalue = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);

	return commonPush(L, "biiii", rvalue, x1, y1, x2, y2);
}
Пример #29
0
/*
 * 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);
}
Пример #30
0
/*
 * SDL.getError()
 */
static int
l_getError(lua_State *L)
{
	return commonPush(L, "s", SDL_GetError());
}