【问题标题】:How to get the color of a specific pixel drawn using SDL_RenderDrawPoint() on SDL2?如何获取在 SDL2 上使用 SDL_RenderDrawPoint() 绘制的特定像素的颜色?
【发布时间】:2021-10-30 10:55:12
【问题描述】:
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawPoint(renderer, (window_height / 2) + xxi[i], -yyi[i] + (window_width / 2));
SDL_RenderPresent(renderer);

现在我想得到 xxi[i], yyi[i] 点的颜色。

但我不知道如何获得。

【问题讨论】:

    标签: graphics 2d rendering sdl sdl-2


    【解决方案1】:

    SDL_RenderReadPixels():

    /**
     * Read pixels from the current rendering target to an array of pixels.
     *
     * **WARNING**: This is a very slow operation, and should not be used
     * frequently.
     *
     * `pitch` specifies the number of bytes between rows in the destination
     * `pixels` data. This allows you to write to a subrectangle or have padded
     * rows in the destination. Generally, `pitch` should equal the number of
     * pixels per row in the `pixels` data times the number of bytes per pixel,
     * but it might contain additional padding (for example, 24bit RGB Windows
     * Bitmap data pads all rows to multiples of 4 bytes).
     *
     * \param renderer the rendering context
     * \param rect an SDL_Rect structure representing the area to read, or NULL
     *             for the entire render target
     * \param format an SDL_PixelFormatEnum value of the desired format of the
     *               pixel data, or 0 to use the format of the rendering target
     * \param pixels a pointer to the pixel data to copy into
     * \param pitch the pitch of the `pixels` parameter
     * \returns 0 on success or a negative error code on failure; call
     *          SDL_GetError() for more information.
     */
    extern DECLSPEC int SDLCALL SDL_RenderReadPixels(SDL_Renderer * renderer,
                                                     const SDL_Rect * rect,
                                                     Uint32 format,
                                                     void *pixels, int pitch);
    

    rect 参数的宽度和高度设置为 1 以读取单个像素。

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      相关资源
      最近更新 更多