Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在SDL中绘制圆、圆弧和矢量图形?_C_Sdl_Sdl 2_Vector Graphics - Fatal编程技术网

C 如何在SDL中绘制圆、圆弧和矢量图形?

C 如何在SDL中绘制圆、圆弧和矢量图形?,c,sdl,sdl-2,vector-graphics,C,Sdl,Sdl 2,Vector Graphics,(我正在使用SDL2) 它用于游戏开发,在我的例子中,作为简单的视听输出和鼠标+键盘输入。它不是像GTK、Qt、wxWindows等那样的“工具箱”,而是跨平台的 除了使用三角或“圆的方程式”,我如何绘制曲线?一般矢量图形怎么样 SDL是一个合适的起点还是我应该去别处看看?如果您想编写自己的圆绘制函数,那么我建议通过绘制像素来适应SDL2 曲线也可以类似地绘制,但更多地使用椭圆绘制算法 实际的矢量图形开始变得更加复杂,您可能必须找到一些能够呈现SVG文件的东西,我不确定SDL2是否有很多选项

(我正在使用SDL2)

它用于游戏开发,在我的例子中,作为简单的视听输出和鼠标+键盘输入。它不是像GTK、Qt、wxWindows等那样的“工具箱”,而是跨平台的

除了使用三角或“圆的方程式”,我如何绘制曲线?一般矢量图形怎么样


SDL是一个合适的起点还是我应该去别处看看?

如果您想编写自己的圆绘制函数,那么我建议通过绘制像素来适应SDL2

曲线也可以类似地绘制,但更多地使用椭圆绘制算法

实际的矢量图形开始变得更加复杂,您可能必须找到一些能够呈现SVG文件的东西,我不确定SDL2是否有很多选项


然而,如果你想简单地拥有一些可以使用的函数,我建议直接使用。它已经实现了许多功能供您使用。

SDL允许第三方LIB在纹理上绘制。如果cairo是可取的,它可以用于如下函数:

cairo_t*cb(cairo_t*cr)
{cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
 cairo_rectangle(cr, 10, 20, 128, 128);
 cairo_stroke(cr);
 return cr;
}
然后可以将cb传递给此函数:

cairo_t*cai(SDL_Window*w,SDL_Renderer*r,cairo_t*(*f)(cairo_t*))
{int width, height, pitch;void *pixels;
 SDL_GetWindowSize(w, &width, &height);
 SDL_Texture*t=SDL_CreateTexture(r,SDL_PIXELFORMAT_ARGB8888,SDL_TEXTUREACCESS_STREAMING,width,height);
 SDL_LockTexture(t, NULL, &pixels, &pitch);
 cairo_surface_t *cs=cairo_image_surface_create_for_data(pixels,CAIRO_FORMAT_ARGB32,width,height,pitch);
 cairo_t*s=cairo_create(cs);
 cairo_t*fr=f(s);SDL_UnlockTexture(t);SDL_RenderCopy(r,t,NULL,NULL);SDL_RenderPresent(r);
 return fr;
}

如果你想做一个没有第三方库的圆或椭圆,包括math.h并使用下面我写的函数。它将绘制锯齿椭圆或圆非常好。在SDL 2.0.2上进行测试,效果良好。它绘制一个象限弧,并镜像其他弧,从而减少对cosf和sinf的调用

