我一直在为通过 C++ 和 SDL 设计的视频游戏开发平铺系统。代码大致基于lazyfoo.net 上提供的教程,但使用不同的系统将图块绘制到屏幕上。代码的工作方式如下:我有一个名为的类Map
,它创建另一个名为Tile
. 它还有一个名为的函数,它从 .map 文件中读取数字并使用两个 for 循环set_tiles
将它们复制到 ' 的数组中;tile
一个用于 x,一个用于 y。然后我根据Tile
's 的特定变量将每个表面blit 到一个临时贴图。这是实际的代码:
地图.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;
}
瓷砖.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
.
安装程序.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 );
}