我写了一个程序来反弹一个矩形。我使用SDL_RenderFillRect绘制一个矩形和SDL_RenderPresent来呈现该矩形,之后SDL_RenderClear清除渲染器以重新执行它。我使用SDL_GetTicks和SDL_Delay让它每秒更新60次但不知何故矩形的速度是不规则的而不是平滑的。过去几天我尝试了不同的东西,但没有任何帮助。我是否应该使用SDL_RenderPresent让我的窗口定期更新?
#include <SDL.h>
#include <math.h>
#include <iostream>
#include <stdio.h>
const int width = 1200;
const int height = 800;
const double PI = 3.141592653589793;
void init();
void quit();
double x = width / 2;
double y = height / 2;
double r = 25;
double vx = 1;
double vy = 1;
double s = 4;
double tickStart = SDL_GetTicks();
double tickEnd = 0;
double tickDelta = 0;
double frameTime = 1001.0 / 60.0;
double delayTime = 0;
int c = 0;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event *event;
SDL_Rect rect;
int main(int argc, char* argv[]) {
init();
// main loop
bool running = true;
while (running) {
event = new SDL_Event;
while (SDL_PollEvent(event) != 0) {
if (event->type == SDL_QUIT) {
running = false;
break;
}
}
//renderer
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
// update position
x = x + vx*s;
y = y + vy*s;
// check wall collision
if (x - r <= 0) {
vx = -vx;
x = r;
}
else if (x + r >= width) {
vx = -vx;
x = width - r;
}
if (y - r <= 0) {
vy = -vy;
y = r;
}
else if (y + r >= height) {
vy = -vy;
y = height - r;
}
// rect
rect.h = rect.w = 2 * r;
rect.x = x - r;
rect.y = y - r;
SDL_RenderFillRect(renderer, &rect);
// present everything at the right time
tickEnd = SDL_GetTicks();
tickDelta = tickEnd - tickStart;
delayTime = frameTime - tickDelta;
std::cout << tickDelta << std::endl;
if (delayTime > 0)
SDL_Delay(delayTime);
tickStart = SDL_GetTicks();
SDL_RenderPresent(renderer);
c++;
}
quit();
return 0;
}
void init()
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("test", 200, 100, width, height, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
}
void quit()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_QUIT;
}
答案 0 :(得分:0)
根据具体情况,调用SDL_Delay()似乎需要大约15ms。渲染器可以通过右对齐标记来垂直同步:renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);