Weird SDL Problem

Started by
5 comments, last by bozaker 18 years, 5 months ago
I'm making a simple tetris clone using SDL. I've used SDL in the past so I'm not new to this. But, I'm having a weird problem with the surfaces I'm using to store my blocks. Here's the relevant code blocks:

// Create a surface to store our blocks
SDL_Surface* g_BlockSource = NULL;

// An array of surfaces to hold the individual blocks
SDL_Surface* g_Blocks[NumberBlocks];

Then after my SDL initialization and all that I do the following:

// Load the bitmap that has our blocks on it
g_BlockSource = SDL_LoadBMP("data/blocks.bmp");

// Copy the individual blocks to the block array
SDL_Rect src;
int block;

for (block = 0; block < NumberBlocks; block++)
{
     src.x = block * BlockWidth; // BlockWidth = 16
     src.y = 0;
     src.w = src.x + BlockHeight; // BlockHeight = 16
     src.h = BlockHeight;
     SDL_BlitSurface(g_BlockSource, &src, g_Blocks[block], NULL);
}
Then in my update function I use this to update the window

// Copy background image over to the Back surface
SDL_BlitSurface(g_Background, NULL, g_BackSurface, NULL);

// Draw the blocks to make sure they loaded right
SDL_BlitSurface(g_Blocks[1], NULL, g_BackSurface, NULL);

// Update the screen by copying the backsurface to the 
// primary surface(actual window)
SDL_UpdateRect(g_BackSurface, 0, 0, 0, 0);
When I run it through VS, the background is displayed fine. But not the block that should be stored in g_Blocks[1]. None of the blocks are displayed. Now, if instead of using a for loop to load the blocks, I split all the blocks up into seperate files and loaded them in by g_Blocks[0] = SDL_LoadBMP("block0.bmp"); It still doesn't workt that way,however if I run the exe from the folder, it displays the block fine. What's wrong with my for loop or what am I missing here? The bitmap contains 8 blocks(0-7) that are 16x16 in a horizontal row.
Advertisement
Try using some debug messages in the load bmp function so you can see when the bmp is loaded correctly and when no. Maybe the problem is the filename with the full dir.
Make sure you aren't doing:


// Create a surface to store our blocks
SDL_Surface* g_BlockSource = NULL;

// An array of surfaces to hold the individual blocks
SDL_Surface* g_Blocks[NumberBlocks];



Before you initialize SDL (call your SDL_Init() function, [or whatever it is]).
Try changing

g_BlockSource = SDL_LoadBMP("data/blocks.bmp");

to

g_BlockSource = SDL_LoadBMP("data\\blocks.bmp");

In addition, make sure when running the exe, that all the resources are in the right places in respect to the executable (ie, that there is indeed a folder named data in the same directory as the binary). When running in debug the compiler will generally execute the project in the project's directory, not the binary's directory (true, at least with MSVC)

Hope that was the problem :/
It's not a problem with the file path or filename. I loaded the full file and blitted it to make sure it was working right. I think it has something to do with the for loop but I never figured it out. I just did it a different way and took note of it.

I appreciate all of your time though. Thanks.
i just skimmed over it quick, but shouldn't
src.w = src.x + BlockHeight; // BlockHeight = 16

be
src.w = BlockWidth; // BlockWidth = 16
This space for rent.
im trying to do a very similar thing (different thing, same method) and i have the same problem... i wish i could figure out what the problem is, because it is driving me crazy. i filled my program with debugging messages to print out the value of the significant variables that i put through the for loop, and nothing seems to be wrong.

This topic is closed to new replies.

Advertisement