cmake SDL_ lightning 曲面()黑屏

bttbmeg0  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(117)

我在试着把想象呈现出来。
因此,当我加载BMP并显示BMP时,显示黑屏
下面是我代码:

#define SDL_MAIN_HANDLED
#include <iostream>
#include "SDL/include/SDL2/SDL.h"
const int screenWidth=640;
const int screenHeight=480;

SDL_Window* window=NULL;
SDL_Surface* screenSurface=NULL;
SDL_Surface* gHelloWorld = NULL;
bool init(){
    bool good = true;
    if(SDL_Init(SDL_INIT_VIDEO)<0){
        std::cout<<"ERROR: Failed to initialize SDL  " << SDL_GetError()<<"\n";
        good = false;

    }else{
        //Create Window
        window =SDL_CreateWindow("SDL Tutorial",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,screenWidth,screenHeight,SDL_WINDOW_SHOWN);
        if (window ==NULL){
            std::cout<<"ERROR: Failed to create "<<SDL_GetError() <<"\n";
            good = false;
        }else{
             //Get window surface
            screenSurface = SDL_GetWindowSurface( window );
        }
    }
    return good;
}

void close(){
    SDL_FreeSurface(gHelloWorld);
    gHelloWorld = NULL;
    SDL_DestroyWindow(window);
    window=NULL;
    SDL_Quit();
}

bool loadMedia(){
    bool ok = true;
    gHelloWorld=SDL_LoadBMP("hello_world.bmp");
    if(gHelloWorld==NULL){
        std::cout<<"ERROR: Failed to load "<<SDL_GetError()<<"\n";
        ok = false;
    }else{
        std::cout<<"Loaded successfully "<<'\n';
    }
    return ok;
}

int main(){
    SDL_Window* window=NULL;
    SDL_Surface* screenSurface=NULL;
    bool Init = init();
    if(Init){
       bool loadMed = loadMedia();
       if(loadMed){
        SDL_Rect area ={30,30,250,250};
        SDL_BlitSurface(gHelloWorld,&area,screenSurface,&area);
       SDL_UpdateWindowSurface(window); //
        SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
     }
    }
    close();
    return 0;
}

我使用Cmake来链接lib(动态链接)
我试着写SDL_Rect,但它不起作用;/

k2arahey

k2arahey1#

您的问题是您在main函数中将表面置空。
下面是没有这个错误的代码版本:

#include <iostream>
#include "SDL/include/SDL2/SDL.h"
const int screenWidth=640;
const int screenHeight=480;

SDL_Window* window=NULL;
SDL_Surface* screenSurface=NULL;
SDL_Surface* gHelloWorld = NULL;
bool init(){
    bool good = true;
    if(SDL_Init(SDL_INIT_VIDEO)<0){
        std::cout<<"ERROR: Failed to initialize SDL  " << SDL_GetError()<<"\n";
        good = false;

    }else{
        //Create Window
        window =SDL_CreateWindow("SDL Tutorial",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,screenWidth,screenHeight,SDL_WINDOW_SHOWN);
        if (window ==NULL){
            std::cout<<"ERROR: Failed to create "<<SDL_GetError() <<"\n";
            good = false;
        }else{
             //Get window surface
            screenSurface = SDL_GetWindowSurface( window );
        }
    }
    return good;
}

void close(){
    SDL_FreeSurface(gHelloWorld);
    gHelloWorld = NULL;
    SDL_DestroyWindow(window);
    window=NULL;
    SDL_Quit();
}

bool loadMedia(){
    bool ok = true;
    gHelloWorld=SDL_LoadBMP("/tmp/image.bmp");
    if(gHelloWorld==NULL){
        std::cout<<"ERROR: Failed to load "<<SDL_GetError()<<"\n";
        ok = false;
    }else{
        std::cout<<"Loaded successfully "<<'\n';
    }
    return ok;
}

int main(){
 // your error was nulling your surfaces.
 //   SDL_Window* window=NULL;
 //   SDL_Surface* screenSurface=NULL;
    bool Init = init();
    if(Init){
       bool loadMed = loadMedia();
       if(loadMed){
        SDL_Rect area ={30,30,250,250};
        SDL_BlitSurface(gHelloWorld,&area,screenSurface,&area);
       SDL_UpdateWindowSurface(window); //
        SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
     }
    }
    close();
    return 0;
}

此外,我建议您在while循环中更新表面,因为有时SDL不会呈现第一个表面,您会获得一个错误的窗口。
以下是学习SDL的好资源:https://lazyfoo.net/tutorials/SDL/,在我的GitHub上,我有一个工具可以给予你一个SDL示例:https://github.com/Monsterduty/mktest .

相关问题