The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Simple example for SDL2_gfx?
joymaker


Joined: 22 Nov 2016
Posts: 9
Does anybody out there have a nice, simple, self-contained example/test program for SDL2_gfx? I'm searching the web in vain to find one.
The one it comes with relies on way too much other test machinery, which comes with SDL2 but is inconvenient to access given the way things install on a Mac.

Which leads to my next question: does this thing actually WORK? I'm not seeing a whole lot of support or usage for the SDL2 version out there, and my first attempt got successful return codes but no pixels drawn!
Re: Simple example for SDL2_gfx?
rtrussell


Joined: 10 Feb 2016
Posts: 88
joymaker wrote:
Which leads to my next question: does this thing actually WORK? I'm not seeing a whole lot of support or usage for the SDL2 version out there, and my first attempt got successful return codes but no pixels drawn!

What specific drawing function(s) are you calling? I use SDL2_gfx extensively, and it certainly 'works' although IMHO it leaves a lot to be desired. For example arcs and sectors take the subtended angle as an integer parameter in degrees! This means it is impossible to get 'pixel-accurate' results if the radius is more than around 60 pixels, which I consider to be somewhat ridiculous.

In an ideal world I'd like to see something with a functionality comparable with GDI Plus in Windows, that is full support for anti-aliasing and all parameters as floating-point values rather than integers. But in the absence of that, SDL2_gfx works acceptably well for my purposes.

Richard.
joymaker


Joined: 22 Nov 2016
Posts: 9
RTRussell: I simply used circleColor, though the renderer was directed to a texture, rather than to the window itself. No pixels. But your report is encouraging, I'll try some more experiments.

Perchance do you have a crisp little nugget of a sample program that I can try building?
rtrussell


Joined: 10 Feb 2016
Posts: 88
joymaker wrote:
Perchance do you have a crisp little nugget of a sample program that I can try building?

You could try this:

Code:
#include <stdio.h>
#include "SDL2_gfxPrimitives.h"

#define WIDTH 640
#define HEIGHT 480

