流畅的相机和瓷砖滚动 C++
smooth camera and tile scrolling c++
I have 50
pixel offset on my camera. The screen size is 640x480, and camera with offset is then 690x530; tile is 16x16 pixel.
Where x
, y
is screen's pixel position,
terrain
is an array of tiles,
startTileXinCamera
is tile on the most left in x axis on camera,
startTileYinCamera
is tile on the most left in y axis on camera,
tileXinCamera
is tile count that covered by camera in x axis,
tileYinCamera
is tile count that covered by camera in y axis,
tileCountX
is total tile count on x axis, not on screen but total for the terrain.
我在 2D 环境中遇到相机和图块滚动的问题。所以,基本上我想按像素平滑地滚动我的图块,但它按图块滚动并且看起来 "laggy" 或卡顿。
这是我的瓦片渲染代码:
void Terrain::render(SDL_Renderer* renderer, Camera* camera)
{
int x = 0, y = 0;
int startTileXinCamera = camera->getCamera()->x / tileWidth;
int startTileYinCamera = camera->getCamera()->y / tileHeight;
int tileXinCamera = camera->cameraSizeToPositionX() / tileWidth;
int tileYinCamera = camera->cameraSizeToPositionY() / tileHeight;
for (int i = startTileYinCamera; i < tileYinCamera; i++)
{
for (int j = startTileXinCamera; j < tileXinCamera; j++)
{
terrainGraphic->render(renderer, x, y, tileClip[terrain[i * tileCountX + j]->type]);
if (x < camera->getCamera()->w)
x += tileWidth;
}
x = 0;
if (y < camera->getCamera()->h)
y += tileHeight;
}
}
问题:
如何按像素平滑地滚动我的图块并消除不需要的效果?
我想通了。我正在使用预定义的 x,y 坐标 int x = 0, y = 0;和 x += tileWidth;放置图块,而不是实际使用图块的坐标。然后通过将图块提取到相机位置来计算图块的坐标,因此它会根据屏幕坐标正确渲染。我希望这不会造成混淆。这是代码,以防万一它可能对那里的人有用。对不起,代码很难看。
void Terrain::render(SDL_Renderer* renderer, Camera* camera)
{
int startTileXinCamera = camera->getCamera()->x / tileWidth;
int startTileYinCamera = camera->getCamera()->y / tileHeight;
int tileXinCamera = camera->cameraSizeToPositionX() / tileWidth;
int tileYinCamera = camera->cameraSizeToPositionY() / tileHeight;
for (int i = startTileYinCamera; i < tileYinCamera; i++)
{
for (int j = startTileXinCamera; j < tileXinCamera; j++)
{
terrainGraphic->render(renderer,
terrain[i * tileCountX + j]->x - camera->getCamera()->x,
terrain[i * tileCountX + j]->y - camera->getCamera()->y,
tileClip[terrain[i * tileCountX + j]->type]);
}
}
}
I have
50
pixel offset on my camera. The screen size is 640x480, and camera with offset is then 690x530; tile is 16x16 pixel. Wherex
,y
is screen's pixel position,terrain
is an array of tiles,startTileXinCamera
is tile on the most left in x axis on camera,startTileYinCamera
is tile on the most left in y axis on camera,tileXinCamera
is tile count that covered by camera in x axis,tileYinCamera
is tile count that covered by camera in y axis,tileCountX
is total tile count on x axis, not on screen but total for the terrain.
我在 2D 环境中遇到相机和图块滚动的问题。所以,基本上我想按像素平滑地滚动我的图块,但它按图块滚动并且看起来 "laggy" 或卡顿。
这是我的瓦片渲染代码:
void Terrain::render(SDL_Renderer* renderer, Camera* camera)
{
int x = 0, y = 0;
int startTileXinCamera = camera->getCamera()->x / tileWidth;
int startTileYinCamera = camera->getCamera()->y / tileHeight;
int tileXinCamera = camera->cameraSizeToPositionX() / tileWidth;
int tileYinCamera = camera->cameraSizeToPositionY() / tileHeight;
for (int i = startTileYinCamera; i < tileYinCamera; i++)
{
for (int j = startTileXinCamera; j < tileXinCamera; j++)
{
terrainGraphic->render(renderer, x, y, tileClip[terrain[i * tileCountX + j]->type]);
if (x < camera->getCamera()->w)
x += tileWidth;
}
x = 0;
if (y < camera->getCamera()->h)
y += tileHeight;
}
}
问题:
如何按像素平滑地滚动我的图块并消除不需要的效果?
我想通了。我正在使用预定义的 x,y 坐标 int x = 0, y = 0;和 x += tileWidth;放置图块,而不是实际使用图块的坐标。然后通过将图块提取到相机位置来计算图块的坐标,因此它会根据屏幕坐标正确渲染。我希望这不会造成混淆。这是代码,以防万一它可能对那里的人有用。对不起,代码很难看。
void Terrain::render(SDL_Renderer* renderer, Camera* camera)
{
int startTileXinCamera = camera->getCamera()->x / tileWidth;
int startTileYinCamera = camera->getCamera()->y / tileHeight;
int tileXinCamera = camera->cameraSizeToPositionX() / tileWidth;
int tileYinCamera = camera->cameraSizeToPositionY() / tileHeight;
for (int i = startTileYinCamera; i < tileYinCamera; i++)
{
for (int j = startTileXinCamera; j < tileXinCamera; j++)
{
terrainGraphic->render(renderer,
terrain[i * tileCountX + j]->x - camera->getCamera()->x,
terrain[i * tileCountX + j]->y - camera->getCamera()->y,
tileClip[terrain[i * tileCountX + j]->type]);
}
}
}