c++ - SDL - 窗口不显示任何内容

标签 c++ linux sdl-2

我在 SDL (C++) 中迈出了我的第一步,并从 www 上学习了一些教程。

但是有一个问题。我已经在我的 Linux Mint 系统上安装了 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;
}

使用这个编译命令:

g++ main.cpp -lSDL2 -o prog

我得到了这个:

A window frame without any inside

没有任何内部结构的窗框。我必须在哪里寻找这个错误



最终代码

首先:感谢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;
}

给我看这个结果:) enter image description here

我看到这真的很容易。这样我就不需要 openGL 来绘图了。这将是下一步。 如果您能帮助我改进此示例以使用 openGL 使其...,那就太好了。

最佳答案

SDL - window doesn't show anything

原因是您没有绘制任何东西。您创建了一个窗口、一个渲染器、一个表面和一个纹理。但是,您没有绘制 任何东西。视觉结果准确地反射(reflect)了您所做的事情。

要简单地显示 BMP 图像,将图像加载到表面并使用 SDL_BlitSurface将其复制到屏幕上。或者要使用纹理,您应该像使用 OpenGL 一样绘制三角形或四边形等图元。

还有一个问题:为什么你的窗口看起来充满了其他内容而不是空白屏幕?
那是因为这些年默认视频模式设置为double buffering ,这意味着有一个要显示的前台缓冲区和一个要渲染的后台缓冲区。当完成单帧渲染时,你应该调用类似SDL_Flip的东西。 (似乎它在 SDL2 中已被废弃)或 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;
}

关于c++ - SDL - 窗口不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21953165/

相关文章:

c++ - SDL_Renderer通过函数后不会显示纹理

c++ - 1 channel 位图到 SDL_Texture

c++ - 如何在C++中比较两个char数组?

linux - 使用自定义引导加载程序创建可引导 ISO 镜像

linux - 从同步任务(launchdaemon)中卸载 LaunchAgent

linux - 我怎样才能在 linux 的命令中间用 xargs 传递所有参数

c++ - 我可以使用带有 SDL2 图像扩展名的 SDL-1.2.15 吗?

C++ Linux 多线程套接字问题

c++ - 什么是 IO 流缓冲?

c++ - 将 MFC 库复制到项目