如何为边框和不同颜色的矩形着色? SDL

时间:2017-07-01 06:26:52

标签: c++ sdl-2

我有一个负责任的功能,用于在屏幕上绘制一个矩形,它在我的程序的主循环内,它执行此操作:

DrawRetangle (SDL_Renderer * Renderer, SDL_Rect MyRect) {

    // Clean the renderer
    SDL_RenderClear (Renderer);

   // Position x, y, width and height of the rectangle.
    MyRect = {x, y, width, height};

    // Rectangle border color
    SDL_SetRenderDrawColor (Renderer, 0, 0, 0, 255);

    // Draw the edges of the rectangle
    SDL_RenderDrawRect (Renderer, & MyRect);

    // Color from within the rectangle
    SDL_SetRenderDrawColor (Renderer, 255, 255, 255, 255);

    // Fill in the rectangle
    SDL_RenderFillRect (Renderer, & MyRect);

    // Show in window
    SDL_RenderPresent (Renderer);

}

请注意,我正在尝试绘制一个带有黑色边框并填充白色的矩形,但它全部变黑,我知道我可以解决这个问题,清洁,绘制边框,显示,清洁,绘制内部和显示,但是,如果我对多个矩形进行分层,这将是不好的,有更好的方法吗?

  

Up 1:

功能是否就是这样,如下所示?

DrawRetangle (SDL_Renderer * Renderer, SDL_Rect MyRect) {

    // Clean the renderer 1
    SDL_RenderClear (Renderer);

   // Position x, y, width and height of the rectangle.
    MyRect = {x, y, width, height};

    // Rectangle border color
    SDL_SetRenderDrawColor (Renderer, 0, 0, 0, 255);

    // Draw the edges of the rectangle
    SDL_RenderDrawRect (Renderer, & MyRect);

    // Show in window 1
    SDL_RenderPresent (Renderer);

    // Clean the renderer 2
    SDL_RenderClear (Renderer);

    // Color from within the rectangle
    SDL_SetRenderDrawColor (Renderer, 255, 255, 255, 255);

    // Fill in the rectangle
    SDL_RenderFillRect (Renderer, & MyRect);

    // Show in window 2
    SDL_RenderPresent (Renderer);

}
  

Up 2:

我找到的另一个解决方案是将下面的函数调用两次,在另一个上面创建一个矩形,一个稍小的内部,看起来像边框。但我仍然认为这不应该是最好的方法。

DrawRetangle (SDL_Renderer * Renderer, SDL_Rect MyRect, int x, int, y, int width, int height, int r, int g, int b, int a) {

       // Position x, y, width and height of the rectangle.
        MyRect = {x, y, width, height};


        // Color from within the rectangle
        SDL_SetRenderDrawColor (Renderer, r, g, b, a);

           // Clean the renderer
        SDL_RenderClear (Renderer);

        // Fill in the rectangle
        SDL_RenderFillRect (Renderer, & MyRect);

        // Show in window
        SDL_RenderPresent (Renderer);

    }

1 个答案:

答案 0 :(得分:0)

在绘制填充矩形之前

更改矩形的大小和位置。 然后试试。

Myrect->x+=borderSize; //whatever size you want
Myrect->y+=borderSize;
Myrect->h-=(borderSize*2);
Myrect->w-=(borderSize*2);
    // Color from within the rectangle
SDL_SetRenderDrawColor (Renderer, 255, 255, 255, 255);

// Fill in the rectangle
SDL_RenderFillRect (Renderer, & MyRect);