2
#include<SDL2/SDL.h>
#include<stdio.h>
#include<stdbool.h>

bool init(char *title, int width, int height);
void close();

SDL_Window *window = NULL;
SDL_Surface *screen = NULL;

bool init(char *title, int width, int height){

    bool success = true;

// SDL_Init 0 on success and returns negative on failure
   if( SDL_Init(SDL_INIT_EVERYTHING) != 0 ){
       SDL_Log("Couldn't initialize SDL: %s",SDL_GetError());
       success = false;
   }

// creating Window and Surface
  window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN );


// SDL_CreateWindow returns window on creation and NULL on failure
    if ( !window ){
        SDL_Log("Couldn't create window: %s",SDL_GetError());
        success = false;
    } else {
        // get window surface
        screen = SDL_GetWindowSurface( window );
        if ( screen == NULL ){
            SDL_Log("Couldn't get window surface: %s",SDL_GetError());
            success = false;
        }
    }
    return success;
}

void close(){
    // deallocate surface
    SDL_FreeSurface( screen );
    // destroy window
    SDL_DestroyWindow( window );
    // SDL_Quit();
    SDL_Quit();
}

int main(){
    bool running  = true;
    SDL_Event event;
    if( init("My Window", 800, 600) ){
       printf("Congrats\n");
    } else {
        printf("Sorry :(\n");
    }

   while( running ) {
      //  start = SDL_GetTicks();
        while ( SDL_PollEvent( &event ) ){
            switch( event.type ){
                case SDL_QUIT:
                    running = false;
                    break;
            }
        SDL_UpdateWindowSurface( window );
        }
    }

    close();
}

I tried to create a separate function to initialize SDL Window and then tried to attach surface to it but it gave Segmentation fault (core dumped). and sometimes it give the error Info: invalid window too.

If i do not create a separate function and run this code in main function only, it works!

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    Why are you calling `SDL_FreeSurface()` on the `SDL_Surface*` that `SDL_GetWindowSurface()` gives you? ["This surface will be freed when the window is destroyed. **Do not free this surface.**"](https://wiki.libsdl.org/SDL_GetWindowSurface) – genpfault Feb 01 '18 at 15:20
  • How do you compile this code, do you have some compilation traces? – Mathieu Feb 01 '18 at 15:31
  • @genpfault I did but same error. – user8751232 Feb 01 '18 at 15:37
  • @purplepsycho There is no error on compilation, it is runtime error. – user8751232 Feb 01 '18 at 15:37
  • 2
    Never define your own function as freaking `close` that aliases with [libc close](https://linux.die.net/man/2/close) (which, to make things funnier, is a weak symbol). Or at least make your function `static`. – keltar Feb 01 '18 at 15:44
  • @keltar: How's [this](https://stackoverflow.com/questions/40495898) as a dupe target? Or maybe [this](https://stackoverflow.com/questions/31304591). – genpfault Feb 01 '18 at 16:10
  • @keltar thanks man!! – user8751232 Feb 01 '18 at 16:11
  • 2
    @genpfault yeah this question gets asked again and again, lilely because lazyfoo tutorials are very popular and name their function `close`, but uses C++ (with mangling) so it isn't a problem there. – keltar Feb 01 '18 at 16:12

1 Answers1

0

You forgot to implement a crucial function which is :

SDL_BlitSurface( SDL_Surface* ,const SDL_Rect* , SDL_Surface* , SDL_Rect* );

This functiton can blit the surface and load the data (graphical info) into SDL_Surface Structure.

If you want to learn more detail about this function , You can check out the link.

Link : https://wiki.libsdl.org/SDL_BlitSurface

nevzatseferoglu
  • 988
  • 7
  • 18