home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / screenshot.c < prev    next >
C/C++ Source or Header  |  1999-08-31  |  2KB  |  76 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include "orbit.h"
  23.  
  24. ScreenShot()
  25. {
  26.     char fname[32];
  27.     char *screen;
  28.     FILE *fd;
  29.     int x, y, c;
  30.  
  31.     /* Construct file name */
  32.     sprintf (fname, "orbit%03d.ppm", screen_shot_num);
  33.     screen_shot_num++;
  34.  
  35.     /* Allocate space for screen */
  36.     if (NULL == (screen = (char *) malloc (ScreenWidth*ScreenHeight*3)))
  37.     {
  38.         Cprint ("Can't allocate memory for screen shot");
  39.         return;
  40.     }
  41.  
  42.     /* Get frame buffer */
  43.     glReadPixels (0, 0, ScreenWidth, ScreenHeight, GL_RGB,
  44.         GL_UNSIGNED_BYTE, screen);
  45.  
  46.     /* Open the file */
  47.     if (NULL == (fd = fopen (fname, "wb")))
  48.     {
  49.         Cprint ("Can't open screen shot file");
  50.         free (screen);
  51.         return;
  52.     }
  53.  
  54.     /* Write the PPM file */
  55.     fprintf (fd, "P6\n%d %d\n255\n", ScreenWidth, ScreenHeight);
  56.  
  57.     for (y=0; y<ScreenHeight; y++)
  58.     {
  59.         for (x=0; x<ScreenWidth; x++)
  60.         {
  61.             c = 0xff & *screen++;
  62.             fputc (c, fd);
  63.             c = 0xff & *screen++;
  64.             fputc (c, fd);
  65.             c = 0xff & *screen++;
  66.             fputc (c, fd);
  67.         }
  68.     }
  69.  
  70.     /* Close and clean up */
  71.     fclose (fd);
  72.     free (screen);
  73.     Cprint ("Screen shot saved in %s", fname);
  74. }
  75.  
  76.