Advertisement

i want to PrintScreen - then name the image..

Started by July 13, 2004 01:22 PM
25 comments, last by PinguinDude 20 years, 4 months ago
then send it somewhere automatically to a file/folder etc.. how would you do this in C++/OGL? is it possible? i am using GLUT and i am sure(i hope) that Glut recognizes the Pr Scr as a special key... anyway does anyone have any idea how you would do this? thanks in advance!
heh
I'm not sure that GLUT recognizes the print screen key. However, you could bind your screenshot code any button GLUT does support. For creating a screenshot, it's fairly easy to use the glReadPixels() function. From there you just need to choose an image format to output your image in; wotsit has a good list. I prefer to output targas, as the header is minimal. Keep in mind that with many file formats you may need to swap color channels from what glReadPixels() outputs.
Advertisement
Here's a chunk of code I wrote a long time ago to grab the current buffer and write it out to a windows .bmp. Note that I'm assuming that you are in an 24bit RGB mode...well hell I'm making lots of assumptions...

#include <stdio.h>#include <stdlib.h>voidWriteBufferToFile( const char* cszFilename ){   if ( 0 == cszFilename )      return;   int nWidth  = glutGet( GLUT_WINDOW_WIDTH );   int nHeight = glutGet( GLUT_WINDOW_HEIGHT );   unsigned char* pcBuffer = new unsigned char[nWidth * nHeight * 3];   glReadBuffer( GL_BACK );   glReadPixels( 0, 0,       nWidth, nHeight,      GL_RGB,      GL_UNSIGNED_BYTE,      (GLvoid*)pcBuffer);   unlink( cszFilename );   FILE* pf = fopen( cszFilename, "wb" );   if ( pf )   {      BITMAPFILEHEADER bfh;      BITMAPINFOHEADER bih;      RGBQUAD          pal[1];      memset( &bfh, 0 , sizeof( BITMAPFILEHEADER ) );      memset( &bih, 0 , sizeof( BITMAPINFOHEADER ) );      bfh.bfType    = 0x4d42; // ascii "BM"      bfh.bfOffBits = sizeof( BITMAPINFOHEADER ) + sizeof( RGBTRIPLE[1] );      bih.biSize          = sizeof( BITMAPINFOHEADER );      bih.biWidth         = nWidth;      bih.biHeight        = nHeight;      bih.biPlanes        = 1;      bih.biBitCount      = 24;      bih.biCompression   = BI_RGB;      bih.biSizeImage     = 0; // autocalced for RGB images      bih.biXPelsPerMeter = 1000;      bih.biYPelsPerMeter = 1000;      bih.biClrUsed       = 1;      bih.biClrImportant  = 0;      int nScanLineSize = nWidth * 3;       int nScanLinePad  = nScanLineSize % 4;       char szPad[] = {0, 0, 0};      bfh.bfSize =          sizeof( BITMAPFILEHEADER ) +          sizeof( BITMAPINFOHEADER ) +         sizeof( RGBQUAD[1] ) +         (nScanLineSize + nScanLinePad) * nHeight;      fwrite( &bfh, sizeof( BITMAPFILEHEADER ), 1, pf );      fwrite( &bih, sizeof( BITMAPINFOHEADER ), 1, pf );      fwrite( &pal, sizeof( RGBQUAD[1] ), 1, pf );      int nOffset = 0;      for ( int i = 0; i < nHeight; i++, nOffset += nScanLineSize )      {         fwrite( &(pcBuffer[nOffset]), nScanLineSize, 1, pf );         if ( nScanLinePad )            fwrite( szPad, nScanLinePad, 1, pf );      }      fclose( pf ); pf = 0;   }   delete [] pcBuffer, pcBuffer = 0;}
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
hi SICrane..well i could as you say make a button that would print to a screen...and then just use the glReadPixels to do it..problem is is that i am writing my code inconsole mode, not in win32 so MauMans code wont work..

other than that i would just need to figure out how to use glReadPixels to write it all out to a file , which im at a loss.. this is something new i am trying....
heh
At first glance, I don't see anything in MauMan's code that would prevent you from using it. What seems to be the problem there? In anycase, it has a demonstration of how to use glReadPixels() which is the important part.
isnt BITMAPFILEHEADER just windows only? so bih and bfh of that type would not work in linux i am afraid? maybe i am wrong..thats what i was referring to. oh yes and bitmap & targas are what i work with most so thats good news...
heh
Advertisement
No. You can make the structures yourself :) I found the structs you need :)

typedef struct tagBITMAPINFOHEADER{        DWORD      biSize;        LONG       biWidth;        LONG       biHeight;        WORD       biPlanes;        WORD       biBitCount;        DWORD      biCompression;        DWORD      biSizeImage;        LONG       biXPelsPerMeter;        LONG       biYPelsPerMeter;        DWORD      biClrUsed;        DWORD      biClrImportant;} BITMAPINFOHEADER, FAR *LPBITMAPINFOHEADER, *PBITMAPINFOHEADER;typedef struct tagBITMAPFILEHEADER {        WORD    bfType;        DWORD   bfSize;        WORD    bfReserved1;        WORD    bfReserved2;        DWORD   bfOffBits;} BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;typedef struct tagRGBQUAD {        BYTE    rgbBlue;        BYTE    rgbGreen;        BYTE    rgbRed;        BYTE    rgbReserved;} RGBQUAD;


Those should work. If they don't tell me :)

[Edited by - vbtest on July 13, 2004 5:24:52 PM]
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)
ok i am going to make up a glut app and see if i can get it to work.. thanks for your help!
heh
the compiler keeps saying(complaining) that the struct names are a redefinition -- where in the world would they be redefined?



C:\Program Files\Microsoft Visual Studio\VC98\MyProjects\final_bathy\bathy.c(52) : error C2011: 'tagBITMAPINFOHEADER' : 'struct' type redefinition
heh
nevermind, i got it all cleared up, hey vbtest you out there, i am going to have a few questions for you..
heh

This topic is closed to new replies.

Advertisement