示例#1
0
void load_level( void )
{
	obj = OBJ_load( OBJ_FILE, 1 );

	unsigned int i = 0;

	while( i != obj->n_objmesh ) {
	
		OBJ_optimize_mesh( obj, i, 128 );

		OBJ_build_mesh( obj, i );
		
		OBJ_free_mesh_vertex_data( obj, i );

		++i;
	}
	
	
	init_physic_world();
	
	load_physic_world();
	
	gContactAddedCallback = contact_added_callback;
	
	/* Get the mesh object name level_clear, which is the cylinder located
	 * in the middle of the level end target. */
   OBJMESH *level_clear = OBJ_get_mesh( obj, "level_clear", 0 );

   /* On top of the usual CF_CUSTOM_MATERIAL_CALLBACK collision flag,
   add CF_NO_CONTACT_RESPONSE. This collision flag makes your rigid
   body object act like a ghost, meaning that it will not respond to collision.
   This can be used to turn off the collision response of any rigid body,
    which is great for this typical scenario, where you only want the object to trigger the callback. */
   level_clear->btrigidbody->setCollisionFlags( level_clear->btrigidbody->getCollisionFlags()  |
												btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK |
												btCollisionObject::CF_NO_CONTACT_RESPONSE );

   /* Make the level_clear mesh invisible for rendering. */
   level_clear->visible = 0;

   i = 0;
   while( i != obj->n_objmesh ) { 

      OBJMESH *objmesh = &obj->objmesh[ i ];

      if( strstr( objmesh->name, "gem" ) ) { 

         objmesh->rotation.z = ( float )( random() % 360 );

         objmesh->btrigidbody->setCollisionFlags( objmesh->btrigidbody->getCollisionFlags() |
												  btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK );
      }
	  
	  ++i;
   }	
	
	
	player = OBJ_get_mesh( obj, "player", 0 );
	
	player->btrigidbody->setFriction( 10.0f );
	
	memcpy( &eye, &player->location, sizeof( vec3 ) );
	
	
	i = 0;
	while( i != obj->n_texture ) { 

		OBJ_build_texture( obj,
						   i,
						   obj->texture_path,
						   TEXTURE_MIPMAP | TEXTURE_16_BITS,
						   TEXTURE_FILTER_2X,
						   0.0f );
		++i;
	}


	i = 0;
	while( i != obj->n_program ) { 

		OBJ_build_program( obj,
						   i,
						   program_bind_attrib_location,
						   program_draw,
						   1,
						   obj->program_path );
		++i;
	}


	i = 0;
	while( i != obj->n_objmaterial ) { 

		OBJ_build_material( obj, i, NULL );
		
		++i;
	}	

	

	font_small = FONT_init( ( char * )"foo.ttf" );

	FONT_load( font_small,
			   font_small->name,
			   1,
			   24.0f,
			   512,
			   512,
			   32,
			   96 );
			   

	font_big = FONT_init( ( char * )"foo.ttf" );

	FONT_load( font_big,
			   font_big->name,
			   1,
			   48.0f,
			   512,
			   512,
			   32,
			   96 );
			   
	/* Declare a memory structure that you will use (and reuse) to load each sound buffer. */
	MEMORY *memory = NULL;
	/* Reset the counter. */
	i = 0;
	while( i != 4 ) {
	
		switch( i ) { 
		
			case 0: {
				/* Load the red.ogg file. */
				memory = mopen( ( char * )"red.ogg", 1 );
				break;
			}
			case 1: {
				/* Load the green.ogg file. */
				memory = mopen( ( char * )"green.ogg", 1 );
				break;
			}
			case 2: {
				/* Load the blue.ogg file. */
				memory = mopen( ( char * )"blue.ogg", 1 );
				break;
			}
			case 3: {
				/* Load the yellow.ogg file. */
				memory = mopen( ( char * )"yellow.ogg", 1 );
				break;
			}
		}

		/* For the current gem buffer index, create a sound buffer using the content
		 * of the memory structure that we have loaded. */
		gems_soundbuffer[ i ] = SOUNDBUFFER_load( ( char * )"gem", memory );

		mclose( memory );
		
		/* Create a new sound source for the current index and link the current sound buffer. */
		gems_sound[ i ] =  SOUND_add( ( char * )"gem", gems_soundbuffer[ i ] );

		SOUND_set_volume( gems_sound[ i ], 1.0f );

		++i;
	}
	
	/* Temporary variable to contain the mesh pointer of the object that will be emitting the sound. */
	OBJMESH *objmesh = NULL;
	/* Load the water.ogg file in memory. */
	memory = mopen( ( char * )"water.ogg", 1 );

	water_soundbuffer = SOUNDBUFFER_load( ( char * )"water", memory );

	/* Free the memory , because the sound buffer is loaded as a static sound and the raw audio buffer has been transferred to the
	audio memory by the previous function call. */
	mclose( memory );

	/* Create the water sound source. */
	water_sound = SOUND_add( ( char * )"water", water_soundbuffer );

	/* Here comes the part of code where we are going to associate the
	sound source to the object. First, get the objmesh pointer for the water object. */
	objmesh = OBJ_get_mesh( obj, "water", 0 );

	/* Assign to the sound source the location of the mesh in 3D space and
	use the radius as the reference distance (how far the sound can be heard). */
	SOUND_set_location( water_sound,
						&objmesh->location,
						objmesh->radius );

	/* Set the volume of the water at 50%. */
	SOUND_set_volume( water_sound, 0.5f );

	SOUND_play( water_sound, 1 );

	//the same as above for the lava sound
	memory = mopen( ( char * )"lava.ogg", 1 );
	
	lava_soundbuffer = SOUNDBUFFER_load( ( char * )"lava", memory );
	
	mclose( memory );
	
	lava_sound = SOUND_add( ( char * )"lava", lava_soundbuffer );

	objmesh = OBJ_get_mesh( obj, "lava", 0 );

	SOUND_set_location( lava_sound,
						&objmesh->location, 
						objmesh->radius );
						
	SOUND_set_volume( lava_sound, 0.5f );

	SOUND_play( lava_sound, 1 );

	//finally for the toxic waste sound
	memory = mopen( ( char * )"toxic.ogg", 1 );
	
	toxic_soundbuffer = SOUNDBUFFER_load( ( char * )"toxic", memory );

	mclose( memory );

	toxic_sound = SOUND_add( ( char * )"toxic", toxic_soundbuffer );
	
	objmesh = OBJ_get_mesh( obj, "toxic", 0 );

	SOUND_set_location( toxic_sound,
						&objmesh->location, 
						objmesh->radius );
	
	SOUND_set_volume( toxic_sound, 0.5f );
	
	SOUND_play( toxic_sound, 1 );

	/* Load the background sound as a streamed buffer. */
	memory = mopen( ( char * )"background.ogg", 1 );

	background_soundbuffer = SOUNDBUFFER_load_stream( ( char * )"background", memory );

	background_sound = SOUND_add( ( char * )"background", background_soundbuffer );
	
	SOUND_set_volume( background_sound, 0.5f );
	/* Play the background sound in a loop. */
	SOUND_play( background_sound, 1 );

	//Start the decompression thread
	THREAD_play( thread );

}
示例#2
0
void load_level( void )
{
	obj = OBJ_load( OBJ_FILE, 1 );

	unsigned int i = 0;

	while( i != obj->n_objmesh ) {
	
		OBJ_optimize_mesh( obj, i, 128 );

		OBJ_build_mesh( obj, i );
		
		OBJ_free_mesh_vertex_data( obj, i );

		++i;
	}
	
	
	init_physic_world();
	
	load_physic_world();
	
	gContactAddedCallback = contact_added_callback;
	
	
   OBJMESH *level_clear = OBJ_get_mesh( obj, "level_clear", 0 );

   level_clear->btrigidbody->setCollisionFlags( level_clear->btrigidbody->getCollisionFlags()  |
												btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK |
												btCollisionObject::CF_NO_CONTACT_RESPONSE );
   level_clear->visible = 0;

   i = 0;
   while( i != obj->n_objmesh ) { 

      OBJMESH *objmesh = &obj->objmesh[ i ];

      if( strstr( objmesh->name, "gem" ) ) { 

         objmesh->rotation.z = ( float )( random() % 360 );

         objmesh->btrigidbody->setCollisionFlags( objmesh->btrigidbody->getCollisionFlags() |
												  btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK );
      }
	  
	  ++i;
   }	
	
	
	player = OBJ_get_mesh( obj, "player", 0 );
	
	player->btrigidbody->setFriction( 6.7f );
	

	memcpy( &eye, &player->location, sizeof( vec3 ) );
	
	
	i = 0;
	while( i != obj->n_texture ) { 

		OBJ_build_texture( obj,
						   i,
						   obj->texture_path,
						   TEXTURE_MIPMAP ,
						   TEXTURE_FILTER_2X,
						   0.0f );
		++i;
	}


	i = 0;
	while( i != obj->n_program ) { 

		OBJ_build_program( obj,
						   i,
						   program_bind_attrib_location,
						   program_draw,
						   1,
						   obj->program_path );
		++i;
	}


	i = 0;
	while( i != obj->n_objmaterial ) { 

		OBJ_build_material( obj, i, NULL );
		
		++i;
	}	

	

	font_small = FONT_init( ( char * )"foo.ttf" );

	FONT_load( font_small,
			   font_small->name,
			   1,
			   24.0f,
			   512,
			   512,
			   32,
			   96 );
			   

	font_big = FONT_init( ( char * )"foo.ttf" );

	FONT_load( font_big,
			   font_big->name,
			   1,
			   48.0f,
			   512,
			   512,
			   32,
			   96 );
			   

	MEMORY *memory = NULL;

	i = 0;
	while( i != 4 ) {
	
		switch( i ) { 
		
			case 0: {

				memory = mopen( ( char * )"red.ogg", 1 );
				break;
			}
			case 1: {

				memory = mopen( ( char * )"green.ogg", 1 );
				break;
			}
			case 2: {

				memory = mopen( ( char * )"blue.ogg", 1 );
				break;
			}
			case 3: {

				memory = mopen( ( char * )"yellow.ogg", 1 );
				break;
			}
		}

		gems_soundbuffer[ i ] = SOUNDBUFFER_load( ( char * )"gem", memory );

		mclose( memory );
		
		gems_sound[ i ] =  SOUND_add( ( char * )"gem", gems_soundbuffer[ i ] );

		SOUND_set_volume( gems_sound[ i ], 1.0f );

		++i;
	}
	

	OBJMESH *objmesh = NULL;
	
	memory = mopen( ( char * )"water.ogg", 1 );

	water_soundbuffer = 
	SOUNDBUFFER_load( ( char * )"water", memory );

	mclose( memory );

	water_sound = SOUND_add( ( char * )"water", water_soundbuffer );

	objmesh = OBJ_get_mesh( obj, "water", 0 );

	SOUND_set_location( water_sound,
	&objmesh->location, 
	objmesh->radius );

	SOUND_set_volume( water_sound, 0.5f );

	SOUND_play( water_sound, 1 );


	memory = mopen( ( char * )"lava.ogg", 1 );
	
	lava_soundbuffer = SOUNDBUFFER_load( ( char * )"lava", memory );
	
	mclose( memory );
	
	lava_sound = SOUND_add( ( char * )"lava", lava_soundbuffer );

	objmesh = OBJ_get_mesh( obj, "lava", 0 );

	SOUND_set_location( lava_sound,
						&objmesh->location, 
						objmesh->radius );
						
	SOUND_set_volume( lava_sound, 0.5f );

	SOUND_play( lava_sound, 1 );


	memory = mopen( ( char * )"toxic.ogg", 1 );
	
	toxic_soundbuffer = SOUNDBUFFER_load( ( char * )"toxic", memory );

	mclose( memory );

	toxic_sound = SOUND_add( ( char * )"toxic", toxic_soundbuffer );
	
	objmesh = OBJ_get_mesh( obj, "toxic", 0 );

	SOUND_set_location( toxic_sound,
						&objmesh->location, 
						objmesh->radius );
	
	SOUND_set_volume( toxic_sound, 0.5f );
	
	SOUND_play( toxic_sound, 1 );


	memory = mopen( ( char * )"background.ogg", 1 );

	background_soundbuffer = SOUNDBUFFER_load_stream( ( char * )"background", memory );

	background_sound = SOUND_add( ( char * )"background", background_soundbuffer );
	
	SOUND_set_volume( background_sound, 0.5f );

	SOUND_play( background_sound, 1 );

	THREAD_play( thread );
}
示例#3
0
bool contact_added_callback( btManifoldPoint &btmanifoldpoint,
							 const btCollisionObject *btcollisionobject0,
							 int part_0, int index_0,
							 const btCollisionObject *btcollisionobject1,
							 int part_1, int index_1 ) {

	OBJMESH *objmesh0 = ( OBJMESH * )( ( btRigidBody * )btcollisionobject0 )->getUserPointer();

	OBJMESH *objmesh1 = ( OBJMESH * )( ( btRigidBody * )btcollisionobject1 )->getUserPointer();
	
	/* If one of the mesh object names is like Òlevel_clearÓ it means that the ball has reached the end target.
	 * Set the game state to 1 to indicate that the level has to be restarted. */
	if( ( strstr( objmesh0->name, "level_clear" ) || 
		  strstr( objmesh1->name, "level_clear" ) ) )
		game_state = 1;
	/* If the two mesh objects that are involved in the collision are a gem and the player. */
	else if( ( strstr( objmesh0->name, "player" ) || 
			   strstr( objmesh1->name, "player" ) )
			&&
			 ( strstr( objmesh0->name, "gem" ) || 
			   strstr( objmesh1->name, "gem" ) ) ) { 
		/* To store the gem mesh pointer and its collision object. */
		OBJMESH *objmesh = NULL;
		btCollisionObject *btcollisionobject = NULL;

		/* Depending on which mesh (either 0 or 1) is the gem, store the appropriate pointers. */
		if( strstr( objmesh0->name, "gem" ) ) {

			objmesh = objmesh0;
			btcollisionobject = ( btCollisionObject * )btcollisionobject0;
		}
		else { 
		
			objmesh = objmesh1;
			btcollisionobject = ( btCollisionObject * )btcollisionobject1;
		}

		/* Temporary variable to store the gem index for the sound source based
		 *  on the name of the current gem that gets picked up by the player. */
		unsigned char index = 0;
		/* If itÕs a red gem, add one gem point and store the index number 0. */
		if( strstr( objmesh->name, "red" ) ) { 
			
			gem_points += 1;
			index = 0;
		}
		else if( strstr( objmesh->name, "green" ) ) { 

			gem_points += 2;
			index = 1;
		}
		
		else if( strstr( objmesh->name, "blue" ) ) { 
			
			gem_points += 3;
			index = 2;
		}
		
		else if( strstr( objmesh->name, "yellow" ) ) { 
		
			gem_points += 4;
			index = 3;
		}

		/*Set the location of the sound source for the appropiate gem picked up and use the current location
		 * and the radius of the mesh to modify the sound source location and reference distance*/
		SOUND_set_location( gems_sound[ index ],
							&objmesh->location,
							/* Gems have a small radius. We will give it a boost so the player will hear the
							 * sound more clearly. */
							objmesh->radius * gem_factor );
		/* Play the sound source. */
		SOUND_play( gems_sound[ index ], 0 );
		/* Set the current gem mesh to be invisible.
		 * It has been picked up, so you donÕt want to draw it again. */
		objmesh->visible = 0;
		/* Remove the rigid body and associated data from the physical world.
		 * When a gem is picked, it cannot be part of the physics
		 * simulation any more. */
		delete objmesh->btrigidbody->getCollisionShape();

		delete objmesh->btrigidbody->getMotionState();

		dynamicsworld->removeRigidBody( objmesh->btrigidbody );

		dynamicsworld->removeCollisionObject( btcollisionobject );

		delete objmesh->btrigidbody;

		objmesh->btrigidbody = NULL;
	}		 

	return false;
}
示例#4
0
bool contact_added_callback( btManifoldPoint &btmanifoldpoint,
							 const btCollisionObject *btcollisionobject0,
							 int part_0, int index_0,
							 const btCollisionObject *btcollisionobject1,
							 int part_1, int index_1 )
{

	OBJMESH *objmesh0 = ( OBJMESH * )( ( btRigidBody * )btcollisionobject0 )->getUserPointer();

	OBJMESH *objmesh1 = ( OBJMESH * )( ( btRigidBody * )btcollisionobject1 )->getUserPointer();
	
	
	if( ( strstr( objmesh0->name, "level_clear" ) || 
		  strstr( objmesh1->name, "level_clear" ) ) )

		game_state = 1;


	else if( ( strstr( objmesh0->name, "player" ) || 
			   strstr( objmesh1->name, "player" ) )
			&&
			 ( strstr( objmesh0->name, "gem" ) || 
			   strstr( objmesh1->name, "gem" ) ) ) { 

		OBJMESH *objmesh = NULL;

		btCollisionObject *btcollisionobject = NULL;

		if( strstr( objmesh0->name, "gem" ) ) {

			objmesh = objmesh0;
			btcollisionobject = ( btCollisionObject * )btcollisionobject0;
		}
		else { 
		
			objmesh = objmesh1;
			btcollisionobject = ( btCollisionObject * )btcollisionobject1;
		}

		unsigned char index = 0;

		if( strstr( objmesh->name, "red" ) ) { 
			
			gem_points += 1;
			index = 0;
		}
		else if( strstr( objmesh->name, "green" ) ) { 

			gem_points += 2;
			index = 1;
		}
		
		else if( strstr( objmesh->name, "blue" ) ) { 
			
			gem_points += 3;
			index = 2;
		}
		
		else if( strstr( objmesh->name, "yellow" ) ) { 
		
			gem_points += 4;
			index = 3;
		}

		SOUND_set_location( gems_sound[ index ],
							&objmesh->location,
							objmesh->radius * gem_factor );


		SOUND_play( gems_sound[ index ], 0 );

		objmesh->visible = 0;

		delete objmesh->btrigidbody->getCollisionShape();

		delete objmesh->btrigidbody->getMotionState();

		dynamicsworld->removeRigidBody( objmesh->btrigidbody );

		dynamicsworld->removeCollisionObject( btcollisionobject );

		delete objmesh->btrigidbody;

		objmesh->btrigidbody = NULL;
	}		 

	return false;
}