There have been a handful of posts in the past that asked for ways to create your own file format, etc. in order to protect you game's bitmaps from tampering.
What I'm providing here is a very simple way to make an image unreadable and therefore uneditable. Granted ( as it's been stated before ) no method is absolute. If someone knows enough and REALLY wants to screw with your game they will find a way.
Also, these code snippet's use the Window's API ( CreateFile, WriteFile, ReadFile, etc.)
Code:
//global declares
HANDLE hFile=NULL;
BITMAPFILEHEADER bmfh;
BYTE Remain[65535];
DWORD dwSize=0, dwRead=0;
//Read the bitmap in
hFile=CreateFile("t1.bmp",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
ReadFile(hFile,&bmfh,sizeof(BITMAPFILEHEADER),&dwRead,NULL);
dwSize=GetFileSize(hFile,NULL);
dwSize-=sizeof(BITMAPFILEHEADER);
ReadFile(hFile,&Remain,dwSize,&dwRead,NULL);
CloseHandle(hFile);
This first snippet just reads the bitmap into memory in 2 parts:
1 - The BITMAPFILEHEADER structure that'll we'll alter
2 - Everything else
It also sets our dwSize variable ( which is the 2nd part of our "encryption" ).
Code:
//this is our "encryption" part 1
bmfh.bfType=19777;
dwRead=0;
hFile=CreateFile("t2.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile(hFile,&bmfh,sizeof(BITMAPFILEHEADER),&dwRead,NULL);
//this is our "encryption" part 2
WriteFile(hFile,&dwSize,sizeof(DWORD),&dwRead,NULL);
for(i=0;i<dwSize;i++)
{
WriteFile(hFile,&Remain[i],sizeof(BYTE),&dwRead,NULL);
}
CloseHandle(hFile);
This 2nd part creates a new file that has 2 changes made to it.
1 - The BITMAPFILEHEADER's bfType is changed to 19777
(normally it's 19778, or "BM")
2 - The size of our remaining bytes is written in between our header and our remaining bytes.
Code:
HANDLE hFile=CreateFile("t2.bmp",GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
ReadFile(hFile,&bmfh,sizeof(BITMAPFILEHEADER),&dwRead,NULL);
ReadFile(hFile,&dwSize,sizeof(DWORD),&dwRead,NULL);
ReadFile(hFile,&Remain,dwSize,&dwRead,NULL);
CloseHandle(hFile);
This 3rd part is the first part of our "decryption". We read the file back in, in it's 3 parts: header, size and remaining bytes.
Code:
bmfh.bfType=19778;
dwRead=0;
hFile=CreateFile("t3.bmp",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile(hFile,&bmfh,sizeof(BITMAPFILEHEADER),&dwRead,NULL);
for(i=0;i<dwSize;i++)
{
WriteFile(hFile,&Remain[i],sizeof(BYTE),&dwRead,NULL);
}
CloseHandle(hFile);
This last part just rewrites the bitmap in it's original form.
Of course, you would want to delete the temporary bitmap ( in our case, "t3.bmp" ) when the game exits. And you could change your bfType to whatever number (WORD) you choose, I just used 19777 for simplicities sake here.
Let me know what you think.