简体   繁体   English

在C / C ++中用SDL2_gfx绘制一个实心圆

[英]Draw a filled circle with SDL2_gfx in C/C++

I am a SDL beginner. 我是SDL初学者。 I would like to draw a black background and a filled blue circle on it with SDL2. 我想用SDL2在其上绘制一个黑色背景和一个填充的蓝色圆圈。 As there is no way to draw a circle with SDL2, I use SDL2_gfx . 由于无法使用SDL2画圆,因此我使用SDL2_gfx I have no problem to draw a black background, but I do not arrive to draw a circle with the function filledCircleRGBA . 我没有问题可以绘制黑色背景,但是我没有到达使用功能fillCircleRGBA绘制圆。

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>


#define DEFAULT_WINDOW_WIDTH  800
#define DEFAULT_WINDOW_HEIGHT 600


void
print_SDL_error()
{
  fputs("SDL_Error: ",  stderr);
  fputs(SDL_GetError(), stderr);
  fputc('\n',           stderr);
}

bool
initSDL()
{
  if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
      fprintf(stderr, "SDL could not initialize!\n");
      print_SDL_error();
      return false;
    }
  return true;
}

SDL_Window*
initMainWindow()
{
  SDL_Window* window;
  window = SDL_CreateWindow("SDL2 test",
                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT,
                SDL_WINDOW_SHOWN);
  if(window == NULL)
    {
      fprintf(stderr, "SDL could not create a window!\n");
      print_SDL_error();
    }
  return window;
}

SDL_Window*
initSDLAndMainWindow(SDL_Window* * window)
{
  return initSDL() ? initMainWindow() : NULL;
}

void
quit(SDL_Window* main_window)
{
  SDL_DestroyWindow(main_window);
  SDL_Quit();
}


int
main(int argc, char* argv[])
{
  SDL_Window* main_window;
  main_window = initMainWindow();
  if(main_window == NULL)
    {
      quit(NULL);
      return EXIT_FAILURE;
    }

  SDL_Renderer* main_window_renderer;
  main_window_renderer = SDL_CreateRenderer(main_window, -1, SDL_RENDERER_ACCELERATED);
  if(main_window_renderer == NULL)
    {
      fprintf(stderr, "Renderer could not be created!\n");
      print_SDL_error();
      quit(main_window);
      return EXIT_FAILURE;
    }

  SDL_SetRenderDrawColor(main_window_renderer, 0, 0, 0, 0);
  SDL_RenderClear(main_window_renderer);
  SDL_RenderPresent(main_window_renderer);
  SDL_Delay(2000);

  if(filledCircleRGBA(main_window_renderer,
              150, 150, 75,
              0, 0, 255, 0) != 0)
    {
      fprintf(stderr, "A circle was not rendered!\n");
      print_SDL_error();
      SDL_DestroyRenderer(main_window_renderer);
      quit(main_window);
      return EXIT_FAILURE;
    }
  SDL_RenderPresent(main_window_renderer);
  SDL_Delay(2000);

  SDL_DestroyRenderer(main_window_renderer);
  quit(main_window);
  return EXIT_SUCCESS;
}

I am running under Debian GNU/Linux 8 "Jessie", and I use libsdl2-dev and libsdl2-gfx-dev of my distribution. 我在Debian GNU / Linux 8“ Jessie”下运行,并且使用我的发行版中的libsdl2-dev和libsdl2-gfx-dev。 To compile, I use gcc -O0 -g sdl2-test.c `sdl2-config --cflags --libs` -lSDL2_gfx . 要进行编译,我使用gcc -O0 -g sdl2-test.c `sdl2-config --cflags --libs` -lSDL2_gfx

Moreover, valgrind notifies me that there are 21 errors from 21 contexts. 此外,valgrind通知我21个上下文中有21个错误。 3 errors come from SDL2 calls, and 19 from nouveau_dri.so that I suppose to be the shared library used by nouveau driver (a free/libre driver for nVidia GPU) so this ones may not be my fault. SDL2调用产生了3个错误,nouveau_dri。产生了19个错误,因此我假设它是nouveau驱动程序(nVidia GPU的自由/自由驱动程序)使用的共享库,所以这可能不是我的错。

Thanks. 谢谢。

You are calling filledCircleRGBA with an alpha value of 0. Your circle is completely transparent. 您正在调用alpha值为0的filledCircleRGBA 。您的圆是完全透明的。

For the errors reported by Valgrind, I just noticed you are not actually calling SDL_Init . 对于Valgrind报告的错误,我只是注意到您实际上并未调用SDL_Init This is causing some leaks, but not all of them. 这导致了一些泄漏,但不是全部。 The others aren't caused by your code. 其他不是由您的代码引起的。

I also see some Conditional jump or move depends on uninitialised value(s) . 我还看到一些Conditional jump or move depends on uninitialised value(s) Valgrind can reports where the uninitialised value was created with the --track-origins=yes option, that way you can see if you passed an uninitialised value to a library function or if the error is in the function. Valgrind可以使用--track-origins=yes选项报告创建未初始化值的--track-origins=yes ,这样您可以查看是否将未初始化值传递给库函数,或者该函数中是否有错误。

You can create a suppression file for Valgrind to hide the leaks that aren't yours. 您可以为Valgrind创建抑制文件以隐藏不属于您的泄漏。 It is best to have the debug symbols for the libraries if you want to create rules specific enought to avoid hiding cases where you create something with an SDL function and don't destroy it. 如果要创建足够具体的规则,最好避免使用库的调试符号,以避免隐藏用SDL函数创建某些东西且不破坏它的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM