无法使用 SDL2 gfx 绘制实心圆 [英] Cannot draw a filled circle with SDL2 gfx

查看:114
本文介绍了无法使用 SDL2 gfx 绘制实心圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有如下代码.想要的结果是创建了一个窗口并绘制了一个实心圆:

#include #include #include int main(){SDL_Window* window = nullptr;SDL_Renderer* 渲染器 = nullptr;如果(SDL_Init(SDL_INIT_EVERYTHING)<0){std::cout <<无法初始化"<<std::endl;返回 1;}window = SDL_CreateWindow("MyGame",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480,SDL_WINDOW_SHOWN);如果(!窗口){std::cout <<无法创建窗口"<<std::endl;返回 1;}渲染器 = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);SDL_RenderClear(渲染器);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(渲染器,circleX,circleY,circleR,circleColour);std::cout <<画圆r"<<圆R<<" x " <<圆X<

问题是没有画圆——窗口全黑.

输出:

绘制圆 r 100 x 300 y 300 circleColour 16711680画圆结果 0

filledCircleColor 返回 0,这意味着没有错误.

应该怎么做才能画圆?我在 Ubuntu 上使用 SDL 2.0.2 和 SDL2 gfx 扩展.

解决方案

关于 SDL_GetWindowSurface, 文档说:您不能将其与 3D 或此窗口上的渲染 API 结合使用.">

对我来说,在你的例子中,SDL_GetWindowSurface 返回 null.

Color<>Color/a> 函数采用 0xRRGGBBAA 形式的颜色.

去掉表面部分后就可以工作了,改成:

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

There is the following code. The desired result is that a window is created and a filled circle is drawn:

#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;
}

The problem is that the circle is not drawn - the window is all black.

The output:

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

filledCircleColor returns 0 what should mean that there's no error.

What should be done so that the circle is drawn? I'm using SDL 2.0.2 on Ubuntu with SDL2 gfx extension.

解决方案

About SDL_GetWindowSurface, the documentation says : "You may not combine this with 3D or the rendering API on this window."

For me, in your example, SDL_GetWindowSurface returns null.

The filledCircleColor() function takes a color of the form 0xRRGGBBAA.

It works after removing the surface part, and changing to :

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

这篇关于无法使用 SDL2 gfx 绘制实心圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