如何直接读写显存
directdraw直接读写显存的方法
#include "stdafx.h"
#include <ddraw.h>
#include <iostream>
#include <afxwin.h>//ÒòΪҪÓõ½CStringËùÒÔÒª°üº¬Õâ¸öÍ·Îļþ
#include "winsock2.h "
using namespace std;
LPDIRECTDRAW lpDD;
LPDIRECTDRAWSURFACE lpDDSPrimary;
LPDIRECTDRAWSURFACE lpDDSBack;
int main(int argc, char* argv[])
{
DDSURFACEDESC t;
HWND desktop;
desktop = GetDesktopWindow();
HDC hDC = ::GetDC(desktop);
DDSURFACEDESC ddsd;
DDSCAPS ddscaps;
HRESULT ddrval;
BYTE *Bitmap;
ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
if( ddrval != DD_OK )
{
printf("error/n");
}
ddrval = lpDD->SetCooperativeLevel(desktop, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if( ddrval != DD_OK )
{
lpDD->Release();
printf("error/n");
}
ddrval = lpDD->SetDisplayMode( 1280, 800, 32);
if( ddrval != DD_OK )
{
lpDD->Release();
printf("error/n");
}
memset( &ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
if( ddrval != DD_OK )
{
lpDD->Release();
printf("error/n");
}
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps, &lpDDSBack);
if( ddrval != DD_OK )
{
lpDDSPrimary->Release();
lpDD->Release();
printf("error/n");
}
memset(&ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddrval = lpDDSPrimary->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
Bitmap = (BYTE*)ddsd.lpSurface;
while(1){
int pos;
for (int y=0;y<480; y++)
{
pos=y*ddsd.lPitch;
for (int x=0; x<640; x++)
{
Bitmap[pos] =25; //R
pos++;
Bitmap[pos] =25; //G
pos++;
Bitmap[pos] =5; //B
pos+=2;
}
}
lpDDSPrimary->Unlock(&ddsd);//unlock
}
//printf("%d/n", ddsd.dwWidth);
return 0;
}
这是俺调试成功的代码,通过查阅大量不成体系的资料,最后终于可以随心所欲的往显存里面写东西了,当然现在这个代码的含义我也不清楚,但达到目的就可以了,我的笔记本式1280*800,32位色,执行此代码会在480*640的区域出现一块颜色,话不多说,有兴趣可以试试哦!
发表于 @ 2010年06月27日