From: Antonio Ospite Date: Wed, 20 Mar 2013 16:36:55 +0000 (+0100) Subject: Allow saving the screen to an output file X-Git-Url: https://git.ao2.it/experiments/SDL.git/commitdiff_plain/6e367459f78f0c19ab70911871bfa6b366ed9710?ds=inline;hp=711ba7d74d03d1b9fdbe4348c9895787232999d7 Allow saving the screen to an output file --- diff --git a/scrolling/sdl-scrolling.c b/scrolling/sdl-scrolling.c index 4f49e25..4b9475f 100644 --- a/scrolling/sdl-scrolling.c +++ b/scrolling/sdl-scrolling.c @@ -17,6 +17,10 @@ * along with this program. If not, see . */ +#include +#include +#include +#include #include #include #include @@ -70,8 +74,10 @@ int main(int argc, char *argv[]) Uint32 time_start; Uint32 time_end; - if (argc != 2) { - fprintf(stderr, "usage: %s \n", argv[0]); + int output_fd = -1; + + if (argc < 2) { + fprintf(stderr, "usage: %s []\n", argv[0]); return -EINVAL; } @@ -90,6 +96,20 @@ int main(int argc, char *argv[]) goto out; } + /* Set up the output file */ + if (argc >= 3) { + if (strncmp(argv[2], "-", 1) == 0) { + output_fd = STDOUT_FILENO; + } else { + output_fd = open(argv[2], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); + if (output_fd < 0) { + perror("open"); + ret = output_fd; + goto out; + } + } + } + lastframe_limit = (map->h - HEIGHT); if (SDL_NumJoysticks() > 0) @@ -148,6 +168,16 @@ int main(int argc, char *argv[]) SDL_BlitSurface(map, &viewport, screen, NULL); SDL_Flip(screen); + if (output_fd >= 0) { + int len = screen->w * screen->h * screen->format->BytesPerPixel; + + ret = write(output_fd, screen->pixels, len); + if (ret != len) { + fprintf(stderr, "invalid write\n"); + running = 0; + } + } + time_end = SDL_GetTicks(); if (time_end - time_start < 1000 / FPS) SDL_Delay((1000 / FPS) - (time_end - time_start)); @@ -155,6 +185,9 @@ int main(int argc, char *argv[]) ret = 0; + if (output_fd >= 0) + close(output_fd); + if (SDL_JoystickOpened(0)) SDL_JoystickClose(joystick);