我正在 cpp 中创建一个分形。对于渲染器,我使用 SDL2,但出现一个我不明白的错误:
35:49: error: invalid conversion from 'SDL_Window*' to 'Uint32' {aka 'unsigned int'} [-fpermissive]
这是我的代码:
#include <SDL2/SDL.h>
#include <numeric>
#include <complex>
#include <cmath>
#include <iostream>
int isInSet(std::complex<double> c)
{
std::complex<double> z(0,0);
for (int i = 0; i < 25; i++)
{
z = std::pow(z, 2) + c;
if (std::norm(z) > 10)
{
return i;
}
}
return 0;
}
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
/*SDL_Window* window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_FULLSCREEN // flags - see below
);*/
//SDL_Renderer* renderer = SDL_CreateRenderer(window , -1, SDL_RENDERER_PRESENTVSYNC);
SDL_CreateWindowAndRenderer(2000, 2000, window, renderer); // here error: invalid conversion from 'SDL_Window*' to 'Uint32' {aka 'unsigned int'} [-fpermissive]
SDL_RenderSetScale(renderer, 2, 2);
for (double x = 0.0; x < 1.0; x = x + 0.01)
{
for (double y = 0.0; y < 1.0; y = y + 0.01)
{
double point_x = std::lerp(-2.0, 2.0, x);
double point_y = std::lerp(-2.0, 2.0, y);
int iters = isInSet(std::complex<double>(point_x, point_y));
if (iters == 0)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderDrawPointF(renderer, x * 1000, y * 1000);
}
else
{
SDL_SetRenderDrawColor(renderer, 255-iters,255-iters,255-iters, 255);
SDL_RenderDrawPointF(renderer, x * 1000, y * 1000);
}
}
SDL_RenderPresent(renderer);
SDL_Delay(10000);
}
return 0;
}
// g++.exe -fdiagnostics-color=always -g J:\Info\cpp.cpp\MyProjects\fractales\Mandelbrot\mandelbrot.cpp -o J:\Info\cpp.cpp\MyProjects\fractales\Mandelbrot\mandelbrot.exe && J:\Info\cpp.cpp\MyProjects\fractales\Mandelbrot\mandelbrot.exe
// g++.exe -std=c20++ J:\Info\cpp.cpp\MyProjects\fractales\Mandelbrot\mandelbrot.cpp -IC:\Users\user.PC-SKAMRA\cpp\SDL2\x86_64-w64-mingw32\include -LC:\Users\user.PC-SKAMRA\cpp\SDL2\x86_64-w64-mingw32\lib -Wall -lmingw32 -lSDL2main -lSDL2 -o J:\Info\cpp.cpp\MyProjects\fractales\Mandelbrot\mandelbrot.exe
为什么我会收到此错误?
根据文档,这是
SDL_CreateWindowAndRenderer
的签名:
int SDL_CreateWindowAndRenderer(
int width, int height, Uint32 window_flags,
SDL_Window **window, SDL_Renderer **renderer);
所以需要五个参数......但你只给出四个。您将
window
作为第三个传递,但该函数需要标志,并且您无法将指针转换为 Uint32
。
解决方案是传递这些标志。如果您不需要任何特定选项,可以使用
0
。这些 window
和 renderer
参数也是指向指针的指针,因此您需要传递指针变量的地址:
SDL_CreateWindowAndRenderer(2000, 2000, 0, &window, &renderer);