Merge pull request #17 from MichielP1807/master
[gifdec.git] / example.c
blobf0b427e978594db708ef54621807301f7dabbf1e
1 /* gifdec example -- simple GIF player using SDL2
2 * compiling:
3 * cc `pkg-config --cflags --libs sdl2` -o example gifdec.c example.c
4 * executing:
5 * ./example animation.gif
6 * */
8 #include <stdio.h>
10 #include <SDL.h>
12 #include "gifdec.h"
14 int
15 main(int argc, char *argv[])
17 SDL_Window *window;
18 SDL_Renderer *renderer;
19 SDL_Surface *surface;
20 SDL_Texture *texture;
21 SDL_Event event;
22 gd_GIF *gif;
23 char title[32] = {0};
24 Uint8 *color, *frame;
25 void *addr;
26 int i, j;
27 Uint32 pixel;
28 int ret, paused, quit;
29 Uint32 t0, t1, delay, delta;
31 if (argc != 2) {
32 fprintf(stderr, "usage:\n %s gif-file\n", argv[0]);
33 return 1;
35 gif = gd_open_gif(argv[1]);
36 if (!gif) {
37 fprintf(stderr, "Could not open %s\n", argv[1]);
38 return 1;
40 frame = malloc(gif->width * gif->height * 3);
41 if (!frame) {
42 fprintf(stderr, "Could not allocate frame\n");
43 return 1;
45 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
46 SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
47 return 1;
49 if (SDL_CreateWindowAndRenderer(gif->width, gif->height, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
50 SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
51 return 1;
53 snprintf(title, sizeof(title) - 1, "GIF %dx%d %d colors",
54 gif->width, gif->height, gif->palette->size);
55 SDL_SetWindowTitle(window, title);
56 color = &gif->gct.colors[gif->bgindex * 3];
57 SDL_SetRenderDrawColor(renderer, color[0], color[1], color[2], 0x00);
58 SDL_RenderClear(renderer);
59 SDL_RenderPresent(renderer);
60 surface = SDL_CreateRGBSurface(0, gif->width, gif->height, 32, 0, 0, 0, 0);
61 if (!surface) {
62 SDL_Log("SDL_CreateRGBSurface() failed: %s", SDL_GetError());
63 return 1;
65 paused = 0;
66 quit = 0;
67 while (1) {
68 while (SDL_PollEvent(&event) && !quit) {
69 if (event.type == SDL_QUIT)
70 quit = 1;
71 if (event.type == SDL_KEYDOWN) {
72 if (event.key.keysym.sym == SDLK_q)
73 quit = 1;
74 else if (event.key.keysym.sym == SDLK_SPACE)
75 paused = !paused;
78 if (quit) break;
79 if (paused) {
80 SDL_Delay(10);
81 continue;
83 t0 = SDL_GetTicks();
84 ret = gd_get_frame(gif);
85 if (ret == -1)
86 break;
87 SDL_LockSurface(surface);
88 gd_render_frame(gif, frame);
89 color = frame;
90 for (i = 0; i < gif->height; i++) {
91 for (j = 0; j < gif->width; j++) {
92 if (!gd_is_bgcolor(gif, color))
93 pixel = SDL_MapRGB(surface->format, color[0], color[1], color[2]);
94 else if (((i >> 2) + (j >> 2)) & 1)
95 pixel = SDL_MapRGB(surface->format, 0x7F, 0x7F, 0x7F);
96 else
97 pixel = SDL_MapRGB(surface->format, 0x00, 0x00, 0x00);
98 addr = surface->pixels + (i * surface->pitch + j * sizeof(pixel));
99 memcpy(addr, &pixel, sizeof(pixel));
100 color += 3;
103 SDL_UnlockSurface(surface);
104 texture = SDL_CreateTextureFromSurface(renderer, surface);
105 SDL_RenderCopy(renderer, texture, NULL, NULL);
106 SDL_RenderPresent(renderer);
107 SDL_DestroyTexture(texture);
108 t1 = SDL_GetTicks();
109 delta = t1 - t0;
110 delay = gif->gce.delay * 10;
111 delay = delay > delta ? delay - delta : 1;
112 SDL_Delay(delay);
113 if (ret == 0)
114 gd_rewind(gif);
116 SDL_FreeSurface(surface);
117 SDL_DestroyRenderer(renderer);
118 SDL_DestroyWindow(window);
119 SDL_Quit();
120 free(frame);
121 gd_close_gif(gif);
122 return 0;