#define INITGUID // make sure directX guids are included
#include <windows.h>   // include important windows stuff
#include <windowsx.h> 
#include <mmsystem.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <ddraw.h> // include directdraw
#define WINDOW_CLASS_NAME "WINCLASS1"
#define SCREEN_WIDTH    640  // size of screen
#define SCREEN_HEIGHT   480
#define SCREEN_BPP      8    // bits per pixel
#define MAX_COLORS      256  // maximum colorstypedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char  UCHAR;
typedef unsigned char  BYTE;#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)#define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))#define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
ddstruct.dwSize=sizeof(ddstruct); }HWND      main_window_handle = NULL; // globally track main window
HINSTANCE hinstance_app      = NULL; // globally track hinstanceLPDIRECTDRAW7         lpdd         = NULL;   // dd object
LPDIRECTDRAWSURFACE7  lpddsprimary = NULL;   // dd primary surface
LPDIRECTDRAWSURFACE7  lpddsback    = NULL;   // dd back surface
LPDIRECTDRAWPALETTE   lpddpal      = NULL;   // a pointer to the created dd palette
LPDIRECTDRAWCLIPPER   lpddclipper  = NULL;   // dd clipper
PALETTEENTRY          palette[256];          // color palette
PALETTEENTRY          save_palette[256];     // used to save palettes
DDSURFACEDESC2        ddsd;                  // a direct draw surface description struct
DDBLTFX               ddbltfx;               // used to fill
DDSCAPS2              ddscaps;               // a direct draw surface capabilities struct
HRESULT               ddrval;                // result back from dd calls
DWORD                 start_clock_count = 0; // used for timingint min_clip_x = 0,                          // clipping rectangle 
    max_clip_x = SCREEN_WIDTH-1,
    min_clip_y = 0,
    max_clip_y = SCREEN_HEIGHT-1;int screen_width  = SCREEN_WIDTH,            // width of screen
    screen_height = SCREEN_HEIGHT,           // height of screen
    screen_bpp    = SCREEN_BPP;              // bits per pixel
char buffer[80];                     // general printing bufferLRESULT CALLBACK WindowProc(HWND hwnd, 
    UINT msg, 
                            WPARAM wparam, 
                            LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
char buffer[80];        // used to print stringsswitch(msg)
{
case WM_CREATE: 
        {
// do initialization stuff here
        // return success
return(0);
} break;
   
case WM_PAINT: 
{
// simply validate the window 
        hdc = BeginPaint(hwnd,&ps);  
        
        // end painting
        EndPaint(hwnd,&ps);        // return success
return(0);
    } break; case WM_DESTROY: 
{ // kill the application, this sends a WM_QUIT message 
PostQuitMessage(0);        // return success
return(0);
} break; default:break;    } // end switch// process any messages that we didn't take care of 
return (DefWindowProc(hwnd, msg, wparam, lparam));} // end WinProc
inline void Plot_Pixel_Faster16(int x,int y,
int red,int green,int blue,
UCHAR *video_buffer,int lpitch16)
{

USHORT pixel = _RGB16BIT565(red,green,blue);
video_buffer[x+y*lpitch16] = pixel;

}///////////////////////////////////////////////////////////int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is the main loop of the game, do all your processing
// here int lpitch16 = (ddsd.lPitch >> 1);

// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE))
   SendMessage(main_window_handle,WM_CLOSE,0,0);// return success or failure or your own return code here
//memset(&ddsd,0,sizeof(ddsd));
//ddsd.dwSize = sizeof(ddsd);
if(FAILED(lpddsprimary->Lock(NULL,&ddsd,
DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,
NULL)))
{
return(0); 
}//int mempitch =  (int)ddsd.lPitch;
UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;for(int index=0; index < 1000; index++)
{
// UCHAR color = rand()%256;
int red = rand()%256;
int green = rand()%256;
int blue = rand()%256;
int x = rand()%640;
int y = rand()%480; Plot_Pixel_Faster16(x,y,red,green,blue,video_buffer,lpitch16);}

解决方案 »

  1.   

    if(FAILED(lpddsprimary->Unlock(NULL)))
    return 0;Sleep(50);
    return(1);} // end Game_Main////////////////////////////////////////////////////////////
    int Game_Init(void *parms = NULL, int num_parms = 0)
    {
    if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
       return(0);if(FAILED(lpdd->SetCooperativeLevel(main_window_handle, DDSCL_NORMAL)))
       return(0);if(FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH,
    SCREEN_HEIGHT,SCREEN_BPP,0,0)))
    {
    return(0);
    }if(FAILED(lpdd->SetCooperativeLevel(main_window_handle,
    DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX |
    DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
    {
    return(0);
    }memset(&ddsd,0,sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);ddsd.dwFlags = DDSD_CAPS;ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;if(FAILED(lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)))
    {     
    return(0);
    }for(int color=1; color <255; color++)
    {
    palette[color].peRed = rand()%256;
    palette[color].peGreen = rand()%256;
    palette[color].peBlue = rand()%256;
    palette[color].peFlags = PC_NOCOLLAPSE;
    } palette[0].peRed = 0;
    palette[0].peBlue = 0;
    palette[0].peBlue = 0;
    palette[0].peFlags = PC_NOCOLLAPSE;palette[255].peBlue = 255;
    palette[255].peGreen = 255;
    palette[255].peRed = 255;
    palette[255].peFlags = PC_NOCOLLAPSE;if(FAILED(lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 |
    DDPCAPS_INITIALIZE,
    palette,&lpddpal,NULL))){
    return(0);}if(FAILED(lpddsprimary->SetPalette(lpddpal)))
    {
    return(0);
    }
    return(1);
    } // end Game_Init/////////////////////////////////////////////////////////////int Game_Shutdown(void *parms = NULL, int num_parms = 0)
    {if (lpdd)
       {
       lpdd->Release();
       lpdd = NULL;
       } // end if
    if(lpddsprimary)
    {
    lpddsprimary->Release();
    lpddsprimary = NULL;}
    if(lpddpal)
    {
    lpddpal->Release();
    lpddpal = NULL;
    }return(1);} // end Game_Shutdownint WINAPI WinMain( HINSTANCE hinstance,
    HINSTANCE hprevinstance,
    LPSTR lpcmdline,
    int ncmdshow)
    {WNDCLASSEX winclass; // this will hold the class we create
    HWND    hwnd;  // generic window handle
    MSG    msg;  // generic message
    HDC        hdc;      // graphics device contextwinclass.cbSize         = sizeof(WNDCLASSEX);
    winclass.style = CS_DBLCLKS | CS_OWNDC | 
                              CS_HREDRAW | CS_VREDRAW;
    winclass.lpfnWndProc = WindowProc;
    winclass.cbClsExtra = 0;
    winclass.cbWndExtra = 0;
    winclass.hInstance = hinstance;
    winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor = LoadCursor(NULL, IDC_ARROW); 
    winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    winclass.lpszMenuName = NULL;
    winclass.lpszClassName = WINDOW_CLASS_NAME;
    winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);hinstance_app = hinstance;if (!RegisterClassEx(&winclass))
    return(0);
    if (!(hwnd = CreateWindowEx(NULL,                  // extended style
                                WINDOW_CLASS_NAME,     // class
        "DirectDraw Initialization Demo", // title
        WS_POPUP |WS_VISIBLE,
          0,0,   // initial x,y
        400,300,  // initial width, height
        NULL,   // handle to parent 
        NULL,   // handle to menu
        hinstance,// instance of this application
        NULL))) // extra creation parms
    return(0);main_window_handle = hwnd;Game_Init();while(TRUE)
    {
        
    if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
       {
           if (msg.message == WM_QUIT)
               break;

      
       TranslateMessage(&msg);   
       DispatchMessage(&msg);
       } // end if
        
           
           Game_Main();
           
    } // end whileGame_Shutdown();return(msg.wParam);} 
    请问为什么屏幕只有一半在变化色彩,我的目的是让屏幕全部变化色彩
      

  2.   

    函数Game_Main中第一句,int lpitch16 = (ddsd.lPitch >> 1);
    这里lpitch16=ddsd.lPitch/2;
      

  3.   

    类型搞错了,video自动被截取了,应该是Plot_Pixel_Faster16(x,y,red,green,blue,(USHORT *)video_buffer,lpitch16);