新增一個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...]遊戲開發