平铺系统C ++ SDL

时间:2013-02-17 05:03:22

标签: c++ sdl

我一直致力于通过C ++和SDL设计的视频游戏的平铺系统。代码基于lazyfoo.net上提供的教程,但是使用不同的系统将图块绘制到屏幕上。代码的工作原理如下:我有一个名为Map的类,它创建了另一个名为Tile的类的数组。它还有一个名为set_tiles的函数,它从.map文件中读取数字,并使用两个for循环将它们复制到tile的数组中;一个用于x,一个用于y。然后我根据Tile的特定变量将每个表面blit到临时地图。这是实际的代码:

Map.cpp

SDL_Surface* Map::set_tiles ( SDL_Surface* tile_image ) {
Setup setup;

SDL_Surface* temp_map = NULL;

//Open the map
std::ifstream map ( "Test.map" );

//Catch any errors
if ( map.fail() ) return NULL;

//Initialize the tiles
for ( int y = 0; y < MAP_HEIGHT / TILE_HEIGHT; y++ ) {
    for ( int x = 0; x < MAP_WIDTH / TILE_WIDTH; x++ ) {
        //Determines the tile type
        int tile_type = -1;

        //Read the tile type from the map
        map >> tile_type;

        //Make sure it's a real tile
        if ( tile_type < 0 || tile_type >= TILE_SPRITES ) {
            map.close();
            return NULL;
        }

        //Error check for the .map file
        if ( map.fail() ) {
            map.close();
            return NULL;
        }

        //Add the tile to the array
        tile_array[x][y] = &Tile ( x, y, tile_type );

        //Create the temp. image crop
        SDL_Rect* temp_crop = &tile_array[x][y]->get_crop();

        //Check for crop errors
        if ( ( temp_crop->h || temp_crop->w ) == 0 ) return NULL;

        //Edit the temp. map
        setup.apply_surface ( x * TILE_WIDTH, y * TILE_HEIGHT, tile_image, temp_map, temp_crop );
    }
}

map.close();

//Return the modified map
return temp_map;

}

Tile.cpp

Tile::Tile ( int x, int y, int tile_type ) {
//Get the offsets
box.x = x;
box.y = y;

//Set the collision box
box.w = TILE_WIDTH;
box.h = TILE_HEIGHT;

//Get the tile type
type = tile_type;

//Choose the crop option
crop.w = TILE_WIDTH;
crop.h = TILE_HEIGHT;

switch ( tile_type ) {
case TILE_RED:
    crop.x = 0 * TILE_WIDTH;
    crop.y = 0 * TILE_HEIGHT;
    break;
case TILE_GREEN:
    crop.x = 1 * TILE_WIDTH;
    crop.y = 0 * TILE_HEIGHT;
    break;
case TILE_BLUE:
    crop.x = 2 * TILE_WIDTH;
    crop.y = 0 * TILE_HEIGHT;
    break;
case TILE_CENTER:
    crop.x = 3 * TILE_WIDTH;
    crop.y = 0 * TILE_HEIGHT;
    break;
case TILE_TOP:
    crop.x = 0 * TILE_WIDTH;
    crop.y = 1 * TILE_HEIGHT;
    break;
case TILE_TOPRIGHT:
    crop.x = 1 * TILE_WIDTH;
    crop.y = 1 * TILE_HEIGHT;
    break;
case TILE_RIGHT:
    crop.x = 2 * TILE_WIDTH;
    crop.y = 1 * TILE_HEIGHT;
    break;
case TILE_BOTTOMRIGHT:
    crop.x = 3 * TILE_WIDTH;
    crop.y = 1 * TILE_HEIGHT;
    break;
case TILE_BOTTOM:
    crop.x = 0 * TILE_WIDTH;
    crop.y = 2 * TILE_HEIGHT;
    break;
case TILE_BOTTOMLEFT:
    crop.x = 1 * TILE_WIDTH;
    crop.y = 2 * TILE_HEIGHT;
    break;
case TILE_LEFT:
    crop.x = 2 * TILE_WIDTH;
    crop.y = 2 * TILE_HEIGHT;
    break;
case TILE_TOPLEFT:
    crop.x = 3 * TILE_WIDTH;
    crop.y = 2 * TILE_HEIGHT;
    break;
}

}

问题是set_tiles在调用时返回NULL指针。通过一些调试我发现它一直到最后然后返回一个NULL,它不会在for循环中的某个地方执行。我也尝试将所有tile crop.x和y设置为0,只是为了看它是否有效,但事实并非如此。

注意 问题不在于我的图像或.map文件。 我认为我对指针的理解可能有点错误,而且在调用apply_surface时我可能犯了一个错误。

Setup.cpp

void Setup::apply_surface ( int x, int y, SDL_Surface* source, SDL_Surface* &destination, SDL_Rect* clip ) {
//Temporary rectangle to hold the offsets
SDL_Rect offset;

//Get the offsets
offset.x = x;
offset.y = y;

//Blit the surface
SDL_BlitSurface ( source, clip, destination, &offset );

}

1 个答案:

答案 0 :(得分:1)

SDL_BlitSurface将一个表面与另一个表面相撞。它们都必须初始化,否则函数什么都不做。您正在向它传递NULL指针。您需要通过调用类似SDL_CreateRGBSurface的内容来初始化指针。