コード例 #1
0
ファイル: SerialComm.cpp プロジェクト: Doufang/PistonRing
//读位数据
bool SerialComm::ReadCommRelay(UINT16 address, UINT16 number, bool* back)
{
	if (IsOpen())
	{
		if (number > 800)
		{
			number = 800;
		}
		byte byComm[8];
		byComm[0] = 0x1;
		byComm[1] = 0x1;
		byComm[2] = HIBYTE(address);
		byComm[3] = LOBYTE(address);
		byComm[4] = HIBYTE(number);
		byComm[5] = LOBYTE(number);

		TblCRC(byComm, 6);
		SendComm(byComm, 8);
		Sleep(10);
		if (ReadComm(myReceiveDate) && (myReceiveDate[1] == 1))
		{
			ByteToBool(myReceiveDate + 3, number, back);
			return true;
		}
	}
	
	return false;
}
コード例 #2
0
//=============================================================================
bool CRpgLmu::GetLowerChip(int x, int y, int anime, TextureInfoSet& texInfoSet)
{
	if(x<0 || x >= m_nWidth || y<0 || y>= m_nHeight)	return false;
	if (!m_saLower.GetPtr()) return false;

	int i, cx = 18, cy = 8, base_cx = 0, base_cy = 0;
	const uint16_t chip = m_saLower[y][x];
	const int nOffset[4][2] = {
		{0, 0},		// 左上
		{8, 0},		// 右上
		{0, 8},		// 左下
		{8, 8},		// 右下
	};
	//img.Create(CHIP_SIZE, CHIP_SIZE);
	//img.CopyPalette(imgChipSet);

	//if(x==2 && y==6){
	//	printf("%02X\n", chip);
	//}

	kuto::Vector2 size((float)CHIP_SIZE / imgChipSet.getWidth(), (float)CHIP_SIZE / imgChipSet.getHeight());
	kuto::Vector2 cacheSize((float)CHIP_SIZE / chipCacheTexture_.getWidth(), (float)CHIP_SIZE / chipCacheTexture_.getHeight());
	texInfoSet.size = 0;
	if(chip<3000){
		int cacheIndex = GetChipCacheIndex(chip, anime);
		if (cacheIndex >= 0) {
			cx = cacheIndex % (chipCacheTexture_.getWidth() / CHIP_SIZE);
			cy = cacheIndex / (chipCacheTexture_.getWidth() / CHIP_SIZE);
			texInfoSet.info[0].texcoord[0].set((float)(cx << 4) / chipCacheTexture_.getWidth(), (float)(cy << 4) / chipCacheTexture_.getHeight());
			texInfoSet.info[0].texcoord[1] = texInfoSet.info[0].texcoord[0] + cacheSize;
			texInfoSet.info[0].texture = &chipCacheTexture_;
			texInfoSet.size++;
			return true;
		}
		cacheIndex = GetEmptyChipCacheIndex();
		int destX = 0, destY = 0;
		if (cacheIndex >= 0) {
			destX = cacheIndex % (chipCacheTexture_.getWidth() / CHIP_SIZE) * CHIP_SIZE;
			destY = cacheIndex / (chipCacheTexture_.getWidth() / CHIP_SIZE) * CHIP_SIZE;
		}
		
		bool ocean_flag[4];

		/*
			0:浅瀬
			1:四隅に深海と境界を持つ浅瀬
			2:四隅に浅瀬と境界を持つ深海
			3:深海
		*/
		int  ocean_type[4];

		GetOceanType(chip, ocean_flag);

		// 海のタイプを調べる
		// 0〜
		if(chip<1000){		// 	海岸線A1(普通の陸)
			base_cx = 0;
			base_cy = 0;
			for(i = 0; i < 4; i++){
				if(!ocean_flag[i])	ocean_type[i] = 0;
				else				ocean_type[i] = 1;
			}
		}
		// 1000〜
		else if(chip<2000){	// 海岸線A2(雪)
			base_cx = 3;
			base_cy = 0;
			for(i = 0; i < 4; i++){
				if(!ocean_flag[i])	ocean_type[i] = 0;
				else				ocean_type[i] = 1;
			}
		}
		// 2000〜
		else if(chip<3000){	// 海岸線A1(普通の陸)
			base_cx = 0;
			base_cy = 0;
			for(i = 0; i < 4; i++){
				if(!ocean_flag[i])	ocean_type[i] = 3;
				else				ocean_type[i] = 2;
			}
		}

		// ベースとなる海をセット
		for(i = 0; i < 4; i++){
			if (cacheIndex >= 0) {
				int x = ((anime % 3)      <<4) + nOffset[i][0];
				int y = ((ocean_type[i]+4)<<4) + nOffset[i][1];
				for (int j = 0; j < 8; j++) {
					char* destPtr = chipCacheTexture_.getData() + (destX + nOffset[i][0]) * 4 + ((j + destY + nOffset[i][1]) * chipCacheTexture_.getWidth() * 4);
					const char* srcPtr = imgChipSet.getData() + x * 4 + ((j + y) * imgChipSet.getWidth() * 4);
					std::memcpy(destPtr, srcPtr, 8 * 4);
				}
			} else {
				int x = ((anime % 3)      <<4) + nOffset[i][0];
				int y = ((ocean_type[i]+4)<<4) + nOffset[i][1];
				//img.Blt(imgChipSet, nOffset[i][0], nOffset[i][1], x, y, CHIP_SIZE>>1, CHIP_SIZE>>1, false);
				texInfoSet.info[i].texcoord[0].set((float)(x) / imgChipSet.getWidth(), (float)(y) / imgChipSet.getHeight());
				texInfoSet.info[i].texcoord[1] = texInfoSet.info[i].texcoord[0] + size * 0.5f;
				texInfoSet.info[i].texture = &imgChipSet;
				texInfoSet.size++;
			}
		}

		/*
			00:海・海岸線無し

			01:海岸線・左上
			02:海岸線・右上
			04:海岸線・右下
			08:海岸線・左下

			10:海岸線・左
			11:海岸線・左+右上
			12:海岸線・左   +右下
			13:海岸線・左+右上+右下

			14:海岸線・上
			15:海岸線・上+右下
			16:海岸線・上   +左下
			17:海岸線・上+右下+左下

			18:海岸線・右
			19:海岸線・右+左下
			1A:海岸線・右   +左上
			1B:海岸線・右+左下+左上

			1C:海岸線・下
			1D:海岸線・下+左上
			1E:海岸線・下   +右上
			1F:海岸線・下+左上+右上

			20:海岸線・左+右
			21:海岸線・上+下

			AB
			CD
			22:海岸線・ABC
			23:海岸線・ABC +右下
			24:海岸線・AB D
			25:海岸線・AB D+左下
			26:海岸線・ BCD
			27:海岸線・ BCD+左上
			28:海岸線・A CD
			29:海岸線・A CD+右上
			2A:海岸線・CABD
			2B:海岸線・BACD
			2C:海岸線・ACDB
			2D:海岸線・ABDC
			2E:海岸線・丸(ABCD)
		*/
		int chip2 = chip%50;

		enum{
			A,	// 四辺
			B,	// 上下
			C,	// 左右
			D,	// 四隅

			OCEAN,	// 海岸線なし(下の海チップをそのまま採用)
		};
		int nChip[4] = {OCEAN, OCEAN, OCEAN, OCEAN};

		if(chip2<0x10){
			bool flag[8];
			ByteToBool((unsigned char)chip2, flag);
			if(flag[0])		nChip[0] = D;	// 左上
			if(flag[1])		nChip[1] = D;	// 右上
			if(flag[2])		nChip[3] = D;	// 右下
			if(flag[3])		nChip[2] = D;	// 左下
		}
		// 0x10〜
		else if(chip2<0x14){
			nChip[0] = nChip[2] = B;
			if(chip2 & 0x01)	nChip[1] = D;
			if(chip2 & 0x02)	nChip[3] = D;
		}
		// 0x14〜
		else if(chip2<0x18){
			nChip[0] = nChip[1] = C;
			if(chip2 & 0x01)	nChip[3] = D;
			if(chip2 & 0x02)	nChip[2] = D;
		}
		// 0x18〜
		else if(chip2<0x1C){
			nChip[1] = nChip[3] = B;
			if(chip2 & 0x01)	nChip[2] = D;
			if(chip2 & 0x02)	nChip[0] = D;
		}
		// 0x1C〜
		else if(chip2<0x20){
			nChip[2] = nChip[3] = C;
			if(chip2 & 0x01)	nChip[0] = D;
			if(chip2 & 0x02)	nChip[1] = D;
		}
		else if(chip2==0x20){
			nChip[0] = nChip[1] = nChip[2] = nChip[3] = B;
		}
		else if(chip2==0x21){
			nChip[0] = nChip[1] = nChip[2] = nChip[3] = C;
		}
		else if(chip2<=0x23){
			nChip[0] = A;
			nChip[2] = B;
			nChip[1] = C;
			if(chip2 & 0x01)	nChip[3] = D;
		}
		else if(chip2<=0x25){
			nChip[1] = A;
			nChip[3] = B;
			nChip[0] = C;
			if(chip2 & 0x01)	nChip[2] = D;
		}
		else if(chip2<=0x27){
			nChip[3] = A;
			nChip[1] = B;
			nChip[2] = C;
			if(chip2 & 0x01)	nChip[0] = D;
		}
		else if(chip2<=0x29){
			nChip[2] = A;
			nChip[0] = B;
			nChip[3] = C;
			if(chip2 & 0x01)	nChip[1] = D;
		}
		else if(chip2==0x2A){
			nChip[0] = nChip[1] = A;
			nChip[2] = nChip[3] = B;
		}
		else if(chip2==0x2B){
			nChip[0] = nChip[2] = A;
			nChip[1] = nChip[3] = C;
		}
		else if(chip2==0x2C){
			nChip[2] = nChip[3] = A;
			nChip[0] = nChip[1] = B;
		}
		else if(chip2==0x2D){
			nChip[1] = nChip[3] = A;
			nChip[0] = nChip[2] = C;
		}
	//	else if(chip2==0x2E){
		else{
			nChip[0] = nChip[1] = nChip[2] = nChip[3] = A;
		}

		// 海岸線を描画
		for(i = 0; i < 4; i++){
			if (cacheIndex >= 0) {
				if(nChip[i] != OCEAN){
					int x = ((base_cx + anime % 3)<<4) + nOffset[i][0];
					int y = ((base_cy + nChip[i]) <<4) + nOffset[i][1];
					for (int j = 0; j < 8; j++) {
						char* destPtr = chipCacheTexture_.getData() + (destX + nOffset[i][0]) * 4 + ((j + destY + nOffset[i][1]) * chipCacheTexture_.getWidth() * 4);
						const char* srcPtr = imgChipSet.getData() + x * 4 + ((j + y) * imgChipSet.getWidth() * 4);
						std::memcpy(destPtr, srcPtr, 8 * 4);
					}
				}
			} else {
				if(nChip[i] != OCEAN){
					int x = ((base_cx + anime % 3)<<4) + nOffset[i][0];
					int y = ((base_cy + nChip[i]) <<4) + nOffset[i][1];
					//img.Blt(imgChipSet, nOffset[i][0], nOffset[i][1], x, y, CHIP_SIZE>>1, CHIP_SIZE>>1, false);
					texInfoSet.info[i+4].texcoord[0].set((float)(x) / imgChipSet.getWidth(), (float)(y) / imgChipSet.getHeight());
					texInfoSet.info[i+4].texcoord[1] = texInfoSet.info[i+4].texcoord[0] + size * 0.5f;
					texInfoSet.info[i+4].texture = &imgChipSet;
				} else {
					texInfoSet.info[i+4].texture = NULL;
				}
				texInfoSet.size++;
			}
		}

		if (cacheIndex >= 0) {
			chipCacheTexture_.updateImage();
			texInfoSet.info[0].texcoord[0].set((float)(destX) / chipCacheTexture_.getWidth(), (float)(destY) / chipCacheTexture_.getHeight());
			texInfoSet.info[0].texcoord[1] = texInfoSet.info[0].texcoord[0] + cacheSize;
			texInfoSet.info[0].texture = &chipCacheTexture_;
			texInfoSet.size++;
			chipCache_[cacheIndex].chipID = chip;
			chipCache_[cacheIndex].animeCount = anime;
			return true;
		}
		return true;
	}
	// 3000〜(3028)
	else if(chip<3050){
		// C1
		cx = 3;
		cy = 4;
		cy += anime % 4;
	}
	// 3050〜(3078)
	else if(chip<3100){
		// C2
		cx = 4;
		cy = 4;
		cy += anime % 4;
	}
	// 3100〜(3128)
	else if(chip<3150){
		// C3
		cx = 5;
		cy = 4;
		cy += anime % 4;
	}
	// 3150〜
	else if(chip<3200){
		// D1
		cx = 0;
		cy = 8;
	}
	// 3200〜
	else if(chip<3250){
		// D2
		cx = 3;
		cy = 8;
	}
	// 3250〜
	else if(chip<3300){
		// D3
		cx = 0;
		cy = 12;
	}
	// 3300〜
	else if(chip<3350){
		// D4
		cx = 3;
		cy = 12;
	}
	// 3350〜
	else if(chip<3400){
		// D5
		cx = 6;
		cy = 0;
	}
	// 3400〜
	else if(chip<3450){
		// D6
		cx = 9;
		cy = 0;
	}
	// 3450〜
	else if(chip<3500){
		// D7
		cx = 6;
		cy = 4;
	}
	// 3500〜
	else if(chip<3550){
		// D8
		cx = 9;
		cy = 4;
	}
	// 3550〜
	else if(chip<3600){
		// D9
		cx = 6;
		cy = 8;
	}
	// 3600〜
	else if(chip<3650){
		// D10
		cx = 9;
		cy = 8;
	}
	// 3650〜
	else if(chip<3700){
		// D11
		cx = 6;
		cy = 12;
	}
	// 3700〜
	else if(chip<3750){
		// D12
		cx = 9;
		cy = 12;
	}
	// 3750〜
	else if(chip<4000);		// たぶん存在しない
	// 4000〜
	else if(chip<5000){
		// 相対位置(ア〜エ→A〜D)
		enum{
			A,
			B,
			C,
			D7,	// Dはテンキーと対応
			D8,
			D9,
			D4,
			D5,
			D6,
			D1,
			D2,
			D3,
		};
		// 相対位置→相対座標
		static const int nOffset2[12][2] = {
			{0, 0},
			{1, 0},
			{2, 0},
			{0, 1},
			{1, 1},
			{2, 1},
			{0, 2},
			{1, 2},
			{2, 2},
			{0, 3},
			{1, 3},
			{2, 3},
		};

		// 4000〜
		if(chip<4050){
			// D1
			base_cx = 0;
			base_cy = 8;
		}
		// 4050〜
		else if(chip<4100){
			// D2
			base_cx = 3;
			base_cy = 8;
		}
		// 4100〜
		else if(chip<4150){
			// D3
			base_cx = 0;
			base_cy = 12;
		}
		// 4150〜
		else if(chip<4200){
			// D4
			base_cx = 3;
			base_cy = 12;
		}
		// 4200〜
		else if(chip<4250){
			// D5
			base_cx = 6;
			base_cy = 0;
		}
		// 4250〜
		else if(chip<4300){
			// D6
			base_cx = 9;
			base_cy = 0;
		}
		// 4300〜
		else if(chip<4350){
			// D7
			base_cx = 6;
			base_cy = 4;
		}
		// 4350〜
		else if(chip<4400){
			// D8
			base_cx = 9;
			base_cy = 4;
		}
		// 4400〜
		else if(chip<4450){
			// D9
			base_cx = 6;
			base_cy = 8;
		}
		// 4450〜
		else if(chip<4500){
			// D10
			base_cx = 9;
			base_cy = 8;
		}
		// 4500〜
		else if(chip<4550){
			// D11
			base_cx = 6;
			base_cy = 12;
		}
		// 4550〜
		else if(chip<4600){
			// D12
			base_cx = 9;
			base_cy = 12;
		}

		/*
			00:中央部

			01:四隅・左上
			02:四隅・右上
			04:四隅・右下
			08:四隅・左下

			10:集合・左
			11:集合・左+右上
			12:集合・左   +右下
			13:集合・左+右上+右下

			14:集合・上
			15:集合・上+右下
			16:集合・上   +左下
			17:集合・上+右下+左下

			18:集合・右
			19:集合・右+左下
			1A:集合・右   +左上
			1B:集合・右+左下+左上

			1C:集合・下
			1D:集合・下+左上
			1E:集合・下   +右上
			1F:集合・下+左上+右上

			20:集合・左+右
			21:集合・上+下

			// ここから地形チップ独自規格∧||∧
			22:集合・左上
			23:集合・左上+四隅・右下

			24:集合・右上
			25:集合・右上+四隅・左下

			26:集合・右下
			27:集合・右下+四隅・左上

			28:集合・左下
			29:集合・左下+四隅・右上

			2A:集合(左上+右上)
			2B:集合(左上+左下)
			2C:集合(左下+右下)
			2D:集合(右上+右下)
			2E:集合(左上+右上+左下+右下)
		*/
		int nChip[4] = {D5, D5, D5, D5};

		int chip2 = chip % 50;
		// 0x00〜
		if(chip2<0x10){
			//bool flag[8];
			//ByteToBool((unsigned char)chip2, flag);
			//for(i = 0; i < 4; i++){
			//	if(flag[i])		nChip[i] = C;
			//}
			if (chip2 & 0x01) nChip[0] = C;
			if (chip2 & 0x02) nChip[1] = C;
			if (chip2 & 0x04) nChip[3] = C;
			if (chip2 & 0x08) nChip[2] = C;
		}
		// 0x10〜
		else if(chip2<0x14){
			nChip[0] = nChip[2] = D4;
			if(chip2 & 0x01)	nChip[1] = C;
			if(chip2 & 0x02)	nChip[3] = C;
		}
		// 0x14〜
		else if(chip2<0x18){
			nChip[0] = nChip[1] = D8;
			if(chip2 & 0x01)	nChip[3] = C;
			if(chip2 & 0x02)	nChip[2] = C;
		}
		// 0x18〜
		else if(chip2<0x1C){
			nChip[1] = nChip[3] = D6;
			if(chip2 & 0x01)	nChip[2] = C;
			if(chip2 & 0x02)	nChip[0] = C;
		}
		// 0x1C〜
		else if(chip2<0x20){
			nChip[2] = nChip[3] = D2;
			if(chip2 & 0x01)	nChip[0] = C;
			if(chip2 & 0x02)	nChip[1] = C;
		}
		else if(chip2==0x20){
			nChip[0] = nChip[2] = D4;
			nChip[1] = nChip[3] = D6;
		}
		else if(chip2==0x21){
			nChip[0] = nChip[1] = D8;
			nChip[2] = nChip[3] = D2;
		}
		else if(chip2<=0x23){
			nChip[0] = nChip[1] = nChip[2] = nChip[3] = D7;
			if(chip2 & 0x01)	nChip[3] = C;
		}
		else if(chip2<=0x25){
			nChip[0] = nChip[1] = nChip[2] = nChip[3] = D9;
			if(chip2 & 0x01)	nChip[2] = C;
		}
		else if(chip2<=0x27){
			nChip[0] = nChip[1] = nChip[2] = nChip[3] = D3;
			if(chip2 & 0x01)	nChip[0] = C;
		}
		else if(chip2<=0x29){
			nChip[0] = nChip[1] = nChip[2] = nChip[3] = D1;
			if(chip2 & 0x01)	nChip[1] = C;
		}
		else if(chip2==0x2A){
			nChip[0] = nChip[2] = D7;
			nChip[1] = nChip[3] = D9;
		}
		else if(chip2==0x2B){
			nChip[0] = nChip[1] = D7;
			nChip[2] = nChip[3] = D1;
		}
		else if(chip2==0x2C){
			nChip[0] = nChip[2] = D1;
			nChip[1] = nChip[3] = D3;
		}
		else if(chip2==0x2D){
			nChip[0] = nChip[1] = D9;
			nChip[2] = nChip[3] = D3;
		}
	//	else if(chip2==0x2E){
		else{
			nChip[0] = D7;
			nChip[1] = D9;
			nChip[2] = D1;
			nChip[3] = D3;
		}

		// チップを描画
		int cacheIndex = GetChipCacheIndex(chip, 0);
		if (cacheIndex >= 0) {
			cx = cacheIndex % (chipCacheTexture_.getWidth() / CHIP_SIZE);
			cy = cacheIndex / (chipCacheTexture_.getWidth() / CHIP_SIZE);
			texInfoSet.info[0].texcoord[0].set((float)(cx << 4) / chipCacheTexture_.getWidth(), (float)(cy << 4) / chipCacheTexture_.getHeight());
			texInfoSet.info[0].texcoord[1] = texInfoSet.info[0].texcoord[0] + cacheSize;
			texInfoSet.info[0].texture = &chipCacheTexture_;
			texInfoSet.size++;
			return true;
		}
		cacheIndex = GetEmptyChipCacheIndex();
		int destX = 0, destY = 0;
		if (cacheIndex >= 0) {
			destX = cacheIndex % (chipCacheTexture_.getWidth() / CHIP_SIZE) * CHIP_SIZE;
			destY = cacheIndex / (chipCacheTexture_.getWidth() / CHIP_SIZE) * CHIP_SIZE;
		}
		
		for(i = 0; i < 4; i++){
			if (cacheIndex >= 0) {
				int x = ( (base_cx+nOffset2[nChip[i]][0])<<4 ) + nOffset[i][0];
				int y = ( (base_cy+nOffset2[nChip[i]][1])<<4 ) + nOffset[i][1];
				for (int j = 0; j < 8; j++) {
					char* destPtr = chipCacheTexture_.getData() + (destX + nOffset[i][0]) * 4 + ((j + destY + nOffset[i][1]) * chipCacheTexture_.getWidth() * 4);
					const char* srcPtr = imgChipSet.getData() + x * 4 + ((j + y) * imgChipSet.getWidth() * 4);
					std::memcpy(destPtr, srcPtr, 8 * 4);
				}
			} else {
				int x = ( (base_cx+nOffset2[nChip[i]][0])<<4 ) + nOffset[i][0];
				int y = ( (base_cy+nOffset2[nChip[i]][1])<<4 ) + nOffset[i][1];
				//img.Blt(imgChipSet, nOffset[i][0], nOffset[i][1], x, y, CHIP_SIZE>>1, CHIP_SIZE>>1, false);
				texInfoSet.info[i].texcoord[0].set((float)(x) / imgChipSet.getWidth(), (float)(y) / imgChipSet.getHeight());
				texInfoSet.info[i].texcoord[1] = texInfoSet.info[i].texcoord[0] + size * 0.5f;
				texInfoSet.info[i].texture = &imgChipSet;
				texInfoSet.size++;
			}
		}

		if (cacheIndex >= 0) {
			chipCacheTexture_.updateImage();
			texInfoSet.info[0].texcoord[0].set((float)(destX) / chipCacheTexture_.getWidth(), (float)(destY) / chipCacheTexture_.getHeight());
			texInfoSet.info[0].texcoord[1] = texInfoSet.info[0].texcoord[0] + cacheSize;
			texInfoSet.info[0].texture = &chipCacheTexture_;
			texInfoSet.size++;
			chipCache_[cacheIndex].chipID = chip;
			chipCache_[cacheIndex].animeCount = 0;
			return true;
		}

		return true;
	}
	// 5000〜
	else{				// ブロックE・標準チップ
		GetChipPos(chip, false, &cx, &cy);
	}


	// cx,cyを座標として採用する(4分割しないチップ)
	//img.Blt(imgChipSet, 0, 0, cx<<4, cy<<4, CHIP_SIZE, CHIP_SIZE, false);
	texInfoSet.info[0].texcoord[0].set((float)(cx << 4) / imgChipSet.getWidth(), (float)(cy << 4) / imgChipSet.getHeight());
	texInfoSet.info[0].texcoord[1] = texInfoSet.info[0].texcoord[0] + size;
	texInfoSet.info[0].texture = &imgChipSet;
	texInfoSet.size++;
	return true;
}