directdraw:动画程序编写——DirectDraw的旅(3)



下面我们开始分析源: 下载源 (编辑者:链接丢失)

属于Win32基本框架我们用蓝色标出而用红色表出是我们要重点学习下面所有都是 FullScreenMode.cpp 文件中内容其中“IDB_DIRECTX”和“IDB_WinXP”都是图片资源ID号我想如何向中添加资源应该不用我多说了吧:)

工程文件:FullScreenMode.cpp

# STRICT
# <windows.h>
# <ddraw.h>
# <mmsystem.h>
# \"resource.h\"
# \"ddutil.h\"

//定义删除指针和释放对象

# SAFE_DELETE(p)
{
  (p)
  {
  delete (p);
  (p)=NULL;
  }
}

# SAFE_RELEASE(p)
{
  (p)
  {
   (p)->Release;
   (p)=NULL;
  }
}

# SCREEN_WIDTH 1024 //定义屏幕宽度

# SCREEN_HEIGHT 768 //定义屏幕高度

# SCREEN_BPP 8 //定义调色板位数

# SPRITE_DIAMETER 32 //定义飘动子画面 直径(宽度和高度样)

# NUM_SPRITES 10 //定义飘动子画面 个数

# HELPTEXT TEXT(\"Press Escape to quit.\") //定义文本住表面



上面出现这个 TEXT( ) 是个系统头文件里定义起作用是检查中是否定义了 Unicode ,如果有就将括号中文本转化成宽自负如果没有则转化成ASCLL 码以下定义是子画面飘动时属性:

struct SPRITE_STRUCT

{
  FLOAT fPosX; //sprite当前坐标x值,如果在化时,即为坐标x值
  FLOAT fPosY; //sprite当前坐标y值,如果在化时,即为坐标y值
  FLOAT fVelX; //sprite在x轴上速度
  FLOAT fVelY; //sprite在y轴上速度
};

CDisplay* g_pDisplay = NULL; /*CDisplay就是ddutil.h(我们又新加入目录中)中定义用于处理表面的间拷贝翻页等操作再次定义个全局变量用于以后对指向表面的间进行操作*/

CSurface* g_pBackSurface = NULL; /* CSurface也是ddutil.h头文件中定义用于对表面本身进行操作如设置色彩键码在此定义是背景图画指针*/

CSurface* g_pLogoSurface = NULL; /*子画面图画指针*/

CSurface* g_pTextSurface = NULL; /*背景文本指针*/

BOOL g_bActive = FALSE; /*定义个bool形变量起到个开关作用流程进行控制*/

DWORD g_dwLastTick; /*用于记录最后系统时间*/

SPRITE_STRUCT g_Sprite[NUM_SPRITES]; /*定义多个子画面其中包括多个 运动时属性*/



Function-prototypes

LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );//主窗口消息处理

HRESULT WinInit( HINSTANCE hInst, nCmdShow, HWND* phWnd, HACCEL* phAccel );//窗口类设置注册并创建窗口



HRESULT InitDirectDraw( HWND hWnd );

VOID FreeDirectDraw;

HRESULT ProcessNextFrame;

VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta );

HRESULT DisplayFrame;

HRESULT RestoreSurfaces;



WinMain

Desc: Entry po to the program. Initializes everything and calls

UpdateFrame when idle from the message pump.

APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, nCmdShow )
{
  MSG msg;
 
  HWND hWnd;
 
  HACCEL hAccel;

  ZeroMemory( &g_Sprite, (SPRITE_STRUCT) * NUM_SPRITES );//清空内存
 
  srand( GetTickCount /*用于获取自windows启动以来经历时间长度(毫秒)*/ ); //设置随机数随时间变化而变化

  ( FAILED( WinInit( hInst, nCmdShow, &hWnd, &hAccel ) ) )
   FALSE;

  ( FAILED( InitDirectDraw( hWnd ) ) )
  {
   MessageBox( hWnd, TEXT(\"DirectDraw init failed. \")
  
   TEXT(\"The sample will now exit. \"), TEXT(\"DirectDraw Sample\"),
  
   MB_ICONERROR | MB_OK );

   FALSE;
  }

  g_dwLastTick = timeGetTime;//获得当前系统时间

  while( TRUE )
  {

// Look for messages, none are found then
// update the state and display it

   ( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
   {
    ( 0 GetMessage(&msg, NULL, 0, 0 ) )
    {

// WM_QUIT was posted, so exit

     ()msg.wParam;
    }

// Translate and dispatch the message

    ( 0 TranslateAccelerator( hWnd, hAccel, &msg ) )
    {
     TranslateMessage( &msg );
     DispatchMessage( &msg );
    }
   }
  
   {
    ( g_bActive )
    {

// Move the sprites, blt them to the back buffer, then
// flip or blt the back buffer to the primary buffer

    ( FAILED( ProcessNextFrame ) /*设置下张页面子画面位置*/)
     {
      SAFE_DELETE( g_pDisplay );
     
      MessageBox( hWnd, TEXT(\"Displaying the next frame failed. \")
     
      TEXT(\"The sample will now exit. \"), TEXT(\"DirectDraw Sample\"),

      MB_ICONERROR | MB_OK );
     
      FALSE;
     }
    }
   
    {

// Make sure we go to sleep we have nothing to do

     WaitMessage;

// Ignore time spent inactive

     g_dwLastTick = timeGetTime;//获得当前系统时间
    }
   }
  }
}



WinInit

Desc: Init the window

HRESULT WinInit( HINSTANCE hInst, nCmdShow, HWND* phWnd, HACCEL* phAccel )
{
  WNDCLASS wc;
  HWND hWnd;
  HACCEL hAccel;

// Register the Window Class 设置窗口类

  wc.lpszClassName = TEXT(\"FullScreenMode\");

  wc.lpfnWndProc = MainWndProc;

  wc.style = CS_VREDRAW | CS_HREDRAW;

  wc.hInstance = hInst;

  wc.hIcon = LoadIcon( hInst, MAKEINTRESOURCE(IDI_MAIN) );

  wc.hCursor = LoadCursor( NULL, IDC_ARROW );

  wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);

  wc.lpszMenuName = NULL;

  wc.cbClsExtra = 0;

  wc.cbWndExtra = 0;

  ( RegisterClass( &wc ) 0 /*注册窗口类*/ )
   E_FAIL;



// Load keyboard accelerators

  hAccel = LoadAccelerators( hInst, MAKEINTRESOURCE(IDR_MAIN_ACCEL) );

// Create and show the window

  hWnd = CreateWindowEx( 0, TEXT(\"FullScreenMode\"), TEXT(\"DirectDraw FullScreenMode Sample\"),

  WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,

  CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL );

  ( hWnd NULL )
   E_FAIL;

  ShowWindow( hWnd, nCmdShow );

  UpdateWindow( hWnd );

  *phWnd = hWnd;

  *phAccel = hAccel;

  S_OK;
}



InitDirectDraw

Desc: Create the DirectDraw object, and init the surfaces

HRESULT InitDirectDraw( HWND hWnd )
{
  HRESULT hr; //接受返回值其实是long型变量

  LPDIRECTDRAWPALETTE pDDPal = NULL; //定义调色板

  iSprite; //定义和sprite个数有关计数器

  g_pDisplay = CDisplay;//动态开辟个CDisplay类

  ( FAILED( hr = g_pDisplay->CreateFullScreenDisplay( hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP ) ) ) //设置为全屏并且g_pDisplay就为后备缓冲区表面句柄即指向后备缓冲区表面指针
  {
   MessageBox( hWnd, TEXT(\"This display card does not support 1024x768x8. \"),
  
   TEXT(\"DirectDraw Sample\"), MB_ICONERROR | MB_OK );
  
   hr;
  }

// Create and the palette when in palettized color

  ( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( IDB_DIRECTX ) ) ) ) //顾名思义就是从bmp图片中获得调色板值并赋值在pDDPal结构指针中
   hr;
 
  ( FAILED( hr = g_pDisplay->SetPalette( pDDPal ) ) ) //用刚才从IDB_DIRECTX中获得调色板制来设置调色板
   hr;

  SAFE_RELEASE( pDDPal );//释放指针

// Create a surface, and draw a bitmap resource _disibledevent=>
 
  ( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pLogoSurface, MAKEINTRESOURCE( IDB_DIRECTX ), SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
   hr;

  ( FAILED( hr = g_pDisplay->CreateSurfaceFromBitmap( &g_pBackSurface, MAKEINTRESOURCE( IDB_WINXP ), SCREEN_WIDTH, SCREEN_HEIGHT ) ) )
   hr;

// Create a surface, and draw text to it.
//创建文本表面

  ( FAILED( hr = g_pDisplay->CreateSurfaceFromText( &g_pTextSurface, NULL, HELPTEXT, RGB(0,0,0), RGB(255, 255, 0) ) ) )
   hr;

// Set the color key for the logo sprite to black
//设置色彩键码为黑色0代表黑色这样在表面拷贝过程中黑色像素点将不会被拷贝

  ( FAILED( hr = g_pLogoSurface->SetColorKey( 0 ) ) )
   hr;

  ( FAILED( hr = g_pTextSurface->SetColorKey( 0 ) ) )
   hr;

// Init all the sprites. All of these sprites look the same,
// using the g_pDDSLogo surface.

  for( iSprite = 0; iSprite < NUM_SPRITES; iSprite )
  {

// Set the sprite\'s position and velocity
// 设置这些 sprite(小怪物)坐标随机产生
 
   g_Sprite[iSprite].fPosX = (float) (rand % SCREEN_WIDTH);
   g_Sprite[iSprite].fPosY = (float) (rand % SCREEN_HEIGHT);



// 速度也随机产生

   g_Sprite[iSprite].fVelX = 500.0f * rand / RAND_MAX - 250.0f;
   g_Sprite[iSprite].fVelY = 500.0f * rand / RAND_MAX - 250.0f;
  }
  S_OK;
}



FreeDirectDraw

Release all the DirectDraw objects 释放所有指针

VOID FreeDirectDraw
{
  SAFE_DELETE( g_pBackSurface );
  SAFE_DELETE( g_pLogoSurface );
  SAFE_DELETE( g_pTextSurface );
  SAFE_DELETE( g_pDisplay );
}



MainWndProc

Desc: The window procedure

LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
  switch (msg)
  {
   WM_COMMAND:
   switch( LOWORD(wParam) )
   {
    IDM_EXIT:

// Received key/menu command to exit app
 
    PostMessage( hWnd, WM_CLOSE, 0, 0 );
 
    0L;
   }
   ; // Continue with default processing

   WM_SETCURSOR:

// Hide the cursor in fullscreen
 
   SetCursor( NULL );

   TRUE;

   WM_SIZE:

// Check to see we are losing our window...

   ( SIZE_MAXHIDEwParam || SIZE_MINIMIZEDwParam )
    g_bActive = FALSE;
  
    g_bActive = TRUE;
  
   ( g_pDisplay )
    g_pDisplay->UpdateBounds;

  

   WM_EXITMENULOOP:

// Ignore time spent in menu

   g_dwLastTick = timeGetTime;

   ;

   WM_EXITSIZEMOVE:

// Ignore time spent resizing

   g_dwLastTick = timeGetTime;

   ;

   WM_MOVE:

   ( g_pDisplay )
    g_pDisplay->UpdateBounds;

   ;

   WM_SYSCOMMAND:

// Prevent moving/sizing and power loss in fullscreen mode

   switch( wParam )
   {
    SC_MOVE:

    SC_SIZE:

    SC_MAXIMIZE:

    SC_MONITORPOWER:

    TRUE;
   }

   ;

   WM_DESTROY:

// Cleanup and close the app

   FreeDirectDraw;

   PostQuitMessage( 0 );

   0L;
  }
 
  DefWindowProc(hWnd, msg, wParam, lParam);
}



ProcessNextFrame

Desc: Move the sprites, blt them to the back buffer, then

flips the back buffer to the primary buffer

HRESULT ProcessNextFrame
{
  HRESULT hr;

// Figure how much time has passed since the last time

  DWORD dwCurrTick = timeGetTime; //get current time 获得当前时间
  DWORD dwTickDf = dwCurrTick - g_dwLastTick; //the dference between current-time (dwCurrTick) and last time (g_dwLastTick)

//计算当前时间 (dwCurrTick) 和最后次时间 (g_dwLastTick) 差值
// Don\'t update no time has passed

  ( dwTickDf 0 ) //如果时间差值为0,即时间没有变化,则不更新屏幕,而返回
   S_OK;

//如果运行到这里,而没有在前面返回,则介绍说明时间有变化,则下面将要更新屏幕



  g_dwLastTick = dwCurrTick; //使最后时间=当前时间

// Move the sprites according to how much time has passed
//根据时间变化来移动子画面,即移动sprites

  for( iSprite = 0; iSprite < NUM_SPRITES; iSprite )
   UpdateSprite( &g_Sprite[ iSprite ], dwTickDf / 1000.0f );

// Display the sprites _disibledevent=>

  ( FAILED( hr = DisplayFrame ) )
  {
   ( hr != DDERR_SURFACELOST )
    hr;

// The surfaces were lost so restore them

   RestoreSurfaces;
  }

  S_OK;
}



UpdateSprite

Desc: Move the sprite around and make it bounce based _disibledevent=> VOID UpdateSprite( SPRITE_STRUCT* pSprite, FLOAT fTimeDelta )
{

// Update the sprite position

  pSprite->fPosX pSprite->fVelX * fTimeDelta;
  pSprite->fPosY pSprite->fVelY * fTimeDelta;

// Clip the position, and bounce it hits the edge

  ( pSprite->fPosX < 0.0f )
  {
   pSprite->fPosX = 0;
   pSprite->fVelX = -pSprite->fVelX;
  }
 
  ( pSprite->fPosX >= SCREEN_WIDTH - SPRITE_DIAMETER )
  {
   pSprite->fPosX = SCREEN_WIDTH - 1 - SPRITE_DIAMETER;
   pSprite->fVelX = -pSprite->fVelX;
  }
 
  ( pSprite->fPosY < 0 )
  {
   pSprite->fPosY = 0;
   pSprite->fVelY = -pSprite->fVelY;
  }

  ( pSprite->fPosY > SCREEN_HEIGHT - SPRITE_DIAMETER )
  {
   pSprite->fPosY = SCREEN_HEIGHT - 1 - SPRITE_DIAMETER;
   pSprite->fVelY = -pSprite->fVelY;
  }
}



DisplayFrame

Desc: Blts a the sprites to the back buffer, then flips the back buffer _disibledevent=> HRESULT DisplayFrame
{
  HRESULT hr;

// Fill the back buffer with black, ignoring errors until the flip

  g_pDisplay->Clear( 0 ); //清空后备缓冲区表面

// Blt the help text _disibledevent=>

  g_pDisplay->Blt( 0, 0, g_pBackSurface, NULL );

//将g_pTextSurface所指向文本拷贝到后备缓冲区表面

  g_pDisplay->Blt( 10, 10, g_pTextSurface, NULL );

// Blt all the sprites _disibledevent=>

  for( iSprite = 0; iSprite < NUM_SPRITES; iSprite )
  {
   g_pDisplay->Blt( (DWORD)g_Sprite[iSprite].fPosX,(DWORD)g_Sprite[iSprite].fPosY,g_pLogoSurface, NULL );
  }

// We are in fullscreen mode, so perform a flip and
// any errors like DDERR_SURFACELOST
//最关键地方在这里请看下面语句只要我们执行翻页操作就可以将改动了图像了显示在屏幕上了

  ( FAILED( hr = g_pDisplay->Present /*翻页操作*/) )
   hr;

  S_OK;
}



RestoreSurfaces

Desc: Restore all the surfaces, and redraw the sprite surfaces.



HRESULT RestoreSurfaces
{
  HRESULT hr;

  LPDIRECTDRAWPALETTE pDDPal = NULL;

  ( FAILED( hr = g_pDisplay->GetDirectDraw->RestoreAllSurfaces ) )
   hr;

// No need to re-create the surface, just re-draw it.

  ( FAILED( hr = g_pTextSurface->DrawText( NULL, HELPTEXT, 0, 0, RGB(0,0,0), RGB(255, 255, 0) ) ) )
   hr;

// We need to release and re-load, and the palette again to
// redraw the bitmap _disibledevent=>

  ( FAILED( hr = g_pDisplay->CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( IDB_DIRECTX ) ) ) )
   hr;

  ( FAILED( hr = g_pDisplay->SetPalette( pDDPal ) ) )
   hr;

  SAFE_RELEASE( pDDPal );

// No need to re-create the surface, just re-draw it.

  ( FAILED( hr = g_pLogoSurface->DrawBitmap( MAKEINTRESOURCE( IDB_DIRECTX ),SPRITE_DIAMETER, SPRITE_DIAMETER ) ) )
   hr;
  S_OK;
}



好了我们分析就到这了我想您现在应该大概了解了DirectDraw大概流程了吧就是先创建DDraw对象然后进行相关设置然后绘制后备缓冲区页然后执行翻页操作这样循环就会产生很好动画效果了其实用DirectDraw编程很简单说白了其实就是:“几个表面的间拷来拷去”只不过这其间可是大有文章可作哟!



Tags:  directdrawerror directdraw不可用 directdraw加速 directdraw

延伸阅读

最新评论

发表评论