C ++ SDL,变量正在使用而未初始化

时间:2015-12-31 17:03:54

标签: c++ sdl initializing

当我尝试编译此代码时,出现错误:

  

正在使用变量'screen'而不进行初始化。

谁能告诉我为什么?

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;
};

1 个答案:

答案 0 :(得分:3)

这意味着您没有初始化下面的screen变量。

SDL_Surface *screen;

您应该使用SDL_CreateRGBSurface

SDL_Surface *screen = SDL_CreateRGBSurface(...);

<强>更新

如果是主显示屏表面,则应使用SDL_CreateWindowSDL_CreateWindowAndRenderer

示例:

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
);

本杰明林德利提供。