我的代码不会显示sprite SDL2

时间:2016-06-24 15:53:05

标签: c sprite sdl-2

屏幕始终是黑色的。告诉我如何正确显示精灵。

这是我的代码:

#define SHAPE_SIZE 32
    void aff_map(SDL_Renderer *renderer)
    {
      SDL_Surface *img;
      SDL_Texture *Tfloor
      int x = 0;
      int y = 0;
      int map[4][8] = {{0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0},
                       {0,0,0,0,0,0,0,0}};
      SDL_Rect SrcR;
      SDL_Rect DestR;

      DestR.x = 0;
      DestR.y = 0;
      DestR.w = SHAPE_SIZE;
      DestR.h = SHAPE_SIZE;

      img = IMG_Load("floor.bmp");
      Tfloor = SDL_CreateTextureFromSurface(renderer, img);

     while (y < 4)
        {
          x = 0;
          while (x < 8)
            {
if (map[y][x] == 0)
              SDL_RenderCopy(renderer, Tfloor, NULL, &DestR);
              x++;
              DestR.x = DestR.x + 32;
            }
          DestR.x = 0;
          DestR.y = DestR.y + 32;
          y++;
        }
      SDL_RenderPresent(renderer);
    }

int main()
    {
      SDL_Window *screen;
      SDL_Event evenements;
      SDL_Renderer *renderer;

      screen = SDL_CreateWindow("Zappy", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 8 * SHAPE_SIZE -32, 4 * SHAPE_SIZE, 0);

      renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED);
      SDL_RenderClear(renderer);

      while (42)
        {
          SDL_WaitEvent(&evenements);
          if (evenements.window.event == SDL_WINDOWEVENT_CLOSE ||
              evenements.key.keysym.sym == SDLK_ESCAPE)
        {
              SDL_DestroyWindow(screen);
              SDL_Quit();
              break;
        }
          aff_map(renderer);
        }
      return 0;
    }

1 个答案:

答案 0 :(得分:1)

错误消息是明确的。 它说&#34; floor.bmp&#34;尚未转换为表面。 这意味着&#39; img&#39;参数为NULL。

尝试以下方法:

  • 在IMG_Load()中指定图片的完整路径,例如&#34; /home/quentin/floor.bmp"
  • 检查IMG_Load()的返回值。