//draw one quadrant arc, and mirror the other 4 quadrants
void sdl_ellipse(SDL_Renderer* r, int x0, int y0, int radiusX, int radiusY)
{
    float pi  = 3.14159265358979323846264338327950288419716939937510;
    float pih = pi / 2.0; //half of pi

    //drew  28 lines with   4x4  circle with precision of 150 0ms
    //drew 132 lines with  25x14 circle with precision of 150 0ms
    //drew 152 lines with 100x50 circle with precision of 150 3ms
    const int prec = 27; // precision value; value of 1 will draw a diamond, 27 makes pretty smooth circles.
    float theta = 0;     // angle that will be increased each loop

    //starting point
    int x  = (float)radiusX * cos(theta);//start point
    int y  = (float)radiusY * sin(theta);//start point
    int x1 = x;
    int y1 = y;

    //repeat until theta >= 90;
    float step = pih/(float)prec; // amount to add to theta each time (degrees)
    for(theta=step;  theta <= pih;  theta+=step)//step through only a 90 arc (1 quadrant)
    {
        //get new point location
        x1 = (float)radiusX * cosf(theta) + 0.5; //new point (+.5 is a quick rounding method)
        y1 = (float)radiusY * sinf(theta) + 0.5; //new point (+.5 is a quick rounding method)

        //draw line from previous point to new point, ONLY if point incremented
        if( (x != x1) || (y != y1) )//only draw if coordinate changed
        {
            SDL_RenderDrawLine(r, x0 + x, y0 - y,    x0 + x1, y0 - y1 );//quadrant TR
            SDL_RenderDrawLine(r, x0 - x, y0 - y,    x0 - x1, y0 - y1 );//quadrant TL
            SDL_RenderDrawLine(r, x0 - x, y0 + y,    x0 - x1, y0 + y1 );//quadrant BL
            SDL_RenderDrawLine(r, x0 + x, y0 + y,    x0 + x1, y0 + y1 );//quadrant BR
        }
        //save previous points
        x = x1;//save new previous point
        y = y1;//save new previous point
    }
    //arc did not finish because of rounding, so finish the arc
    if(x!=0)
    {
        x=0;
        SDL_RenderDrawLine(r, x0 + x, y0 - y,    x0 + x1, y0 - y1 );//quadrant TR
        SDL_RenderDrawLine(r, x0 - x, y0 - y,    x0 - x1, y0 - y1 );//quadrant TL
        SDL_RenderDrawLine(r, x0 - x, y0 + y,    x0 - x1, y0 + y1 );//quadrant BL
        SDL_RenderDrawLine(r, x0 + x, y0 + y,    x0 + x1, y0 + y1 );//quadrant BR
    }
}
//绘制一个象限弧,并镜像其他四个象限
无效sdl_椭圆(sdl_渲染器*r,int x0,int y0,int radiusX,int radiusY)
{
浮点数pi=3.14159265358979323846264338327950288419716939937510;
浮点pih=pi/2.0;//pi的一半
//用4x4圆圈绘制28条线,精度为150毫秒
//用25x14圆圈绘制132条线,精度为150毫秒
//用100x50圆圈绘制152条线,精度为150 3ms
const int prec=27;//精度值;值为1将绘制一个菱形,27将生成非常平滑的圆。
浮点θ=0;//每个循环将增加的角度
//起点
int x=(浮点)半径x*cos(θ);//起点
int y=(浮点)半径y*sin(θ);//起点
int-x1=x;
int y1=y;
//重复,直到θ>=90;
float step=pih/(float)prec;//每次添加到θ的量(度)

对于(θ=步长;θ这是上文提到的中点圆算法的一个示例。它不需要数学库,速度非常快。(渲染时间约为500微秒)这是Windows用于栅格化圆的方法

void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
{
   const int32_t diameter = (radius * 2);

   int32_t x = (radius - 1);
   int32_t y = 0;
   int32_t tx = 1;
   int32_t ty = 1;
   int32_t error = (tx - diameter);

   while (x >= y)
   {
      //  Each of the following renders an octant of the circle
      SDL_RenderDrawPoint(renderer, centreX + x, centreY - y);
      SDL_RenderDrawPoint(renderer, centreX + x, centreY + y);
      SDL_RenderDrawPoint(renderer, centreX - x, centreY - y);
      SDL_RenderDrawPoint(renderer, centreX - x, centreY + y);
      SDL_RenderDrawPoint(renderer, centreX + y, centreY - x);
      SDL_RenderDrawPoint(renderer, centreX + y, centreY + x);
      SDL_RenderDrawPoint(renderer, centreX - y, centreY - x);
      SDL_RenderDrawPoint(renderer, centreX - y, centreY + x);

      if (error <= 0)
      {
         ++y;
         error += ty;
         ty += 2;
      }

      if (error > 0)
      {
         --x;
         tx += 2;
         error += (tx - diameter);
      }
   }
}
void DrawCircle(SDL_渲染器*渲染器、int32_t centreX、int32_t centreY、int32_t radius)
{
常数int32_t直径=(半径*2);
int32_t x=(半径-1);
int32_t y=0;
int32_t tx=1;
int32_t ty=1;
int32_t误差=(tx-直径);
而(x>=y)
{
//以下每一项都渲染圆的八分之一
SDL_RenderDrawPoint(渲染器,centreX+x,centreY-y);
SDL_RenderDrawPoint(渲染器,centreX+x,centreY+y);
SDL_RenderDrawPoint(渲染器,centreX-x,centreY-y);
SDL_RenderDrawPoint(渲染器,centreX-x,centreY+y);
SDL_RenderDrawPoint(渲染器,centreX+y,centreY-x);
SDL_RenderDrawPoint(渲染器,centreX+y,centreY+x);
SDL_RenderDrawPoint(渲染器,centreX-y,centreY-x);
SDL_RenderDrawPoint(渲染器,centreX-y,centreY+x);
如果(错误0)
{
--x;
tx+=2;
误差+=(tx-直径);
}
}
}

如果
prec
基于半径值而不是始终相同,这是否好?这将提高大椭圆的质量,但保持较小椭圆的速度。