2015-02-05 183 views
0

我刚开始在C++和VS 2013社区中使用SDL。 我想画一个圆圈,所以我在谷歌搜索,发现http://content.gpwiki.org/index.php/SDL:Tutorials:Drawing_and_Filling_Circles。但是,当我试图在一个单独的fill_circle.cpp文件中实现它时,将其包含在我的main.cpp中并调用函数,我得到的错误说fill_circle()已经在fill_circle.obj中定义了函数fill_circle(),并且与其他库。 所以我试图直接在我的main.cpp中实现drawing函数,但是我得到了一个类似的错误,说在fill_circle.obj中已经定义了void __cdecl fill_circle(struct SDL_Surface *,int,int,int,unsigned int)。绘制和填充圆形

我不知道该怎么做这些错误,并希望你有人能帮助我:)

编辑:后完全地去除fill_circle.cpp和debug文件夹和实施main.cpp中的功能PROGRAMM会编译但在运行时抛出一个错误。

我的main.cpp:

#include <SDL.h> 
#include <iostream> 
#include <cmath> 

void fill_circle(SDL_Surface *surface, int cx, int cy, int radius, Uint32 pixel) 
{ 
    static const int BPP = 4; 

    double r = (double)radius; 

    for (double dy = 1; dy <= r; dy += 1.0) 
    { 
     // This loop is unrolled a bit, only iterating through half of the 
     // height of the circle. The result is used to draw a scan line and 
     // its mirror image below it. 

     // The following formula has been simplified from our original. We 
     // are using half of the width of the circle because we are provided 
     // with a center and we need left/right coordinates. 

     double dx = floor(sqrt((2.0 * r * dy) - (dy * dy))); 
     int x = cx - dx; 

     // Grab a pointer to the left-most pixel for each half of the circle 
     Uint8 *target_pixel_a = (Uint8 *)surface->pixels + ((int)(cy + r - dy)) * surface->pitch + x * BPP; 
     Uint8 *target_pixel_b = (Uint8 *)surface->pixels + ((int)(cy - r + dy)) * surface->pitch + x * BPP; 

     for (; x <= cx + dx; x++) 
     { 
      *(Uint32 *)target_pixel_a = pixel; 
      *(Uint32 *)target_pixel_b = pixel; 
      target_pixel_a += BPP; 
      target_pixel_b += BPP; 
     } 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    //Main loop flag 
    bool b_Quit = false; 
    //Event handler 
    SDL_Event ev; 
    //SDL window 
    SDL_Window *window = NULL; 

    SDL_Surface *windowSurface; 

    if (SDL_Init(SDL_INIT_VIDEO) < 0) 
    { 
     std::cout << "Video Initialisation Error: " << SDL_GetError() << std::endl; 
    } 
    else 
    { 
     window = SDL_CreateWindow("SDL_Project", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN); 
     if (window == NULL) 
     { 
      std::cout << "Window Creation Error: " << SDL_GetError() << std::endl; 
     } 
     else 
     { 
      windowSurface = SDL_GetWindowSurface(window); 

      fill_circle(windowSurface, 10, 10, 20, 0xffffffff); 

      //Main loop 
      while (!b_Quit) 
      { 
       //Event Loop 
       while (SDL_PollEvent(&ev) != 0) 
       { 
        //Quit Event 
        if (ev.type == SDL_QUIT) 
        { 
         b_Quit = true; 
        } 
       } 
       SDL_UpdateWindowSurface(window); 
      } 

     } 
    } 

    SDL_DestroyWindow(window); 
    SDL_Quit(); 

    return 0; 
} 
+0

改变函数名称 - 'Pauls_fill_Circle()',从看一眼就好像你看到的命名空间ambigutity的结果。 – 2015-02-05 14:57:05

+0

我仍然遇到运行时错误:SDL_Project.exe中0x011F6CF9的第一次机会异常:0xC0000005:访问冲突写入位置0x03604C10。 如果有这种异常的处理程序,程序可能会安全地继续。 – Phorskin 2015-02-05 15:20:41

+0

fill_circle(windowSurface,10,10,20,0xffffffff);将在表面之外绘制,因此程序将崩溃 – Martin 2015-02-05 18:26:57

回答

1

在(10,10),如您有20凭借其中心的半径的圆:

fill_circle(windowSurface, 10, 10, 20, 0xffffffff); 

会让你外面解决像素的分配表面

... = (Uint8 *)surface->pixels + ((int)(cy + r - dy)) * surface->pitch + x * BPP; 
    ... = (Uint8 *)surface->pixels + ((int)(cy - r + dy)) * surface->pitch + x * BPP; 

这应该会导致崩溃。

我在某些项目中使用了相同的算法,并且在SDL 1.2中使用相同的算法,但它的编写方式并不十分安全。

1

这可以帮助你:

void draw_circle(SDL_Point center, int radius, SDL_Color color) 
{ 
    SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); 
    for (int w = 0; w < radius * 2; w++) 
    { 
     for (int h = 0; h < radius * 2; h++) 
     { 
      int dx = radius - w; // horizontal offset 
      int dy = radius - h; // vertical offset 
      if ((dx*dx + dy*dy) <= (radius * radius)) 
      { 
       SDL_RenderDrawPoint(renderer, center.x + dx, center.y + dy); 
      } 
     } 
    } 
}