【问题标题】:SDL fails to display image?SDL 无法显示图像?
【发布时间】:2019-03-28 12:09:52
【问题描述】:

我的代码没有显示任何内容。我得到的只是一个没有图像的窗口。

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

using namespace std;

SDL_Window *gWindow=NULL;
SDL_Surface *gScreenSurface=NULL;
SDL_Surface *gHelloWorld=NULL;

const int SCREEN_WIDTH=640, SCREEN_HEIGHT=480;
bool init(){
    bool success = true;

    if(SDL_Init (SDL_INIT_VIDEO) < 0 ) {
        printf("SDL could not initialize! SDL_Error : %s \n", SDL_GetError() );
        success=false;
    }
    else{
        gWindow = SDL_CreateWindow ( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( gWindow == NULL ){
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
            success=false;
        }
        else {
            gScreenSurface = SDL_GetWindowSurface (gWindow);
        }
    }
    return success;
}

bool loadMedia(){
    bool success=true;

    gHelloWorld = SDL_LoadBMP ( "hello_world.bmp" );
    if (gHelloWorld == NULL ){
        printf( "Unable to load image %s! SDL Error: %s\n", "hello_world.bmp", SDL_GetError() );
        success=false;
    }
    return success;
}

void close(){
    SDL_FreeSurface( gHelloWorld );
    gHelloWorld=NULL;

    SDL_DestroyWindow( gWindow );
    gWindow=NULL;

    SDL_Quit();
}

int main(int argc, char* args[]){
    if(!init()){
        printf( "failed to initialize!\n" );
    }
    else {
        if( !loadMedia() ) {
            printf ("failed to laod media! \n");
        }
        else {
            SDL_BlitSurface( gHelloWorld, NULL, SDL_GetWindowSurface(gWindow), NULL );
            SDL_UpdateWindowSurface ( gWindow );
            SDL_Delay (2000);
        }
    }
    close();

我希望它显示一个 bmp 图像,该图像位于 loadBMP() 函数中指定的路径中,但我得到的只是一个空的透明窗口。

我正在使用 KDE Konsole,如果这与此有关的话。

【问题讨论】:

  • 您是否在控制台上收到"Unable to load image %s! SDL Error: %s\n" 错误消息?
  • 您尝试过不同的 .bmp 文件吗?在我安装 sdl2 并使用 clang++ main.cpp `sdl2-config --libs` 编译后,您的代码对我来说运行良好。另外,我假设您的图像与您的可执行文件位于同一目录中?
  • 我无法想象 g++clang++ 对此有影响... ;-)
  • 当窗口管理器要求您(或无条件地重绘)时,您应该有适当的更新循环和重绘。 Draw&delay 不是一种显示事物的方式。
  • @MiguelÁngelRetamozoSanchez:我认为这只是 C 编译器的一个问题,或者如果您将 close() 声明为 extern "C" 以便 C++ 不会将其命名为默认文件或翻译单元本地匿名命名空间。

标签: c++ linux sdl


【解决方案1】:

KDE 嗯?等离子composites 默认;尝试禁用合成或添加a proper event-handling loop,以便您的进程有机会处理重绘事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2020-07-24
    • 2013-12-31
    • 1970-01-01
    相关资源
    最近更新 更多