Pregunta

I am in the beginning of a game of brick breaker type and I'm stuck in the SDL_Flip step. My CodeBlocks compiler says nothing and the console doesn't crash, but yet the SDL window shutdown and the console process returned code 3. When I ran the debugger it says:

SDL_Flip()
Display(Bricks=0x28f69c, screen=0x0)

and the Display type error was said located at the line of my SDL_Flip(screen);

Here's a glimpse of my code. My Brick_Coordinates and Brick_Surface struct are already initialize (my coordinates for Brick_Coordinates and NULL for Brick_Surface) by another function before that one:

void Display(BrickStruct Bricks[12][10],SDL_Surface *screen)
{
    int i=0,j=0;

    for(j=0;j<10;j++)
    {
        if( (j+1)%2==0 )  // If we are on even lines, display only 11 bricks
        {
            for(i=0;i<11;i++)
            {

                Bricks[i][j].Brick_Surface = IMG_Load("BrickTest1.png");
                SDL_BlitSurface(Bricks[i][j].Brick_Surface, NULL, screen, &Bricks[i][j].Brick_Coordinates);
                SDL_Flip(screen); 

            }
        }
        else   // If we are on odd lines, display the 12 bricks
        {
            for(i=0;i<12;i++)
            {

            }
        }
    }
}

My Structure looks like this:

typedef struct BrickStruct
{
    int type;
    SDL_Rect Brick_Coordinates;   
    SDL_Surface *Brick_Surface;  
}BrickStruct;

In my main, my code is like this:

SDL_Surface *screen= NULL; 

BrickStruct Bricks[12][10];  // I create my 2D array of struct named Bricks

Display(Bricks,screen);

I've already tested with a fprintf the values of my coordinates initialized. These are good. And apparently my SDL_Blit is working. But The Flip isn't. My screen surface is big enough for all my images (480x540 and my images are 40x20). I was wondering if that problem has to do with an impossibility for Blit to place an image on top of another but the Flip doesn't even work when I try with only one image (without my loops). Can somebody please have the kindness to indicate me where is located my problem ? Thanks in advance

¿Fue útil?

Solución

There reason was that that you didn't save screen into the global variable.

You probably had a line in your SDL_Initialisation similar to this:

SDL_Surface *screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);

This creates a new local variable called screen. Since you wanted to save this into the global one, you should change it to:

screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);

Otros consejos

According to your debugger and your example code your screen structure is null. So your call to SDL_BlitSurface will fail. The reason it probably works for you when you do your Display call inside your Initialize is that you've just initialized your screen and used it right after.

You need to store the surface you are writing to and use it again when you're blitting.

Also, as others have recommended, you should take a look at a tutorial for SDL and perhaps some more C tutorials to reinforce some concepts.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top