不能得出与SDL2 GFX实心圆(Cannot draw a filled circle with

2019-10-23 01:27发布

有下面的代码。 期望的结果是一个窗口被创建和实心圆绘制:

#include <SDL2/SDL.h>
#include <iostream>
#include <SDL2/SDL2_gfxPrimitives.h>

int main()
{
   SDL_Window* window = nullptr;
   SDL_Renderer* renderer = nullptr;

   if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
   {
      std::cout << "Could not initialise" << std::endl;
      return 1;
   }

   window = SDL_CreateWindow("MyGame",
                             SDL_WINDOWPOS_UNDEFINED,
                             SDL_WINDOWPOS_UNDEFINED,
                             640,
                             480,
                             SDL_WINDOW_SHOWN);
   if(!window)
   {
      std::cout << "Could not create the window" << std::endl;
      return 1;
   }

   renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
   SDL_RenderClear(renderer);

   Sint16 circleR = 100;
   Sint16 circleX = 300;
   Sint16 circleY = 300;
   SDL_Surface* windowSurface = SDL_GetWindowSurface(window);
   Uint32 circleColour = SDL_MapRGB(windowSurface->format, 255, 0, 0);

   int result = filledCircleColor(renderer, circleX, circleY, circleR, circleColour);

   std::cout << "drawing the circle r " << circleR << " x " << circleX << " y " << circleY << " circleColour " << circleColour << std::endl;
   std::cout << "draw circle result " << result << std::endl;

   SDL_RenderPresent(renderer);

   bool run = true;
   while(run)
   {
      SDL_Event event;
      while (SDL_PollEvent(&event))
      {
         if(event.type == SDL_QUIT)
         {
            run = false;
         }
      }

   }

   SDL_Quit();
   return 0;
}

问题是,圆不绘制 - 窗口是全黑的。

输出:

drawing the circle r 100 x 300 y 300 circleColour 16711680
draw circle result 0

filledCircleColor返回0应该是什么意思,有没有错误。

应该做些什么,以便绘制圆? 我使用SDL 2.0.2在Ubuntu与SDL2 GFX延伸。

Answer 1:

关于SDL_GetWindowSurface , 该文件说:“你可能无法与3D或在此窗口渲染API结合这一点。”

对我来说,你的榜样, SDL_GetWindowSurface返回null。

所述filledCircleColor()函数采用形式0xRRGGBBAA的颜色。

它除去表面的一部分,并改变后的工作:

int result = filledCircleColor(renderer, circleX, circleY, circleR, 0xFF0000FF);


文章来源: Cannot draw a filled circle with SDL2 gfx
标签: c++ sdl sdl-2