gpt4 book ai didi

c++ - 有哪些技巧可以使具有大量计算的函数变得干净?

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:36 25 4
gpt4 key购买 nike

<分区>

如何实现这样的功能:

void Map::Display()
{
if(initialized)
{
HRESULT hr;
int hScrollPos = GetScrollPos(M_HWnd, SB_HORZ);
int vScrollPos = GetScrollPos(M_HWnd, SB_VERT);

D2D1_RECT_F region = {0,0,TILE_WIDTH,TILE_HEIGHT};
D2D1_RECT_F tFRegion = {0,0,TILE_WIDTH,21}; // tile front's region
Coor coor;
int tileHeight;

RECT rect;
GetWindowRect(M_HWnd, &rect);
int HWndWidth = rect.right - rect.left;
int HWndHeight = rect.bottom - rect.top;


pRT->BeginDraw();

pRT->Clear(D2D1::ColorF(0.45f, 0.76f, 0.98f, 1.0f));

pRT->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
for(int x=0; x<nTiles; x++)
{
coor = ppTile[x]->Getcoor();
tileHeight = ppTile[x]->Getheight();

pRT->SetTransform(D2D1::Matrix3x2F::Identity());

if((coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos > 0 - TILE_WIDTH &&
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos < HWndWidth &&
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos > 0 - (TILE_HEIGHT * 2.5) &&
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos < HWndHeight)
{
/* Draws tiles */
pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos
));

pRT->FillRectangle( &region, pBmpTileBrush[ppTile[x]->GetType() + 1]);

/* Draws tiles' front */
if((coor.Y - 1) / 2 < mapSizeY - 1) // If we are not in the front row,
{
if(coor.X > 1)
{
for(int diffH = tileHeight - ppTile[x + mapSizeX - 1]->Getheight(); diffH == 0; diffH--)
{
pRT->SetTransform(D2D1::Matrix3x2F::Identity());

pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos + (TILE_HEIGHT * 0.75) + (diffH * TILE_PIXEL_PER_LAYER)
));

pRT->FillRectangle( &tFRegion, pBmpTileFrontBrush[ppTile[x]->GetType()]);
}
}

if(((coor.X -1) / 2) + 1 < mapSizeX)
{
for(int diffH = tileHeight - ppTile[x + mapSizeX]->Getheight(); diffH == 0; diffH--)
{
pRT->SetTransform(D2D1::Matrix3x2F::Identity());

pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos + (TILE_HEIGHT * 0.75) + (diffH * TILE_PIXEL_PER_LAYER)
));

pRT->FillRectangle( &tFRegion, pBmpTileFrontBrush[ppTile[x]->GetType()]);
}
}

if(coor.X == 1 || (coor.X - 1) / 2 == mapSizeY - 1) // If the tile if at any of left or right edge,
{
for(int n = ((TH * 1.5) / TPPL) - (ppTile[x + mapSizeY + mapSizeY - 1]->Getheight() - tileHeight); n>=0; n--)
{
pRT->SetTransform(D2D1::Matrix3x2F::Identity());

pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.X - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.Y - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos + (TILE_HEIGHT * 0.75) + (n * TILE_PIXEL_PER_LAYER)
));

pRT->FillRectangle( &tFRegion, pBmpTileFrontBrush[ppTile[x]->GetType()]);
}
}
}

else // If we are in the front row
{
for(int h = tileHeight; h >= 0; h--)
{
pRT->SetTransform(D2D1::Matrix3x2F::Identity());

pRT->SetTransform(D2D1::Matrix3x2F::Translation(
(coor.GetX() - 1) * (TILE_WIDTH * 0.5) - hScrollPos,
((coor.GetY() - 1) * (TILE_HEIGHT * 0.5) * 1.5f) + ((MAX_MAP_HEIGHT - tileHeight) * (TILE_PIXEL_PER_LAYER)) + TILE_HEIGHT - vScrollPos + (TILE_HEIGHT * 0.75) + (h * TILE_PIXEL_PER_LAYER)
));

pRT->FillRectangle( &tFRegion, pBmpTileFrontBrush[ppTile[x]->GetType()]);
}
}
}
}
pRT->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);

hr = pRT->EndDraw();
}
}

这个:

