When I run the code below inside of one of my functions called drawTile() (inside of another one called drawTilemap() ), I get a segfault. On Windows, nothing happens, and the code works fine, yet on my Linux VM (64-bit/Ubuntu, if that helps), I get a segmentation fault.
SDL_RenderCopyEx(mainRenderer, tilesetTexture, &rect1, &rect2, 0, ¢er, flip);
where mainRenderer = the global SDL_Renderer variable,
tilesetTexture = the global SDL_Texture that renders the full tileset (or individual tiles, in this case),
rect1 = a local SDL_Rect variable that specifies the tilesetTexture clip rectangle,
rect2 = a local SDL_Rect variable that specifies where on the screen the tile is to be rendered to,
center = a local SDL_Point variable that specifies the centerpoint of the rotating operations,
and flip = a local SDL_RenderFlip parameter that specifies which way(s) to flip the drawn image.
Like I said, on Windows, when I build and run, it runs perfectly. On Linux, when I redownload the library files (just to be safe), build, and run, it always segfaults on this line. I backtraced the issue in GDB, and here's what it told me:
#0 0x00007ffff7b1b21c in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#1 0x00007ffff7b1b5fb in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
#2 0x000000000000b05f in drawTile (id=0, xCoord=0, yCoord=0, width=48, flip=SDL_FLIP_NONE)
#3 0x000000000040af6c in drawTilemap (startX=0, startY=0, endX=20, endY=15, updateScreen=0)
<my notes: There are 6 in total it displays, but all of them expand out to my main(), so nothing I didn't know past this.>
<As well, drawTile() is the function that calls SDL_RenderCopyEx(). The line it breaks on is the line that calls SDL_RenderCopyEx.>
I went through and searched all my variables for improper pointer initialization, passing bad arguments, even that the clipping bounds in rect1 weren't faulty, everything, but I found nothing. What's strange is that, I mentioned how I ran this line inside of a function inside another function; when I run drawTile() (which a call for is nested in drawTilemap(), naturally) by itself, it doesn't segfault. What's even stranger, is that when I navigated to either of the frames that say "?? ()" and type "list", it gives me random lines from either of my C sources.
Here's a larger look at my source:
From myInclude.c:
void drawTilemap(int startX, int startY, int endX, int endY, bool updateScreen)
{
for(int dy = startY; dy < endY; dy++)
for(int dx = startX; dx < endX; dx++)
drawTile(tilemap[dy][dx], dx * TILE_SIZE, dy * TILE_SIZE, TILE_SIZE, SDL_FLIP_NONE);
if (updateScreen)
SDL_RenderPresent(mainRenderer);
}
void drawTile(int id, int xCoord, int yCoord, int width, SDL_RendererFlip flip)
{
SDL_Rect rect1 = {.x = (id / 8) * width, .y = (id % 8) * width, .w = width, .h = width};
SDL_Rect rect2 = {.x = xCoord, .y = yCoord, .w = width, .h = width};
SDL_Point center = {.x = width / 2, .y = width / 2};
SDL_RenderCopyEx(mainRenderer, tilesetTexture, &rect1, &rect2, 0, ¢er, flip);
}
and from main.c:
int mainLoop(player* playerSprite)
{
//... setting up other, unrelated variables
//on Linux, seg fault after calling drawTilemap
drawTilemap(0, 0, WIDTH_IN_TILES, HEIGHT_IN_TILES, false);
//WIDTH_IN_TILES is defined as 20, and HEIGHT_IN_TILES is defined as 15
//... rest of code that isn't run because of the segfault
}
(The body of this message came from my StackOverflow question
here, if you want to take a look at that as well)