我有一个用 C++ 编写的 Mandelbrot 设置渲染代码,并且我正在使用 SDL2 来显示图像。当我想让我的程序具有交互性时,我遇到了一个问题。通过按 W,我的代码应该清除以前的图像并显示具有新参数的新图像,但无论我做什么,以前的渲染都不会清除。顺便说一句,我注意到每次按 W 时,我的内存使用量都会更高。我该如何解决这个问题?
#include <complex>
#include <iostream>
#include <SDL.h>
#include <chrono>
#include <omp.h>
using namespace std;
int main(int argc, char** argv) {
const int resolution= 2000;
const int rows = resolution;
const int cols = resolution;
complex<long double> position;
int dots[rows][cols];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
dots[i][j] = 0;
}
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL initialization failed: " << SDL_GetError() << std::endl;
return 1;
}
const int screenWidth = cols * 1; // Adjust the window size as needed
const int screenHeight = rows * 1;
SDL_Window* window = SDL_CreateWindow("Mandelbrot set Visualization", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Rect rect;
long double startx = -2;
long double endx = 2;
long double starty = 2;
long double endy = -2;
long double stepx = (endx - startx) / (cols - 1);
long double stepy = (endy - starty) / (rows - 1);
rect.w = screenWidth / cols;
rect.h = screenHeight / rows;
int numThreads = 16;
omp_set_num_threads(numThreads);
#pragma omp parallel
{
int threadID = omp_get_thread_num(); // Get the thread ID
#pragma omp for
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
long double real = startx + j * stepx;
long double imag = starty + i * stepy;
position = complex<long double>(real, imag);
complex<long double> iterate(0, 0);
int color = 0;
for (int n = 0; n < 256; n++) {
iterate = iterate * iterate + position;
color++;
if (abs(iterate) > 2) {
dots[i][j] = color;
break;
}
}
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
SDL_SetRenderDrawColor(renderer, 0, dots[i][j], dots[i][j], 255);
rect.x = j * (screenWidth / cols);
rect.y = i * (screenHeight / rows);
SDL_RenderFillRect(renderer, &rect);
}
}
SDL_RenderPresent(renderer);
// Main game loop
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
else if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_w) {
// Zoom or change parameters, as needed
cout << "loop start";
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
long double decreaseAmountx = (abs(endx) + abs(startx)) / 20;
long double decreaseAmounty = (abs(endy) + abs(starty)) / 20;
endx -= decreaseAmountx;
startx += decreaseAmountx;
endy += decreaseAmounty;
starty -= decreaseAmounty;
stepx = (endx - startx) / (cols - 1);
stepy = (endy - starty) / (rows - 1);
// Render the Mandelbrot set with new parameters
#pragma omp parallel
{
int threadID = omp_get_thread_num(); // Get the thread ID
#pragma omp for
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
long double real = startx + j * stepx;
long double imag = starty + i * stepy;
position = complex<long double>(real, imag);
complex<long double> iterate(0, 0);
int color = 0;
for (int n = 0; n < 256; n++) {
iterate = iterate * iterate + position;
color++;
if (abs(iterate) > 2) {
dots[i][j] = color;
break;
}
}
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
SDL_SetRenderDrawColor(renderer, 0, dots[i][j], dots[i][j], 255);
rect.x = j * (screenWidth / cols);
rect.y = i * (screenHeight / rows);
SDL_RenderFillRect(renderer, &rect);
}
}
cout << "Present";
SDL_RenderPresent(renderer);
}
else if (event.key.keysym.sym == SDLK_r) {
// Trigger an SDL_QUIT event to exit the application
SDL_Event quitEvent;
quitEvent.type = SDL_QUIT;
SDL_PushEvent(&quitEvent);
}
}
}
}
// Cleanup and quit
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
问题出在您在现有
dots
数组中分配新值的循环中。
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
long double real = startx + j * stepx;
long double imag = starty + i * stepy;
position = complex<long double>(real, imag);
complex<long double> iterate(0, 0);
int color = 0;
for (int n = 0; n < 256; n++) {
iterate = iterate * iterate + position;
color++;
if (abs(iterate) > 2) {
dots[i][j] = color; // <- here
break;
}
}
}
}
旧值仍在数组中,因此您将获得叠加效果。
首先,我建议不要在堆栈上分配数组,而是在堆上分配数组。只需
#include <memory>
并将 int dots[rows][cols];
替换为
auto dots = std::make_unique<int[][cols]>(rows);
然后,为了测试我的理论,我在缩放循环中添加了几行:
auto newdots = std::make_unique<int[][cols]>(rows); // <- this
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
long double real = startx + j * stepx;
long double imag = starty + i * stepy;
position = complex<long double>(real, imag);
complex<long double> iterate(0, 0);
int color = 0;
for(int n = 0; n < 256; n++) {
iterate = iterate * iterate + position;
color++;
if(abs(iterate) > 2) {
newdots[i][j] = color; // <- this too
break;
}
}
}
}
std::swap(newdots, dots); // <- and finally this
通过这些更改,它可以正确重新绘制。
注意:每次分配一个新数组都需要时间,因此您可能应该清除已有的数组。