C++ 包含多个图像的SDL_曲面

C++ 包含多个图像的SDL_曲面,c++,sdl,C++,Sdl,假设我有一个SDL_曲面,它只是一个图像。 如果我想让SDL_表面有三个图像副本,一个在另一个下面,该怎么办 我提出了这个函数,但它没有显示任何内容: void ElementView::adjust() { int imageHeight = this->img->h; int desiredHeight = 3*imageHeight; int repetitions = desiredHeight / imageHeight ; int r

假设我有一个
SDL_曲面
,它只是一个图像。 如果我想让SDL_表面有三个图像副本,一个在另一个下面,该怎么办

我提出了这个函数,但它没有显示任何内容:

void ElementView::adjust() 
{
    int imageHeight = this->img->h;
    int desiredHeight = 3*imageHeight;

    int repetitions =  desiredHeight / imageHeight ;
    int remainder = desiredHeight % imageHeight ;

    SDL_Surface* newSurf = SDL_CreateRGBSurface(img->flags, img->w, desiredHeight, 32, img->format->Rmask, img->format->Gmask, img->format->Bmask,img->format->Amask);

    SDL_Rect rect;
    memset(&rect, 0, sizeof(SDL_Rect));
    rect.w = this->img->w;
    rect.h = this->img->h;

    for (int i = 0 ; i < repetitions ; i++) 
    {
        rect.y = i*imageHeight;
        SDL_BlitSurface(img,NULL,newSurf,&rect);
    }
    rect.y += remainder;
    SDL_BlitSurface(this->img,NULL,newSurf,&rect);

    if (newSurf != NULL) {
        SDL_FreeSurface(this->img);
        this->img = newSurf;
    }
}
void元素视图::调整()
{
int IMAGEHEIGH=此->img->h;
int desiredHeight=3*imageHeight;
int重复=所需高度/图像高度;
整数余数=所需高度%imageHeight;
SDL_Surface*newSurf=SDL_创建RGB曲面(img->flags,img->w,desiredHeight,32,img->format->Rmask,img->format->Gmask,img->format->Bmask,img->format->Amask);
SDL_Rect;
memset(&rect,0,sizeof(SDL_rect));
rect.w=此->img->w;
rect.h=此->img->h;
for(int i=0;i<重复;i++)
{
rect.y=i*imageHeight;
SDL_BlitSurface(img、NULL、newSurf和rect);
}
矩形y+=余数;
SDL_BlitSurface(此->img、NULL、newSurf和rect);
如果(newSurf!=NULL){
SDL_自由曲面(此->img);
此->img=newSurf;
}
}
我想你应该

  • 创建一个新曲面,其长度是初始曲面的3倍
  • 使用类似于现有代码(SDL_BlitSurface)的代码从img复制到新曲面,但将目标作为新曲面
  • SDL_在您的原始
    img
  • 将新曲面指定给
    img
编辑:这里是一些示例代码,但没有时间测试它

void adjust(SDL_Surface** img)
{
    SDL_PixelFormat *fmt = (*img)->format;
    SDL_Surface* newSurf = SDL_CreateRGBSurface((*img)->flags, (*img)->w, (*img)->h * 3, fmt->BytesPerPixel * 8, fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask);

    SDL_Rect rect;
    memset(&rect, 0, sizeof(SDL_Rect));
    rect.w = (*img)->w;
    rect.h = (*img)->h;

    int i = 0;
    for (i ; i < 3; i++) 
    {
        SDL_BlitSurface(*img,NULL,newSurf,&rect);
        rect.y += (*img)->h;
    }

    SDL_FreeSurface(*img);
    *img = newSurf;
}
空隙调整(SDL\U表面**img)
{
SDL_像素格式*fmt=(*img)->格式;
SDL_Surface*newSurf=SDL_CreateRGB Surface((*img)->标志,(*img)->w,(*img)->h*3,fmt->字节像素*8,fmt->Rmask,fmt->Gmask,fmt->Bmask,fmt->Amask);
SDL_Rect;
memset(&rect,0,sizeof(SDL_rect));
rect.w=(*img)->w;
rect.h=(*img)->h;
int i=0;
对于(i;i<3;i++)
{
SDL_BlitSurface(*img、NULL、newSurf和rect);
矩形y+=(*img)->h;
}
SDL_自由曲面(*img);
*img=newSurf;
}

你有什么理由不可以将原始图像进行多次blit吗?@Xymostech,我不应该触摸将表面绘制到屏幕上的代码。我认为问题在于
img
不够大,无法容纳3个副本。看起来没有一种好方法可以创建任意大小的新曲面。为什么你不能触摸绘制实际图形的代码?我尝试用
SDL\u CreateRGBSurface
创建一个新的曲面,它与
img
一样,但具有所需的高度,然后快速移动到该曲面上,但它显示一个空图像。@l19你确定你确实更改了真实图像,而不仅仅是指向它的指针吗?您可能释放了正确的图像,然后忘记保存新图像。提示:您需要一个双指针才能正确执行此操作。
SDL_Surface*Surface=SDL_CreateRGBSurface(…);SDL_BlitSurface(img、NULL、surface和rect);如果(surface!=NULL){SDL_FreeSurface(img);img=surface;}
@l19,就像Xymostech说的,传递一个指向你的SDL_surface*img的指针,所以SDL_surface**img或者一个引用,你需要改变它指向的位置@l19不,不,不!按照我给你的步骤,在你复制数据之前你不能释放,使用SDL_BlitSurface@emartel,查看我之前的评论