Esempio n. 1
0
void SoundInterface::commandPlaySound(void) {
	int32_t freg = cb->popValue().toInt();
	float balance = cb->popValue().toFloat();
	float volume = cb->popValue().toFloat();
	Any any = cb->popValue();
	
	// If passed parameter is integer, the sound is preloaded. Otherwise load it.
	if (any.type() == Any::Int) {
		CBChannel* channel = new CBChannel;
		channel->setMixer(al_get_default_mixer());
		int32_t id = any.toInt();
		channel->playSound((*sounds[id]), volume, balance, freg);
		int32_t nextChannel = nextChannelId();
		channels[nextChannel] = channel;
	}
	else {
		CBChannel* channel = new CBChannel;
		channel->setMixer(al_get_default_mixer());
		ALLEGRO_PATH *path = any.toString().getPath();
		const char *cpath = al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP);
		channel->playSound(cpath, volume, balance, freg);
		int32_t nextChannel = nextChannelId();
		channels[nextChannel] = channel;
	}
}
void convertToVariant(NPP npp, const Any& any, NPVariant* variant, bool result)
{
    switch (any.getType())
    {
    case Any::Empty:
        NULL_TO_NPVARIANT(*variant);
        break;
    case Any::Bool:
        BOOLEAN_TO_NPVARIANT(any.toBoolean(), *variant);
        break;
    case Any::Int32:
    case Any::Uint32:
        INT32_TO_NPVARIANT(static_cast<int32_t>(any), *variant);
        break;
    case Any::Int64:
    case Any::Uint64:
    case Any::Float32:
    case Any::Float64:
        DOUBLE_TO_NPVARIANT(static_cast<double>(any), *variant);
        break;
    case Any::Dynamic:
        if (any.isString())
        {
            std::string value = any.toString();
            if (value.length() == 0)
            {
                STRINGN_TO_NPVARIANT(0, 0, *variant);
            }
            else if (!result)
            {
                STRINGN_TO_NPVARIANT(value.c_str(), static_cast<uint32_t>(value.length()), *variant);
            }
            else {
                void* buffer = NPN_MemAlloc(value.length());
                if (!buffer)
                {
                    STRINGN_TO_NPVARIANT(0, 0, *variant);
                }
                else
                {
                    memmove(buffer, value.c_str(), value.length());
                    STRINGN_TO_NPVARIANT(static_cast<NPUTF8*>(buffer), static_cast<uint32_t>(value.length()), *variant);
                }
            }
        }
        else
        {
            assert(any.isObject());
            convertToVariant(npp, any.toObject(), variant, result);
        }
        break;
    default:
        VOID_TO_NPVARIANT(*variant);
        break;
    }
}