user3818296
user3818296

Reputation:

Draw a filled circle with SDL2_gfx in C/C++

I am a SDL beginner. I would like to draw a black background and a filled blue circle on it with SDL2. As there is no way to draw a circle with SDL2, I use SDL2_gfx. I have no problem to draw a black background, but I do not arrive to draw a circle with the function filledCircleRGBA.

#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. To compile, I use gcc -O0 -g sdl2-test.c `sdl2-config --cflags --libs` -lSDL2_gfx.

Moreover, valgrind notifies me that there are 21 errors from 21 contexts. 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.

Thanks.

Upvotes: 2

Views: 4202

Answers (1)

Leiaz
Leiaz

Reputation: 2917

You are calling filledCircleRGBA with an alpha value of 0. Your circle is completely transparent.

For the errors reported by Valgrind, I just noticed you are not actually calling 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). 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.

You can create a suppression file for Valgrind to hide the leaks that aren't yours. 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.

Upvotes: 2

Related Questions