SDL无法显示图像?

时间:2019-03-28 12:09:53

标签: c++ linux sdl

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

#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,如果这样做必须这样做。

1 个答案:

答案 0 :(得分:1)

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

相关问题