2015-02-05 2 views
0

방금 ​​C++ 및 VS 2013 커뮤니티에서 SDL로 시작했습니다. 원을 그리기 위해 Google에서 검색하여 http://content.gpwiki.org/index.php/SDL:Tutorials:Drawing_and_Filling_Circles을 찾았습니다. 그러나 필자가 별도의 fill_circle.cpp 파일에 구현하려고 할 때 main.cpp에이 파일을 포함하고 함수를 호출하면 fill_circle.obj 함수에 fill_circle() 함수가 이미 정의되어 있고 충돌이 있다고하는 오류가 발생합니다. 다른 libs. 그래서 main.cpp에 직접 drawing 함수를 구현하려고 시도했지만 void __cdecl fill_circle (struct SDL_Surface *, int, int, int, unsigned int)가 fill_circle.obj에 이미 정의되어 있다는 비슷한 오류가 발생합니다. 내가 이러한 오류와 함께 할 당신의 누군가가 나 :서클 그리기 및 채우기

편집을 도울 수 있기를 바랍니다 무엇을 잘 모릅니다

: completly fill_circle.cpp 및 디버그 폴더를 제거하고을 programm 것 MAIN.CPP의 기능을 구현 한 후 컴파일하지만 런타임시 오류가 발생합니다.

내 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의 결과를보고있는 것 같다 빠른 눈에서. –

+0

여전히 런타임 오류가 발생합니다. SDL_Project.exe의 0x011F6CF9에서 첫 번째 예외가 발생했습니다. 0xC0000005 : 0x03604C10 위치 쓰기 액세스 위반입니다. 이 예외 처리기가 있으면 프로그램을 안전하게 계속할 수 있습니다. – Phorskin

+0

fill_circle (windowSurface, 10, 10, 20, 0xffffffff); 표면 바깥 쪽을 끌고 프로그램이 충돌합니다. – Martin

답변

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); 
      } 
     } 
    } 
} 
관련 문제