Try putting this:
- Code:
#pragma pack(1)
struct tgaheader
{
BYTE byIdLength;
BYTE byColorMapType;
BYTE byImageType;
short nColorMapFirstIndex;
short nColorMapLength;
BYTE byColorMapEntrySize;
short nXOrigin;
short nYOrigin;
short nImageWidth;
short nImageHeight;
BYTE byPixelDepth;
BYTE byImageDescriptor;
};
struct TGA_FILE_FOOTER
{
long lExtensionAreaOffset;
long lDeveloperDirectoryOffset;
char cSignature[18];
};
#pragma pack()
void writetga( TCHAR* file, int w, int h, unsigned short* source )
{
DWORD size = w * 2 * h + 20, bytesread;
BYTE* tgabuff = new BYTE[size];
tgaheader head;
memset(&head;, 0, sizeof(head));
head.byImageType = 2;
head.nImageWidth = w;
head.nImageHeight = h;
head.byPixelDepth = 16;
memcpy(tgabuff, &head;, sizeof(head));
unsigned short* dst = (unsigned short*)tgabuff + 9 + (h - 1) * w;
unsigned short* src = (unsigned short*)source;
for ( int i = 0; i < h; i++ )
{
for ( int x = 0; x < w; x++ )
{
int red = (*(src + x) & (31 << 11)) >> 11;
int green = (*(src + x) & (31 << 6)) >> 6;
int blue = *(src + x) & 31;
*(dst + x) = (red << 10) + (green << 5) + blue;
}
src += w;
dst -= w;
}
HANDLE tga = CreateFile( file, GENERIC_WRITE, FILE_SHARE_READ,
0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
WriteFile( tga, tgabuff, size, &bytesread;, 0 );
CloseHandle( tga );
delete tgabuff;
}
at the beginning of your file.
Then, when you want a screenshot, put:
- Code:
writetga(_T("screenshot.tga"), SCRWIDTH, SCRHEIGHT, getbuffer());