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是不适合的 所以改为单例模式

privateconstructor Init;public    class function Instance: TTextureManager;

残缺代码见 [https://gitee.com/tom-cat/sdl...]