简体   繁体   中英

SDL 1.2 -> SDL 2.0 (crashing program)

So I used the SDL 2.0 migration guide, and finally made the code compile without errors... but now it crashes, this is the first time I had a program crash, and there is no compiler to guide me. I am using LazyFoo's 29th sdl tutorial, to see if I can migrate it. I honestly think I made an abomination of a program, and I'm tossing it to you guys because I'm clueless. Here is my progress: http://www.pastebucket.com/21174

You appear to have a couple of issues.

First of all you are not loading any of the images in the load_images() function so whenever you call anything on them ie rendering they will be NULL pointers.

Next is your init() function.

bool init()
{
  SDL_Init;  // <---- REMOVE THIS LINE

  //Initialize all SDL subsystems
  if( SDL_Init( SDL_INIT_EVERYTHING) == -1 )
  {
    return false;
  }


  SDL_Window *sdlWindow;        // <----- REMOVE THIS VARIABLE
  SDL_Window *window;           
  SDL_Texture *sdlRenderer;     // <----- REMOVE THIS VARIABLE

  // Create an application window with the following settings:

  // NO NEED FOR SDL_WINDOW_OPENGL replace with SDL_WINDOW_SHOWN

  window = SDL_CreateWindow(
                          "Why is this even alive?",         //    window title
                          SDL_WINDOWPOS_UNDEFINED,           //    initial x position
                          SDL_WINDOWPOS_UNDEFINED,           //    initial y position
                          640,                               //    width, in pixels
                          480,                               //    height, in pixels
                          SDL_WINDOW_SHOWN                   //    flags - see below
                          );

  // sdlWindow should be just window and you should do a NULL check before creating the render
  if (window != NULL) {
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

    if (renderer == NULL) {
      printf("Could not create renderer: %s\n", SDL_GetError());
      SDL_DestroyWindow(window);
    }
  }
  else {
    printf("Could not create window: %s\n", SDL_GetError());
    return false;
  }
}

Give that a try for now and see how you get on. I recommend you look to use an IDE to help you out as these were all very simple errors that would be picked up straight away normally.

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