SDL 1.2 -> SDL 2.0(崩溃程序)

SDL 1.2 -> SDL 2.0 (crashing program)

本文关键字:SDL 崩溃 程序 gt      更新时间:2023-10-16

所以我使用了 SDL 2.0 迁移指南,最终使代码编译无误...但是现在它崩溃了,这是我第一次遇到程序崩溃,没有编译器来指导我。我正在使用 LazyFoo 的第 29 个 sdl 教程,看看我是否可以迁移它。老实说,我认为我制作了一个可憎的程序,我把它扔给你们,因为我一无所知。这是我的进步:http://www.pastebucket.com/21174

您似乎有几个问题。

首先,您没有在load_images()函数中加载任何图像,因此每当您对它们调用任何内容(即渲染)时,它们都将是 NULL 指针。

接下来是您的init()函数。

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: %sn", SDL_GetError());
      SDL_DestroyWindow(window);
    }
  }
  else {
    printf("Could not create window: %sn", SDL_GetError());
    return false;
  }
}

现在试一试,看看你过得怎么样。我建议您使用 IDE 来帮助您,因为这些都是非常简单的错误,通常会立即拾取。