int main(int argc, char* argv[])
{

   if (SDL_Init(SDL_INIT_VIDEO))
   {
      printf ("SDL_Init Error: %s", SDL_GetError());
      return 1;
   }

   SDL_Window *window = SDL_CreateWindow("SDL2_gfx test", 100, 100, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
   if (window == NULL)
   {
      printf ("SDL_CreateWindow Error: %s", SDL_GetError());
      SDL_Quit();
      return 2;
   }

   SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
   if (renderer == NULL)
   {
      SDL_DestroyWindow(window);
      printf ("SDL_CreateRenderer Error: %s", SDL_GetError());
      SDL_Quit();
      return 3;
   }

   SDL_Event e;

   int quit = 0;
   while (!quit)
   {
      if (SDL_PollEvent(&e))
      {
         if (e.type == SDL_QUIT)
            quit = 1;
      }
      SDL_SetRenderDrawColor(renderer, 0, 0, 0xFF, 0xFF);
      SDL_RenderClear(renderer);
      thickLineColor(renderer, 0, 0, WIDTH, HEIGHT, 20, 0xFF00FFFF) ;
      thickLineColor(renderer, 0, HEIGHT, WIDTH, 0, 20, 0xFF00FFFF) ;
      SDL_RenderPresent(renderer);
      SDL_Delay(10);
   }

   SDL_DestroyRenderer(renderer);
   SDL_DestroyWindow(window);
   SDL_Quit();
   return 0;
}
A nice example
joymaker


Joined: 22 Nov 2016
Posts: 9
Thanks, rtrussel, that worked great! My bug was not in the misunderstanding of the primitives, but in a misunderstanding of color specification. (I created something with an alpha of zero.)

I juiced up your examples just a little by adding some circles, here's the result. I think you should share it widely, there is a real lack of such sample code that I can find anywhere on the web!

Ken

Code:
//
//  TestGFX.c
//  SDL Examples
//
//  Created by Richard Russell (rtrussell) on 12/7/16.
//

#include <stdio.h>
#include "SDL2_gfxPrimitives.h"

#define WIDTH 1200
#define HEIGHT 900

int main(int argc, char* argv[])
{
   
    if (SDL_Init(SDL_INIT_VIDEO))
    {
        printf ("SDL_Init Error: %s", SDL_GetError());
        return 1;
    }
   
    SDL_Window *window = SDL_CreateWindow("SDL2_gfx test", 100, 100, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
    if (window == NULL)
    {
        printf ("SDL_CreateWindow Error: %s", SDL_GetError());
        SDL_Quit();
        return 2;
    }
   
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == NULL)
    {
        SDL_DestroyWindow(window);
        printf ("SDL_CreateRenderer Error: %s", SDL_GetError());
        SDL_Quit();
        return 3;
    }
   
    SDL_Event e;
   
    int quit = 0;
    while (!quit)
    {
        if (SDL_PollEvent(&e))
        {
            if (e.type == SDL_QUIT)
                quit = 1;
        }
        SDL_SetRenderDrawColor(renderer, 0, 0, 0xFF, 0xFF);
        SDL_RenderClear(renderer);
        thickLineColor(renderer, 0, 0, WIDTH, HEIGHT, 20, 0xFF00FFFF) ;
        thickLineColor(renderer, 0, HEIGHT, WIDTH, 0, 20, 0xFF00FFFF) ;
        circleColor(renderer, WIDTH/2, HEIGHT/2, 33, 0xff00ff00);
        filledCircleColor(renderer, WIDTH/2, HEIGHT/2, 30, 0xff00ffcc);
        SDL_RenderPresent(renderer);
        SDL_Delay(10);
    }
   
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
A nice example
joymaker


Joined: 22 Nov 2016
Posts: 9
Thanks, rtrussel, that worked great! My bug was not in the misunderstanding of the primitives, but in a misunderstanding of color specification. (I created something with an alpha of zero.)

I juiced up your examples just a little by adding some circles, here's the result. I think you should share it widely, there is a real lack of such sample code that I can find anywhere on the web!

Ken

Code:
//
//  TestGFX.c
//  SDL Examples
//
//  Created by Richard Russell (rtrussell) on 12/7/16.
//

#include <stdio.h>
#include "SDL2_gfxPrimitives.h"

#define WIDTH 1200
#define HEIGHT 900

int main(int argc, char* argv[])
{
   
    if (SDL_Init(SDL_INIT_VIDEO))
    {
        printf ("SDL_Init Error: %s", SDL_GetError());
        return 1;
    }
   
    SDL_Window *window = SDL_CreateWindow("SDL2_gfx test", 100, 100, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
    if (window == NULL)
    {
        printf ("SDL_CreateWindow Error: %s", SDL_GetError());
        SDL_Quit();
        return 2;
    }
   
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == NULL)
    {
        SDL_DestroyWindow(window);
        printf ("SDL_CreateRenderer Error: %s", SDL_GetError());
        SDL_Quit();
        return 3;
    }
   
    SDL_Event e;
   
    int quit = 0;
    while (!quit)
    {
        if (SDL_PollEvent(&e))
        {
            if (e.type == SDL_QUIT)
                quit = 1;
        }
        SDL_SetRenderDrawColor(renderer, 0, 0, 0xFF, 0xFF);
        SDL_RenderClear(renderer);
        thickLineColor(renderer, 0, 0, WIDTH, HEIGHT, 20, 0xFF00FFFF) ;
        thickLineColor(renderer, 0, HEIGHT, WIDTH, 0, 20, 0xFF00FFFF) ;
        circleColor(renderer, WIDTH/2, HEIGHT/2, 33, 0xff00ff00);
        filledCircleColor(renderer, WIDTH/2, HEIGHT/2, 30, 0xff00ffcc);
        SDL_RenderPresent(renderer);
        SDL_Delay(10);
    }
   
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}