Why do I have initialization problems in my code?

104 views Asked by At

I'm a beginner in C language, first year in programming. There is a problem where when I initialize an integer from a struct, it doesn't initialize, not only that but there are other problems that are happening in this code which I can not even begin to explain, I'll provide the struct and the code that is related with it, I use SDL 1.2 (due to project obligation in University).

This is inside one of the ints that I have initialized

Header:

typedef struct{
    SDL_Surface *background;
    SDL_Surface *credits;
    SDL_Surface *quitbtn[2];
    SDL_Surface *creditsbtn[2];
    SDL_Surface *continuebtn[2];
    SDL_Surface *gitbtn[2];
    SDL_Surface *settingsbtn[2];
    SDL_Surface *gitbtn_s;
    SDL_Surface *leftarrow;
    SDL_Surface *rightarrow;
    SDL_Surface *btnreset;
    int isselected[5];
}menuitems;

typedef struct{
    SDL_Surface *gamebackground;
}gameitems;

typedef struct{
    SDL_Surface *pausemenu;
    SDL_Surface *resumebtn[2];
    SDL_Surface *settingsbtnreal[2];
    int selectedresume;
    int selectedsettingsreal;
}pauseitems;

typedef struct{
    SDL_Surface *settings;
    SDL_Surface *donebtn[2];
    SDL_Surface *fsbtn[3];
    int isselected[2];
}settingsitems;

typedef struct{
    Mix_Music *music;
    Mix_Chunk *scratch;
    TTF_Font *font;
    TTF_Font *fontBig;
    int volume;
}misc;

Code:

int load_files(menuitems *MI, pauseitems *PI, gameitems *GI, settingsitems *SI, misc *M)
{
   



    //Due to a problem, I am not able to initialize 


    MI->background = load_image("images/background.png");
    MI->credits = IMG_Load("images/credits.png");
    MI->quitbtn[0] = IMG_Load("images/exitbtn_u.png");
    MI->quitbtn[1] = IMG_Load("images/exitbtn_s.png");
    MI->creditsbtn[0] = IMG_Load("images/creditsbtn_u.png");
    MI->creditsbtn[1] = IMG_Load("images/creditsbtn_s.png");
    MI->continuebtn[0] = IMG_Load("images/continuebtn_u.png");
    MI->continuebtn[1] = IMG_Load("images/continuebtn_s.png");
    MI->gitbtn[0] = IMG_Load("images/gitbtn_u.png");
    MI->gitbtn[1] = IMG_Load("images/gitbtn_s.png");
    MI->settingsbtn[0] = IMG_Load("images/settingsbtn_u.png");
    MI->settingsbtn[1] = IMG_Load("images/settingsbtn_s.png");
    MI->btnreset = IMG_Load("images/btnreset.png");
    MI->isselected[0] = 0;
    MI->isselected[1] = 0;
    MI->isselected[2] = 0;
    MI->isselected[3] = 0;
    MI->isselected[4] = 0;

    GI->gamebackground = load_image("images/gamebackground.png");


    SI->settings = IMG_Load("images/settings.png");
    SI->donebtn[0] = IMG_Load("images/done_u.png");
    SI->donebtn[1] = IMG_Load("images/done_s.png");
    SI->fsbtn[0]= IMG_Load("images/fsbtn_u.png");
    SI->fsbtn[1] = IMG_Load("images/fsbtn_s1.png");
    SI->fsbtn[2] = IMG_Load("images/fsbtn_s2.png");
    SI->isselected[0] = 0;
    SI->isselected[1] = 0;

    

    PI->pausemenu = IMG_Load("images/pausemenu.png");
    PI->resumebtn[0] = IMG_Load("images/resumebtn_u.png");
    PI->resumebtn[1] = IMG_Load("images/resumebtn_s.png");
    PI->settingsbtnreal[0] = IMG_Load("images/settingsbtnreal_u.png");
    PI->settingsbtnreal[1] = IMG_Load("images/settingsbtnreal_s.png");

    M->music = Mix_LoadMUS("sounds/beat.mp3");
    M->scratch = Mix_LoadWAV("sounds/scratch.wav");
    M->font = TTF_OpenFont("fonts/Retro.ttf", 48);
    M->fontBig = TTF_OpenFont("fonts/Retro.ttf", 72);
    M->volume = 128;
    
    if (M->music == NULL || M->font == NULL || M->scratch == NULL || SI->settings == NULL || MI->background == NULL || MI->quitbtn[0] == NULL || MI->creditsbtn[0] == NULL || MI->continuebtn[0] == NULL || MI->gitbtn[0] == NULL || MI->settingsbtn[0] == NULL || MI->quitbtn[1] == NULL || MI->creditsbtn[1] == NULL || MI->continuebtn[1] == NULL || MI->gitbtn[1] == NULL || MI->settingsbtn[1] == NULL || SI->donebtn[1] == NULL || SI->donebtn[0] == NULL || GI->gamebackground == NULL || PI->pausemenu == NULL || PI->settingsbtnreal[1] == NULL || PI->settingsbtnreal[0] == NULL || PI->resumebtn[1] == NULL || PI->resumebtn[0] == NULL)
    {
        return 0;
    }
    printf("%d\n", SI->isselected[0]);
    // If everything loaded fine
    return 1;
    
}

Sorry if this is unorganized as this is my first time posting here! (Note that load_image is the same as IMG_Load)

1

There are 1 answers

0
Jevro1337 On

Added new structure (Merged old existing SDL_Surface to this structure)

typedef struct{
    SDL_Surface *quitbtn[2];
    SDL_Surface *creditsbtn[2];
    SDL_Surface *continuebtn[2];
    SDL_Surface *gitbtn[2];
    SDL_Surface *settingsbtn[2];
    SDL_Surface *resumebtn[2];
    SDL_Surface *settingsbtnreal[2];
    SDL_Surface *donebtn[2];
    SDL_Surface *fsbtn[3];
    int isselected[9];
}btn;

Fixed my issue.