SDL - window doesn't show anything

前端 未结 2 918
南笙
南笙 2021-01-15 02:21

I\'m doing my first steps in SDL (C++) an took some tutorials from the www.

But there is one problem. I have installed SDL2 on my Linux Mint System, compiled the tut

2条回答
  •  盖世英雄少女心
    2021-01-15 03:12

    SDL - window doesn't show anything

    The reason is that you're not drawing anything. You created a window, a renderer, a surface, and a texture. However you're not drawing anything. And the visual result reflects exactly what you've done.

    To simply display a BMP image, load the image into a surface and use SDL_BlitSurface to copy it to the screen. Or to work with textures, you shall draw primitives like triangles or quads just like working with OpenGL.

    Also another problem: why your window looks filled with other content than a blank screen?
    That's because these years the default video mode is set to double buffering, which means there's a front buffer to be shown and a back buffer to render on. When finishing rendering a single frame, you shall call something like SDL_Flip (seems it's been obsoleted in SDL2) or SDL_UpdateWindowSurface.

    EDITED: I've edited your code and here's something that works: (removed renderer/texture, added SDL_BlitSurface and SDL_UpdateWindowSurface)

    #ifdef __cplusplus
        #include 
    #else
        #include 
    #endif
    
    #include 
    #include 
    
    #include 
    
    int main ( int argc, char** argv )
    {
        if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
            std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
            return 1;
        }
    
        SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480,
        SDL_WINDOW_SHOWN);
        if (win == NULL){
            std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
            return 1;
        }
    
        SDL_Surface *bmp = SDL_LoadBMP("cb.bmp");
        if (bmp == NULL){
            std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
            return 1;
        }
    
        SDL_BlitSurface(bmp, 0, SDL_GetWindowSurface(win), 0);
        SDL_UpdateWindowSurface(win);
    
        SDL_Delay(4000);
    
        SDL_DestroyWindow(win);
        SDL_Quit();
    
        return 0;
    }
    

提交回复
热议问题