Skip to content

Commit 7ddbf06

Browse files
committedJan 8, 2018
SDLaffgif.c: Choose window size depending on GIF resolution
+ some minor modifications
1 parent c88a3a0 commit 7ddbf06

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed
 

‎SDLaffgif.c

+26-8
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,37 @@ int main(int argc, char* * argv) {
8989
char * gifname;
9090
struct ngiflibSDL_animation * animation;
9191
int i;
92+
int width, height;
93+
char windows_caption[256];
94+
char icon_caption[256];
9295

9396
if(argc>1)
9497
gifname = argv[1];
9598
else
9699
gifname = "amigagry.gif";
100+
97101
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
98102
fprintf(stderr, "Erreur d'init de la SDL !\n");
99103
return 1;
100104
}
101105
atexit(SDL_Quit);
102-
SDL_WM_SetCaption("SDL gif viewer", "gif viewer"); /* window caption, icon caption */
106+
107+
animation = SDL_LoadAnimatedGif(gifname);
108+
if(animation == NULL) {
109+
fprintf(stderr, "Failed to read %s\n", gifname);
110+
return 1;
111+
}
112+
113+
if(animation->image_count == 0) {
114+
fprintf(stderr, "%s contains 0 images !\n", gifname);
115+
return 1;
116+
}
117+
width = animation->images[0].surface->w;
118+
height = animation->images[0].surface->h;
119+
120+
snprintf(windows_caption, sizeof(windows_caption), "SDL gif viewer : %s", gifname);
121+
snprintf(icon_caption, sizeof(icon_caption), "gif viewer : %s", gifname);
122+
SDL_WM_SetCaption(windows_caption, icon_caption); /* window caption, icon caption */
103123
vidinf = SDL_GetVideoInfo();
104124
printf("bpp of the \"best\" video mode = %d \n", vidinf->vfmt->BitsPerPixel);
105125
modes = SDL_ListModes(NULL, SDL_HWSURFACE);
@@ -113,14 +133,14 @@ int main(int argc, char* * argv) {
113133
}
114134
}
115135

116-
bpp = SDL_VideoModeOK(640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
136+
bpp = SDL_VideoModeOK(width, height, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
117137

118138
if(!bpp){
119139
printf("Mode not available.\n");
120140
exit(-1);
121141
}
122-
printf("SDL Recommends 640x480@%dbpp.\n", bpp);
123-
screen = SDL_SetVideoMode(640, 480, bpp, SDL_HWSURFACE | SDL_DOUBLEBUF);
142+
printf("SDL Recommends %dx%d@%dbpp.\n", width, height, bpp);
143+
screen = SDL_SetVideoMode(width, height, bpp, SDL_HWSURFACE | SDL_DOUBLEBUF);
124144
#if 0
125145
/* code to display single image : */
126146
ShowGIF(gifname, screen, 0,0);
@@ -131,9 +151,7 @@ int main(int argc, char* * argv) {
131151
}
132152
#endif
133153

134-
animation = SDL_LoadAnimatedGif(gifname);
135-
printf("SDL_LoadAnimatedGif(%s) => %p\n", gifname, animation);
136-
if(animation != NULL) {
154+
{
137155
printf("%d images\n", animation->image_count);
138156
for(;;)
139157
for(i = 0; i < animation->image_count; i++) {
@@ -154,7 +172,7 @@ int main(int argc, char* * argv) {
154172
if(animation->image_count > 1) {
155173
if(animation->images[i].delay_time == 0) {
156174
SDL_Delay(100); /* default delay of 1/10th of seconds */
157-
} else {
175+
} else if(animation->images[i].delay_time > 0) {
158176
SDL_Delay(10*animation->images[i].delay_time);
159177
}
160178
while(SDL_PollEvent(&event)) {

‎ngiflibSDL.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ struct ngiflibSDL_animation * SDL_LoadAnimatedGif(const char * file)
177177
surface->format->palette->colors[i].r = current_palette[i].r;
178178
surface->format->palette->colors[i].g = current_palette[i].g;
179179
surface->format->palette->colors[i].b = current_palette[i].b;
180-
printf("#%02x%02x%02x ", current_palette[i].r, current_palette[i].g, current_palette[i].b);
180+
//printf("#%02x%02x%02x ", current_palette[i].r, current_palette[i].g, current_palette[i].b);
181181
}
182182
for(; i < gif->ncolors; i++) {
183183
surface->format->palette->colors[i].r = gif->palette[i].r;
184184
surface->format->palette->colors[i].g = gif->palette[i].g;
185185
surface->format->palette->colors[i].b = gif->palette[i].b;
186-
printf("#%02x%02x%02x ", gif->palette[i].r, gif->palette[i].g, gif->palette[i].b);
186+
//printf("#%02x%02x%02x ", gif->palette[i].r, gif->palette[i].g, gif->palette[i].b);
187187
}
188188
printf("\n");
189189
psrc = p; pdst = surface->pixels;
@@ -193,6 +193,7 @@ struct ngiflibSDL_animation * SDL_LoadAnimatedGif(const char * file)
193193
psrc += gif->width;
194194
}
195195
SDL_UnlockSurface(surface);
196+
animation->images[image_count].delay_time = -1;
196197
animation->images[image_count].delay_time = gif->delay_time;
197198
animation->images[image_count].surface = surface;
198199
image_count++;

‎ngiflibSDL.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
SDL_Surface *SDL_LoadGIF(const char *file);
77

88
struct ngiflibSDL_image {
9-
unsigned int delay_time;
9+
int delay_time;
1010
SDL_Surface * surface;
1111
};
1212

0 commit comments

Comments
 (0)
Please sign in to comment.