如何在SDL 2.0绘制点,线或矩形中指定“宽度”或“点大小”

时间:2014-02-04 18:25:08

标签: c sdl-2

我正在尝试使用SDL 2.0函数SDL_RenderDrawPoints()来绘制屏幕上的数据点。

在“processing.org”中,我可以使用strokeWeight()来更改“点数”的大小。如何在SDL 2.0中执行此操作

3 个答案:

答案 0 :(得分:6)

SDL本身不支持它,使用SDL_gfx库。

这是一个函数thicklineRGBA,允许你指定行宽。

int thickLineRGBA (SDL_Renderer *rd, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint8 width, Uint8 r, Uint8 g, Uint8 b, Uint8 a)

答案 1 :(得分:2)

SDL_RenderSetScale

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

注意

绘图坐标在渲染器使用之前由x / y缩放因子缩放。这允许使用单个坐标系进行分辨率独立绘图。

如果这会导致渲染后端缩放或子像素绘制,则将使用适当的质量提示进行处理。为获得最佳结果,请使用整数缩放因子。

取自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();
}

此示例将以方形图案绘制三个系列的四个点:

  • 1.0刻度,50,50至100,100
  • 在125,50至175,100
  • 时的2.0比例
  • 3.0刻度,200,50至250,100

答案 2 :(得分:1)

不能把它放在评论中,所以这里是答案形式。

任何尝试使用@olevegard解决方案的人都要注意,这只能缩放SDL_RenderDrawPoints次呼叫 - 即。例如,它不适用于对角线

您可以更改SDL_RenderDrawPoints的{​​{1}}并查看此屏幕截图中的结果(我添加了aditional level SDL_RenderDrawLines

SDL_RenderDrawLines

SDL_RenderDrawLines

SDL_RenderDrawPoints

SDL_RenderDrawPoints