Alex44
Alex44

Reputation: 3855

SDL - window doesn't show anything

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 tutorial code:

#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <stdio.h>
#include <SDL2/SDL.h>

#include <iostream>

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_Renderer *ren = SDL_CreateRenderer(win, -1,
    SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (ren == NULL){
        std::cout << "SDL_CreateRenderer 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_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
    SDL_FreeSurface(bmp);
    if (tex == NULL){
        std::cout << "SDL_CreateTextureFromSurface Error: "
            << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_Delay(4000);

    SDL_DestroyTexture(tex);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    SDL_Quit();

    return 0;
}

with this compile command:

g++ main.cpp -lSDL2 -o prog

And I got this:

A window frame without any inside

A window frame without any inside. Where I have to look for this error?



Final Code

First: thanks to starrify!

#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <stdio.h>
#include <SDL2/SDL.h>

#include <iostream>

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 *window = SDL_CreateWindow("Hello World!", 100, 100, 640, 480,
    SDL_WINDOW_SHOWN);
    if (window == NULL){
        std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
        return 1;
    }
/*
    SDL_Renderer *ren = SDL_CreateRenderer(window, -1,
    SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (ren == NULL){
        std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
        return 1;
    }
*/
    SDL_Surface *surface_bmp = SDL_LoadBMP("cb.bmp");
    if (surface_bmp == NULL){
        std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
        return 1;
    }
/*
    SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, surface_bmp);
    SDL_FreeSurface(surface_bmp);
    if (tex == NULL){
        std::cout << "SDL_CreateTextureFromSurface Error: "
            << SDL_GetError() << std::endl;
        return 1;
    }
*/

    SDL_Surface *surface_window = SDL_GetWindowSurface(window);

    /*
     * Copies the bmp surface to the window surface
     */
    SDL_BlitSurface(surface_bmp,
                    NULL,
                    surface_window,
                    NULL);

    /*
     * Now updating the window
     */
    SDL_UpdateWindowSurface(window);

    /*
     * This function used to update a window with OpenGL rendering.
     * This is used with double-buffered OpenGL contexts, which are the default.
     */
/*    SDL_GL_SwapWindow(window); */

    SDL_Delay(5000);

/*    SDL_DestroyTexture(tex);*/
/*    SDL_DestroyRenderer(ren);*/
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

Show me this result :) enter image description here

I saw that it was truly easy. An in this way I don't need no openGL for drawing. This will be the next step. It ould be nice, if you help me to improve this example to get it with openGL to...

Upvotes: 2

Views: 8422

Answers (2)

Jonny D
Jonny D

Reputation: 2344

Since you're using SDL2, you should not be using an SDL_Surface to update the screen. That's the old way that SDL 1.2 had to use. The new renderer subsystem is what you want for general use because it is hardware-accelerated (uses the GPU) while SDL_BlitSurface() and SDL_UpdateWindowSurface() are not (all pixel copying is done on the CPU).

So here's what to do. Do not use SDL_GetWindowSurface(). Use your previous code for creating a renderer and texture. Replace SDL_BlitSurface() with a call to SDL_RenderCopy(). Replace SDL_UpdateWindowSurface() with a call to SDL_RenderPresent().

Alternatively, you can use SDL_gpu (note: I'm the author), which feels like the older SDL 1.2 blitting API, but has even more features and optimizations than SDL_Renderer.

Upvotes: 2

starrify
starrify

Reputation: 14781

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 <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <stdio.h>
#include <SDL2/SDL.h>

#include <iostream>

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

Upvotes: 4

Related Questions