SDL2尝试裁剪纹理或曲面

时间:2015-01-19 11:12:36

标签: c++ visual-studio-2012 sdl-2

我有一个大的PNG文件,其中包含我想在16x16网格中使用的所有纹理。使用SDL2和SDL2_Image库我可以将其作为一个大纹理加载,并一直试图将该纹理切割成较小的纹理。他们是这样做的吗?

我知道我可以在RenderCopy结束时裁剪图像。但我打算在整个程序中的点处修改纹理,这样就可以更容易地处理多个纹理(我在考虑一个数组)而不是一个大纹理。

我很高兴使用Surfaces代替这样做。

1 个答案:

答案 0 :(得分:0)

SDL_Surface* surf = IMG_Load("Image.PNG");
int width = surf->w;
int height = surf->h;
int clipPerRow = 16;
int clipPerColumn = 16;
SDL_Texture* texture = SDL_CreateTextureFromSurface(RENDERER,surf);
SDL_FreeSurface(surf);
SDL_Texture* clip[clipPerRow][clipPerColumn];
for(int i=0; i<clipPerRow; i++)
{
    for(int j=0;j<clipPerColumn;j++)
    {
        clip[i][j] = SDL_CreateTexture(RENDERER, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, width/clipPerRow, height/clipPerColumn);
        SDL_SetTextureBlendMode(clip[i][j], SDL_BLENDMODE_BLEND);
        SDL_Rect rect = {i*width/clipPerRow,j*height/clipPerColumn, width/clipPerRow, height/clipPerColumn};
        SDL_SetRenderTarget(RENDERER, clip[i][j]);
        SDL_RenderCopy(RENDERER, texture, &rect, NULL);

    }
}
SDL_SetRenderTarget(RENDERER, NULL);
int x= 100;
int y =100;
while(!quit){
    while(SDL_PollEvent(&e)) if(e.type == SDL_QUIT) quit = 1;
    SDL_SetRenderDrawColor( RENDERER, 0x00, 0x00,0x00,0x00);
    SDL_RenderClear( RENDERER);
    for(int i=0; i<clipPerRow; i++)
    {
        for(int j=0;j<clipPerColumn;j++)
        {
            SDL_Rect rect = {x+i*width/clipPerRow,y+j*height/clipPerColumn, width/clipPerRow, height/clipPerColumn};
            SDL_RenderCopy(RENDERER,clip[i][j],NULL,&rect);
        }
    }
    SDL_RenderPresent(RENDERER);
}