⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.bak

📁 2D即时战略游戏源码.仿红色警戒之类的。不过有点粗糙
💻 BAK
📖 第 1 页 / 共 5 页
字号:
#include "havoc.h"
#include"draw.h"
extern "C"{
#include"lobby.h"
#include"comm.h"
#include"input.h"
//

}

#include"wizard.h"
#define FLOORNUM  3
#define BUILDNUM  1
#define MYARMYNUM 1
#define ENEMYNUM  1
#define EXPNUM    1
#define OTHERSNUM 1
#define BULLETNUM  1
#define TIMER_ID        1
#define TIMER_RATE      50

void DoSystemMessage( LPDPMSG_GENERIC lpMsg, DWORD dwMsgSize, DPID idFrom, DPID idTo );
void DoApplicationMessage( LPGENERICMSG lpMsg, DWORD dwMsgSize, DPID idFrom, DPID idTo );
void SendGameMessage( LPGENERICMSG lpMsg, DPID idTo );
HRESULT ReceiveMessages( void );

HANDLE                  ghThread;               // handle to wizard thread
HINSTANCE                ghinst;             // program instance
HWND                     ghWndMain;          // main window
DWORD                    gdwKeys;            // gameplay keys

DWORD gwKeys=0;
BOOL gbIsActive=FALSE; 
BOOL bMud=FALSE;

GUID				Guid;
LPVOID				pv;
LPVOID                          glpvReceiveBuffer = NULL;   // buffer to store received messages
DWORD                           gdwReceiveBufferSize = 0;   // size of buffer


LPDIRECTDRAWSURFACE     lpGround; 
LPDIRECTDRAWSURFACE     lpShape; 


LPDIRECTDRAWSURFACE     Floor[FLOORNUM];      // DirectDraw 
LPDIRECTDRAWSURFACE     Build[BUILDNUM];       //offscreen 
LPDIRECTDRAWSURFACE     Myarmy[MYARMYNUM];       
LPDIRECTDRAWSURFACE     Enemy[ENEMYNUM]; 
LPDIRECTDRAWSURFACE     PicExp[EXPNUM]; 
LPDIRECTDRAWSURFACE     Others[OTHERSNUM]; //others
LPDIRECTDRAWSURFACE     PicRect;
LPDIRECTDRAWSURFACE     PicShape;
LPDIRECTDRAWSURFACE     PicBullet[BULLETNUM];
LPDIRECTDRAWSURFACE     RadarBack;//Buffer for Radar
LPDIRECTDRAWSURFACE     lpDDPP;//Operative Plan and Radar surface;
LPDIRECTDRAWSURFACE     PicBlack;
LPDIRECTDRAWSURFACE     PicWhite;
                                                  

LPDIRECTDRAWPALETTE     lpDDPal;        // DirectDraw palette
BOOL                    bActive;        // is application active?








/*
 * This function is called if the initialization function fails
 */
extern BOOL initFail(LPCTSTR msg )
{
    finiObjects();
    MessageBox(hWindow, msg,"Shang Gan Ling ", MB_OK );
    DestroyWindow( hWindow );
    return FALSE;

} /* initFail */


static void finiObjects( void )
{int i;
    if( lpDD != NULL )
    {    if( lpDDSBack != NULL )
        {
            lpDDSBack->Release();
            lpDDSBack = NULL;
        }
        if( lpDDSPrimary != NULL )
        {
            lpDDSPrimary->Release();
            lpDDSPrimary = NULL;
        }

		
		for(i=0;i<FLOORNUM;i++)
        { if(Floor[i] != NULL )
          {
            Floor[i]->Release();
            Floor[i] = NULL;
          }
		}
        for(i=0;i<BUILDNUM;i++)
        { if(Build[i] != NULL )
          {
            Build[i]->Release();
            Build[i] = NULL;
          }
		}
       	for(i=0;i<MYARMYNUM;i++)
        { if(Myarmy[i] != NULL )
          {
            Myarmy[i]->Release();
            Myarmy[i] = NULL;
          }
		}
       	for(i=0;i<ENEMYNUM;i++)
        { if(Enemy[i] != NULL )
          {
            Enemy[i]->Release();
            Enemy[i] = NULL;
          }
		}
       	for(i=0;i<EXPNUM;i++)
        { if(PicExp[i] != NULL )
          {
            PicExp[i]->Release();
            PicExp[i] = NULL;
          }
		}

       	for(i=0;i<OTHERSNUM;i++)
        { if(Others[i] != NULL )
          {
            Others[i]->Release();
            Others[i] = NULL;
          }
		}

       	for(i=0;i<BULLETNUM;i++)
        { if(PicBullet[i] != NULL )
          {
            PicBullet[i]->Release();
            PicBullet[i] = NULL;
          }
		}

       if(lpGround!=NULL) 
	   {lpGround->Release();
        lpGround=NULL;
	   }

	   if(PicRect!=NULL)
	   {PicRect->Release();
	   PicRect=NULL;
	   }

	   if(PicShape!=NULL)
	   {PicShape->Release();
	    PicShape=NULL;
	   }

	   if(PicBlack!=NULL)
	   {PicBlack->Release();
	    PicBlack=NULL;
	   }
	   if(PicWhite!=NULL)
	   {PicWhite->Release();
	    PicWhite=NULL;
	   }
       
        lpDD->Release();
        lpDD = NULL;
    }
} /* finiObjects */




 void EndPlay()
 {}

