简体   繁体   中英

SDL_BlitSurface() blackscreen

I'm trying to put an imagine on surface.

So it's showed a black screen when I'm load BMP and display bmp

here's my code:

#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;
}

And I'm using Cmake to link lib (dynamic linking)

I tried to write SDL_Rect, and its not work;/

Your problem is that you are nulling your sufaces in the main function.

Here is a version of your code without that mistake:

#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;
}

Also, I recommend that you make your surface updates inside your while loop because somethimes SDL don't render the first surface and you obta a false window.

Here is a good resource for learning SDL: https://lazyfoo.net/tutorials/SDL/ and on my GitHub I have a tool to give you an SDL example: https://github.com/Monsterduty/mktest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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