-2

我有一个使用 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;
}
4

1 回答 1

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

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

于 2013-09-13T20:55:12.037 回答