Search code examples
c++cpointerssdlsdl-1.2

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


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 '->' ?)"`


Solution

  • 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