Pietras9
Pietras9

Reputation: 13

C++ SDL, variable is being used without being initialized

When I try to compile this code, I get an error:

The variable 'screen' is being used without being initialized.

Can anyone tell me why?

class Board
{
    public:
    int board[BOARD_HEIGHT][BOARD_WIDTH];
    void drawboard(SDL_Surface *screen) {
        for (int i = 0; i < BOARD_HEIGHT; i++) {
            for (int j = 0; j < BOARD_WIDTH; j++) {
            if (i == 0 || i == BOARD_HEIGHT || j == BOARD_WIDTH || j == 0) {
                DrawRectangle(screen,(j*BLOCK_HW),(i*BLOCK_HW) , BLOCK_HW, BLOCK_HW, 0x000000FF, 0x000000FF);
                board[i][j] = FILLED;
            }
        }
    }
}

int main(int argc, char **argv) 
{
    SDL_Surface *screen;
    Board board;
    board.drawboard(screen);
    SDL_FreeSurface(screen);
    SDL_Quit();
    return 0;
};

Upvotes: 0

Views: 557

Answers (1)

J3soon
J3soon

Reputation: 3133

It means you didn't initiallize your screen variable below.

SDL_Surface *screen;

You should use SDL_CreateRGBSurface.

SDL_Surface *screen = SDL_CreateRGBSurface(...);

Update:

If this is for the main display surface, you should use SDL_CreateWindow or SDL_CreateWindowAndRenderer

Example:

SDL_Window *window;                    // Declare a pointer

SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

// Create an application window with the following settings:
window = SDL_CreateWindow(
    "An SDL2 window",                  // window title
    SDL_WINDOWPOS_UNDEFINED,           // initial x position
    SDL_WINDOWPOS_UNDEFINED,           // initial y position
    640,                               // width, in pixels
    480,                               // height, in pixels
    SDL_WINDOW_OPENGL                  // flags - see below
);

Provided by Benjamin Lindley.

Upvotes: 3

Related Questions