SFML 在 pong 克隆中有一些参差不齐的表现 (OS X)
SFML Some jagged performance in pong clone (OS X)
想知道是否有人可以帮助我发现性能问题,因为我是 C++ 的新手。使用 SFML 制作乒乓球游戏,暂时只使用 RectangleShape class,没有图像。
我正在进行惰性碰撞检查(没有四叉树),但考虑到现在场景中有两个对象,它应该不会造成问题:
我的游戏循环中的代码:
window.clear();
const float time = clock.getElapsedTime().asSeconds();
for (GameObject* object : gameObjects) {
object->update(input, time);
}
sf::FloatRect *paddleBounds = p.getBounds();
sf::FloatRect *ballBounds = b.getBounds();
if (paddleBounds->intersects(*ballBounds, intersection)) {
if (intersection.width > intersection.height) {
b.changeYDirection();
}
else {
b.changeXDirection();
}
collisionManger.correctOverlap(ballBounds, &intersection, b.getSpeed(), &correction);
}
checkForPoints(&b);
clock.restart();
for (GameObject* object : gameObjects) {
object->render(window);
}
检查分数(这个看是否应该打分)
void Game::checkForPoints(Ball *ball) {
bool ballOutOfBounds = false;
if (ball->getBounds()->left < 0) {
aiScore++;
ballOutOfBounds = true;
}
else if (ball->getBounds()->left > 800) {
playerScore++;
ballOutOfBounds = true;
}
if (ballOutOfBounds) {
ball->resetPosition();
}
}
碰撞管理器:
void CollisionManager::correctOverlap(sf::FloatRect *rectone, sf::FloatRect *intersection, sf::Vector2f *velocity, sf::Vector2f *correction) {
if (intersection->width > intersection->height) {
if (velocity->y < 0) {
correction->y = velocity->y;
}
else if (velocity->y > 0) {
correction->y = -velocity->y;
}
}
else {
if (velocity->x < 0) {
correction->x = velocity->x;
}
else if (velocity->x > 0) {
correction->x = -velocity->x;
}
}
}
球更新:
void Ball::update(InputManager &im, float time) {
bounds.left += m_speed.x * time;
bounds.top += m_speed.y * time;
if (bounds.top < 0) {
bounds.top = 1;
changeYDirection();
}
else if (bounds.top > 600 - bounds.height) {
bounds.top = 600 - bounds.height - 1;
changeYDirection();
}
m_rect.setPosition(bounds.left, bounds.top);
}
现在游戏大部分运行流畅。但偶尔球会跳过屏幕 30-40 像素。
changeYDirection 和 changeXDirection 只是将 x/y 值乘以速度矢量的 -1。
所以这是一个与代码完全无关的愚蠢问题。 Flux 应用程序在夜间将屏幕调暗橙色导致性能下降。
想知道是否有人可以帮助我发现性能问题,因为我是 C++ 的新手。使用 SFML 制作乒乓球游戏,暂时只使用 RectangleShape class,没有图像。
我正在进行惰性碰撞检查(没有四叉树),但考虑到现在场景中有两个对象,它应该不会造成问题:
我的游戏循环中的代码:
window.clear();
const float time = clock.getElapsedTime().asSeconds();
for (GameObject* object : gameObjects) {
object->update(input, time);
}
sf::FloatRect *paddleBounds = p.getBounds();
sf::FloatRect *ballBounds = b.getBounds();
if (paddleBounds->intersects(*ballBounds, intersection)) {
if (intersection.width > intersection.height) {
b.changeYDirection();
}
else {
b.changeXDirection();
}
collisionManger.correctOverlap(ballBounds, &intersection, b.getSpeed(), &correction);
}
checkForPoints(&b);
clock.restart();
for (GameObject* object : gameObjects) {
object->render(window);
}
检查分数(这个看是否应该打分)
void Game::checkForPoints(Ball *ball) {
bool ballOutOfBounds = false;
if (ball->getBounds()->left < 0) {
aiScore++;
ballOutOfBounds = true;
}
else if (ball->getBounds()->left > 800) {
playerScore++;
ballOutOfBounds = true;
}
if (ballOutOfBounds) {
ball->resetPosition();
}
}
碰撞管理器:
void CollisionManager::correctOverlap(sf::FloatRect *rectone, sf::FloatRect *intersection, sf::Vector2f *velocity, sf::Vector2f *correction) {
if (intersection->width > intersection->height) {
if (velocity->y < 0) {
correction->y = velocity->y;
}
else if (velocity->y > 0) {
correction->y = -velocity->y;
}
}
else {
if (velocity->x < 0) {
correction->x = velocity->x;
}
else if (velocity->x > 0) {
correction->x = -velocity->x;
}
}
}
球更新:
void Ball::update(InputManager &im, float time) {
bounds.left += m_speed.x * time;
bounds.top += m_speed.y * time;
if (bounds.top < 0) {
bounds.top = 1;
changeYDirection();
}
else if (bounds.top > 600 - bounds.height) {
bounds.top = 600 - bounds.height - 1;
changeYDirection();
}
m_rect.setPosition(bounds.left, bounds.top);
}
现在游戏大部分运行流畅。但偶尔球会跳过屏幕 30-40 像素。
changeYDirection 和 changeXDirection 只是将 x/y 值乘以速度矢量的 -1。
所以这是一个与代码完全无关的愚蠢问题。 Flux 应用程序在夜间将屏幕调暗橙色导致性能下降。