Ejemplo n.º 1
0
	void SpriteRenderer::setOrigin(OriginPosition origin_position){
		int x_size = getSpriteSize().x;
		int y_size = getSpriteSize().y;


		switch(origin_position){
			case CENTER:
				this->origin_offset = Vector2(x_size / 2, y_size / 2);
				break;
			case LEFT_BOTTOM:
				this->origin_offset = Vector2(0, y_size);
				break;
			case LEFT_MIDDLE:
				this->origin_offset = Vector2(0, y_size / 2);
				break;
			case LEFT_TOP:
				this->origin_offset = Vector2(0, 0);
				break;
			case MIDDLE_BOTTOM:
				this->origin_offset = Vector2(x_size / 2, y_size);
				break;
			case MIDDLE_TOP:
				this->origin_offset = Vector2(x_size / 2, 0);
				break;
			case RIGHT_BOTTOM:
				this->origin_offset = Vector2(x_size, y_size);
				break;
			case RIGHT_MIDDLE:
				this->origin_offset = Vector2(x_size, y_size / 2);
				break;
			case RIGHT_TOP:
				this->origin_offset = Vector2(x_size, 0);
				break;
		}

		this->origin = origin_position;
		this->sprite.setOrigin(this->origin_offset.x, this->origin_offset.y);
	}
