SDL_CreateRGBSurfaceFrom无法编译?

问题描述

我正在尝试使用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,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};

    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 * textSurface->w,AMASK)))
        return (-1);
    SDL_FreeSurface(textSurface);
    SDL_FreeSurface(newSurface);
    TTF_Quit();
    SDL_Quit();
    return (0);
}

解决方法

似乎SDL_CreateRGBSurfaceFrom被隐式定义,因此将返回默认的int值。确保您包括SDL.h。