• # paquage sdl2 de stretch

    Posté par . En réponse au message erreur de segmentation sur SDL_Init. Évalué à 1.

    oui j'utilise la version de ma distribution.
    J'ai utilisé valgrind -v sans plus de résultats.
    Quant à gdb, maintenant il m'indique pour SDL_Init:

    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff781536c in ?? () from /usr/lib/x86_64-linux-gnu/libSDL2-2.0.so.0
    

    Je n'ai plus la même erreur sans rien avoir touché.
    ET POURTANT, lorsque je vire la quasi totalité de mon code pour nelaisser que ma fonction d'initialisation, SDL_Init se comporte à ravir...
    Je vous refile la totalité de mon code, je n'arrive pas à le formater pour qu'il rende dans les balises:

    #include <SDL2/SDL.h>
    #include <SDL2/SDL_image.h>
    #include <SDL2/SDL_ttf.h>
    #include <stdio.h>
    #include<stdlib.h>
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;
    SDL_Window* gWindow = NULL;
    SDL_Renderer* gRenderer = NULL;
    TTF_Font *gFont = NULL;
    void free1(SDL_Texture *mTexture)
    {
     //Free texture if it exists
     if( mTexture != NULL )
     {
     SDL_DestroyTexture( mTexture );
     mTexture = NULL;
     }
    }
    int loadFromRenderedText(SDL_Texture *mTexture, char textureText[200], SDL_Color textColor, SDL_Rect *position )
    {
     //Get rid of preexisting texture
     free1(mTexture);
     //Render text surface
     SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText, textColor );
     if( textSurface == NULL )
     {
     printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
     }
     else
     {
     //Create texture from surface pixels
     mTexture = SDL_CreateTextureFromSurface( gRenderer, textSurface );
     if( mTexture == NULL )
     {
     printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
     }
     else
     {
     //Get image dimensions
     position->w = textSurface->w;
     position->h = textSurface->h;
     }
     //Get rid of old surface
     SDL_FreeSurface( textSurface );
     }
     //Return success
     return mTexture != NULL;
    }
    void render(SDL_Texture *mTexture, SDL_Rect* clip)
    {
     SDL_RenderCopy( gRenderer, mTexture, clip, clip);
    }
    int init()
    {
     //Initialization flag
     int success = 1;
     //Initialize SDL
     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
     {
     printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
     success = 0;
     }
     else
     {
     //Set texture filtering to linear
     if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
     {
     printf( "Warning: Linear texture filtering not enabled!" );
     }
     //Create window
     gWindow = SDL_CreateWindow( "SDL Test", 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 = 0;
     }
     else
     {
     //Create vsynced renderer for window
     gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
     if( gRenderer == NULL )
     {
     printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
     success = 0;
     }
     else
     {
     //Initialize renderer color
     SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
     //Initialize PNG loading
     int imgFlags = IMG_INIT_PNG;
     if( !( IMG_Init( imgFlags ) & imgFlags ) )
     {
     printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
     success = 0;
     }
     //Initialize SDL_ttf
     if( TTF_Init() == -1 )
     {
     printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
     success = 0;
     }
     }
     }
     }
     return success;
    }
    int loadMedia(SDL_Texture *mTexture, SDL_Rect *position)
    {
     //Loading success flag
     int success = 1;
     //Open the font
     gFont = TTF_OpenFont( "lazy.ttf", 28 );
     if( gFont == NULL )
     {
     printf( "Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError() );
     success = 0;
     }
     else
     {
     //Render text
     SDL_Color textColor = { 0, 0, 0, 255};
     char text[200];
     sprintf(text, "The quick brown fox jumps over the lazy dog");
     if( !loadFromRenderedText(mTexture, text, textColor, position ) )
     {
     printf( "Failed to render text texture!\n" );
     success = 0;
     }
     }
     return success;
    }
    void close(SDL_Texture *mTexture)
    {
     SDL_DestroyTexture( mTexture );
     mTexture = NULL;
     //Free global font
     TTF_CloseFont( gFont );
     gFont = NULL;
     //Destroy window 
     SDL_DestroyRenderer( gRenderer );
     SDL_DestroyWindow( gWindow );
     gWindow = NULL;
     gRenderer = NULL;
     //Quit SDL subsystems
     TTF_Quit();
     IMG_Quit();
     SDL_Quit();
    }
    int main( int argc, char* args[] )
    {
     SDL_Texture* mTexture = NULL;
     SDL_Rect position;
     position.x = 100;
     position.y = 100;
     position.w = 0;
     position.h = 0;
     //Start up SDL and create window
     if( !init() )
     {
     printf( "Failed to initialize!\n" );
     }
     else
     {
     //Load media
     if( !loadMedia(mTexture, &position) )
     {
     printf( "Failed to load media!\n" );
     }
     else
     { 
     //Main loop flag
     int quit = 0;
     //Event handler
     SDL_Event e;
     //While application is running
     while( !quit )
     {
     //Handle events on queue
     while( SDL_PollEvent( &e ) != 0 )
     {
     //User requests quit
     if( e.type == SDL_QUIT )
     {
     quit = 1;
     }
     }
     //Clear screen
     //SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
     //SDL_RenderClear( gRenderer );
     ////Render current frame
     //render(mTexture, &position );
     ////Update screen
     //SDL_RenderPresent( gRenderer );
     }
     }
     }
     //Free resources and close SDL
     close(mTexture);
     return 0;
    }
    

    meric pour votre retour. à bientôt!