问题描述
我一直在使用一个平铺系统的视频游戏,我一直在设计通过C + +和SDL。代码是松散地基于lazyfoo.net上可用的教程,但使用不同的系统来绘制瓷砖到屏幕。代码工作如下:我有一个名为 Map
的类,创建另一个类 Tile
的数组。它还有一个名为 set_tiles
的函数,它从.map文件中读取数字,并将它们复制到 tile
s使用两个for循环;一个用于x,一个用于y。然后我根据 Tile
的特定变量将每个表面拼接到一个临时映射。下面是实际代码:
I have been working on a tiling system for a video game I've been designing through C++ and SDL. The code is loosely based off of the tutorials available on lazyfoo.net, but with a different system to draw the tiles to the screen. The code works as follows: I have a class called Map
that creates an array of another class called Tile
. It also has a function called set_tiles
that reads numbers from a .map file and copies them to the array of tile
's using two for loops; one for the x's and one for the y's. I then blit each surface to a temporary map according to the Tile
's specific variables. Here is the actual code:
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::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,它不在do的某个地方在for循环。我也尝试将所有 tile
crop.x's和y的设置为0,只是为了看它是否可以工作,但不是。
The problem is that set_tiles
returns a NULL pointer when it is called. Through a bit of debugging I found that it makes it all the way to the end and then returns a NULL, it doesn't do it somewhere in the for loops. I've also tried setting all tile
crop.x's and y's to 0, just to see if it would work, but it doesn't.
注意
问题不是我的图片或.map文件。
我认为我的指针的理解可能有点错误,并且我可能在调用 apply_surface
时犯了一个错误。
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 );
}
推荐答案
SDL_BlitSurface
将表面对齐到另一个表面。它们都必须初始化,否则函数什么也不做。你传递一个NULL指针。您需要通过调用类似于的方式初始化指针。
SDL_BlitSurface
blits one surface to another. They both must be initialized, otherwise the function does nothing. You are passing a NULL pointer to it. You need to initialize the pointer with a call to something like SDL_CreateRGBSurface.
这篇关于平铺系统C ++ SDL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!