Reading BMP image files in the game

Source: Internet
Author: User
Tags bmp image
Display images using DirectX SDK. One is reading BMP files from files, and the other is reading files from memory (to read files for packaging files). I have tested it, calling this function to read image files is faster than the built-in system. Unfortunately, I have not completed the function. Currently, only 24 Real-color BMP images are supported, because my game runs in 32 bits, I have added ......

Parameter description: whether the filename file name iscolorkey in the target page of lpddstemp has a transparent color_key.
Bool loadbitmap (lpdirectdrawsu *** ce7 lpddstemp, char * filename, bool iscolorkey, colorref color_key)
{
Bitmapfileheader head;
Bitmapinfoheader Info;
File * FP;

Fp = fopen (filename, "rb ");
If (FP = NULL ){
Return-1;
}

Fread (& head, sizeof (bitmapfileheader), 1, FP );

If (head. bftype! = 19778 ){
Fclose (FP );
Return-2;
}

Fread (& info, sizeof (bitmapinfoheader), 1, FP );

Ddsu *** cedesc2 ddsd;

Memset (& ddsd, 0, sizeof (ddsd ));
Ddsd. dwsize = sizeof (ddsd );
If (lpddstemp-> getsu *** cedesc (& ddsd )! = Dd_ OK ){
Fclose (FP );
Return-3;
}

If (ddsd. dwwidth! = (DWORD) info. biwidth) | (ddsd. dwheight! = (DWORD) info. biheight )){
Fclose (FP );

Return-4;
}

Switch (info. bibitcount ){
Case 8:
{
Rgbquad * RGB = new rgbquad [256];
If (RGB = NULL ){
Fclose (FP );
Return-5;
}

Fread (RGB, sizeof (rgbquad), 256, FP );
Byte * buffer = new byte [info. bisizeimage];
If (buffer = NULL ){
Fclose (FP );
Return-6;
}
Fseek (FP, head. bfoffbits, seek_set );
Fread (buffer, sizeof (byte), info. bisizeimage, FP );
Fclose (FP );

Break;
}
Case 24:
{
Byte * buffer = new byte [info. bisizeimage];
If (buffer = NULL ){
Fclose (FP );
Return-7;
}

Fseek (FP, head. bfoffbits, seek_set );
Fread (buffer, info. bisizeimage, 1, FP );
Fclose (FP );

Memset (& ddsd, 0, sizeof (ddsd ));
Ddsd. dwsize = sizeof (ddsd );

If (lpddstemp-> lock (null, & ddsd, ddlock_wait | ddlock_writeonly, 0 )! = Dd_ OK ){
Fclose (FP );
Return-8;
}

Byte * pTMP = (byte *) ddsd. lpsu *** CE;
Byte * PRC = buffer + info. bisizeimage;
Int size_width = info. biwidth * 3 + (info. biwidth % 4 );
Int size1 = 0;
Int size2 = 0;

For (INT I = 0; I <info. biheight; I ++ ){
PRC-= size_width;
For (Int J = 0; j <info. biwidth; j ++ ){
Memcpy (& pTMP [size1], & PRC [size2], 3 );
PTMP [size1 + 3] = 0;

Size1 + = 4;
Size2 + = 3;
}

Size2 = 0;
Size1 = 0;

PTMP + = ddsd. lpitch;
}

If (lpddstemp-> unlock (null )! = Dd_ OK ){
Return-9;
}
Delete [] buffer;
Buffer = NULL;

Break;
}
}

If (iscolorkey ){
If (ddsetcolorkey (lpddstemp, color_key )! = Dd_ OK ){
Return-10;
}
}

Return true;
}

Parameter description: whether the iscolorkey of the BMP buffer memory data on the target page of lpddstemp has a transparent color_key.
Bool loadmembitmap (lpdirectdrawsu *** ce7 lpddstemp, byte * BMP buffer, bool iscolorkey, colorref color_key)
{
Bitmapfileheader head;
Bitmapinfoheader Info;
Long BMP _offset = 0;

If (BMP buffer = NULL ){
Return false;
}

Memcpy (& head, & BMP buffer [BMP _offset], sizeof (bitmapfileheader ));
BMP _offset + = sizeof (bitmapfileheader );

If (head. bftype! = 19778 ){
Return false;
}

Memcpy (& info, & BMP buffer [BMP _offset], sizeof (bitmapinfoheader ));
BMP _offset + = sizeof (bitmapinfoheader );

Ddsu *** cedesc2 ddsd;

Memset (& ddsd, 0, sizeof (ddsd ));
Ddsd. dwsize = sizeof (ddsd );
If (lpddstemp-> getsu *** cedesc (& ddsd )! = Dd_ OK ){
Return false;
}

If (ddsd. dwwidth! = (DWORD) info. biwidth) | (ddsd. dwheight! = (DWORD) info. biheight )){
Return false;
}

Switch (info. bibitcount ){
Case 8:
{
Rgbquad * RGB = new rgbquad [256];
If (RGB = NULL ){
Return false;
}

// Fread (RGB, sizeof (rgbquad), 256, FP );
Byte * buffer = new byte [info. bisizeimage];
If (buffer = NULL ){
Return false;
}

Break;
}
Case 24:
{
Byte * buffer = new byte [info. bisizeimage];
If (buffer = NULL ){
Return false;
}

Memcpy (buffer, & BMP buffer [head. bfoffbits], info. bisizeimage );

Memset (& ddsd, 0, sizeof (ddsd ));
Ddsd. dwsize = sizeof (ddsd );
If (lpddstemp-> lock (null, & ddsd, ddlock_wait | ddlock_writeonly, 0 )! = Dd_ OK ){
Return false;
}

Byte * pTMP = (byte *) ddsd. lpsu *** CE;
Byte * PRC = buffer + info. bisizeimage;
Int size_tmp = info. biwidth * 3 + info. biwidth % 4;
Int size1 = 0;
Int size2 = 0;

For (INT I = 0; I <info. biheight; I ++ ){
PRC-= size_tmp;
For (Int J = 0; j <info. biwidth; j ++ ){
Memcpy (& pTMP [size1], & PRC [size2], 3 );
PTMP [size1 + 3] = 0;

Size1 + = 4;
Size2 + = 3;
}

Size2 = 0;
}

If (lpddstemp-> unlock (null )! = Dd_ OK ){
Return false;
}
Delete [] buffer;
Buffer = NULL;

Break;
}
}

If (iscolorkey ){
If (ddsetcolorkey (lpddstemp, color_key )! = Dd_ OK ){
Return false;
}
}

Return true;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.