Ejemplo n.º 1
0
const char *imageInit(love_image *self, const char *filename) {

	int type = getType(filename);

	if (!loadNoGameGraphics(self, filename)) {

		if (!fileExists(filename)) {
			luaError(L, "Could not open image, does not exist");
			return NULL;
		}

		if (type == 0) { // PNG

			self->texture = sfil_load_PNG_file(filename, SF2D_PLACE_RAM);

		} else if (type == 1) { // JPG
			
			self->texture = sfil_load_JPEG_file(filename, SF2D_PLACE_RAM);

		} else if (type == 2) { // BMP
			
			self->texture = sfil_load_BMP_file(filename, SF2D_PLACE_RAM);

		} else if (type == 4) {

			luaError(L, "Unknown image type");

		}

	}

	return NULL;

}
Ejemplo n.º 2
0
/*		Read in	t.Image = { File = "Something", Width = #, Delay = #, Clip = {x, y, w, h} }
			OR 	t.Image = "Filename"
*/
int _parseEntityImage(lua_State* ls, StaticObject* so, int virtualIndex = -1)
{
	//Virtual index can NOT be an offset from the top, must be an absolute position.
	// This is because the lua_next() will screw up unless we specify an absolute.
	if (virtualIndex < 0)
		virtualIndex = lua_gettop(ls) + virtualIndex + 1;

	//printf("vi: %i top:%i\n", virtualIndex, lua_gettop(ls));
	//luaStackdump(ls);

	string file;

	if (lua_isstring(ls, virtualIndex))
	{
		file = game->mMap->mWorkingDir + lua_tostring(ls, virtualIndex);
		if (!so->LoadImage(file))
		{
			return luaError(ls, "", "Could not load entity image: " + file);
		}
		return 1;
	}

	if (!lua_istable(ls, virtualIndex))
		return 0;

	string key;
	int width = 0, delay = 1000;

	lua_pushnil(ls); /* first key */
	while (lua_next(ls, virtualIndex) != 0)
	{
		/* key = index -2, value = index -1 */
		if (lua_isstring(ls, -2))
		{
			key = lua_tostring(ls, -2);
			if (key == "File")
			{
				file = game->mMap->mWorkingDir + lua_tostring(ls, -1);
				if (!so->LoadImage(file))
				{
					return luaError(ls, "", "Could not load entity image: " + file);
				}
			}
			else if (key == "Width")
				width = (int)lua_tonumber(ls, -1);
			else if (key == "Delay")
				delay = (int)lua_tonumber(ls, -1);
		}
		lua_pop(ls, 1); //pop value, keep key for next iteration
	}

	// TODO: A better way to handle this?
	if (width > 0)
	{
		so->ToHorizontalAnimation(width, delay);
	}

	return 1;
}
Ejemplo n.º 3
0
/* entity = Entity.Create(entityInfoTable, x<nil>, y<nil>);
	Create a new entity instance and place it on the map at (x, y)
	If (x, y) are nil, it will create a new entity and return it, but NOT
	add it to the map. It's our responsibility to do Entity.Add(ent, x, y)
	later, or to do something else special with it (Such as adding to the
	players party)
*/
int entity_Create(lua_State* ls)
{
	Entity* e;
	point2d p;

	// Make sure they passed in a table as the first parameter
	if (!lua_istable(ls, 1))
	{
		return luaError(ls, "Entity.Create", "First param must be a table");
	}

	// Grab t.Type and create an entity associated with that type
	lua_pushstring(ls, "Type");
	lua_gettable(ls, 1);
	int type = (int)lua_tonumber(ls, -1);
	lua_pop(ls, 1);

	// Create the entity class based on the type provided

	e = _createEntity(type);
	if (!e)
	{
		return luaError(ls, "Entity.Create", "Invalid type: " + its(type));
	}

	// Configure the class via all the properties the class defines
	if (!_parseEntityProperties(ls, e, 1))
	{
		return luaError(ls, "Entity.Create", "Error parsing properties");
	}

	int count = lua_gettop(ls);

	if (count > 1)
	{
		//Finally, add it to the map and return a reference to it
		e->mMap = game->mMap;
		game->mMap->AddEntity(e);

		p.x = (int)lua_tonumber(ls, 2);
		p.y = (int)lua_tonumber(ls, 3);

		e->SetPosition(p);
	}

	lua_pushlightuserdata(ls, e);
	return 1;
}
Ejemplo n.º 4
0
//	.Remove(entity) - Remove the specified entity from the map. Returns true on success, false otherwise.
int entity_Remove(lua_State* ls)
{
	luaCountArgs(ls, 1);

	bool result;
	if (lua_isnil(ls, 1))
	{
		result = false;
	}
	else
	{
		Entity* e = (Entity*)lua_touserdata(ls, 1);

		if (e)
		{
            if (e->mType == ENTITY_REMOTEACTOR || e->mType == ENTITY_LOCALACTOR)
            {
                luaError(ls, "Entity.Remove", "Tried to remove Remote/Local Actor: " + e->mName);
            }

            result = game->mMap->RemoveEntity(e);
		}
	}

	lua_pushboolean(ls, result);
	return 1;
}
Ejemplo n.º 5
0
//	.SetText(ent, "text", r, g, b, maxWidth<0>) - Set displayed text of a text object entity
int entity_SetText(lua_State* ls)
{
	luaCountArgs(ls, 2);

	int args = lua_gettop(ls);
	int width = 0;
	color c;
	string text;

	TextObject* o = (TextObject*)_getReferencedEntity(ls);
	if (!o || o->mType != ENTITY_TEXT)
	{
		return luaError(ls, "Entity.SetText", "Invalid Entity type");
	}

	text = lua_tostring(ls, 2);
	c.r = (int)lua_tonumber(ls, 3);
	c.g = (int)lua_tonumber(ls, 4);
	c.b = (int)lua_tonumber(ls, 5);

	if (args > 5)
		width = (int)lua_tonumber(ls, 6);

	o->SetText(text, c, width);
	return 0;
}
Ejemplo n.º 6
0
//	.SetFont(ent, "face"<nil>, size<nil>, style<nil>) - Set displayed text of a text object entity
//		Any non-defined parameter will use user defaults
int entity_SetFont(lua_State* ls)
{
	luaCountArgs(ls, 1);

	int args = lua_gettop(ls);
	int size = 0, style = 0;
	string face;

	TextObject* o = (TextObject*)_getReferencedEntity(ls);
	if (!o || o->mType != ENTITY_TEXT)
	{
		return luaError(ls, "Entity.SetFont", "Invalid Entity type");
	}

	if (args > 1 && lua_isstring(ls, 2))
		face = lua_tostring(ls, 2);

	if (args > 2 && lua_isnumber(ls, 3))
		size = (int)lua_tonumber(ls, 3);

	if (args > 3 && lua_isnumber(ls, 4))
		style = (int)lua_tonumber(ls, 4);

	o->SetFont(face, size, style);
	return 0;
}
Ejemplo n.º 7
0
int initSocket(lua_State *L) {
	if (!initializeSocket) {
		socketBuffer = memalign(0x1000, 0x100000);

		Result socketIsInitialized = socInit(socketBuffer, 0x100000);

		if (R_FAILED(socketIsInitialized)) {
			luaError(L, "Failed to initialize LuaSocket.");
		} else {
			initializeSocket = true;
		}
	}

	luaL_Reg reg[] = {
		{"udp", newUDP},
		{"tcp", socketNewTCP},
		{"shutdown", socketShutdown},
		{ 0, 0 },
	};

	luaL_newmetatable(L, "Socket");
	
	lua_pushvalue(L, -1);
	
	lua_setfield(L, -2, "__index");

	luaL_newlib(L, reg);

	return 1;
}
Ejemplo n.º 8
0
bool displayTelemetryScreen()
{
#if defined(LUA)
  if (TELEMETRY_SCREEN_TYPE(s_frsky_view) == TELEMETRY_SCREEN_TYPE_SCRIPT) {
    uint8_t state = isTelemetryScriptAvailable(s_frsky_view);
    switch (state) {
      case SCRIPT_OK:
        return true;  // contents will be drawed by Lua Task
      case SCRIPT_NOFILE:
        return false;  // requested lua telemetry screen not available
      case SCRIPT_SYNTAX_ERROR:
      case SCRIPT_PANIC:
      case SCRIPT_KILLED:
        luaError(state, false);
        return true;
    }
    return false;
  }
#endif

  if (TELEMETRY_SCREEN_TYPE(s_frsky_view) == TELEMETRY_SCREEN_TYPE_NONE) {
    return false;
  }

  lcdDrawTelemetryTopBar();

  if (s_frsky_view < MAX_TELEMETRY_SCREENS) {
    displayCustomTelemetryScreen(s_frsky_view);
  }

  return true;
}
Ejemplo n.º 9
0
int imageSetFilter(lua_State *L) { // image:setFilter()

	love_image *self = luaobj_checkudata(L, 1, CLASS_TYPE);

	char *minMode = luaL_checkstring(L, 2);
	char *magMode = luaL_optstring(L, 3, minMode);

	u32 minFilter;
	u32 magFilter;

	if (strcmp(minMode, "linear") != 0 && 
		strcmp(minMode, "nearest") != 0 &&
		strcmp(magMode, "linear") != 0 &&
		strcmp(magMode, "nearest" != 0)) {
			luaError(L, "Invalid Image Filter.");
			return 0;
		}

	if (strcmp(minMode, "linear") == 0) minFilter = GPU_TEXTURE_MIN_FILTER(GPU_LINEAR);
	if (strcmp(magMode, "linear") == 0) magFilter = GPU_TEXTURE_MAG_FILTER(GPU_LINEAR);
	if (strcmp(minMode, "nearest") == 0) minFilter = GPU_TEXTURE_MIN_FILTER(GPU_NEAREST);
	if (strcmp(magMode, "nearest") == 0) magFilter = GPU_TEXTURE_MAG_FILTER(GPU_NEAREST);

	sf2d_texture_set_params(self->texture, magFilter | minFilter);

	self->minFilter = minMode;
	self->magFilter = magMode;

	return 0;

}
Ejemplo n.º 10
0
// .Register("id", "luafunc", userdata<nil>) - Returns the unique handle of the newly created listener
int events_Register(lua_State* ls)
{
	DEBUGOUT("events_Register");
	luaCountArgs(ls, 2);

	int numArgs = lua_gettop( ls );

	if (!lua_isstring(ls, 1) || !lua_isstring(ls, 2))
		return luaError(ls, "Events.Register", "Bad Params");

	string id = lua_tostring(ls, 1);
	string func = lua_tostring(ls, 2);

	LuaMessageListener* luaListener = new LuaMessageListener();
	luaListener->luaState = ls;
	luaListener->luaFunction = func;

	if (numArgs > 2)
	{
		lua_pushvalue(ls, 3); //copy the value at index to the top of the stack
		luaListener->luaReference = luaL_ref(ls, LUA_REGISTRYINDEX); //create a reference to the stack top and pop
	}

	MessageListener* ml = messenger.AddListener(id, listener_luaActivate, listener_luaDestroy, luaListener);

	lua_pushnumber(ls, ml->handle); //return our handle
	return 1;
}
Ejemplo n.º 11
0
// Referenced entity is always the first parameter of the state.
// This should always return a valid entity pointer. If it's invalid, there will be a longjmp from within lua.
Entity* _getReferencedEntity(lua_State* ls, int index = 1)
{

	Entity* e = NULL;

	if (lua_istable(ls, index))
	{
		// try to pull the pointer from the table
		lua_pushstring(ls, "__centity");
		lua_gettable(ls, index);
		e = (Entity*)(lua_touserdata(ls, -1));
		lua_pop(ls, 1);
	}
	else // assume userdata
	{
		e = (Entity*)(lua_touserdata(ls, index));
		if (!_verifyEntity(e))
		{
			string err = "index " + its(index) + " not a valid entity pointer.";
			luaError(ls, "", err);
		}
	}

	return e;
}
Ejemplo n.º 12
0
int fontNew(lua_State *L) { // love.graphics.newFont()

	if (fontCounter <= fontLimit) {

		fontCounter += 1;
        
        int argc = lua_gettop(L);
        char *filename = 0;
        int fontSize = 14;
        
        //newFont(fontSize)
        if( argc == 1){
            fontSize = lua_isnoneornil(L, 1) ? NULL : luaL_checkinteger(L, 1);
        }
        
        //newFont(filename, fontsize)
        if( argc == 2 ) {
            filename = lua_isnoneornil(L, 1) ? NULL : luaL_checkstring(L, 1);
            fontSize = lua_isnoneornil(L, 2) ? NULL : luaL_checkinteger(L, 2);
        }

		love_font *self = luaobj_newudata(L, sizeof(*self));

		luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);

		if (filename) {

			const char *error = fontInit(self, filename, fontSize);

			if (error) luaError(L, error);

		} else {

			fontDefaultInit(self, fontSize);
		}

		return 1;

	} else {

		luaError(L, "LovePotion currently has a 2 font limit. This limitation will hopefully be lifted in future versions.");
		return 0;

	}


}
Ejemplo n.º 13
0
std::string luaToString(lua_State* plua_state, int index)
{
    const char* result = lua_tostring(plua_state, index);
    if (!result)
        luaError(plua_state, strFormat("luaToString from index %d failed.", index));

    return std::string(lua_tostring(plua_state, index));
}
Ejemplo n.º 14
0
void LuaWrapper::reloadScripts()
{
    for (auto script : m_scripts)
    {
        m_err = luaL_dofile(m_state, script.c_str());
        luaError();
    }
}
Ejemplo n.º 15
0
int imageNew(lua_State *L) { // love.graphics.newImage()

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

	if (!fileExists(filename)) luaError(L, "Could not open image, does not exist");

	int type = getType(filename);
	if (type == 4) luaError(L, "Unknown image type");

	love_image *self = luaobj_newudata(L, sizeof(*self));
	luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);

	const char *error = imageInit(self, filename);
	if (error) luaError(L, error);

	return 1;

}
Ejemplo n.º 16
0
int MeiqueCache::readPackage(lua_State* L)
{
    StringMap pkgData;
    readLuaTable(L, lua_gettop(L), pkgData);
    lua_pop(L, 1);
    std::string name = pkgData["name"];
    if (name.empty())
        luaError(L, "Package entry without name.");
    MeiqueCache* self = getSelf(L);
    self->setPackage(name, pkgData);
    return 0;
}
Ejemplo n.º 17
0
int joystickGetAxes(lua_State *L) {
	
	love_joystick *self = luaobj_checkudata(L, 1, CLASS_TYPE);
	
	int numAxesCheck = lua_gettop(L);
	for (int i = 2; i <= numAxesCheck; i++) {
		
		int axisId = luaL_checkinteger(L, i);
		
		if( axisId < 5 ) { // Circle Axes
			
			circlePosition circleData;
			hidCircleRead(&circleData);

			if( axisId == 1 ) lua_pushinteger(L, circleData.dx);
			if( axisId == 2 ) lua_pushinteger(L, circleData.dy);
			
			circlePosition cStickData;
			irrstCstickRead(&cStickData);

			if( axisId == 3 ) lua_pushinteger(L, cStickData.dx);
			if( axisId == 4 ) lua_pushinteger(L, cStickData.dy);

		} else if( axisId < 8 ) { // Gyro Axes
			
			HIDUSER_EnableGyroscope();
			angularRate gyroData;
			hidGyroRead(&gyroData);
			
			if( axisId == 5 ) lua_pushinteger(L, gyroData.x);
			if( axisId == 6 ) lua_pushinteger(L, gyroData.y);
			if( axisId == 7 ) lua_pushinteger(L, gyroData.z);
			
		} else if ( axisId < 11 ) { // Accelloremeter Axes
		
			HIDUSER_EnableAccelerometer();
			accelVector accelData;
			hidAccelRead(&accelData);

			if( axisId == 8 ) lua_pushinteger(L, accelData.x);
			if( axisId == 9 ) lua_pushinteger(L, accelData.y);
			if( axisId == 10 ) lua_pushinteger(L, accelData.z);
		
		} else {
			luaError(L, "AxisId out of bounds");
		}
		
	}
	
	return numAxesCheck-1;
	
}
Ejemplo n.º 18
0
int joystickNew(lua_State *L, int id) {
		
	love_joystick *self = luaobj_newudata(L, sizeof(*self));
	
	luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);
	
	const char *error = joystickInit(self, id);
	
	if (error) luaError(L, error);
	
	return 1;
	
}
Ejemplo n.º 19
0
int MeiqueCache::readMeiqueConfig(lua_State* L)
{
    MeiqueCache* self = getSelf(L);
    StringMap opts;
    readLuaTable(L, lua_gettop(L), opts);
    lua_pop(L, 1);
    try {
        self->m_sourceDir = OS::normalizeDirPath(opts.at("sourceDir"));
        self->m_buildType = opts.at("buildType") == "debug" ? Debug : Release;
        self->m_compilerId = opts.at("compiler");
        self->m_installPrefix = opts["installPrefix"];
    } catch (std::out_of_range&) {
        luaError(L, MEIQUECACHE " file corrupted or created by a old version of meique.");
    }
    return 0;
}
Ejemplo n.º 20
0
int imageNew(lua_State *L) { // love.graphics.newImage()

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

	love_image *self = luaobj_newudata(L, sizeof(*self));
	luaobj_setclass(L, CLASS_TYPE, CLASS_NAME);

	const char *error = imageInit(self, filename);
	if (error) luaError(L, error);

	sf2d_texture_set_params(self->texture, defaultFilter);
	self->minFilter = defaultMinFilter;
	self->magFilter = defaultMagFilter;

	return 1;

}
Ejemplo n.º 21
0
void ApplicationManager::loadLuaFiles()
{
	std::vector<std::string> luafiles;

	G_FILE* fis = g_fopen("luafiles.txt", "rt");

	if (fis)
	{
		char line[1024];
		while (true)
		{
			if (g_fgets(line, 1024, fis) == NULL)
				break;
				
			size_t len = strlen(line);
			
			if (len > 0 && line[len - 1] == 0xa)
				line[--len] = 0;

			if (len > 0 && line[len - 1] == 0xd)
				line[--len] = 0;
			
			if (len > 0)
				luafiles.push_back(line);
		}

		g_fclose(fis);
	}
	
	GStatus status;
	for (size_t i = 0; i < luafiles.size(); ++i)
	{
		application_->loadFile(luafiles[i].c_str(), &status);
		if (status.error())
			break;
	}

	if (!status.error())
	{
		gapplication_enqueueEvent(GAPPLICATION_START_EVENT, NULL, 0);
		application_->tick(&status);
	}

	if (status.error())
        luaError(status.errorString());
}
Ejemplo n.º 22
0
static int graphicsSetColor(lua_State *L) { // love.graphics.setColor()

	if (lua_isnumber(L, -1)) {

		currentR = luaL_checkinteger(L, 1);
		currentG = luaL_checkinteger(L, 2);
		currentB = luaL_checkinteger(L, 3);
		currentA = luaL_optnumber(L, 4, currentA);

	} else if (lua_istable(L, -1)) {

		luaError(L, "Table support for setColor is not implemented yet. Use unpack(insertTableHere) until it is.");

	}

	return 0;

}
Ejemplo n.º 23
0
int joystickGetAxis(lua_State *L) {
	
	love_joystick *self = luaobj_checkudata(L, 1, CLASS_TYPE);
	int axisId = luaL_checkinteger(L, 2);
	
	
	if( axisId < 3 ) { // Circle Axes
		
		circlePosition circleData;
		hidCircleRead(&circleData);

		if( axisId == 1 ) lua_pushinteger(L, circleData.dx);
		if( axisId == 2 ) lua_pushinteger(L, circleData.dy);
		
	} else if( axisId < 6 ) { // Gyro Axes
		
		HIDUSER_EnableGyroscope();
		angularRate gyroData;
		hidGyroRead(&gyroData);
		
		if( axisId == 3 ) lua_pushinteger(L, gyroData.x);
		if( axisId == 4 ) lua_pushinteger(L, gyroData.y);
		if( axisId == 5 ) lua_pushinteger(L, gyroData.z);
		
	} else if ( axisId < 9 ) { // Accelloremeter Axes
	
		HIDUSER_EnableAccelerometer();
		accelVector accelData;
		hidAccelRead(&accelData);
		if( axisId == 6 ) lua_pushinteger(L, accelData.x);
		if( axisId == 7 ) lua_pushinteger(L, accelData.y);
		if( axisId == 8 ) lua_pushinteger(L, accelData.z);
	
	} else {
		luaError(L, "AxisId out of bounds");
	}
	
	return 1;
	
}
Ejemplo n.º 24
0
void LuaWrapper::loadScript(const std::string & script)
{
    m_err = luaL_dofile(m_state, script.c_str());
    luaError();
    m_scripts.push_back(script);
}
Ejemplo n.º 25
0
void LuaWrapper::callFunc(const int numArgs, const int numRet)
{
    m_err = lua_pcall(m_state, numArgs, numRet, 0);
    luaError();
}
Ejemplo n.º 26
0
void ApplicationManager::drawFrame() {
	if (networkManager_)
		networkManager_->tick();

	// if we're in app mode, skip the first 10 frames as black screen
	// because a pattern like surfaceChanged(a,b), drawFrame, drawFrame, drawFrame, ..., surfaceChanged(b,a) is possible
	// and we want to execute lua files after getting the final device resolution
	if (player_ == false) {
		if (nframe_++ < 10) {
			glClearColor(0, 0, 0, 1);
			glClear (GL_COLOR_BUFFER_BIT);

			return;
		}
	}

	if (player_ == false) {
		if (applicationStarted_ == false) {
			if (!appName.empty())
				play(appName.c_str());
			else
			{
				loadProperties();
				loadLuaFiles();
			}
			skipFirstEnterFrame_ = true;

			/*
			 bool licensed = (licenseKey_[15] == 'f' && licenseKey_[16] == 'f');
			 if (licensed)
			 {
			 loadLuaFiles();
			 skipFirstEnterFrame_ = true;
			 }
			 else
			 {
			 application_->getApplication()->setBackgroundColor(0, 0, 0);
			 splashScreen_ = new SplashScreen(application_->getApplication());
			 application_->getApplication()->stage()->addChild(splashScreen_);
			 splashScreen_->unref();
			 }
			 */

			applicationStarted_ = true;
		}

		/*		if (splashScreen_ && splashScreen_->isFinished())
		 {
		 application_->getApplication()->stage()->removeChild(splashScreen_);
		 splashScreen_ = NULL;
		 application_->getApplication()->setBackgroundColor(1, 1, 1);

		 loadLuaFiles();
		 skipFirstEnterFrame_ = true;
		 }*/
	}

	if (skipFirstEnterFrame_ == true) {
		skipFirstEnterFrame_ = false;
	} else {
		GStatus status;
		application_->enterFrame(&status);
		if (status.error())
			luaError(status.errorString());
          gaudio_AdvanceStreamBuffers();		  
	}

	application_->clearBuffers();
	application_->renderScene(1);
	drawIPs();
}
Ejemplo n.º 27
0
int joystickGetGamepadMapping(lua_State *L) { //joystick:getGamepadMapping()
	
	luaError(L, "joystick:getGamepadMapping() is not implemented");
	return 0;
	
}
Ejemplo n.º 28
0
int joystickGetHat(lua_State *L) { //joystick:getHat()
	
	 luaError(L, "joystick:getHat() is not implemented");
	return 0; //M'lua
	
}