Ejemplo n.º 2
0
void assets::parseZbe()
{
	// TODO: actually handle the return values on all these file functions.
	//       They're all ignored so there is not fault tolerance.

	// Open up the file to parse
	openFile();

	// Get the version number out of the zeg file
	uint16 version = load<uint16>(zbeData);

	// Check to see if it has the testing flag
#ifndef ZBE_TESTING
	if (version & 1 << 15)
	{
		iprintf("Error: Attempting to open a\nTesting ZBE file without\ntesting enabled.\n");
		die();
	}

	// Check to see if it's the supported version
	if (version != ZBE_VERSION_SUPPORTED)
#else
	if (version != (ZBE_VERSION_SUPPORTED | 1 << 15))
#endif
	{
		iprintf("Error: Attempting to open a\nZBE file with an unsupported\nversion\n");
		die();
	}

	// Clear the testing flag and print the version
	iprintf("v %d", (version & 0x7FFF) );
#ifdef ZBE_TESTING
	iprintf(" with testing flag\n");
#else
	iprintf("\n");
#endif

	// Get the total number of assets
	uint32 numAssets = load<uint32>(zbeData);
	iprintf("#assets %d\n", numAssets);

	// Get the number of tiles
	uint32 numTiles = load<uint32>(zbeData);
	iprintf("#gfx %d\n", numTiles);

	// Get all of the gfx tiles
	for (uint32 i = 0; i < numTiles; i++)
	{
		// Make a new gfxAsset for the vector
		gfxAsset *newAsset = new gfxAsset;

		// The very first byte of data in gfx tiles is its width, second is height
		uint8 width = load<uint8>(zbeData);
		uint8 height = load<uint8>(zbeData);
		uint8 top = load<uint8>(zbeData);
		uint8 left = load<uint8>(zbeData);
		iprintf(" %d x %d @ (%d, %d)\n", width, height, top, left);

		// Set its dimensions
		newAsset->dimensions = vector2D<uint8>(width, height);
		newAsset->topleft = vector2D<uint8>(left, top);

		// Set its spriteSize
		newAsset->size = getSpriteSize(width, height);

		// Get the length of this gfx tiles
		newAsset->length = load<uint16>(zbeData);
		iprintf(" len %d\n, ", newAsset->length);

		// Add the current position in the file to the vector
		fpos_t curPos;
		fgetpos(zbeData, &curPos);
		newAsset->position = curPos;

		// Set that it is not loaded, assign a value to the union
		// to get it using the proper value
		newAsset->mmLoaded = newAsset->vmLoaded = false;
		newAsset->offset = NULL;

		// Push this asset on the array
		gfxAssets.push_back(newAsset);

		// Seek past this object
		fseek(zbeData, newAsset->length, SEEK_CUR);
	}




	// pause if we're testing
	pauseIfTesting();




	// Get the number of background tilesets
	uint32 numTilesets = load<uint32>(zbeData);
	iprintf("#tilesets %d\n", numTilesets);

	// Load up all those tilesets
	for (uint32 i = 0; i < numTilesets; i++)
	{
		// Make a new gfxAsset for the vector
		gfxAsset *newAsset = new gfxAsset;

		// Load up the length
		newAsset->length = load<uint16>(zbeData);
		iprintf(" #%d: %dB\n", i, newAsset->length);

		// Save the position
		fpos_t curPos;
		fgetpos(zbeData, &curPos);
		newAsset->position = curPos;

		// Seek past the data
		fseek(zbeData, newAsset->length, SEEK_CUR);

		// Duh.. add it to the tilesetAssets vector
		tilesetAssets.push_back(newAsset);
	}



	// pause if we're testing
	pauseIfTesting();




	// Get the number of palettes
	uint32 numPals = load<uint32>(zbeData);
	iprintf("#pals %d\n", numPals);

	// Get all of the palettes
	for (uint32 i = 0; i < numPals; i++)
	{
		// Make a new paletteAsset
		paletteAsset *newAsset = new paletteAsset;

		// Get the length of this palette
		newAsset->length = load<uint16>(zbeData);
		iprintf(" %d's len %d\n", i, newAsset->length);

		// Add the current position in the file to the asset
		fpos_t curPos;
		fgetpos(zbeData, &curPos);
		newAsset->position = curPos;

		// Initialize the offset so the union is set up
		// Then mark that it isn't loaded
		newAsset->index = 0;
		newAsset->mmLoaded = newAsset->vmLoaded = false;

		// Push the new asset on to the array
		paletteAssets.push_back(newAsset);

		// Seek past this object
		fseek(zbeData, newAsset->length, SEEK_CUR);
	}




	// pause if we're testing
	pauseIfTesting();




	// Number of backrounds
	uint32 numBackgrounds = load<uint32>(zbeData);
	iprintf("#bgs %d\n", numBackgrounds);

	// Get all the backgrounds
	for (unsigned int i = 0; i < numBackgrounds; i++)
	{
		// make a new backgroundAsset
		backgroundAsset *newAsset = new backgroundAsset();

		// set its width and height
		newAsset->w = load<uint32>(zbeData);
		newAsset->h = load<uint32>(zbeData);

		// Save the position of all the tiles data
		fpos_t curPos;
		fgetpos(zbeData, &curPos);
		newAsset->position = curPos;

		// Get number of palettes
		uint8 numPalettes = load<uint8>(zbeData);

		iprintf(" %d x %d using %d palettes\n", (int) newAsset->w, (int) newAsset->h, (int) numPalettes);

		// Seek past all those palette ids
		fseek(zbeData, numPalettes * sizeof(uint32), SEEK_CUR);

		// Get the size of the map data
		newAsset->length = load<uint32>(zbeData);
		iprintf(" %dB map data\n", newAsset->length);

		// Seek past all the map data
		fseek(zbeData, newAsset->length, SEEK_CUR);

		// Put this backgroundAsset on the vector
		backgroundAssets.push_back(newAsset);
	}





	// pause if we're testing
	pauseIfTesting();





	// Number of objects
	uint32 numObjects = load<uint32>(zbeData);
	iprintf("#objs %d\n", numObjects);

	// Get all the objects
	for (uint32 i = 0; i < numObjects; i++)
	{
		// Weight of this object
		uint8 weight = load<uint8>(zbeData);

		// Number of animations
		uint32 numAnimations = load<uint32>(zbeData);
		iprintf(" %d: %d animations\n", i, numAnimations);

		// Make the objectAsset and allocate for enough animations
		objectAsset *newAsset = new objectAsset(weight);
		newAsset->animations = new frameAsset**[numAnimations + 1];
		// null terminate that dimension
		newAsset->animations[numAnimations] = NULL;

		// Get all the animations
		for (uint32 j = 0; j < numAnimations; j++)
		{
			// Get the number of frames for this animation
			uint16 numFrames = load<uint16>(zbeData);
			iprintf("  %d has %d frames\n", j, numFrames);

			// Allocate enough pointers for this animation
			newAsset->animations[j] = new frameAsset*[numFrames + 1];
			// null terminate it
			newAsset->animations[j][numFrames] = NULL;

			// Get all the frames
			for (uint32 k = 0; k < numFrames; k++)
			{
				// Make a new frameAsset (Init the gfx pointer to NULL)
				frameAsset *thisFrame = new frameAsset;

				// The gfx for this animation frame
				uint32 gfxId = load<uint32>(zbeData);
				thisFrame->gfx = gfxAssets[gfxId];

				// The palette for this animation frame
				uint32 palId = load<uint32>(zbeData);
				thisFrame->pal = paletteAssets[palId];

				// The time to display this frame
				thisFrame->time = load<uint8>(zbeData);

				// Set it up on the array
				newAsset->animations[j][k] = thisFrame;

				iprintf("   %d w/ %d for %d blanks\n", gfxId, palId, thisFrame->time);
			} // this animation

		} // all animations

		// make a new objectAsset
		objectAssets.push_back(newAsset);

	} // all objects




	// pause if we're testing
	pauseIfTesting();




	// Number of levels
	uint32 numLvls = load<uint32>(zbeData);
	iprintf("#lvls %d\n", numLvls);

	// for each level
	for (uint32 i = 0; i < numLvls; i++)
	{
		// Make a new levelAsset
		levelAsset *newAsset = new levelAsset;
		newAsset->objects = NULL;

		// Get the level name's length and allocate space for the string
		uint32 nameLen = load<uint32>(zbeData);
		if (nameLen > 0)
		{
			newAsset->name = new char[nameLen + 1];
			// Load up the name string
			for (unsigned int c = 0; c < nameLen; c++)
				newAsset->name[c] = (char) load<uint8>(zbeData);
			newAsset->name[nameLen] = '\0';
		}
		iprintf(" %d: %s\n", i, newAsset->name);

		// Get the level's dimensions

		// Set the location variable
		fpos_t curPos;
		fgetpos(zbeData, &curPos);
		newAsset->position = curPos;

		// Push this asset onto the levelAssets vector
		levelAssets.push_back(newAsset);

		// Skip over the level's dimensions
		load<uint32>(zbeData);
		load<uint32>(zbeData);

#ifdef ZBE_TESTING
		// Skip over the test explanation message
		uint32 expLen = load<uint32>(zbeData);
		fseek(zbeData, expLen * sizeof(uint8), SEEK_CUR);

		// Skip over the debug explanation
		uint32 dbgLen = load<uint32>(zbeData);
		fseek(zbeData, dbgLen * sizeof(uint8), SEEK_CUR);
		iprintf(" exp %d chars, dbg %d chars\n", (int) expLen, (int) dbgLen);

		// Skip the timer
		uint16 timer = load<uint16>(zbeData);
		iprintf(" %d timer", (int) timer);
#endif

		// Skip over the backgrounds
		// NOTE: keep this up to date!
		//                        bg0     bg1     bg2     bg3     tileset
		const static int bgSize = 4 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 4;
		fseek(zbeData, bgSize, SEEK_CUR);
		iprintf(" bgs\n");

		// The total number of bytes it takes to represent one level object in the
		// assets file.
		// NOTE: Keep this up to date!
		//                            object Id  X   Y   hgrav vgrav
		const static int lvlObjSize = 4 +        2 + 2 + 4 +   4;

		// number of level heroes
		uint32 numLvlHeroes = load<uint32>(zbeData);

		// Seek past those heroes
		fseek(zbeData, lvlObjSize * numLvlHeroes, SEEK_CUR);
		iprintf(" %d heroes", numLvlHeroes);

		// number of level objects
		uint32 numLvlObjs = load<uint32>(zbeData);

		// Seek past all the level objects
		fseek(zbeData, lvlObjSize * numLvlObjs, SEEK_CUR);
		iprintf(" %d objs\n", numLvlObjs);
	}

	closeFile();
}