Tile* Map::GetClickedTile(short xPos, short yPos)
{
Tile* pNoClickedTile = NULL;
int hScrollPos = GetScrollPos(M_HWnd, SB_HORZ);
int vScrollPos = GetScrollPos(M_HWnd, SB_VERT);

if(xPos < (mapSizeX * TILE_WIDTH) - hScrollPos) // If the click is within width of the map then...
{
Coor coor;
int height;
int currentTile;
int tileDistanceFromTop;


/* Checks if click is in an odd row of tiles */
int column = (xPos + hScrollPos) / TILE_WIDTH;

for (int y=mapSizeY-1; y>=0; y--)
{
currentTile = column + (y * (mapSizeX+mapSizeX-1));

coor = ppTile[currentTile]->Getcoor();
height = ppTile[currentTile]->Getheight();

tileDistanceFromTop = ((coor.Y / 2) * TILE_HEIGHT * 1.5f) + // Distance between two tiles
( (MAX_MAP_HEIGHT - height) * TILE_PIXEL_PER_LAYER) -
vScrollPos +
SPACE_LEFT_FOR_BACKGROUND;

/*if (tileDistanceFromTop < 0) // If the tile is partially hidden,
tileDistanceFromTop = tileDistanceFromTop % TILE_HEIGHT; // then % TILE_HEIGHT*/

if( yPos > tileDistanceFromTop &&
yPos < tileDistanceFromTop + TILE_HEIGHT)
{
/* Get relative coordinates */
int rpx = xPos % TILE_WIDTH;
int rpy = ( (yPos - SPACE_LEFT_FOR_BACKGROUND) -
(y * (TILE_HEIGHT /2) ) -
( ( MAX_MAP_HEIGHT - height) * TILE_PIXEL_PER_LAYER) +
vScrollPos) %
TILE_HEIGHT;

/* Checks if click is withing area of current tile */
if (rpy + (rpx / (TILE_WIDTH /16)) > TILE_HEIGHT * 0.25f && // if click is Down Right the Upper Left slope and,
rpy + (rpx / (TILE_WIDTH /16)) < TILE_HEIGHT * 1.25f && // it is UL the LR slope and,
rpy - (rpx / (TILE_WIDTH /16)) < TILE_HEIGHT * 0.75f && // it is UR the LL slope and,
rpy - (rpx / (TILE_WIDTH /16)) > TILE_HEIGHT * -0.25f) // it is DL the UR slope,
return ppTile[currentTile]; // Then return currentTile
}
}


/* Checks if click is in an even row of tiles */
column = (xPos + hScrollPos - (TILE_WIDTH/2)) / TILE_WIDTH;

for (int y=mapSizeY-2; y>=0; y--)
{
currentTile = column + (y * (mapSizeX+mapSizeX-1)) + mapSizeX;

coor = ppTile[currentTile]->Getcoor();
height = ppTile[currentTile]->Getheight();

tileDistanceFromTop = (((coor.Y - 1) / 2) * TILE_HEIGHT * 1.5f) + // Distance between two tiles
( (MAX_MAP_HEIGHT - height) * TILE_PIXEL_PER_LAYER) +
(TILE_HEIGHT * 0.75) -
vScrollPos +
SPACE_LEFT_FOR_BACKGROUND;

/*if (tileDistanceFromTop < 0)
tileDistanceFromTop = tileDistanceFromTop % TILE_HEIGHT;*/

if( yPos > tileDistanceFromTop &&
yPos < tileDistanceFromTop + TILE_HEIGHT)
{
/* Get relative coordinates */
int rpx = xPos % TILE_WIDTH;
int rpy = (int)((yPos - SPACE_LEFT_FOR_BACKGROUND) -
(y * (TILE_HEIGHT /2) ) -
( ( MAX_MAP_HEIGHT - height) * TILE_PIXEL_PER_LAYER) -
(TILE_HEIGHT * 0.675) +
vScrollPos) %
TILE_HEIGHT;

/* Checks if click is withing area of current tile */
if (rpy + (rpx / (TILE_WIDTH /16)) > TILE_HEIGHT * 0.25f && // if click is Down Right the Upper Left slope and,
rpy + (rpx / (TILE_WIDTH /16)) < TILE_HEIGHT * 1.25f && // it is UL the LR slope and,
rpy - (rpx / (TILE_WIDTH /16)) < TILE_HEIGHT * 0.75f && // it is UR the LL slope and,
rpy - (rpx / (TILE_WIDTH /16)) > TILE_HEIGHT * -0.25f) // it is DL the UR slope,
return ppTile[currentTile]; // Then return currentTile // Then return currentTile
}
}
}
return pNoClickedTile;
}

甚至这样:

int Map::GetTileNByCoor(Coor coor)
{
return ((coor.X / 2 + ((coor.Y - 1) * mapSizeY) - (coor.Y / 2));
}

变得更容易阅读?随着我的代码变得越来越大,我意识到拥有一个干净、易于阅读的代码是多么重要,即使有时不是必需的。有哪些技巧可以使上述代码更清晰?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com