c - 我在 SDL 中显示 Sprite 时遇到问题

标签 c sdl

我正在使用 SDL 开发平台游戏,但我有一个错误,我不知道如何修复它。

我读了一张看起来像这样的 map :

0=sprites/background.jpeg
1=sprites/dirt.jpeg

2
sprites/ww_left.png
sprites/ww_right.png

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 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 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 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 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 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 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 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 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 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 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 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 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 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 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 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 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 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 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 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 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 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 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 0 0 0 0 0 0
0 2 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

我通过简单地 blitting 我的图片来显示 1 和 2,我通过从我的图片中取出一个像素来显示 0。

这些是我的显示功能: https://github.com/vladydan/Darien_Reborn/blob/master/srcs/aff_map.c

我的问题是背景覆盖了我的角色 Sprite 。 这是我开始时的显示,看起来不错: enter image description here

这就是当我移动太多时会发生的情况: enter image description here

如果我继续向右移动,显示会再次正常,但总有那么一刻我的 sprite 会被覆盖。 我知道它是从哪里来的,但我不知道如何修复它,你有什么想法吗?

最佳答案

 while (++i != 16)
    {
      pos.x = 0;
      j = -1;
      while (++j != 15)
    {
      if (game->map.map[i][j] == 1)
        SDL_BlitSurface(game->map.sprites[1], NULL, game->sdl.screen,
                &pos);
      else if ((game->map.map[i][j] == 0 || game->map.map[i][j] == 2))
        aff_background(game, pos);
      if (game->map.map[i][j] == 2)
        aff_sprite(game, pos);
      pos.x += 100;
    }
      pos.y += 52;
    }
  SDL_Flip(game->sdl.screen);
  return (0);
}

看起来你只是在以某种逻辑控制的顺序任意渲染。我认为最好的办法是按照您给我们的索引降序渲染,逐层渲染,第一层作为背景。

您可能还想尝试使用 SDL_Texture 以及使用硬件加速,这也可能解决您的渲染问题。也可能是渲染模式问题。

关于c - 我在 SDL 中显示 Sprite 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873802/

相关文章:

c - unsigned int 存储有符号值

c - 使用 Code::Blocks 对 SDL 函数的 undefined reference

c++ - 在渲染多个对象时,在 C++ 中使用 OpenGL 有困难

c++ - SDL 插件库未正确链接

visual-studio-2012 - 如何配置 SDL 2.0 以与 Visual Studio 2012 一起使用?

c - 我如何检测C中的回文?

c - 结构数组上的 free()

c - SDL链接错误,无法在vs2005中打开文件 'SDL_lib.obj'

使用 strcasecmp 比较 C 中的 char*

c - 如何使用 kerberos 安全地签名和加密消息?