Pascal游戏开发入门(二):渲染图片

码农天地 -
Pascal游戏开发入门(二):渲染图片
Pascal游戏开发入门(二):渲染图片渲染静态图片

新增一个Texture,然后Render出来
创建Texture,并获取尺寸

procedure TGame.Init(title: string; x, y, h, w, flags: integer);
begin
  .....

  pt := IMG_LoadTexture(pr, 'assets/run.png');

  SDL_QueryTexture(pt, nil, nil, @srcRect.w, @srcRect.h);
  destRect.x := srcRect.x;
  destRect.y := srcRect.y;
  destRect.w := srcRect.w;
  destRect.h := srcRect.h;

  ......
end;  

渲染出来

procedure TGame.Render();
begin
  SDL_SetRenderDrawColor(pr, 238, 238, 238, 255);
  SDL_RenderClear(pr);
  
  SDL_RenderCopy(pr, pt, @srcRect, @destRect);

  SDL_RenderPresent(pr);
end; 
渲染动画

渲染动画就就快速交替渲染多张图片

procedure TGame.Update();
begin
  srcRect.x := 96 * (round(SDL_GetTicks() / 100) mod 8);
end;
动画反转

本例中,如果人物需要朝相反方向行走,不用再搞一套素材

SDL_RenderCopyEx(pr, pt, @srcRect, @destRect,0, nil, SDL_FLIP_HORIZONTAL);
代码整理

代码味道

Texture有多个,不能简单的使用变量。要有一个Texture容器渲染时的Rect要和Texture的Render在一起防止错乱

新增一个TextureManager来统一的管理Texture,并解决以上两个问题

type
  TTextureDict = specialize TFPGMap<string, PSDL_Texture>;

  TTextureManager = class
  private
    textureMap: TTextureDict;
  public
    destructor Destroy();
    function Load(filename: string; id: string; pr: PSDL_Renderer): boolean;
    procedure Draw(id: string; x, y, w, h: integer; pr: PSDL_Renderer;
      flip: integer = 0);
    procedure DrawFrame(id: string; x, y, w, h, row, frame: integer;
      pr: PSDL_Renderer; flip: integer = 0);
  end;

由于多个TextureManager是不合适的 所以改为单例模式

private
constructor Init;
public
    class function Instance: TTextureManager;

完整代码见 [https://gitee.com/tom-cat/sdl...]

特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

加个好友,技术交流

1628738909466805.jpg