c语言播放视频或动图的代码
时间: 2023-08-12 18:34:42 浏览: 146
基于c语言的的视频播放
C语言本身没有直接播放视频或动图的功能,需要通过调用其他库或工具来实现。以下是使用SDL库播放视频和动图的示例代码:
播放视频:
```c
#include <stdio.h>
#include <SDL2/SDL.h>
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL_Init failed: %s\n", SDL_GetError());
return -1;
}
SDL_Window *window = SDL_CreateWindow("Video Player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL);
if (!window) {
printf("SDL_CreateWindow failed: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
printf("SDL_CreateRenderer failed: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
SDL_Surface *surface = SDL_LoadBMP("video.bmp");
if (!surface) {
printf("SDL_LoadBMP failed: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
printf("SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError());
SDL_FreeSurface(surface);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
SDL_FreeSurface(surface);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 640;
rect.h = 480;
SDL_Event event;
int quit = 0;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = 1;
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
```
播放动图:
```c
#include <stdio.h>
#include <SDL2/SDL.h>
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL_Init failed: %s\n", SDL_GetError());
return -1;
}
SDL_Window *window = SDL_CreateWindow("GIF Player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL);
if (!window) {
printf("SDL_CreateWindow failed: %s\n", SDL_GetError());
SDL_Quit();
return -1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
printf("SDL_CreateRenderer failed: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
SDL_Surface *surface = SDL_LoadBMP("animation.bmp");
if (!surface) {
printf("SDL_LoadBMP failed: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 640;
rect.h = 480;
SDL_Event event;
int quit = 0;
int frame = 0;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = 1;
break;
}
}
SDL_Rect clip;
clip.x = (frame % 10) * 64;
clip.y = (frame / 10) * 64;
clip.w = 64;
clip.h = 64;
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
printf("SDL_CreateTextureFromSurface failed: %s\n", SDL_GetError());
SDL_FreeSurface(surface);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return -1;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, &clip, &rect);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(texture);
frame++;
if (frame >= 64) {
frame = 0;
}
SDL_Delay(100);
}
SDL_FreeSurface(surface);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
```
需要注意的是,这里的示例代码仅供参考,实际使用时需要根据具体情况进行修改和调整。
阅读全文