mapblockset.cpp

来自「一个symbian 冒险游戏代码」· C++ 代码 · 共 86 行

CPP
86
字号
#include "MapBlockSet.h"
#include "GameApp.h"
#include <gr/SurfaceFormat.h>
#include <io/OutputStream.h>
#include <io/FileInputStream.h>
#include <io/FileOutputStream.h>
#include <io/IOException.h>
#include <img/ImageReader.h>
#include <assert.h>
#include <string.h>
#include <config.h>


using namespace gr;
using namespace io;
using namespace img;
using namespace lang;


void MapBlockSet::load( const char *filename, int blockwidth, int blockheight )
{
	if ( strcmp( name, filename ) == 0 ) //already loaded?
		return;

	String::cpy( name, sizeof(name), filename );

	String::cpy( loadname, sizeof(loadname), filename );
	if ( GameApp::get()->isHalfResolution() )
	{
		// "filename.ext" => "filename_half.ext"
		const char* halfIdentifier = "_half";
		const int originalLength = strlen( loadname );
		const int identifierLength = strlen( halfIdentifier );
		assert( originalLength == (int) strlen( name ) );
		assert( (int)sizeof(loadname) >= (originalLength + identifierLength) );
		int index = originalLength - 1;
		for ( ; index >= 0; index-- ) if ( loadname[index] == '.' ) break;							// find '.'
		const int extensionIndex = index;
		for ( int i = 0; i < identifierLength; i++ ) loadname[index++] = halfIdentifier[i];			// add half identifier
		for ( int i = extensionIndex; i < originalLength; i++ ) loadname[index++] = name[i];		// add original extension
		loadname[index] = 0;																		// terminate string
		assert( index < (int)sizeof(loadname) );

		// half width, half height
		blockwidth >>= 1;
		blockheight >>= 1;
	}

	FileInputStream fin( GameApp::get()->expandPath( ("data/maps/blockbitmaps/"+String(loadname)).c_str() ) );
	ImageReader reader( &fin, ImageReader::guessFileFormat(loadname) );
	
	if ( reader.format().type() != SurfaceFormat::SURFACE_P8 )
		throwError( IOException( Format("Map block bitmap {0} needs to be 8-bit", loadname) ) );

	width = reader.surfaceWidth();
	height = reader.surfaceHeight();
	format = reader.format();
	pitch = width * ( format.bitsPerPixel()/8 );
	int bitsperpixel = format.bitsPerPixel();
	SurfaceFormat dstpalfmt = SurfaceFormat::SURFACE_A4R4G4B4;
	dstpalfmt.copyPixels( palette, SurfaceFormat(), 0, reader.paletteFormat(), reader.paletteData(), SurfaceFormat(), 0, 256 );
	
	data.resize( height * pitch );
	reader.readSurface( data.begin(), pitch, width, height, format, palette, SurfaceFormat::SURFACE_A4R4G4B4 );
	palette[data[0]] &= 0x0FFF;	// make first pixel color transparent

	// calculate block bitmap bits
	assert( blockwidth > 0 );
	assert( blockheight > 0 );
	int xblocks = width / blockwidth;
	int yblocks = height / blockheight;
	int numblocks = xblocks * yblocks;
	blockbits.resize( numblocks );
	assert( xblocks > 0 );
	for ( int i = 0 ; i < numblocks ; ++i )
	{
		int xindex = i % xblocks;
		int yindex = i / xblocks;
		int x = xindex * blockwidth;
		int y = yindex * blockheight;
		blockbits[i] = &data[ y * pitch + ( x * ( bitsperpixel >> 3 ) ) ];
	}
}

// End of file

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?