SDL_ConvertSurface导致核心转储

时间:2016-08-11 05:28:32

标签: c++ sdl

查看本指南http://lazyfoo.net/tutorials/SDL/05_optimized_surface_loading_and_soft_stretching/index.php后,我想使用SDL_BlitScaled缩放曲面,然后使用SDL_CreateTextureFromSurface为曲面创建纹理。这是导致核心转储的load_media代码。你能帮助我解释为什么会造成这种情况吗?我认为导致问题的是ConvertSurface这一行。

bool Texture::load_from_file( std::string path ) {
  //deallocate preexisiting texture
  free();

  //final texture
  SDL_Texture* new_texture = NULL;

  SDL_Surface* optimizedSurface = NULL;

  //load image at path
  SDL_Surface* loaded_surface = SDL_LoadBMP( path.c_str() );
  temp = loaded_surface;
  window_surface = SDL_GetWindowSurface( g_window );

  if ( loaded_surface == NULL ) {
    printf("Unable to load image %s. SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
  }
  else {
    //set key image
    //SDL_SetColorKey ( loaded_surface, SDL_TRUE, SDL_MapRGB( window_surface->format, 0, 0xFF, 0xFF ) );
    optimizedSurface = SDL_ConvertSurface( loaded_surface, window_surface->format, NULL );

    SDL_Rect stretchRect;
    stretchRect.x = 0;
    stretchRect.y = 0;
    stretchRect.w = SCREEN_WIDTH;
    stretchRect.h = SCREEN_HEIGHT;
    SDL_BlitScaled( optimizedSurface, NULL, window_surface, &stretchRect );

    //create texture from pixels
    new_texture = SDL_CreateTextureFromSurface( g_renderer, window_surface );
    if ( new_texture == NULL ) {
      printf("Unable to create texture from %s. SDL Error: %s\n", path.c_str(), SDL_GetError() );
    }
    else {
      //get dimensions
      m_width = window_surface->w;
      m_height = window_surface->h;
    }

    //deallocate old SDL_Surface
    SDL_FreeSurface( loaded_surface );
  }

  m_texture = new_texture;
  return m_texture != NULL;
}

0 个答案:

没有答案