新增一个Texture,而后Render出来
建立Texture,并获取尺寸git
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;
渲染动画就就快速交替渲染多张图片code
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);
代码味道图片
新增一个TextureManager来统一的管理Texture,并解决以上两个问题ip
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是不合适的 因此改成单例模式ci
private constructor Init; public class function Instance: TTextureManager;
完整代码见 [https://gitee.com/tom-cat/sdl...]游戏开发