Joystick example / 01-joystick-polling

Why are one and wing initialized with 640 and 480, even though they are later queried with SDL_GetWindowSize.

SDL_AppResult SDL_AppIterate(void *appstate)
{
    int winw = 640, winh = 480;
...
    SDL_GetWindowSize(window, &winw, &winh);

Because SDL_GetWindowSize() can fail and if so, the values โ€‹โ€‹of both variables would not be set by this function. And this would cause further calculations to be based on uninitialized variables, and therefore on garbage data, which could lead to the application crashing.

1 Like

One more note โ€” it seems that if SDL is initialized and the window context is created correctly, there is no need to check the result of the SDL_GetWindowSize() function, because in such a case it will never fail (it only copies data from the window object field to the pointers given in the parameters).

1 Like