• Advertisement
Sign in to follow this  

SDL Tile Surface - Problem

This topic is 2898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I am implementing a tile function for using SDL. It takes a source image and tiles it to the size specified using SDL_BlitSurface(). However, when I save the tiled image it is always black. SDL_BlitSurface() returns successfully every time, and SDL_CreateRGBSurface() returns non-null. What could be going wrong? Here is the function:
SDL_Surface * SDL_TileSurface(SDL_Surface * source, u32 tile_x, u32 tile_y) {
    const SDL_PixelFormat& fmt = *(source->format);
    SDL_Surface * ret = SDL_CreateRGBSurface(
        SDL_SWSURFACE,
        source->w * tile_x,
        source->h * tile_y,
        fmt.BitsPerPixel,
        fmt.Rmask,
        fmt.Gmask,
        fmt.Bmask,
        fmt.Amask
    );

    assert(ret != NULL);

    for(u32 i = 0; i < tile_x; i++) {
        for(u32 d = 0; d < tile_y; d++) {
            SDL_Rect dst_rect;
            dst_rect.x = i * source->w;
            dst_rect.y = d * source->h;
            dst_rect.w = source->w;
            dst_rect.h = source->h;

            assert(SDL_BlitSurface(source, NULL, ret, &dst_rect) == 0);
        }
    }

    SDL_SaveBMP(ret, "tmp.bmp");

    return ret;
}
Thanks beforehand! EDIT: I appears that that function is not the issue. It still returns a black texture however. Here is my (more) complete code.
SDL_Surface * SDL_TileSurface(SDL_Surface * source, u32 tile_x, u32 tile_y) {
    const SDL_PixelFormat& fmt = *(source->format);
    SDL_Surface * ret = SDL_CreateRGBSurface(
        SDL_SWSURFACE,
        source->w * tile_x,
        source->h * tile_y,
        fmt.BitsPerPixel,
        fmt.Rmask,
        fmt.Gmask,
        fmt.Bmask,
        fmt.Amask
    );

    assert(ret != NULL);

    for(u32 i = 0; i < tile_x; i++) {
        for(u32 d = 0; d < tile_y; d++) {
            SDL_Rect dst_rect;
            dst_rect.x = i * source->w;
            dst_rect.y = d * source->h;
            dst_rect.w = source->w;
            dst_rect.h = source->h;

            assert(SDL_BlitSurface(source, NULL, ret, &dst_rect) == 0);
        }
    }

    return ret;
}

int main (int argc, char** argv) {
    vector<string> TextureList;
    getFilesInDirectory("Data/Textures/", "png", TextureList, true);

    vector<string> OldTilesetList;
    getFilesInDirectory("Data/Tilesets/", "png", OldTilesetList, false);

    for(u32 i = 0; i < OldTilesetList.size(); i++) {
        remove(OldTilesetList.c_str());
    }

    for(u32 i = 0; i < TextureList.size(); i++) {
        SDL_Surface * texture = IMG_Load(TextureList.c_str());
        assert(texture->w % 64 == 0 && texture->h % 64 == 0);

        // scale the texture to 64 x 64
        SDL_Surface * scaledTexture = shrinkSurface(texture, texture->w / 64, texture->h / 64);
        SDL_FreeSurface(texture);

        // tile the texture to 256 x 256
        SDL_Surface * tiledSurface = SDL_TileSurface(scaledTexture, 4, 4);
        SDL_FreeSurface(scaledTexture);

        // save the texture
        c8 file[256];
        sprintf(file, "Data/Tilesets/%u.bmp", i);
        SDL_SaveBMP(tiledSurface, file);
    }

    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
SDL_TileSurface() and saving the resulting image work fine on my computer. You didn't provide code for shrinkSurface (and so I commented that out). I'd take a look at that function and see if anything dodgy is going on in there.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement