是规定吧...图片分辨率是3440*240的游戏背景图片..
当我把他直接写入主表面的时候有成功了...怎么回事..
要看代码吗..写的有点乱..
DX7GL.h
#ifndef _DX7GL_H_
#define _DX7GL_H_
#ifndef DSBCAPS_CTRLDEFAULT
#define DSBCAPS_CTRLDEFAULT (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME)
#endif
#define INITGUID
#define DIRECTINPUT_VERSION 0x0800
#define DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)) ; ddstruct.dwSize = sizeof(ddstruct) ;}
#define _RGB32BIT(a,r,g,b) ((b) + ((g) << 8) + ((r) << 16) + ((a) << 24))
#define SOUND_NULL 0
#define SOUND_LOADED 1
#define SOUND_PLAYING 2
#define SOUND_STOPPED 3
#include <windows.h>
#include <MMsystem.h>
#include <ddraw.h>
#include <objbase.h>
#include <initguid.h>
#include <Guiddef.h>
#include <stdio.h>
#include <dinput.h>
#include <dsound.h>
#include "dmusici.h"
#include "dmusicf.h"
#include "dmusicc.h"
typedef unsigned short USHORT ;
typedef unsigned short WORD ;
typedef unsigned char UCHAR ;
typedef unsigned char BYTE ;
#pragma comment(lib,"dinput8.lib")
#pragma comment(lib,"ddraw.lib")
#pragma comment(lib,"dxguid.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "dsound.lib")
struct INTS
{
int x,y,height,width,Bit,L_BYTE ;
unsigned char* image_PTR ;
BITMAPFILEHEADER bitmapfileheader ;
BITMAPINFOHEADER bitmapinfoheader ;
int (*Flip)(struct INTS*,unsigned char*,int,int) ;
int (*Load)(struct INTS*,LPWSTR) ;
int (*Del)(struct INTS*) ;
} ;
void INTS_Init(struct INTS*) ;
struct DX7Graph
{
LPDIRECTDRAW7 major_lpdd ; // 接口
DDSURFACEDESC2 major_ddsd ; // 主表面及其描述设备..
LPDIRECTDRAWSURFACE7 major_surface ;
RECT major_rect ;
DDSURFACEDESC2 sub_ddsd ; // 后备表面及其描述设备..
LPDIRECTDRAWSURFACE7 sub_surface ;
RECT sub_rect ;
LPDIRECTINPUT8 lpdi ; //主输入指针
LPDIRECTINPUTDEVICE8 lpdikey ; //设备指针
UCHAR keyindex[256] ; //键盘数组..
LPDIRECTSOUND lpds ; //DirectSound音频指针..
LPDIRECTSOUNDBUFFER Sound_Buffer ; //缓冲指针..
DSBUFFERDESC dsbd ; //音频描述结构..
WAVEFORMATEX wfmtx ; //wav结构..
unsigned long state ; //状态值..
LPDIRECTDRAWCLIPPER lpddclipper ;
int (*Init_Main_Surface)(struct DX7Graph*,HWND) ; // 若干函数指针
int (*Create_OffScreen_Surface)(struct DX7Graph*,int,int,bool,int) ;
int (*Paint)(struct DX7Graph*,HWND,int,int,unsigned char*,unsigned char*,long) ;
int (*Extra_OffScreen)(struct DX7Graph*,DDSURFACEDESC2&,LPDIRECTDRAWSURFACE7 FAR*,int,int,bool,int) ;
// 此函数用于更多的额外的表面及其描述设备..
int (*Create_KeyBoard_Device)(struct DX7Graph*,HWND,HINSTANCE) ;
int (*Read_W_A_V)(struct DX7Graph*,LPWSTR,HWND,int) ;
void(*Clipper)(struct DX7Graph*,int,LPRECT) ;
int (*Del_DX7Graph)(struct DX7Graph*) ;
} ;
void DX7Graph_Mapper_Slot(struct DX7Graph*,LPRECT,LPRECT) ;
// 函数指针映射表声明..
#endif
#include "DX7GL.h"
// 映射函数声明..
static int mapper_Flip(struct INTS*,unsigned char*,int,int) ;
static int mapper_Load(struct INTS*,LPWSTR) ;
static int mapper_Del(struct INTS*) ;
static int mapper_Init_Main_Surface(struct DX7Graph*,HWND) ;
static int mapper_Create_OffScreen_Surface(struct DX7Graph*,int,int,bool,int) ;
static int mapper_Paint(struct DX7Graph*,HWND,int,int,unsigned char*,unsigned char*,long) ;
static int mapper_Extra_OffScreen(struct DX7Graph*,DDSURFACEDESC2&,LPDIRECTDRAWSURFACE7 FAR*,int,int,bool,int) ;
static int mapper_Create_KeyBoard_Device(struct DX7Graph*,HWND,HINSTANCE) ;
static int mapper_Read_W_A_V(struct DX7Graph*,LPWSTR,HWND,int) ;
static void mapper_Clipper(struct DX7Graph*,int,LPRECT) ;
static int mapper_Del_DX7Graph(struct DX7Graph*) ;
void INTS_Init(struct INTS* st) // 函数指针映射表实现..
{
st->Flip = mapper_Flip ;
st->Load = mapper_Load ;
st->Del = mapper_Del ;
}
void DX7Graph_Mapper_Slot(struct DX7Graph* DX7_Slot,LPRECT m,LPRECT s) // 函数指针映射表实现..
{
DX7_Slot->Init_Main_Surface = mapper_Init_Main_Surface ;
DX7_Slot->Create_OffScreen_Surface = mapper_Create_OffScreen_Surface ;
DX7_Slot->Paint = mapper_Paint ;
DX7_Slot->Extra_OffScreen = mapper_Extra_OffScreen ;
DX7_Slot->Create_KeyBoard_Device = mapper_Create_KeyBoard_Device ;
DX7_Slot->Read_W_A_V = mapper_Read_W_A_V ;
DX7_Slot->Clipper = mapper_Clipper ;
DX7_Slot->Del_DX7Graph = mapper_Del_DX7Graph ;
if(m == NULL)
goto kante ;
else
{
DX7_Slot->major_rect.left = m->left ;
DX7_Slot->major_rect.top = m->top ;
DX7_Slot->major_rect.right = m->right ;
DX7_Slot->major_rect.bottom = m->bottom ;
}
kante:if(s == NULL)
return ;
else
{
DX7_Slot->sub_rect.left = s->left ;
DX7_Slot->sub_rect.top = s->top ;
DX7_Slot->sub_rect.right = s->right ;
DX7_Slot->sub_rect.bottom = s->bottom ;
}
return ;
}
// 映射函数实现..当位图标志位为正时..使用此函数矫正Y轴
int mapper_Flip(struct INTS *st,unsigned char *image,int bytes_per_line,int height)
{
unsigned char* buffer ;
int index ;
if (!(buffer = (UCHAR *)malloc(bytes_per_line*height)))
MessageBox(NULL,TEXT("malloc buffer failed"),TEXT("DirectDraw7"),MB_ICONERROR | MB_ICONERROR) ;
memcpy(buffer,image,bytes_per_line*height);
for (index=0; index < height ; index++)
memcpy(&image[((height-1) - index)*bytes_per_line],&buffer[index*bytes_per_line],bytes_per_line) ;
free(buffer) ;
return(1) ;
}
// 映射函数实现..读取位图结构头..及数据段 ..注意字节对齐..不然图片会撕裂以及灰阶..
int mapper_Load(struct INTS *st,LPWSTR TRG)
{
static int correct = 65535 ;
FILE* fp ;
_wfopen_s(&fp,TRG,TEXT("rb")) ;
if(fp == 0)
MessageBox(NULL,TEXT("Open bmp failed"),TEXT("DirectDraw7"),MB_ICONERROR | MB_ICONERROR) ;
fread(&st->bitmapfileheader,sizeof(BITMAPFILEHEADER),1,fp) ;
fread(&st->bitmapinfoheader,sizeof(BITMAPINFOHEADER),1,fp) ;
if (st->bitmapinfoheader.biHeight < 0)
correct = (-(st->bitmapinfoheader.biHeight)) ;
else
correct = st->bitmapinfoheader.biHeight ;
st->height = correct ;
st->width = st->bitmapinfoheader.biWidth ;
st->Bit = st->bitmapinfoheader.biBitCount ;
if(st->Bit == 8)
MessageBox(NULL,TEXT("没写调色板代码...禁止使用8位图元.."),TEXT("载入位图错误"),MB_ICONERROR) ;
st->L_BYTE = ((st->width)*((st->Bit)/8) + 3)/4*4 ;
st->image_PTR = (unsigned char*)malloc((st->L_BYTE)*(correct)) ;
if(fread(st->image_PTR,1,(st->L_BYTE)*(correct),fp) == 0)
MessageBox(NULL,TEXT("read image data failed"),TEXT("s_Load"),MB_ICONERROR) ;
if (st->bitmapinfoheader.biHeight > 0)
st->Flip(st,st->image_PTR,st->L_BYTE,st->height) ;
fclose(fp) ;
return 0 ;
}
// 映射函数实现..释放RES
int mapper_Del(struct INTS *st)
{
free(st->image_PTR) ;
return 0 ;
}
// 映射函数实现..初始化Direct主表面..
int mapper_Init_Main_Surface(struct DX7Graph* DX7_Slot,HWND swnd)
{
if (FAILED(DirectDrawCreateEx(NULL,(void **)(&(DX7_Slot->major_lpdd)),IID_IDirectDraw7,NULL)))
MessageBox(NULL,TEXT("DirectDrawCreateEx Failed"),TEXT("DirectDraw7"),MB_ICONERROR) ;
if (FAILED(DX7_Slot->major_lpdd->SetCooperativeLevel(swnd,DDSCL_NORMAL)))
MessageBox(NULL,TEXT("SetCooperativeLevel Failed"),TEXT("DirectDraw7"),MB_ICONERROR) ;
DDRAW_INIT_STRUCT(DX7_Slot->major_ddsd) ;
DX7_Slot->major_ddsd.dwFlags = DDSD_CAPS ;
DX7_Slot->major_ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE ;
if(FAILED(DX7_Slot->major_lpdd->CreateSurface((&(DX7_Slot->major_ddsd)),(&(DX7_Slot->major_surface)),NULL)))
MessageBox(NULL,TEXT("CreateSurface Failed"),TEXT("DirectDraw7"),MB_ICONERROR) ;
return 5 ;
}
// 映射函数实现..初始化离屏表面..
DX7GL.cpp
[code=cint mapper_Create_OffScreen_Surface(struct DX7Graph *DX7_Slot,int width,int height,bool V,int KeyVaule)
{
DDRAW_INIT_STRUCT(DX7_Slot->sub_ddsd) ;
if(V == true)
{
DX7_Slot->sub_ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = KeyVaule ;
DX7_Slot->sub_ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = KeyVaule ;
}
DX7_Slot->sub_ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_CKSRCBLT ;
DX7_Slot->sub_ddsd.dwWidth = width ;
DX7_Slot->sub_ddsd.dwHeight = height ;
DX7_Slot->sub_ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY ;
if(FAILED(DX7_Slot->major_lpdd->CreateSurface(&(DX7_Slot->sub_ddsd),&(DX7_Slot->sub_surface),NULL)))
MessageBox(NULL,TEXT("创建离屏表面失败"),TEXT("DirectDraw7"),MB_ICONERROR) ;
return 6 ;
}
// 映射函数实现..此函数用于写入表面像素..
int mapper_Paint(struct DX7Graph *DX7_Slot,HWND hwnd,int height,int width,unsigned char* dx7_surface,unsigned char* image_buffer,long lPitch)
{
static unsigned char* PLA = image_buffer ;
for(int x = 0 ; x < height ; x++)
{
for (int y=0 ; y < width ; y++)
{
*((DWORD*)(dx7_surface + y*4 + x * lPitch)) = _RGB32BIT(0,*(image_buffer + 2),*(image_buffer + 1),*image_buffer ) ;
image_buffer = image_buffer + 3 ;
}
}
image_buffer = PLA ;
return 5 ;
}
// 映射函数实现..用于创建更多的表面缓冲..
int mapper_Extra_OffScreen(struct DX7Graph* DX7_Slot,DDSURFACEDESC2& ddsdk,LPDIRECTDRAWSURFACE7 FAR* DS7_PTR,int width,int height,bool Q,int Color)
{
DDRAW_INIT_STRUCT(ddsdk) ;
if(Q == true)
{
ddsdk.ddckCKSrcBlt.dwColorSpaceLowValue = Color ;
ddsdk.ddckCKSrcBlt.dwColorSpaceHighValue = Color ;
}
ddsdk.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_CKSRCBLT ;
ddsdk.dwWidth = width ;
ddsdk.dwHeight = height ;
ddsdk.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY ;
if(FAILED(DX7_Slot->major_lpdd->CreateSurface(&(ddsdk),DS7_PTR,NULL)))
MessageBox(NULL,TEXT("创建离屏表面失败"),TEXT("DirectDraw7"),MB_ICONERROR) ;
return 6 ;
}
int mapper_Read_W_A_V(struct DX7Graph* DX7_Slot,LPWSTR filename,HWND hWnd,int dwFlags)
{
HMMIO hwav ;
MMCKINFO parent,child ;
UCHAR *snd_buffer = NULL,*audio_ptr_1 = NULL,*audio_ptr_2 = NULL ;
DWORD audio_length_1 = 0,audio_length_2 = 0 ;
DSBUFFERDESC* LPdsbd = &(DX7_Slot->dsbd) ;
WAVEFORMATEX* LPwfmtx = &(DX7_Slot->wfmtx) ;
__asm PUSH ECX __asm PUSH EDI __asm PUSH EAX
__asm CLD __asm MOV EAX,0 __asm MOV ECX,8
__asm MOV EDI,LPdsbd __asm REP STOSD __asm MOV ECX,9
__asm MOV EDI,LPwfmtx __asm REP STOS WORD PTR ES:[EDI]
__asm POP EDI __asm POP ECX __asm POP EAX
if (DirectSoundCreate(NULL,&(DX7_Slot->lpds),NULL) != DS_OK)
MessageBox(NULL,TEXT("DirectSoundCreate failed"),TEXT("DirectSound"),MB_ICONERROR) ;
if (DX7_Slot->lpds->SetCooperativeLevel(hWnd,DSSCL_NORMAL) != DS_OK)
MessageBox(NULL,TEXT("SetCooperativeLevel failed"),TEXT("DirectSound"),MB_ICONERROR) ;
DX7_Slot->state = SOUND_NULL ;
parent.ckid = (FOURCC)0 ;
parent.cksize = 0 ;
parent.fccType = (FOURCC)0 ;
parent.dwDataOffset = 0 ;
parent.dwFlags = 0 ;
child = parent ;
if((hwav = mmioOpen(filename,NULL,MMIO_READ | MMIO_ALLOCBUF)) == NULL)
MessageBox(NULL,TEXT("mmioOpen failed(hwav assignment failed"),TEXT("mmio块打开"),MB_ICONERROR) ;
parent.fccType = mmioFOURCC('W','A','V','E') ;
if (mmioDescend(hwav,&parent,NULL,MMIO_FINDRIFF))
{
mmioClose(hwav, 0) ;
MessageBox(NULL,TEXT("hha~~ RIFF mmioDescend failed"),TEXT("RIFF块标志寻找"),MB_ICONERROR) ;
}
child.ckid = mmioFOURCC('f','m','t',' ') ;
if (mmioDescend(hwav,&child,&parent,0))
{
MessageBox(NULL,TEXT("NO!!!Subblock 'f','m','t',' ' mmioDescend failed"),TEXT("mmio块内跳转"),MB_ICONERROR) ;
mmioClose(hwav, 0) ;
}
if (mmioRead(hwav,(char*)&(DX7_Slot->wfmtx),sizeof(WAVEFORMATEX)) != sizeof(WAVEFORMATEX))
{
MessageBox(NULL,TEXT("My Dear Sir 'f','m','t',' 'mmioRead failed"),TEXT("mmio块读取"),MB_ICONERROR) ;
mmioClose(hwav, 0) ;
}
if (DX7_Slot->wfmtx.wFormatTag != WAVE_FORMAT_PCM)
{
MessageBox(NULL,TEXT("Oh My God!!This is not even WAVE_FORMAT_PCM"),TEXT("DirectSound"),MB_ICONERROR) ;
mmioClose(hwav,0) ;
}
if (mmioAscend(hwav,&child,0))
{
mmioClose(hwav, 0) ;
MessageBox(NULL,TEXT("you are very pathetic 0.0 fmt mmioAscend failed"),TEXT("mmio子块跳出"),MB_ICONERROR) ;
}
child.ckid = mmioFOURCC('d','a','t','a') ;
if (mmioDescend(hwav,&child,&parent,MMIO_FINDCHUNK))
{
MessageBox(NULL,TEXT("You could be mad Subblock 'f','m','t',' ' mmioDescend failed"),TEXT("mmio子块错误"),MB_ICONERROR) ;
mmioClose(hwav,0) ;
}
snd_buffer = (UCHAR *)malloc(child.cksize) ;
mmioRead(hwav,(char*)snd_buffer,child.cksize) ;
mmioClose(hwav,0) ;
DX7_Slot->state = SOUND_LOADED ;
DX7_Slot->wfmtx.cbSize = 0 ;
DX7_Slot->dsbd.dwSize = sizeof(DSBUFFERDESC) ;
DX7_Slot->dsbd.dwFlags = dwFlags | DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE ;
DX7_Slot->dsbd.dwBufferBytes = child.cksize ;
DX7_Slot->dsbd.lpwfxFormat = &(DX7_Slot->wfmtx) ;
if (FAILED(DX7_Slot->lpds->CreateSoundBuffer(&(DX7_Slot->dsbd),&(DX7_Slot->Sound_Buffer),NULL)))
{
MessageBox(NULL,TEXT("CreateSoundBuffer failed"),TEXT("DirectSound"),MB_ICONERROR) ;
free(snd_buffer);
}
if (FAILED(DX7_Slot->Sound_Buffer->Lock(0,child.cksize,(void **)&audio_ptr_1,&audio_length_1,(void**)&audio_ptr_2,&audio_length_2,DSBLOCK_FROMWRITECURSOR)))
{
MessageBox(NULL,TEXT("Lock failed"),TEXT("DirectSound"),MB_ICONERROR) ;
free(snd_buffer);
}
memcpy(audio_ptr_1,snd_buffer,audio_length_1) ;
memcpy(audio_ptr_2,(snd_buffer + audio_length_1),audio_length_2) ;
if (FAILED(DX7_Slot->Sound_Buffer->Unlock(audio_ptr_1,audio_length_1,audio_ptr_2,audio_length_2)))
{
MessageBox(NULL,TEXT("Unlock failed"),TEXT("DirectSound"),MB_ICONERROR) ;
free(snd_buffer) ;
}
return 5 ;
}
][/code]
BitBlt
The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.
BOOL BitBlt(
HDC hdcDest, // handle to destination device context
int nXDest, // x-coordinate of destination rectangle's upper-left
// corner
int nYDest, // y-coordinate of destination rectangle's upper-left
// corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source device context
int nXSrc, // x-coordinate of source rectangle's upper-left
// corner
int nYSrc, // y-coordinate of source rectangle's upper-left
// corner
DWORD dwRop // raster operation code
);
Parameters
hdcDest
Handle to the destination device context.
nXDest
Specifies the logical x-coordinate of the upper-left corner of the destination rectangle.
nYDest
Specifies the logical y-coordinate of the upper-left corner of the destination rectangle.
nWidth
Specifies the logical width of the source and destination rectangles.
nHeight
Specifies the logical height of the source and the destination rectangles.
hdcSrc
Handle to the source device context.
nXSrc
Specifies the logical x-coordinate of the upper-left corner of the source rectangle.
nYSrc
Specifies the logical y-coordinate of the upper-left corner of the source rectangle.
dwRop
Specifies a raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color.
The following list shows some common raster operation codes: Value Description
BLACKNESS Fills the destination rectangle using the color associated with index 0 in the physical palette. (This color is black for the default physical palette.)
DSTINVERT Inverts the destination rectangle.
MERGECOPY Merges the colors of the source rectangle with the specified pattern by using the Boolean AND operator.
MERGEPAINT Merges the colors of the inverted source rectangle with the colors of the destination rectangle by using the Boolean OR operator.
NOTSRCCOPY Copies the inverted source rectangle to the destination.
NOTSRCERASE Combines the colors of the source and destination rectangles by using the Boolean OR operator and then inverts the resultant color.
PATCOPY Copies the specified pattern into the destination bitmap.
PATINVERT Combines the colors of the specified pattern with the colors of the destination rectangle by using the Boolean XOR operator.
PATPAINT Combines the colors of the pattern with the colors of the inverted source rectangle by using the Boolean OR operator. The result of this operation is combined with the colors of the destination rectangle by using the Boolean OR operator.
SRCAND Combines the colors of the source and destination rectangles by using the Boolean AND operator.
SRCCOPY Copies the source rectangle directly to the destination rectangle.
SRCERASE Combines the inverted colors of the destination rectangle with the colors of the source rectangle by using the Boolean AND operator.
SRCINVERT Combines the colors of the source and destination rectangles by using the Boolean XOR operator.
SRCPAINT Combines the colors of the source and destination rectangles by using the Boolean OR operator.
WHITENESS Fills the destination rectangle using the color associated with index 1 in the physical palette. (This color is white for the default physical palette.)
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Windows NT: To get extended error information, callGetLastError.
Remarks
If a rotation or shear transformation is in effect in the source device context, BitBlt returns an error. If other transformations exist in the source device context (and a matching transformation is not in effect in the destination device context), the rectangle in the destination device context is stretched, compressed, or rotated as necessary.
If the color formats of the source and destination device contexts do not match, the BitBlt function converts the source color format to match the destination format.
When an enhanced metafile is being recorded, an error occurs if the source device context identifies an enhanced-metafile device context.
Not all devices support the BitBlt function. For more information, see the RC_BITBLT raster capability entry in the GetDeviceCaps function as well as the following functions: MaskBlt, PlgBlt, StretchBlt.
BitBlt returns an error if the source and destination device contexts represent different devices.
ICM: No color management is performed when blits occur.
Windows CE: In Windows CE version 1.0, the dwRop parameter can only be assigned the following values:
SRCCOPY
SRCAND
SRCPAINT
SRCINVERT
In Windows CE version 2.0, the dwRop parameter can be any ROP3.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in wingdi.h.
Import Library: Use gdi32.lib.
See Also
Bitmaps Overview, Bitmap Functions