GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


:D by JazzD
#include "CScreenshot.h"

#include "Cdebug.h"

//#include "mmgr.h"

CScreenshot::CScreenshot()
{
  image = 0;
  textureExist = false;
  type = 0;
}


CScreenshot::~CScreenshot()
{
   FreeImage();
}

void CScreenshot::FreeImage()
{
   if(image)
      {
         free(image);
         image = 0;
         textureExist = false;
         type = 0;
      }
}



bool CScreenshot::WriteTGA(char *file, short int width, short int height, unsigned char *outImage)
{

   FILE *pFile;              
   unsigned char uselessChar; 
   short int uselessInt;     
   unsigned char imageType;  
   int index;                
   unsigned char bits;  
   long Size;                 
   int colorMode;
   unsigned char tempColors;


   pFile = fopen(file, "wb");

 
   if(!pFile) { fclose(pFile); return false; }
 
   imageType = 2; colorMode = 3; bits = 24;
   uselessChar = 0; uselessInt = 0;

  
   fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
   fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);


   fwrite(&imageType, sizeof(unsigned char), 1, pFile);


   fwrite(&uselessInt, sizeof(short int), 1, pFile);
   fwrite(&uselessInt, sizeof(short int), 1, pFile);
   fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);
   fwrite(&uselessInt, sizeof(short int), 1, pFile);
   fwrite(&uselessInt, sizeof(short int), 1, pFile);


   fwrite(&width, sizeof(short int), 1, pFile);
   fwrite(&height, sizeof(short int), 1, pFile);
   fwrite(&bits, sizeof(unsigned char), 1, pFile);

   fwrite(&uselessChar, sizeof(unsigned char), 1, pFile);


   Size = width * height * colorMode;


   for(index = 0; index < Size; index += colorMode)
      {
         tempColors = outImage[index];
         outImage[index] = outImage[index + 2];
         outImage[index + 2] = tempColors;
      }


   fwrite(outImage, sizeof(unsigned char), Size, pFile);
   fclose(pFile);

   return true;
}



void CScreenshot::SaveTGAScreenShot(char *filename, int w, int h)
{
   unsigned char *outputImage = 0;
   char        checkname[256],picname[100];


   for (int i=0 ; i<=9999; i++) 
    {
        sprintf (picname, "%s%i%i%i%i.tga", filename, (int)(i/1000%10), (int)(i/100)%10, (int)(i/10)%10, i%10);
        sprintf (checkname, "base/screenshot/%s", picname);
        if(!FileExists(checkname)) {
            break;
        }
    }


  
   outputImage = (unsigned char*)malloc(w * h * 3);

 
   memset(outputImage, 0, w * h * 3);


   glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, outputImage);

   WriteTGA(checkname, w, h, (unsigned char*)outputImage);

   free(outputImage);
}