SDL_CreateWindow失败,SDL_GetError不显示任何内容。为什么会失败?

时间:2013-09-13 19:45:24

标签: sdl sdl-2 createwindow

我有一个使用SDL 1.2的项目,并希望将其迁移到SDL 2.0。为了简单起见,我尝试编写一个简单的程序,以熟悉这些变化。

但是,SDL_CreateWindow失败,并且SDL_GetError没有显示任何内容。所以我不知道为什么创建窗口不起作用。

窗口实际上已创建,但随后消失。

#include <SDL.h>
#include <stdio.h>              // printf etc
#include <stdlib.h>             // abs, malloc, realloc
#include <stdint.h>             // uint8_t, uint16_t, int32_t

#define WIDTH 853
#define HEIGHT 480

int main(int argc, char** argv) {
        SDL_Renderer*   renderer;
        SDL_Window*             window;
        SDL_Texture*    screenTexture;
        SDL_Event               event;
        Uint8*                  screenBuffer;

        FILE* fp;      
        int quit = 0;

        fp = fopen("SDL2test.log", "w");
        if (!fp) {
                printf("\nCould not create log file");
                return -1;
        }

        if (SDL_Init(SDL_INIT_VIDEO) < 0 ) {
                fprintf(fp, "\nCould not initialise SDL: %s\n", SDL_GetError() );
                return -2;
        }

        window = NULL;
        if ( NULL == ( window = SDL_CreateWindow("SDL2 test",SDL_WINDOWPOS_UNDEFINED,                                                                                                   SDL_WINDOWPOS_UNDEFINED,                                                                                                    WIDTH,                                                                                                    HEIGHT,                                                                                                    0)));{
                fprintf(fp, "Could not create SDL window: %s\n", SDL_GetError() );
                SDL_Delay(3000);
                SDL_Quit();
                return -3;
        }     
        if ( !( renderer = SDL_CreateRenderer(window, -1, 0) ) ); {
        SDL_Quit();
        return -4;
    }     
        if ( !( screenTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGB24,                                                                                                    SDL_TEXTUREACCESS_STATIC,                                                                                                    WIDTH,                                                                                                    HEIGHT))); 

        screenBuffer = (Uint8*)malloc( sizeof(Uint8) * 3 * WIDTH * HEIGHT );
        if (NULL == screenBuffer){
                SDL_Quit();
                return -5;
        }     
        memset(screenBuffer, 0, 3 * WIDTH * HEIGHT); //set to black
        SDL_UpdateTexture(      screenTexture,
                                                NULL,
                                                screenBuffer,
                                                WIDTH * sizeof(Uint8) );


        while( !quit && ( SDL_WaitEvent(&event) ) ) {              
                SDL_RenderClear(renderer);
                SDL_RenderCopy(renderer, screenTexture, NULL, NULL);
                SDL_RenderPresent(renderer);
        }

        SDL_Delay(3000);
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
}

1 个答案:

答案 0 :(得分:3)

这是你的创建窗口if语句:

if ( NULL == ( window = SDL_CreateWindow( ...code... ) ) );    //what is ; doing here
{
    fprintf(fp, "Could not create SDL window: %s\n", SDL_GetError() );
    SDL_Delay(3000);
    SDL_Quit();
    return -3;
}

您是否注意到第一行末尾的;?  这意味着括号中的代码部分每次都会执行。基本上代码看起来像这样:

if ( NULL == ( window = SDL_CreateWindow( ...code... ) ) );   //end of if


{    //gets executed every time
    fprintf(fp, "Could not create SDL window: %s\n", SDL_GetError() );
    SDL_Delay(3000);
    SDL_Quit();
    return -3;
}

代码中至少有一个这样的错误。