void Flip(void)
{ HRESULT             ddrval;
   // Flip the surfaces
    while( 1 )
    {   
        ddrval = lpDDSPrimary->Flip( NULL, 0 );
	 
		
        if( ddrval == DD_OK )
        {
            break;
        }
        if( ddrval == DDERR_SURFACELOST )
        {
            ddrval = restoreAll();
            if( ddrval != DD_OK )
            {
                break;
            }
        }
        if( ddrval != DDERR_WASSTILLDRAWING )
        {
            break;
        }
    }          
}

void BltSplash(void)
{char File[32];
 LPDIRECTDRAWSURFACE  lpSplash;
	sprintf(File,"bmp\\splash%d.bmp",0);
    	
  lpSplash=DDLoadBitmap(lpDD,File,0,0);
  //DDSetColorKey(Floor[i],CLR_INVALID);
  if(lpSplash==NULL)
     initFail(NULL);
  else {JFBlt(lpDDSBack, 0, 0, 640,480,lpSplash,0,0,
        DDBLTFAST_NOCOLORKEY );
    	 Flip();
       if(lpSplash!=NULL)
	    {lpSplash->Release();
	     lpSplash=NULL;
	     }

		}
}

int ProcessKeys(void)
{switch(gwKeys )
{case VK_F5: 
	if(ProgramState==GETSELECT)
	    return ALONE;
	break;
  
case VK_F6:	
 if(ProgramState==GETSELECT)
	    return MUD;
 break;
}
return ProgramState;

}
void InitAloneGame(void)
{InitGame();
}



/*
 * ReceiveGameMessages
 *
 * Checks if there are any messages for us and receives them
 */
HRESULT ReceiveMessages( void )
{
    DPID                idFrom, idTo;
    LPVOID              lpvMsgBuffer;
    DWORD               dwMsgBufferSize;
    HRESULT             hr;

    // read all messages in queue
    dwMsgBufferSize = gdwReceiveBufferSize;
    lpvMsgBuffer = glpvReceiveBuffer;
    
    while (TRUE)
    {
        // see what's out there
        idFrom = 0;
        idTo = 0;

        hr = DPlayReceive(&idFrom, &idTo, DPRECEIVE_ALL, lpvMsgBuffer, &dwMsgBufferSize);
        if (hr == DPERR_BUFFERTOOSMALL)
        {
            if (lpvMsgBuffer == NULL)
            {
                lpvMsgBuffer = GlobalAllocPtr(GHND, dwMsgBufferSize);
                if (lpvMsgBuffer == NULL)
                    return (DPERR_NOMEMORY);
                glpvReceiveBuffer = lpvMsgBuffer;
                gdwReceiveBufferSize = dwMsgBufferSize;
            }
            else if (dwMsgBufferSize > gdwReceiveBufferSize)
            {
                lpvMsgBuffer = GlobalReAllocPtr(lpvMsgBuffer, dwMsgBufferSize, 0);
                if (lpvMsgBuffer == NULL)
                    return (DPERR_NOMEMORY);
                glpvReceiveBuffer = lpvMsgBuffer;
                gdwReceiveBufferSize = dwMsgBufferSize;
            }
        }
        else if ((hr == DP_OK) && 
                 ((dwMsgBufferSize >= sizeof(GENERICMSG)) || 
                  (dwMsgBufferSize >= sizeof(DPMSG_GENERIC))))
        {
            if (idFrom == DPID_SYSMSG)
            {
                DoSystemMessage((LPDPMSG_GENERIC) lpvMsgBuffer, dwMsgBufferSize, idFrom, idTo);
            }
            else
            {
                DoApplicationMessage((LPGENERICMSG) lpvMsgBuffer, dwMsgBufferSize, idFrom, idTo);
            }
        }
        else
            break;
    };

    return hr;
}

/*
 * DoSystemMessage
 *
 * Evaluates system messages and performs appropriate actions
 */
