简体   繁体   English

SDL-窗口不显示任何内容

[英]SDL - window doesn't show anything

I'm doing my first steps in SDL (C++) an took some tutorials from the www. 我正在使用SDL(C ++)迈出第一步,并从www获得了一些教程。

But there is one problem. 但是有一个问题。 I have installed SDL2 on my Linux Mint System, compiled the tutorial code: 我已经在Linux Mint System上安装了SDL2,并编译了教程代码:

#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. 没有任何内部的窗框。 Where I have to look for this error ? 我必须在哪里寻找这个错误



Final Code 最终密码

First: thanks to starrify! 第一:感谢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 :) 告诉我这个结果:) 在此处输入图片说明

I saw that it was truly easy. 我看到这确实很容易。 An in this way I don't need no openGL for drawing. 这样,我不需要openGL进行绘制。 This will be the next step. 这将是下一步。 It ould be nice, if you help me to improve this example to get it with openGL to... 如果您能帮助我改进此示例,以使其与openGL一起使用,...

SDL - window doesn't show anything SDL-窗口不显示任何内容

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. 要仅显示BMP图像,请将图像加载到表面上并使用SDL_BlitSurface将其复制到屏幕上。 Or to work with textures, you shall draw primitives like triangles or quads just like working with OpenGL. 或使用纹理,应像使用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. 这是因为这些年默认的视频模式设置为double buffering ,这意味着要显示一个前缓冲区和一个要渲染的后缓冲区。 When finishing rendering a single frame, you shall call something like SDL_Flip (seems it's been obsoleted in SDL2) or SDL_UpdateWindowSurface . 完成渲染单个帧时,应调用SDL_Flip类的SDL_Flip (似乎在SDL2中已作废)或SDL_UpdateWindowSurface

EDITED: I've edited your code and here's something that works: (removed renderer/texture, added SDL_BlitSurface and SDL_UpdateWindowSurface) 编辑:我已经编辑了您的代码,这是可行的:(删除了渲染器/纹理,添加了SDL_BlitSurface和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;
}

Since you're using SDL2, you should not be using an SDL_Surface to update the screen. 由于使用的是SDL2,因此不应使用SDL_Surface来更新屏幕。 That's the old way that SDL 1.2 had to use. 那是SDL 1.2必须使用的旧方法。 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). 新的渲染器子系统是您通用的,因为它是硬件加速的(使用GPU),而SDL_BlitSurface()和SDL_UpdateWindowSurface()则不是(所有像素复制都在CPU上完成)。

So here's what to do. 所以这是做什么的。 Do not use SDL_GetWindowSurface(). 不要使用SDL_GetWindowSurface()。 Use your previous code for creating a renderer and texture. 使用以前的代码创建渲染器和纹理。 Replace SDL_BlitSurface() with a call to SDL_RenderCopy(). 将SDL_BlitSurface()替换为对SDL_RenderCopy()的调用。 Replace SDL_UpdateWindowSurface() with a call to SDL_RenderPresent(). 将SDL_UpdateWindowSurface()替换为对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. 另外,您也可以使用SDL_gpu(注意:我是作者),感觉像是较早的SDL 1.2 blitting API,但具有比SDL_Renderer更多的功能和优化功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM