SDL_MapRGB, how to get screen->format using pointer to pointer

390 views Asked by At

I'm generating a level using a function, and so I'm sending a pointer to pointer of my screen to update him. But when I try to use SDL_MapRGB i'm getting an error on the *screen->format part. Is there a way to do this ? Here's the code i'm using :

void generateLevel(SDL_Surface** screen)
{
    int i=0, j=0;
    char object =' ';
    FILE* level = NULL;
    SDL_Surface* lvl[LARGEUR_MAP][HAUTEUR_MAP];
    SDL_Rect posElem;

    //Ouverture du fichier contenant les infos du niveau
    level = fopen("lvl.txt","r");

    if(level == NULL)
    {
        fprintf(stderr,"Erreur lors de l'ouverture du fichier");
        exit(EXIT_FAILURE);
    }

    //Boucle pour lire le fichier et placer les éléments du décor
    for(j=0;j<HAUTEUR_MAP;j++)
    {
        for(i=0;i<LARGEUR_MAP;i++)
        {
            object = fgetc(level);
            if(object == '\n')
                object = fgetc(level);
            switch(object)
            {
                case 'm':
                    lvl[i][j] = IMG_Load("images\\mur.jpg");
                    posElem.x = i*TAILLE_BLOC;
                    posElem.y = j*TAILLE_BLOC;
                    SDL_BlitSurface(lvl[i][j], NULL, *screen, &posElem);
                    break;
            }


        }
    }

    SDL_FillRect(*screen, NULL, SDL_MapRGB(*screen->format,255,255,255));
    SDL_Flip(*screen);
    fclose(level);
}

The error comes on the end of the code, on the SDL_FillRect(); Everything's working fine but this, and I can't figure out how to do this. Tried with *screen->format, **screen->format, screen->format, and even &screen->format (how desperate I am ^^).

Edit : The error

error: request for member 'format' in '* screen', which is of pointer type 'SDL_Surface*' (maybe you meant to use '->' ?)"`

2

There are 2 answers

0
Some programmer dude On BEST ANSWER

The unary * dereference operator has lower operator precedence than the structure pointer access operator ->. That means you're actually doing *(screen->format).

You need some parentheses to get the right precedence: (*screen)->format

1
Sourav Ghosh On

TL;DR solution --> Change your code to (*screen)->format.

Reason:

As per the operator precedence rule,

The -> operator [Structure and union member access through pointer] has higher priority over the * [Indirection (dereference)] operator.

So, your code *screen->format, is effectively behaving like

 *(screen->format)

which means,

  1. it's trying to access the format member variable from screen pointer
  2. It's [ideally] trying to dereference screen->format.

But, as per the definition, SDL_Surface** screen, screen is SDL_Surface**, not SDL_Surface*. That's why you're getting the error.

What you want is actually to

  1. Dereference screen to get a SDL_Surface* pointer [(*screen)]
  2. Then, access the format member variable from that pointer. [(*screen)->format]