gpt4 book ai didi

c++ - 我将如何为下一次迭代清理 SDL_surface 中的翻转图像?

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:38 24 4
gpt4 key购买 nike

我正在尝试使用 SDL 库模拟一个简单的钟摆。我需要用新的迭代值更新屏幕并删除屏幕上已有的图像。我该怎么做?

Screenshot of output

我的代码如下

const double g = 9.81, l = 200.0, h = 0.5, u0 = 0, theta0 = 3.1415/3;
const int xoffset = 350, yoffset = 300, r = 10;


//simplified equations 1
double thetadot(double u)
{
return u;
}

//simplified equations 2
double udot(double theta)
{
return (-g / l) * sin(theta);
}


int main(int argc, char *argv[])
{
double theta, thetanext, u, unext, ku1, ku2, ku3, ku4, kt1, kt2, kt3, kt4;
if (SDL_Init(SDL_INIT_VIDEO) != 0)
return 1;

atexit(SDL_Quit);
SDL_Surface *screen = SDL_SetVideoMode(width, height, 0, SDL_DOUBLEBUF);
if (screen == NULL)
return 2;
//putting inital values to the function
u = u0;
theta = theta0;

while(true)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
return 0;
}

double x = xoffset + l * sin(theta);
double y = yoffset + l * cos(theta);

SDL_LockSurface(screen);

//string hanging position
draw_circle(screen, xoffset, yoffset, 10, 0x0000ff00);
fill_circle(screen, xoffset, yoffset, 10, 0x0000ff00);

//draw string
draw_line(screen, xoffset, yoffset, x, y, 0xff3366ff);

//draw bob's current position
fill_circle(screen, (int)x, (int)y, r, 0xff004400);
draw_circle(screen, (int)x, (int)y, r, 0xff3366ff);

SDL_Delay(150);
SDL_Flip(screen);

//Numerical integration of equation 1
kt1 = thetadot(u);
kt2 = thetadot(u + 0.5 * h * kt1);
kt3 = thetadot(u + 0.5 * h * kt2);
kt4 = thetadot(u + h * kt3);
thetanext = theta + (h / 6) * (kt1 + 2 * kt2 + 2 * kt3 + kt4);

//Numerical integration of equation 2
ku1 = udot(theta);
ku2 = udot(theta + 0.5 * h * ku1);
ku3 = udot(theta + 0.5 * h * ku2);
ku4 = udot(theta + h * ku3);
unext = u + (h / 6) * (ku1 + 2 * ku2 + 2 * ku3 + ku4);

//updating values
u = unext;
theta = thetanext;
}
return 0;
}

最佳答案

您可以尝试使用 SDL_FillRect 清除屏幕在绘制每一帧之前:

int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);

例如您可以将参数 SDL_FillRect(screen, NULL, 0x000000) 传递给它以清除整个屏幕黑色。

关于c++ - 我将如何为下一次迭代清理 SDL_surface 中的翻转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20142943/

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