Beispiel #1
0
int ApplicationBinder::getOrientation(lua_State* L)
{
	Binder binder(L);
	(void)binder.getInstance("Application", 1);

	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	switch (application->getApplication()->orientation())
	{
	case ePortrait:
		lua_pushstring(L, PORTRAIT);
		break;
	case ePortraitUpsideDown:
		lua_pushstring(L, PORTRAIT_UPSIDE_DOWN);
		break;
	case eLandscapeLeft:
		lua_pushstring(L, LANDSCAPE_LEFT);
		break;
	case eLandscapeRight:
		lua_pushstring(L, LANDSCAPE_RIGHT);
		break;
	}

	return 1;
}
int EventDispatcherBinder::dispatchEvent(lua_State* L)
{
	StackChecker checker(L, "EventDispatcherBinder::dispatchEvent", 0);

	Binder binder(L);
	EventDispatcher* eventDispatcher = static_cast<EventDispatcher*>(binder.getInstance("EventDispatcher", 1));

	luaL_checktype(L, 2, LUA_TTABLE);
//	lua_getfield(L, 2, "type");
	lua_getfield(L, 2, "getType");
	lua_pushvalue(L, 2);
	lua_call(L, 1, 1);
	std::string event = luaL_checkstring(L, -1);
	lua_pop(L, 1);
	LuaEvent e = LuaEvent(LuaEvent::Type(event.c_str()));

    LuaApplication *application = (LuaApplication*)luaL_getdata(L);
    lua_State *mainL = application->getLuaState();

    lua_pushvalue(L, 2);    // push event to main thread
    if (mainL != L)
        lua_xmove(L, mainL, 1);

    eventDispatcher->dispatchEvent(&e);

    lua_pop(mainL, 1);      // pop event from main thread

	return 0;
}
Beispiel #3
0
int ApplicationBinder::setOrientation(lua_State* L)
{
	Binder binder(L);
	(void)binder.getInstance("Application", 1);

	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	const char* orientation = luaL_checkstring(L, 2);

	if (strcmp(orientation, PORTRAIT) == 0)
	{
		application->getApplication()->setOrientation(ePortrait);
	}
	else if (strcmp(orientation, PORTRAIT_UPSIDE_DOWN) == 0)
	{
		application->getApplication()->setOrientation(ePortraitUpsideDown);
	}
	else if (strcmp(orientation, LANDSCAPE_LEFT) == 0)
	{
		application->getApplication()->setOrientation(eLandscapeLeft);
	}
	else if (strcmp(orientation, LANDSCAPE_RIGHT) == 0)
	{
		application->getApplication()->setOrientation(eLandscapeRight);
	}
	else
	{
		GStatus status(2008, "orientation");	// Parameter %s must be one of the accepted values.
		return luaL_error(L, status.errorString());
	}

	return 0;
}
int TileMapBinder::create(lua_State* L)
{
	StackChecker checker(L, "TileMapBinder::create", 1);
	
	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	Binder binder(L);
	
	int width = luaL_checkinteger(L, 1);
	int height = luaL_checkinteger(L, 2);
	GTextureBase* texturebase = static_cast<GTextureBase*>(binder.getInstance("TextureBase", 3));	
	int tilewidth = luaL_checkinteger(L, 4);
	int tileheight = luaL_checkinteger(L, 5);
	int spacingx = luaL_optinteger(L, 6, 0);
	int spacingy = luaL_optinteger(L, 7, 0);
	int marginx = luaL_optinteger(L, 8, 0);
	int marginy = luaL_optinteger(L, 9, 0);
	int displaywidth = luaL_optinteger(L, 10, tilewidth);
	int displayheight = luaL_optinteger(L, 11, tileheight);

	TileMap* tilemap = new TileMap(application->getApplication(),
								   width, height,
								   texturebase,
								   tilewidth, tileheight,
								   spacingx, spacingy,
								   marginx, marginy,
								   displaywidth, displayheight);
	binder.pushInstance("TileMap", tilemap);
		
	return 1;
}
Beispiel #5
0
int ApplicationBinder::isPlayerMode(lua_State* L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);
    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));
    lua_pushboolean(L, application->isPlayerMode());
    return 1;
}
Beispiel #6
0
int ApplicationBinder::getFps(lua_State* L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    lua_pushinteger(L, g_getFps());

    return 1;
}
Beispiel #7
0
int ApplicationBinder::getApiVersion(lua_State* L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    lua_pushliteral(L, GIDEROS_VERSION);

    return 1;
}
Beispiel #8
0
int ApplicationBinder::getLogicalScaleY(lua_State* L)
{
	Binder binder(L);
	(void)binder.getInstance("Application", 1);

	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	lua_pushnumber(L, application->getLogicalScaleY());

	return 1;
}
Beispiel #9
0
int ApplicationBinder::getDeviceHeight(lua_State* L)
{
	Binder binder(L);
	(void)binder.getInstance("Application", 1);

	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	lua_pushnumber(L, application->getHardwareHeight());

	return 1;
}
Beispiel #10
0
int ApplicationBinder::getTextureMemoryUsage(lua_State* L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    lua_pushnumber(L, gtexture_getMemoryUsage() / 1024.0);

    return 1;
}
Beispiel #11
0
int ApplicationBinder::exit(lua_State *L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    extern void g_exit();
    g_exit();

    return 0;
}
Beispiel #12
0
int MeshBinder::create(lua_State *L)
{
    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    bool is3d = lua_toboolean(L, 1);

    Binder binder(L);

    binder.pushInstance("Mesh", new GMesh(application->getApplication(),is3d));

    return 1;
}
Beispiel #13
0
int ViewportBinder::create(lua_State* L)
{
	StackChecker checker(L, "ViewportBinder::create", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    Binder binder(L);

    Viewport* shape = new Viewport(application->getApplication());
	binder.pushInstance("Viewport", shape);

	return 1;
}
Beispiel #14
0
int ShapeBinder::create(lua_State* L)
{
	StackChecker checker(L, "ShapeBinder::create", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    Binder binder(L);

    Shape* shape = new Shape(application->getApplication());
	binder.pushInstance("Shape", shape);

	return 1;
}
Beispiel #15
0
int RenderTargetBinder::create(lua_State *L)
{
    LuaApplication *application = static_cast<LuaApplication*>(luaL_getdata(L));

    Binder binder(L);

    int width = luaL_checkinteger(L, 1);
    int height = luaL_checkinteger(L, 2);
    bool smoothing = lua_toboolean(L, 3);

    binder.pushInstance("RenderTarget", new GRenderTarget(application->getApplication(), width, height, smoothing ? eLinear : eNearest));

    return 1;
}
Beispiel #16
0
int ApplicationBinder::setLogicalDimensions(lua_State* L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    int width = luaL_checkinteger(L, 2);
    int height = luaL_checkinteger(L, 3);

    application->getApplication()->setLogicalDimensions(width, height);

    return 0;
}
Beispiel #17
0
int ApplicationBinder::setScaleMode(lua_State *L)
{
	Binder binder(L);
	(void)binder.getInstance("Application", 1);

	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    const char* scaleMode = luaL_checkstring(L, 2);

    if (strcmp(scaleMode, NO_SCALE) == 0)
	{
        application->getApplication()->setLogicalScaleMode(eNoScale);
    }
    else if (strcmp(scaleMode, CENTER) == 0)
	{
        application->getApplication()->setLogicalScaleMode(eCenter);
    }
    else if (strcmp(scaleMode, PIXEL_PERFECT) == 0)
	{
        application->getApplication()->setLogicalScaleMode(ePixelPerfect);
    }
    else if (strcmp(scaleMode, LETTERBOX) == 0)
	{
        application->getApplication()->setLogicalScaleMode(eLetterBox);
    }
    else if (strcmp(scaleMode, CROP) == 0)
	{
        application->getApplication()->setLogicalScaleMode(eCrop);
    }
    else if (strcmp(scaleMode, STRETCH) == 0)
	{
        application->getApplication()->setLogicalScaleMode(eStretch);
    }
    else if (strcmp(scaleMode, FIT_WIDTH) == 0)
	{
        application->getApplication()->setLogicalScaleMode(eFitWidth);
    }
    else if (strcmp(scaleMode, FIT_HEIGHT) == 0)
	{
        application->getApplication()->setLogicalScaleMode(eFitHeight);
    }
	else
	{
        GStatus status(2008, "scaleMode");	// Parameter %s must be one of the accepted values.
        return luaL_error(L, status.errorString());
	}

	return 0;
}
Beispiel #18
0
int DibBinder::create(lua_State* L)
{
	StackChecker checker(L, "DibBinder::create", 1);

	LuaApplication* luaapplication = static_cast<LuaApplication*>(luaL_getdata(L));
	Application* application = luaapplication->getApplication();

	const char* filename = luaL_checkstring(L, 1);
	
	Binder binder(L);

    Dib* dib = new Dib(application, filename, false, false, false, 0x00000000);
	binder.pushInstance("Dib", dib);

	return 1;
}
Beispiel #19
0
int ApplicationBinder::getContentHeight(lua_State* L)
{
	Binder binder(L);
	(void)binder.getInstance("Application", 1);

	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	Orientation orientation = application->orientation();

	if (orientation == eLandscapeLeft || orientation == eLandscapeRight)
		lua_pushnumber(L, application->getLogicalWidth());
	else
		lua_pushnumber(L, application->getLogicalHeight());

	return 1;
}
Beispiel #20
0
int ApplicationBinder::setBackgroundColor(lua_State* L)
{
	Binder binder(L);
	(void)binder.getInstance("Application", 1);

	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	unsigned int color = luaL_checkinteger(L, 2);

	int r = (color >> 16) & 0xff;
	int g = (color >> 8) & 0xff;
	int b = color& 0xff;

	application->getApplication()->setBackgroundColor(r/255.f, g/255.f, b/255.f);

	return 0;
}
Beispiel #21
0
int ApplicationBinder::configureFrustum(lua_State* L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    lua_Number fov = luaL_checknumber(L, 2);
    if (fov<0) fov=0;
    if (fov>180) fov=180;

    lua_Number farplane=0;
    if (!lua_isnoneornil(L, 3))
    	farplane = luaL_checknumber(L, 3);
    application->getApplication()->configureFrustum(fov,farplane);

    return 0;
}
Beispiel #22
0
int ApplicationBinder::getBackgroundColor(lua_State* L)
{
	Binder binder(L);
	(void)binder.getInstance("Application", 1);

	LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	float r, g, b;
	application->getApplication()->getBackgroundColor(&r, &g, &b);

	int ir = std::min((int)(r * 256), 255);
	int ig = std::min((int)(g * 256), 255);
	int ib = std::min((int)(b * 256), 255);

	lua_pushinteger(L, (ir << 16) | (ig << 8) | ib);

	return 1;
}
Beispiel #23
0
int ApplicationBinder::setFps(lua_State* L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    int fps = luaL_checkinteger(L, 2);

    if (fps != 30 && fps != 60 && fps != -30 && fps != -60)
    {
        GStatus status(2008, "fps");	// Parameter %s must be one of the accepted values.
        return luaL_error(L, status.errorString());
    }

    g_setFps(fps);

    return 0;
}
Beispiel #24
0
int SpriteBinder::create(lua_State* L)
{
	StackChecker checker(L, "Sprite", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

	Binder binder(L);
    Sprite* sprite = new Sprite(application->getApplication());
	binder.pushInstance("Sprite", sprite);

/*	lua_getglobal(L, "Graphics");
	lua_getfield(L, -1, "new");
	lua_remove(L, -2);
	lua_pushvalue(L, -2);
	lua_call(L, 1, 1);
	lua_setfield(L, -2, "graphics"); // sprite.graphics = Graphics.new(sprite)
	*/

	return 1;
}
Beispiel #25
0
int PixelBinder::create(lua_State* L)
{
	StackChecker checker(L, "PixelBinder::create", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    Binder binder(L);

    Pixel* bitmap = new Pixel(application->getApplication());
	unsigned int color = luaL_optinteger(L, 1, 0);
	lua_Number alpha = luaL_optnumber(L, 2, 1.0);
	lua_Number w = luaL_optnumber(L, 3, 1.0);
	lua_Number h = luaL_optnumber(L, 4, w);
	int r = (color >> 16) & 0xff;
	int g = (color >> 8) & 0xff;
	int b = color & 0xff;

	bitmap->setColor(r/255.f,g/255.f,b/255.f,alpha);
	bitmap->setDimensions(w,h);

	binder.pushInstance("Pixel", bitmap);
	return 1;
}
Beispiel #26
0
int ApplicationBinder::getScaleMode(lua_State* L)
{
    Binder binder(L);
    (void)binder.getInstance("Application", 1);

    LuaApplication* application = static_cast<LuaApplication*>(luaL_getdata(L));

    switch (application->getApplication()->getLogicalScaleMode())
    {
    case eNoScale:
        lua_pushstring(L, NO_SCALE);
        break;
    case eCenter:
        lua_pushstring(L, CENTER);
        break;
    case ePixelPerfect:
        lua_pushstring(L, PIXEL_PERFECT);
        break;
    case eLetterBox:
        lua_pushstring(L, LETTERBOX);
        break;
    case eCrop:
        lua_pushstring(L, CROP);
        break;
    case eStretch:
        lua_pushstring(L, STRETCH);
        break;
    case eFitWidth:
        lua_pushstring(L, FIT_WIDTH);
        break;
    case eFitHeight:
        lua_pushstring(L, FIT_HEIGHT);
        break;
    }

    return 1;
}
Beispiel #27
0
int FontBinder::create(lua_State* L)
{
	StackChecker checker(L, "FontBinder::create", 1);

	LuaApplication* luaapplication = static_cast<LuaApplication*>(luaL_getdata(L));
	Application* application = luaapplication->getApplication();

	const char* glympfile = luaL_checkstring(L, 1);
	const char* imagefile = luaL_checkstring(L, 2);
	bool smoothing = lua_toboolean(L, 3) != 0;

	Binder binder(L);
	
    GStatus status;
    Font *font = new Font(application, glympfile, imagefile, smoothing, &status);
    if (status.error())
    {
        delete font;
        return luaL_error(L, status.errorString());
	}

	binder.pushInstance("Font", font);
	return 1;
}
Beispiel #28
0
static int enterFrame(lua_State* L)
{
    StackChecker checker(L, "enterFrame", 0);

    LuaApplication *luaApplication = static_cast<LuaApplication*>(luaL_getdata(L));
    Application *application = luaApplication->getApplication();

	setEnvironTable(L);

	luaL_rawgetptr(L, LUA_REGISTRYINDEX, &key_events);
	luaL_nullifytable(L, -1);
	lua_pop(L, 1);

    gevent_Tick();

    PluginManager& pluginManager = PluginManager::instance();
    for (size_t i = 0; i < pluginManager.plugins.size(); ++i)
        if (pluginManager.plugins[i].enterFrame)
            pluginManager.plugins[i].enterFrame(L);

    application->enterFrame();

	return 0;
}
int TextureBinder::create(lua_State* L)
{
	StackChecker checker(L, "TextureBinder::create", 1);

	LuaApplication* luaapplication = static_cast<LuaApplication*>(luaL_getdata(L));
	Application* application = luaapplication->getApplication();

	const char* filename = luaL_checkstring(L, 1);

	bool smoothing = lua_toboolean(L, 2);

	bool maketransparent = false;
	unsigned int transparentcolor = 0x00000000;
    Wrap wrap = eClamp;
    Format format = eRGBA8888;
	if (!lua_isnoneornil(L, 3))
	{
		if (lua_type(L, 3) != LUA_TTABLE)
			return luaL_typerror(L, 3, "table");

		lua_getfield(L, 3, "transparentColor");
		if (!lua_isnil(L, -1))
		{
			maketransparent = true;
			transparentcolor = luaL_checkinteger(L, -1);
		}
		lua_pop(L, 1);

        lua_getfield(L, 3, "wrap");
        if (!lua_isnil(L, -1))
        {
            const char *wrapstr = luaL_checkstring(L, -1);
            if (strcmp(wrapstr, "clamp") == 0)
                wrap = eClamp;
            else if (strcmp(wrapstr, "repeat") == 0)
                wrap = eRepeat;
            else
            {
                GStatus status(2008, "wrap");		// Error #2008: Parameter %s must be one of the accepted values.
                luaL_error(L, status.errorString());
            }
        }
        lua_pop(L, 1);

        lua_getfield(L, 3, "format");
        if (!lua_isnil(L, -1))
        {
            const char *formatstr = luaL_checkstring(L, -1);
            if (strcmp(formatstr, "rgba8888") == 0)
                format = eRGBA8888;
            else if (strcmp(formatstr, "rgb888") == 0)
                format = eRGB888;
            else if (strcmp(formatstr, "rgb565") == 0)
                format = eRGB565;
            else if (strcmp(formatstr, "rgba4444") == 0)
                format = eRGBA4444;
            else if (strcmp(formatstr, "rgba5551") == 0)
                format = eRGBA5551;
            else
            {
                GStatus status(2008, "format");		// Error #2008: Parameter %s must be one of the accepted values.
                luaL_error(L, status.errorString());
            }
        }
        lua_pop(L, 1);
    }

	
	Binder binder(L);

	Texture* texture = 0;
	try
	{
        texture = new Texture(application, filename, smoothing ? eLinear : eNearest, wrap, format, maketransparent, transparentcolor);
	}
	catch (const GiderosException& e)
	{
		return luaL_error(L, e.what());
	}

	binder.pushInstance("Texture", texture);
	return 1;
}
int EventDispatcherBinder::addEventListener(lua_State* L)
{
	StackChecker checker(L, "EventDispatcherBinder::addEventListener", 0);

	Binder binder(L);
	EventDispatcher* eventDispatcher = static_cast<EventDispatcher*>(binder.getInstance("EventDispatcher", 1));

	luaL_checktype(L, 2, LUA_TSTRING);
	luaL_checktype(L, 3, LUA_TFUNCTION);

    bool hasData = !lua_isnoneornil(L, 4);

	createEventsTable(L, 1);	// create self.__events table if it's not created

	EventBinderMap& map = getOrCreateEventBinderMap(eventDispatcher);

	const char* event = lua_tostring(L, 2);
	int eventid = StringId::instance().id(event);

	const std::vector<CppLuaBridge*>& bridges = map[eventid]; 

	lua_getfield(L, 1, "__events");		// key is CppLuaBridge*, value is 'event check closure'

	// check if the event is already registered
	bool isFound = false;
	for (std::size_t i = 0; i < bridges.size(); ++i)
	{
		lua_pushlightuserdata(L, bridges[i]);
		lua_rawget(L, -2);	// we get the event check closure
		if (hasData == false)
		{
			lua_pushvalue(L, 3);	// function
			lua_call(L, 1, 1);
		}
		else
		{
			lua_pushvalue(L, 3);	// function
			lua_pushvalue(L, 4);	// data
			lua_call(L, 2, 1);
		}

		if (lua_toboolean(L, -1))
		{
			lua_pop(L, 1);
			isFound = true;
			break;
		}
		else
			lua_pop(L, 1);
	}

	if (isFound == true)
	{
		lua_pop(L, 1);		// pop __events, leave stack as it is
		return 0;
	}

    LuaApplication *application = (LuaApplication*)luaL_getdata(L);
    lua_State *mainL = application->getLuaState();

    CppLuaBridge* bridge = new CppLuaBridge(mainL);

	// create event closure
	luaL_rawgetptr(L, LUA_REGISTRYINDEX, &key_eventClosures);
	lua_pushlightuserdata(L, bridge);	// key=bridge
	if (hasData == false)				// value=closure
	{
        // self ve function'in eventClosure'in icine upvalue olarak koyulmasi garbage collect edilmesini engelliyor
        // bu yuzden {self, function} seklinde bi weak table yaratip ilk upvalue olarak onu set ediyoruz
		luaL_newweaktable(L);

        lua_pushvalue(L, 1);	// self
		lua_rawseti(L, -2, 1);

		lua_pushvalue(L, 3);	// function
        lua_rawseti(L, -2, 2);

        lua_pushcclosure(L, &eventClosure, 1);
	}
	else
	{
        // self, function ve data'nin eventClosure'in icine upvalue olarak koyulmasi garbage collect edilmesini engelliyor
        // bu yuzden {self, function, data} seklinde bi weak table yaratip ilk upvalue olarak onu set ediyoruz
		luaL_newweaktable(L);

        lua_pushvalue(L, 1);	// self
		lua_rawseti(L, -2, 1);

        lua_pushvalue(L, 3);	// function
        lua_rawseti(L, -2, 2);

        lua_pushvalue(L, 4);	// data
        lua_rawseti(L, -2, 3);

        lua_pushcclosure(L, &eventClosureWithData, 1);
	}
	lua_rawset(L, -3);					// envtable["eventClosures"][bridge] = closure
	lua_pop(L, 1);						// pop envtable["eventClosures"]

	// create event check closure
	lua_pushlightuserdata(L, bridge);
	if (hasData == false)
	{
		lua_pushvalue(L, 3);	// function
		lua_pushcclosure(L, &eventCheckClosure, 1);
	}
	else
	{
		lua_pushvalue(L, 3);	// function
		lua_pushvalue(L, 4);	// data
		lua_pushcclosure(L, &eventCheckClosureWithData, 2);
	}
	lua_rawset(L, -3);
	
	map.push_back(eventid, bridge);

	bridge->unref();
	
	eventDispatcher->addEventListener(LuaEvent::Type(event), bridge, &CppLuaBridge::luaEvent);

	lua_pop(L, 1);			// pop __events, leave stack as it is

	return 0;
}