2

我正在尝试使用 SDL_CreateRGBSurfaceFrom 从另一个表面创建一个 RGB 表面,因为它是一个 TTF 表面,我想更改深度以访问表面像素,这个函数应该返回一个 SDL_Surface* 指针,但编译器说它返回一个 int而且我无法将返回值分配给我的 SDL_Surface* 指针,这很奇怪,因为 SDL_CreateRGBSurface 完全按照预期的方式工作。

这是代码和编译器消息:

SDL_Surface     *tmp = SDL_CreateRGBSurfaceFrom(mySurface->pixels, mySurface->w, mySurface->h, 32, 32 * mySurface->w, RMASK, GMASK, BMASK, AMASK);

Compiling src/main.c: error: incompatible integer to pointer conversion assigning to 'SDL_Surface *' (aka 'struct SDL_Surface *') from 'int' [-,-Wint-conversion]
  ...= SDL_CreateRGBSurfaceFrom(mySurface->pixels, mySurface->w, mySurface->h, 32, 32 * mySurface->w, RMASK, GMASK, BMASK, AMASK)
     ^ 

最小可复制程序:

# include <SDL.h>
# include <SDL_ttf.h>
# if SDL_BYTEORDER == SDL_BIG_ENDIAN
#  define RMASK 0xff000000
#  define GMASK 0x00ff0000
#  define BMASK 0x0000ff00
#  define AMASK 0x000000ff
# else
#  define RMASK 0x000000ff
#  define GMASK 0x0000ff00
#  define BMASK 0x00ff0000
#  define AMASK 0xff000000
# endif

int             main(void)
{
    SDL_Surface     *textSurface;
    SDL_Surface     *newSurface;
    TTF_Font        *font;
    SDL_Color       white = {255, 255, 255, 255};

    font = TTF_OpenFont()
    if (SDL_Init() || TTF_Init()
    || !(font = TTF_OpenFont("assets/fonts/SweetCreamy.ttf", 30))
    || !(textSurface = TTF_RenderText_Solid(font, "Test", white))
    || !(newSurface = SDL_CreateRGBSurfaceFrom(textSurface->pixels, textSurface->w, textSurface->h, 32, 32 * textSurface->w, RMASK, GMASK, BMASK, AMASK)))
        return (-1);
    SDL_FreeSurface(textSurface);
    SDL_FreeSurface(newSurface);
    TTF_Quit();
    SDL_Quit();
    return (0);
}
4

1 回答 1

0

似乎 SDL_CreateRGBSurfaceFrom 被隐式定义,因此默认返回一个 int 。确保您包含 SDL.h。

于 2020-09-28T14:39:31.097 回答