void DoSystemMessage( LPDPMSG_GENERIC lpMsg, DWORD dwMsgSize, DPID idFrom, DPID idTo )
{
    switch( lpMsg->dwType)
    {
    case DPSYS_CREATEPLAYERORGROUP:
        {
            LPDPMSG_CREATEPLAYERORGROUP lpAddMsg = (LPDPMSG_CREATEPLAYERORGROUP) lpMsg;

            if( gbIsHost)
            {
                gHostMsg.GameType = gGameType;
                SendGameMessage((LPGENERICMSG) &gHostMsg, lpAddMsg->dpId);
            }
        }
        break;

    case DPSYS_DESTROYPLAYERORGROUP:
        {
           // LPSHIP lpShip;
           LPDPMSG_DESTROYPLAYERORGROUP lpDestroyMsg = (LPDPMSG_DESTROYPLAYERORGROUP) lpMsg;

           //if ((sizeof(SHIP) != lpDestroyMsg->dwLocalDataSize) || 
           //   (NULL == lpDestroyMsg->lpLocalData))
           //   break;

           //lpShip = lpDestroyMsg->lpLocalData;
           // ReleasePlayerLocalSoundData(lpShip);
        }
        break;

    case DPSYS_HOST:
        {           
            gbIsHost = TRUE;            
            //UpdateTitle();//do info
        }

        break;

    case DPSYS_SESSIONLOST:
        // inform user that session was lost
        initFail("IDS_DPLAY_ERROR_SL");
        gbSessionLost = TRUE;
        break;
    }
}

/*
 * DoApplicationMessage
 *
 * Evaluates an application message and performs appropriate actions
 */
void DoApplicationMessage( LPGENERICMSG lpMsg, DWORD dwMsgSize, DPID idFrom, DPID idTo )
{        
    HRESULT hr;

    switch( lpMsg->byType )
    {
    case MSG_HOST:
        {
            LPHOSTMSG       lpHost;

            if( !gbIsHost )
            {
                lpHost = (LPHOSTMSG) lpMsg;
                // receive the field layout
                gGameType = lpHost->GameType;

                // have host initializtion at this point
                gbHaveHostInit = TRUE;
                
                // start updating screen
                gbIsActive = TRUE;
            }
        }
        break;

    case MSG_BUILD:
         break;

    case MSG_FIRE:
         break;

    case MSG_MOVE:
       
        break;

    case MSG_CALLFIX:
        
        break;

    case MSG_TALK:
       
        break;

    default:
        {
            initFail("Unknown message type" );
        }
        break;
    }
}

/*
 * SendGameMessage
 *
 * Sends a message to specified player(s)
 */
void SendGameMessage( LPGENERICMSG lpMsg, DPID idTo )
{
    int             nBytes;
    DWORD           dwFlags = 0;

    if (gbSessionLost)
    {
        // no sends when we are not in the session
        return;
    }

    switch( lpMsg->byType )
    {
    case MSG_HOST:
        nBytes = sizeof( HOSTMSG );
        dwFlags = DPSEND_GUARANTEED;
        break;

    case MSG_BUILD:
        //nBytes = sizeof( BUILDMSG );
        break;

    case MSG_FIRE:
        //nBytes = sizeof( FIREMSG );
        break;

    case MSG_MOVE:
        //nBytes = sizeof( MOVEMSG );
        break;

    case MSG_CALLFIX:
       // nBytes = sizeof( CALLFIXMSG );
        break;

    case MSG_TALK:
        //nBytes = sizeof( TALKMSG );
        break;

    default:
        return;
    }

    if (gbReliable)
    {
        dwFlags = DPSEND_GUARANTEED;
    }

    // Send the message to the relevant player(s)
    DPlaySend(gOurID, idTo, dwFlags, (LPVOID)lpMsg, nBytes);    
}




/*
 * LaunchedByLobby
 *
 * Determines if we were launched by a lobby. If so, gets the connection settings
 * and creates our player using the information from the lobby
 */
BOOL LaunchedByLobby(void)
{
    HRESULT hr;
    HWND    hwndStatus;

    // create a lobby object
    hr = DPLobbyCreate();
    if (FAILED(hr))
    {
        initFail("IDS_DPLOBBY_ERROR_C");
        return FALSE;
    }

    // get connection settings from the lobby (into glpdplConnection)
    hr = DPLobbyGetConnectionSettings();
    if (FAILED(hr))
    {
        if (DPERR_NOTLOBBIED == hr)
        {
            // we were not lobbied - start up game normally
            hr = DPLobbyRelease();
            if (FAILED(hr))
            {
                initFail("IDS_DPLOBBY_ERROR_C");
                goto FAIL;
            }
            // move on
            return FALSE;
        }
        else
        {
            initFail("IDS_DPLOBBY_ERROR_C");
            goto FAIL;
        }
    }

    // are we hosting or joining ?
    if (glpdplConnection->dwFlags & DPLCONNECTION_CREATESESSION)
    {
        gbIsHost = TRUE;
    }

    // set our session flags
    glpdplConnection->lpSessionDesc->dwFlags = DPSESSION_MIGRATEHOST | 
                                                 DPSESSION_KEEPALIVE;

    // let lobby know our connection flags
    hr = DPLobbySetConnectionSettings();
    if (FAILED(hr))
    {
        initFail("IDS_DPLOBBY_ERROR_C");
        goto FAIL;
    }

    if ( !gbIsHost ) 
    {
        // show splash screen and 
        // connection status if we are joining a game
       // UpdateWindow(ghWndMain);
        //hwndStatus = ShowConnectStatus();
    }
    else
    {
        // we are hosting, don't need connection status
        hwndStatus = NULL;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -