参照 https://tieba.baidu.com/p/3569073088?tpl=5&red_tag=1777318765
使用mingw工具链
#include <stdbool.h> #include <stdio.h> #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include "include/SDL_anigif.h" #define TITLE "SDL2 test gif" #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 int main(int argc, char *argv[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Window *window = SDL_CreateWindow(TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN); SDL_Renderer *render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); //获取gif图最大数量 int gifMax = AG_LoadGIF("gif009.gif", NULL, 0); AG_Frame *frames = (AG_Frame *)malloc(sizeof(AG_Frame) * gifMax); AG_LoadGIF("gif009.gif", frames, gifMax); SDL_Rect rect = {0, 0, 0, 0}; rect.w = frames[0].surface->w; rect.h = frames[0].surface->h; SDL_Texture **textures = (SDL_Texture **)malloc(sizeof(SDL_CreateTextureFromSurface(render, frames[0].surface)) * gifMax); int i; for (i = 0; i < gifMax; i++) { textures[i] = SDL_CreateTextureFromSurface(render, frames[i].surface); } bool quit = false; SDL_Event event; i = 0; while (!quit) { while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: quit = true; break; } } // SDL_SetRenderDrawColor(render, 255, 200, 100, 100); SDL_RenderClear(render); SDL_RenderCopy(render, textures[i], NULL, &rect); i = (i + 1) % gifMax; SDL_RenderPresent(render); SDL_Delay(frames->delay); } free(frames); free(textures); SDL_DestroyRenderer(render); SDL_DestroyWindow(window); SDL_Quit(); return 0; }