SpineAnimation::SpineAnimation(const char* skeletonDataFile, const char* atlasFile, float scale)
		:SpineSkeleton(skeletonDataFile, atlasFile, scale)
	{
		state = spAnimationState_create(spAnimationStateData_create(skeleton->data));
        state->context = this;
        state->listener = callback;
	}
void SkeletonAnimation::initialize () {
	_ownsAnimationStateData = true;
	_state = spAnimationState_create(spAnimationStateData_create(_skeleton->data));
	_state->rendererObject = this;
	_state->listener = animationCallback;

	_spAnimationState* stateInternal = (_spAnimationState*)_state;
	stateInternal->disposeTrackEntry = disposeTrackEntry;
}
void SkeletonAnimation::initialize () {
	listenerInstance = 0;
	listenerMethod = 0;

	ownsAnimationStateData = true;
	state = spAnimationState_create(spAnimationStateData_create(skeleton->data));
	state->context = this;
	state->listener = callback;
}
Beispiel #4
0
WIPAnimator::WIPAnimator(const char* atlas_path,const char* json_path,i32 w,i32 h):time_scale(1)
{
	_out_buffer = WIPRenderTexture::create(w,h);
	_frame_ref = 0;

	spAtlas* atlas = spAtlas_createFromFile(atlas_path, 0);
	spSkeletonJson* json = spSkeletonJson_create(atlas);
	json->scale = 0.8f;
	spSkeletonData *skeletonData = spSkeletonJson_readSkeletonDataFile(json, json_path);
	if (!skeletonData) {
		printf("Error: %s\n", json->error);
		getchar();
		exit(0);
	}
	//spAnimation* walkAnimation = spSkeletonData_findAnimation(skeletonData, "walk");
	spSkeletonJson_dispose(json);
	spSkeletonBounds* bounds = spSkeletonBounds_create();

	spAnimationStateData* stateData = spAnimationStateData_create(skeletonData);
	//spAnimationStateData_setMixByName(stateData, "walk", "jump", 0.2f);
	//spAnimationStateData_setMixByName(stateData, "jump", "run", 0.2f);
	//spAnimationStateData_setMixByName(stateData, "shoot", "walk", 0.2f);

	//drawable = new SkeletonDrawable(skeletonData,stateData);
	spBone_setYDown(true);
	skeleton = spSkeleton_create(skeletonData);
	state = spAnimationState_create(stateData);
	time_scale = 1;

	//spSkeleton* skeleton = drawable->skeleton;
	skeleton->flipX = false;
	skeleton->flipY = false;
	//spSkeleton_setSkinByName(skeleton, "goblin");
	spSkeleton_setToSetupPose(skeleton);

	skeleton->x = 0;
	skeleton->y = 0;
	spSkeleton_updateWorldTransform(skeleton);

	spSlot* headSlot = spSkeleton_findSlot(skeleton, "head");

	state->listener = callback;
	if (false) {
		spAnimationState_setAnimationByName(state, 0, "test", true);
	} else {

		spAnimationState_setAnimationByName(state, 0, "flying", true);
		/*
		spAnimationState_addAnimationByName(drawable->state, 0, "jump", false, 3);
		spAnimationState_addAnimationByName(drawable->state, 0, "run", true, 0);
		spAnimationState_addAnimationByName(drawable->state,0,"shoot",true,3);
		spAnimationState_addAnimationByName(drawable->state, 0, "walk", true,3);
		*/
	}
	//spAnimationState_setAnimation(drawable->state, 0, walkAnimation, false);
	//drawable->bounds = bounds;
}
static void testRunner(const char* jsonName, const char* atlasName)
{
	///////////////////////////////////////////////////////////////////////////
	// Global Animation Information
	spAtlas* atlas = spAtlas_createFromFile(atlasName, 0);
	ASSERT(atlas != 0);

	spSkeletonData* skeletonData = readSkeletonJsonData(jsonName, atlas);
	ASSERT(skeletonData != 0);

	spAnimationStateData* stateData = spAnimationStateData_create(skeletonData);
	ASSERT(stateData != 0);
	stateData->defaultMix = 0.2f; // force mixing

	///////////////////////////////////////////////////////////////////////////
	// Animation Instance 
	spSkeleton* skeleton = spSkeleton_create(skeletonData);
	ASSERT(skeleton != 0);

	spAnimationState* state = spAnimationState_create(stateData);
	ASSERT(state != 0);


	///////////////////////////////////////////////////////////////////////////
	// Run animation
	spSkeleton_setToSetupPose(skeleton);
	SpineEventMonitor eventMonitor(state);
//	eventMonitor.SetDebugLogging(true);


	AnimList anims; // Let's chain all the animations together as a test
	size_t count = enumerateAnimations(anims, skeletonData);
	if (count > 0) spAnimationState_setAnimationByName(state, 0, anims[0].c_str(), false);
	for (size_t i = 1; i < count; ++i) {
		spAnimationState_addAnimationByName(state, 0, anims[i].c_str(), false, 0.0f);
	}

	// Run Loop
	for (int i = 0; i < MAX_RUN_TIME && eventMonitor.isAnimationPlaying(); ++i) {
		const float timeSlice = 1.0f / 60.0f;
		spSkeleton_update(skeleton, timeSlice);
		spAnimationState_update(state, timeSlice);
		spAnimationState_apply(state, skeleton);
	}

	
	///////////////////////////////////////////////////////////////////////////
	// Dispose Instance
	spSkeleton_dispose(skeleton);
	spAnimationState_dispose(state);

	///////////////////////////////////////////////////////////////////////////
	// Dispose Global
	spAnimationStateData_dispose(stateData);
	spSkeletonData_dispose(skeletonData);
	spAtlas_dispose(atlas);
}
Beispiel #6
0
Skeleton::Skeleton(spSkeletonData* data, spAtlas* atlas)
    : skeleton_(nullptr), state_(nullptr), time_scale_(1.0f), num_vertices_(0),
      texture_(nullptr), max_vertices_(0), animation_data_(nullptr),
      atlas_(atlas), data_(data)
{
    skeleton_ = spSkeleton_create(data);
    spSkeleton_setToSetupPose(skeleton_);
    animation_data_ = spAnimationStateData_create(data);
    state_ = spAnimationState_create(animation_data_);

    array_.reconfigure([this] { vertex_buffer_.bind(); });
}
spAnimationStateData* USpineSkeletonDataAsset::GetAnimationStateData(spAtlas* atlas) {
	if (!animationStateData) {
		spSkeletonData* skeletonData = GetSkeletonData(atlas, false);
		animationStateData = spAnimationStateData_create(skeletonData);
	}
	for (auto& data : MixData) {
		if (!data.From.IsEmpty() && !data.To.IsEmpty()) {
			const char* fromChar = TCHAR_TO_UTF8(*data.From);
			const char* toChar = TCHAR_TO_UTF8(*data.To);
			spAnimationStateData_setMixByName(animationStateData, fromChar, toChar, data.Mix);
		}
	}
	animationStateData->defaultMix = DefaultMix;
	return this->animationStateData;
}
void SkeletonAnimationFbo::loadSkeletonAndAtlasData()
{
    releaseSkeletonRelatedData();

    if (mAtlasFile.isEmpty() || !mAtlasFile.isValid()){
        qDebug()<<"SkeletonAnimation::loadSkeletonAndAtlasData Error: Invalid AtlasFile:"<<mAtlasFile;
        return;
    }

    if (mSkeletonDataFile.isEmpty() || !mSkeletonDataFile.isValid()){
        qDebug()<<"SkeletonAnimation::loadSkeletonAndAtlasData Error: Invalid SkeletonDataFile:"<<mSkeletonDataFile;
        return;
    }

    std::string atlasFile = QQmlFile::urlToLocalFileOrQrc(mAtlasFile).toStdString();
    mspAtlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
    if (!mspAtlas){
        qDebug()<<"SkeletonAnimation::loadSkeletonAndAtlasData Error: Atals is null. AtlasFile:"<<mAtlasFile;
        return;
    }

    spSkeletonJson* json = spSkeletonJson_create(mspAtlas);
    json->scale = mScale;
    std::string skeletonFile = QQmlFile::urlToLocalFileOrQrc(mSkeletonDataFile).toStdString();
    spSkeletonData* skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonFile.c_str());
    if (!skeletonData){
        qDebug()<<"SkeletonAnimation::loadSkeletonAndAtlasData Error: Failed to load json. jason error:"<<(json->error ? json->error : "null.");
        releaseSkeletonRelatedData();
        return;
    }

    spSkeletonJson_dispose(json);

    mspSkeleton = spSkeleton_create(skeletonData);
    mspRootBone = mspSkeleton->bones[0];

    const bool res = spSkeleton_setSkinByName(mspSkeleton, mSkin.toStdString().c_str());
    if (!res && !mSkin.isEmpty())
        qDebug()<<"SkeletonAnimation::loadSkeletonAndAtlasData Error: Invalid skin:"<<mSkin;

    mspAnimationState = spAnimationState_create(spAnimationStateData_create(mspSkeleton->data));
    mspAnimationState->rendererObject = this;
    mspAnimationState->listener = animationCallback;

    mSkeletonLoaded = true;

    mTimer.invalidate();
}
static void LoadSpineboyExample(spAtlas* &atlas, spSkeletonData* &skeletonData, spAnimationStateData* &stateData, spSkeleton* &skeleton, spAnimationState* &state)
{
	///////////////////////////////////////////////////////////////////////////
	// Global Animation Information
	atlas = spAtlas_createFromFile(SPINEBOY_ATLAS, 0);
	ASSERT(atlas != nullptr);

	skeletonData = readSkeletonJsonData(SPINEBOY_JSON, atlas);
	ASSERT(skeletonData != nullptr);

	stateData = spAnimationStateData_create(skeletonData);
	ASSERT(stateData != nullptr);
	stateData->defaultMix = 0.4f; // force mixing

	///////////////////////////////////////////////////////////////////////////
	// Animation Instance 
	skeleton = spSkeleton_create(skeletonData);
	ASSERT(skeleton != nullptr);

	state = spAnimationState_create(stateData);
	ASSERT(state != nullptr);
}
Beispiel #10
0
Player::Player(Game& g, b2Vec2 p) : Character(g),swordFix(NULL), left(false), right(false), landed(true), contact(false), lastproc(0), life(100), attacking(false), attackcooldown(0)
{
	bodyDef.type = b2_dynamicBody;
	bodyDef.position.Set(p.x, p.y);
	bodyDef.fixedRotation = true;

	body = g.w.CreateBody(&bodyDef);

	dynamicBox.SetAsBox(.25f, 0.9f);
	
	fixtureDef.shape = &dynamicBox;
	fixtureDef.density = 5.0f;
	fixtureDef.friction = 0.0f;
	
	fixtureDef.filter.categoryBits = PLAYER;
	fixtureDef.filter.maskBits = MONSTER | TRIGGER | WALL | SWORD;

	(body->CreateFixture(&fixtureDef))->SetUserData(this);

	//add "feet" to detect floor
	dynamicBox.SetAsBox(0.2, 0.05, b2Vec2(0, 0.9f), 0);
	fixtureDef.isSensor = true;
	fixtureDef.density = 0.1;
	fixtureDef.filter.categoryBits = FOOT;
	fixtureDef.filter.maskBits = WALL | MONSTER;
	(body->CreateFixture(&fixtureDef))->SetUserData(this);

	//add sword to kill monsters
	dynamicBox.SetAsBox(0.4, 0.1, b2Vec2(0.65, 0), 0);
	swordDef.shape = &dynamicBox;
	swordDef.isSensor = true;
	swordDef.density = 0.1;
	swordDef.filter.categoryBits = SWORD;
	swordDef.filter.maskBits = MONSTER;

	// Loads the Spine model
	ALLEGRO_PATH *path, *resourceDir, *file;
	resourceDir= al_get_standard_path(ALLEGRO_RESOURCES_PATH);
	std::cerr << al_path_cstr(resourceDir, ALLEGRO_NATIVE_PATH_SEP) << std::endl;

	if (modelAtlas == NULL) {
		file = al_create_path("data/animations/hero.atlas");
		path =  al_clone_path(resourceDir);
		al_join_paths(path, file); al_destroy_path(file);
		modelAtlas = spAtlas_createFromFile(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), NULL);
		if (!modelAtlas) throw Failure("Failed to load the hero's atlas.");
		al_destroy_path(path);
		jsonSkel = spSkeletonJson_create(modelAtlas);

		file = al_create_path("data/animations/hero.json");
		path =  al_clone_path(resourceDir);
		al_join_paths(path, file); al_destroy_path(file);
		modelData = spSkeletonJson_readSkeletonDataFile(jsonSkel, al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));
		if (!modelData) throw Failure("Failed to load the hero's data.");
		al_destroy_path(path); al_destroy_path(resourceDir);

		stateData = spAnimationStateData_create(modelData);
		spAnimationStateData_setMixByName(stateData, "walk", "rest", 0.2f);
		spAnimationStateData_setMixByName(stateData, "rest", "walk", 0.2f);
		spAnimationStateData_setMixByName(stateData, "rest", "slash", 0.1f);
		spAnimationStateData_setMixByName(stateData, "slash", "rest", 0.1f);
		spAnimationStateData_setMixByName(stateData, "walk", "slash", 0.1f);
		spAnimationStateData_setMixByName(stateData, "slash", "walk", 0.1f);
	
		model = loadSkeleton(modelData, stateData);
		if (!model) throw Failure("Failed to load the hero's skeleton.");

		spAnimationState_setAnimationByName(model->state, 0, "rest",  true);
	}
}
SpineAnimation::SpineAnimation(JNIEnv* env, spSkeletonData* sd, SpineCallback* cb) {
	this->callback = cb;
	this->skeleton = spSkeleton_create(sd);
	this->state = spAnimationState_create(spAnimationStateData_create(skeleton->data));
	this->bounds = new spBounds();
	this->x = 0;
	this->y = 0;

	// Count slots with attachments AND bones only
	int i, bCount = 0;
	for(i = 0; i < skeleton->slotCount; i++) {
		spSlot* slot = skeleton->drawOrder[i];
		spBone* bone = slot->bone;

		if(bone && slot->attachment) {
			bCount++;
		}
	}

	// This will allocate the native array
	this->callback->onSkeletonCreate(env, bCount);

	int bIndex = 0;

	// Set to initial pose
	spSkeleton_setToSetupPose(this->skeleton);

	// Update transform so the bone's world position is set
	spSkeleton_updateWorldTransform(skeleton);

	for(i = 0; i < skeleton->slotCount; i++) {
		spSlot* slot = skeleton->drawOrder[i];
		spBone* bone = slot->bone;

		if(bone && slot->attachment) {
			this->callback->addBone(
					env,
					bIndex,
					slot);

			// create the buffer for this bone
			const char* boneName = slot->bone->data->name;

			this->boneVertBuffers[boneName] = new float[BUFFER_SIZE];  // 4 x 2 coords

			// Compute the starting verts
			spRegionAttachment_computeWorldVertices((spRegionAttachment*) slot->attachment, x, y, bone, this->boneVertBuffers[boneName]);

			bIndex++;
		}
	}

	// Loop again tp set the parents
	bIndex = 0;
	for(i = 0; i < skeleton->slotCount; i++) {
		spSlot* slot = skeleton->drawOrder[i];

		if(slot->bone && slot->attachment) {
			spBone* parent = slot->bone->parent;

			if(parent) {
				this->callback->setBoneParent(
						env,
						bIndex,
						parent->data->name);
			}

			bIndex++;
		}
	}

	this->center = new float[2]; // x, y

	// Initialize the mutex to lock between draw and step
	int mutexInitState = pthread_mutex_init (&mutex , NULL );

	if(mutexInitState != 0) {
		callback->onError(env, "Error initializing mutex: %s", strerror(errno));
	}
}
Beispiel #12
0
    SpineDrawable::SpineDrawable(const std::string& atlasFile, const std::string& skeletonFile):
        Component(TYPE)
    {
        atlas = spAtlas_createFromFile(atlasFile.c_str(), 0);
        if (!atlas)
        {
            ouzel::Log(ouzel::Log::Level::ERR) << "Failed to load atlas";
            return;
        }

        if (skeletonFile.find(".json") != std::string::npos)
        {
            // is json format
            spSkeletonJson* json = spSkeletonJson_create(atlas);
            skeletonData = spSkeletonJson_readSkeletonDataFile(json, skeletonFile.c_str());
            
            if (!skeletonData)
            {
                ouzel::Log(ouzel::Log::Level::ERR) << "Failed to load skeleton: " << json->error;
                return;
            }
            spSkeletonJson_dispose(json);
        }
        else
        {
            // binary format
            spSkeletonBinary* binary = spSkeletonBinary_create(atlas);
            skeletonData = spSkeletonBinary_readSkeletonDataFile(binary, skeletonFile.c_str());
            
            if (!skeletonData)
            {
                ouzel::Log(ouzel::Log::Level::ERR) << "Failed to load skeleton: " << binary->error;
                return;
            }
            spSkeletonBinary_dispose(binary);
        }

        bounds = spSkeletonBounds_create();

        skeleton = spSkeleton_create(skeletonData);

        animationStateData = spAnimationStateData_create(skeletonData);
        
        animationState = spAnimationState_create(animationStateData);
        animationState->listener = listener;
        animationState->rendererObject = this;

        spSkeleton_setToSetupPose(skeleton);
        spSkeleton_updateWorldTransform(skeleton);

        updateMaterials();
        updateBoundingBox();

        indexBuffer = std::make_shared<ouzel::graphics::Buffer>(*ouzel::engine->getRenderer());
        indexBuffer->init(ouzel::graphics::Buffer::Usage::INDEX, ouzel::graphics::Buffer::DYNAMIC);

        vertexBuffer = std::make_shared<ouzel::graphics::Buffer>(*ouzel::engine->getRenderer());
        vertexBuffer->init(ouzel::graphics::Buffer::Usage::VERTEX, ouzel::graphics::Buffer::DYNAMIC);

        whitePixelTexture = ouzel::engine->getCache().getTexture(ouzel::TEXTURE_WHITE_PIXEL);

        updateHandler.updateHandler = std::bind(&SpineDrawable::handleUpdate, this, std::placeholders::_1);
        ouzel::engine->getEventDispatcher().addEventHandler(&updateHandler);
    }