Search code examples
csdl

C, SDL: error: cannot convert 'SDL_Surface*' to 'SDL_Surface* (*)[15]


so I'm working on a code for filling a screen with a table of surfaces; here's the code:

main.c

#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include <SDL/SDL.h>
#include <SDL_image.h>
#include "maploader.h"

#define W 510
#define H 510
#define SIZE 34

void pause();

int main ( int argc, char** argv )
{
    SDL_Surface *screen = NULL;
    SDL_Surface *surfaces[15][15];



    SDL_Init(SDL_INIT_VIDEO);
    screen = SDL_SetVideoMode(W, H, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption("demon game", NULL);
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
    mapload(screen, surfaces[15][15], NULL);

    SDL_Flip(screen);
    pause();
    SDL_QUIT;
    return EXIT_SUCCESS;
}


void pause()
{
    int continuer = 1;
    SDL_Event event;

    while (continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
            continuer = 0;
        }
    }
}

maploader.c

#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif

#include <SDL/SDL.h>
#include <SDL_image.h>
#define W 510
#define H 510
#define SIZE 34



SDL_Surface *surfaces[15][15];


void mapload(SDL_Surface *screen, SDL_Surface *surfaces[][15], int lvl)
{
    FILE *level = NULL;
    char elements[125];
    int i, j, k = 0;
    SDL_Rect elementposition = {0,0};


    level = fopen("level.txt", "r");
    if (level == NULL)
    {
        exit(0);
    }
    fgets(elements, 125, level);
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
    for (i=0; i<15; i++)
    {
        for (j=0; j<15; j++)
        {
            if (elements[k] == "0")
            {
                surfaces[i][j] = IMG_Load("mur.jpg");
            }
            else if (elements[k] == "1")
            {
                surfaces[i][j] = IMG_Load("caisse.jpg");
            }
            else if (elements[k] == "2")
            {
                surfaces[i][j] = IMG_Load("objectif.png");
            }
            else
            {
                surfaces[i][j] = NULL;
            }
            k++;

        }

    }

    for (i=0; i<15; i++)
    {
        for (j=0; j<15; j++)
        {
            SDL_BlitSurface(surfaces[i][j], NULL, screen, &elementposition);
            elementposition.x += SIZE;
        }
        elementposition.y += SIZE;
    }




}

the only error I get from compiling is the following: "cannot convert 'SDL_Surface*' to 'SDL_Surface* ()[15]' for argument '2' to 'void mapload(SDL_Surface, SDL_Surface* (*)[15], int)'|"

apparently, the error is initialized from the second argument of the mapload function, but I don't have a clear idea about what exactly is wrong. any ideas, please?


Solution

  • This,

    mapload(screen, surfaces[15][15], NULL);
    

    should be

    mapload(screen, surfaces, NULL);
    

    But now you should ask yourself, if you didn't know that then probably,

    • The signature of void mapload(SDL_Surface *screen, SDL_Surface *surfaces[][15], int lvl) is altogether wrong.
    • You need to study what are arrays in and their relation to pointers.

    Note that surfaces[15][15] means the 16th element of the 16th array of pointers, none of which exists because you only allocated 15 of each. So you need to understand arrays in , how they are allocated and how you can have a dynamic array.

    Also, the fact that you tell a compiler that a function is expecting an array is not very relevant inside such function, so the syntax SDL_Surface *surfaces[][15] seems strange to a c programmer.

    And finally, since surfaces is a global variable you don't need to pass it as a parameter, but then you should ask yourself, should it be a global variable?