Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to specify "width" or "point size" in SDL 2.0 draw points, lines or rect

Tags:

c

sdl-2

I am trying to use SDL 2.0 function SDL_RenderDrawPoints() to plot data points on the screen.

In "processing.org", I can use strokeWeight() to change the size of my "points". How to do this in SDL 2.0

like image 792
Zhe Hu Avatar asked Feb 04 '14 18:02

Zhe Hu


3 Answers

SDL does not support it natively,use the SDL_gfx library.

There's a function thicklineRGBA which allows you to specify line width.

int thickLineRGBA (SDL_Renderer *rd, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 width, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
like image 112
Mahesh Bansod Avatar answered Dec 06 '22 15:12

Mahesh Bansod


SDL_RenderSetScale

int SDL_RenderSetScale(SDL_Renderer* renderer,
                   float         scaleX,
                   float         scaleY)

Note

The drawing coordinates are scaled by the x/y scaling factors before they are used by the renderer. This allows resolution independent drawing with a single coordinate system.

If this results in scaling or subpixel drawing by the rendering backend, it will be handled using the appropriate quality hints. For best results use integer scaling factors.

Taken from the SDL Wiki

Examlple

#include <SDL2/SDL.h>

#include <iostream>

int main()
{
    SDL_Renderer* renderer;
    SDL_Window* window;
    SDL_Point points[4];
    SDL_Point  startingPoint;
    startingPoint.x = 50;
    startingPoint.y = 50;
    float scale = 1.0;

    if ( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
        std::cout << "Failed to init SDL : " << SDL_GetError();

    window = SDL_CreateWindow( "Client", 50, 50, 500, 500, 0 );

    if ( window == nullptr )
        std::cout << "Failed to apply video mode : " << SDL_GetError();

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

    if ( renderer == nullptr )
        std::cout << "Could not create renderer!";

    SDL_RenderSetLogicalSize( renderer, 500, 500 );

    // Clear background
    SDL_SetRenderDrawColor( renderer, 0, 0, 0, 255 );
    SDL_RenderClear( renderer );
    SDL_SetRenderDrawColor( renderer, 255, 255, 255, 255 );

    // Create first 4 points
    points[0].x = startingPoint.x;
    points[0].y = startingPoint.y;

    points[1].x = startingPoint.x + 50;
    points[1].y = startingPoint.y;

    points[2].x = startingPoint.x;
    points[2].y = startingPoint.y + 50;

    points[3].x = startingPoint.x + 50;
    points[3].y = startingPoint.y + 50;

    SDL_RenderDrawPoints( renderer, points, 4 );

    // Create seconds 4 points
    startingPoint.x = 125;
    scale = 2.0;

    points[0].x = startingPoint.x;
    points[0].y = startingPoint.y;

    points[1].x = startingPoint.x + 50;
    points[1].y = startingPoint.y;

    points[2].x = startingPoint.x;
    points[2].y = startingPoint.y + 50;

    points[3].x = startingPoint.x + 50;
    points[3].y = startingPoint.y + 50;

    // Apply scale
    for ( int i = 0; i < 4 ; ++i )
    {
        points[i].x /= scale;
        points[i].y /= scale;
    }

    SDL_RenderSetScale( renderer, scale, scale );
    SDL_RenderDrawPoints( renderer, points, 4 );

    // Create third 4 points
    startingPoint.x = 200;
    scale = 3.0;

    points[0].x = startingPoint.x;
    points[0].y = startingPoint.y;

    points[1].x = startingPoint.x + 50;
    points[1].y = startingPoint.y;

    points[2].x = startingPoint.x;
    points[2].y = startingPoint.y + 50;

    points[3].x = startingPoint.x + 50;
    points[3].y = startingPoint.y + 50;

    // Apply scale
    for ( int i = 0; i < 4 ; ++i )
    {
        points[i].x /= scale;
        points[i].y /= scale;
    }

    SDL_RenderSetScale( renderer, scale, scale );
    SDL_RenderDrawPoints( renderer, points, 4 );

    SDL_RenderPresent( renderer );

    std::cin.ignore();
}

This example will draw three series of four points in a square pattern :

  • 1.0 scale at 50, 50 to 100, 100
  • 2.0 scale at 125, 50 to 175, 100
  • 3.0 scale at 200, 50 to 250, 100
like image 31
olevegard Avatar answered Dec 06 '22 14:12

olevegard


Can't put this in comment so here it is in form of answer.

Anyone trying to use @olevegard solution please be aware that this scales ONLY SDL_RenderDrawPoints calls - ie. for example it's not working for diagonal lines

You can change SDL_RenderDrawPoints for SDL_RenderDrawLines and see result as in this screenshots (I've added aditional level scale = 6;)

SDL_RenderDrawLines

SDL_RenderDrawLines

SDL_RenderDrawPoints

SDL_RenderDrawPoints

like image 39
yatsek Avatar answered Dec 06 '22 15:12

yatsek