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

问题描述 投票:0回答:4

我正在尝试使用SDL 2.0函数

SDL_RenderDrawPoints()
在屏幕上绘制数据点。

在“processing.org”中,我可以使用

strokeWeight()
来更改“点”的大小。如何在 SDL 2.0 中执行此操作

c sdl-2
4个回答
11
投票

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)

7
投票

SDL_RenderSetScale

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

注意

绘制坐标在被渲染器使用之前按 x/y 缩放因子进行缩放。这允许使用单个坐标系进行与分辨率无关的绘图。

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

取自 SDL Wiki

示例

#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
  • 2.0 刻度为 125、50 至 175、100
  • 3.0 刻度为 200、50 至 250、100

6
投票

无法将此放在评论中,因此这里以答案的形式。

任何尝试使用 @olevegard 解决方案的人请注意,这仅可扩展

SDL_RenderDrawPoints
调用 - 即。例如,它不适用于对角线

您可以将

SDL_RenderDrawPoints
更改为
SDL_RenderDrawLines
并查看此屏幕截图中的结果(我添加了附加级别
scale = 6;

SDL_RenderDrawLines

SDL_RenderDrawLines

SDL_RenderDrawPoints

SDL_RenderDrawPoints


0
投票

使用矩形:

对于水平线和垂直线,只需使用 SDL_DrawFillRect 并将 SDL_Rect 的高度/宽度设置为厚度。

老实说,这种方法实际上只对垂直线和水平线有意义,但是,对于对角线,在这篇文章之后,您可以将矩形保存到纹理并旋转纹理。当其他方法都失败时,请使用纹理。

© www.soinside.com 2019 - 2024. All rights reserved.