目前位置: VCer资源中心 >>> VCer文章 >>> 图形图象处理

[本帖已阅读6743次 分值80 回复4次] 张贴资源 发回信箱 控制面板

C++源代码游戏编程--WinMain()函数集

提供者:guifang 张贴时间:2004-06-20 11:46:28.0 出处:www.gameres.com 作者:不祥

C++源代码游戏编程--WinMain()函数集(2004-06-20 11:46:28.0)


小桂子


 
级别: VCer排长
头衔: VCer会员

经验: 526
作品: 10
分会: 华中分会
注册: 2004-02-12 20:40:46.0
登录: 2007-04-15 23:42:28.0

C++源代码游戏编程--WinMain()函数集

 

 

C++源代码游戏编程--WinMain()函数交流

//******************************************************************

//函数:WinMain()

//功能:应用程序入口

//******************************************************************

int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow)

{

    MSG msg;

    hInst=hInstance;

    //初始化主窗口

    if (!InitWindow( hInstance, nCmdShow))

    return FALSE;

    //初始化DirectDraw环境

    if (!InitDDraw())

    {

        MessageBox(hWndMain, "初始化DirectDraw过程中出错!", "Error", MB_OK);

        FreeObjects();

        DestroyWindow(hWndMain);

        return FALSE;

    }

    //进入消息循环

    while(1)

    {

        if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))

        {

            if(!GetMessage(&msg, NULL, 0, 0 )) return msg.wParam;

            TranslateMessage(&msg);

            DispatchMessage(&msg);

        }

        else if(bActive)

        {

            UpdateFrame();

        }

        else WaitMessage();

        }

    return msg.wParam;

}

///////////////////////////////////////////////

//-----------------------------------------------------------------------------

// Name: WinMain()

// Desc: Initialization, message loop

//-----------------------------------------------------------------------------

int PASCAL

WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

    MSG msg;

    if (InitApp(hInstance, nCmdShow) != DD_OK)

        return FALSE;

    while (GetMessage(&msg, NULL, 0, 0))

    {

        TranslateMessage(&msg);

        DispatchMessage(&msg);

    }

    return msg.wParam;

}

/////////////////////////////////////////////////////////////////////////////////////////////

// 主循环

/////////////////////////////////////////////////////////////////////////////////////////////

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)

{

    MSG msg;

    if(!InitApp(hInst, nCmdShow)) return FALSE;

    while(1)

    {

        if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))

        {

            if(!GetMessage(&msg, NULL, 0, 0 )) return msg.wParam;

            TranslateMessage(&msg);

            DispatchMessage(&msg);

        }

        else if(bActive)

        {

            Update();

            // 翻转上一层缓冲区内容到屏幕。

            Screen->Flip();

        }

        else WaitMessage();

    }

}

////////////////////////////////////////////////

///////////////////////////////////////////////////

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow)

{

    // any windows programmer should be familiar with this

    MSG msg;

    lpCmdLine = lpCmdLine;

    hPrevInstance = hPrevInstance;

    if(!doInit(hInstance, nCmdShow))

    {

        return FALSE;

    }

    while (1)

    {

        if(PeekMessage(&msg, NULL, 0,0,PM_NOREMOVE))

        {

            if(!GetMessage(&msg, NULL,0,0))

                return msg.wParam;

            TranslateMessage(&msg);

            DispatchMessage(&msg);

        }

        else if(bActive)

        {

            fire(480, 0, 600, 50);

            // draw fire on bottom row and then flip

            while(1)

            {

                ddrval = lpDDSPrimary->Flip(NULL, DDFLIP_WAIT);

                if(ddrval == DD_OK)

                {

                    break;

                }

                if(ddrval == DDERR_SURFACELOST)

                {

                    if(ddrval != DD_OK)

                    {

                        break;

                    }

                }

                if(ddrval != DDERR_WASSTILLDRAWING)

                {

                    break;

                }

            }

        } // if bActive

        else

        {

            //make sure we go to sleep if have nothiing to do

            WaitMessage();

        }

    }

} // win main

//////////////////////////////////////////////

//-----------------------------------------------------------------------------

// Name: WinMain()

// Desc: 入口,初始化,进行消息循环

//-----------------------------------------------------------------------------

int PASCAL

WinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

    MSG msg;

    if (InitApp(hInstance, nCmdShow) != DD_OK)

        return FALSE;

    Map[0].Width=width;

    Map[0].Hight=height;

    Map[0].Data=(char *)malloc( Map[0].Width * Map[0].Hight );

    Map[0].Pro=(char *)malloc( Map[0].Width * Map[0].Hight );

    //将地图全部置为可到达

    int i,j;

    reachable=1;

    for (j=0;j<Map[0].Hight;j++)

        for (i=0;i<Map[0].Width;i++)

        {

            *(Map[0].Data+i*Map[0].Hight+j)=reachable;

        }

    for (j=0;j<Map[0].Hight;j++)

        for (i=0;i<Map[0].Width;i++)

        {

            *(Map[0].Pro+i*Map[0].Hight+j)=select_pro;

        }

    //消息循环

    while (GetMessage(&msg, NULL, 0, 0))

    {

        TranslateMessage(&msg);

        DispatchMessage(&msg);

    }

    return msg.wParam;

}

//-----------------------------------------------------------------------------

// Name: WinMain()

// Desc: Entry point to the program. Initializes everything and calls

// UpdateFrame() when idle from the message pump.

//-----------------------------------------------------------------------------

int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow )

{

    MSG msg;

    HWND hWnd;

    ZeroMemory( &g_Sprite, sizeof(SPRITE_STRUCT) * NUM_SPRITES );

    srand( GetTickCount() );

    if( FAILED( WinInit( hInst, nCmdShow, &hWnd ) ) )

        return FALSE;

    // Make a timer go off to re-init the table of random values every once in a while

    SetTimer( hWnd, 0, 1500, NULL );

    if( FAILED( InitDirectDraw( hWnd ) ) )

    {

        if( g_pDisplay )

            g_pDisplay->GetDirectDraw()->SetCooperativeLevel( NULL, DDSCL_NORMAL );

        MessageBox( hWnd, TEXT("DirectDraw init failed. ")

        TEXT("The sample will now exit. "), TEXT("DirectDraw Sample"),

        MB_ICONERROR | MB_OK );

        return FALSE;

    }

    g_dwLastTick = timeGetTime();

    while( TRUE )

    {

        // Look for messages, if none are found then

        // update the state and display it

        if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )

        {

            if( 0 == GetMessage(&msg, NULL, 0, 0 ) )

            {

                // WM_QUIT was posted, so exit

                return (int)msg.wParam;

            }

            // Translate and dispatch the message

            TranslateMessage( &msg );

            DispatchMessage( &msg );

        }

        else

        {

            if( g_bActive )

            {

                // Move the sprites, blt them to the back buffer, then

                // flip or blt the back buffer to the primary buffer

                if( 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 );

                    return FALSE;

                }

            }

            else

            {

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

                WaitMessage();

                // Ignore time spent inactive

                g_dwLastTick = timeGetTime();

            }

        }

    }

}

本人e-mail: fuzipin@sina,com

希望通过交流进一步提高我们的游戏开发水平,OK

 

 

 

 

 

 

注:转载文章需注明来源:VCer.net 文章地址:http://vcer.net/2552.html

  如果你觉得VCer.net不错,而且你愿意为VCer.net捐赠一元钱,那么点击后面的捐赠按钮吧:) vcer.net捐赠

[回复该贴] [加入个人书签]
[投票结果]

A: 评分 10 0% (0 票)
B: 评分 5 0% (0 票)
C: 评分 0 0% (0 票)
D: 评分 -5 0% (0 票)
E: 评分 -10 0% (0 票)

 


re:C++源代码游戏编程--WinMain()函数集
应该是需要 DirectX 的 SDK

fefe82 于 2005-04-29 10:08:27.0 编辑 [回复该贴]

re:C++源代码游戏编程--WinMain()函数集
不好意思。我是菜菜鸟…… 我粘来试运行了一下,包含了Windows.h,系统提示出错信息如下…… C:\VS work\dx\dx.cpp(23) : error C2065: 'InitWindow' : undeclared identifier C:\VS work\dx\dx.cpp(29) : error C2065: 'InitDDraw' : undeclared identifier C:\VS work\dx\dx.cpp(33) : error C2065: 'hWndMain' : undeclared identifier C:\VS work\dx\dx.cpp(35) : error C2065: 'FreeObjects' : undeclared identifier C:\VS work\dx\dx.cpp(61) : error C2065: 'bActive' : undeclared identifier C:\VS work\dx\dx.cpp(65) : error C2065: 'UpdateFrame' : undeclared identifier C:\VS work\dx\dx.cpp(97) : error C2084: function 'int __stdcall WinMain(struct HINSTANCE__ *,struct HINSTANCE__ *,char *,int)' already has a body C:\VS work\dx\dx.cpp(101) : error C2065: 'InitApp' : undeclared identifier C:\VS work\dx\dx.cpp(101) : error C2065: 'DD_OK' : undeclared identifier C:\VS work\dx\dx.cpp(153) : error C2065: 'Update' : undeclared identifier C:\VS work\dx\dx.cpp(157) : error C2065: 'Screen' : undeclared identifier C:\VS work\dx\dx.cpp(157) : error C2227: left of '->Flip' must point to class/struct/union C:\VS work\dx\dx.cpp(185) : error C2065: 'doInit' : undeclared identifier C:\VS work\dx\dx.cpp(215) : error C2065: 'fire' : undeclared identifier C:\VS work\dx\dx.cpp(223) : error C2065: 'ddrval' : undeclared identifier C:\VS work\dx\dx.cpp(223) : error C2065: 'lpDDSPrimary' : undeclared identifier C:\VS work\dx\dx.cpp(223) : error C2227: left of '->Flip' must point to class/struct/union C:\VS work\dx\dx.cpp(223) : error C2065: 'DDFLIP_WAIT' : undeclared identifier C:\VS work\dx\dx.cpp(233) : error C2065: 'DDERR_SURFACELOST' : undeclared identifier C:\VS work\dx\dx.cpp(247) : error C2065: 'DDERR_WASSTILLDRAWING' : undeclared identifier C:\VS work\dx\dx.cpp(301) : error C2065: 'Map' : undeclared identifier C:\VS work\dx\dx.cpp(301) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(301) : error C2228: left of '.Width' must have class/struct/union type C:\VS work\dx\dx.cpp(301) : error C2065: 'width' : undeclared identifier C:\VS work\dx\dx.cpp(303) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(303) : error C2228: left of '.Hight' must have class/struct/union type C:\VS work\dx\dx.cpp(303) : error C2065: 'height' : undeclared identifier C:\VS work\dx\dx.cpp(305) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(305) : error C2228: left of '.Data' must have class/struct/union type C:\VS work\dx\dx.cpp(305) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(305) : error C2228: left of '.Width' must have class/struct/union type C:\VS work\dx\dx.cpp(305) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(305) : error C2228: left of '.Hight' must have class/struct/union type C:\VS work\dx\dx.cpp(307) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(307) : error C2228: left of '.Pro' must have class/struct/union type C:\VS work\dx\dx.cpp(307) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(307) : error C2228: left of '.Width' must have class/struct/union type C:\VS work\dx\dx.cpp(307) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(307) : error C2228: left of '.Hight' must have class/struct/union type C:\VS work\dx\dx.cpp(313) : error C2065: 'reachable' : undeclared identifier C:\VS work\dx\dx.cpp(315) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(315) : error C2228: left of '.Hight' must have class/struct/union type C:\VS work\dx\dx.cpp(317) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(317) : error C2228: left of '.Width' must have class/struct/union type C:\VS work\dx\dx.cpp(321) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(321) : error C2228: left of '.Data' must have class/struct/union type C:\VS work\dx\dx.cpp(321) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(321) : error C2228: left of '.Hight' must have class/struct/union type C:\VS work\dx\dx.cpp(325) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(325) : error C2228: left of '.Hight' must have class/struct/union type C:\VS work\dx\dx.cpp(327) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(327) : error C2228: left of '.Width' must have class/struct/union type C:\VS work\dx\dx.cpp(331) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(331) : error C2228: left of '.Pro' must have class/struct/union type C:\VS work\dx\dx.cpp(331) : error C2109: subscript requires array or pointer type C:\VS work\dx\dx.cpp(331) : error C2228: left of '.Hight' must have class/struct/union type C:\VS work\dx\dx.cpp(331) : error C2065: 'select_pro' : undeclared identifier C:\VS work\dx\dx.cpp(369) : error C2065: 'g_Sprite' : undeclared identifier C:\VS work\dx\dx.cpp(369) : error C2065: 'SPRITE_STRUCT' : undeclared identifier C:\VS work\dx\dx.cpp(369) : error C2065: 'NUM_SPRITES' : undeclared identifier C:\VS work\dx\dx.cpp(373) : error C2065: 'WinInit' : undeclared identifier C:\VS work\dx\dx.cpp(381) : error C2065: 'InitDirectDraw' : undeclared identifier C:\VS work\dx\dx.cpp(385) : error C2065: 'g_pDisplay' : undeclared identifier C:\VS work\dx\dx.cpp(387) : error C2227: left of '->GetDirectDraw' must point to class/struct/union C:\VS work\dx\dx.cpp(387) : error C2227: left of '->SetCooperativeLevel' must point to class/struct/union C:\VS work\dx\dx.cpp(387) : error C2065: 'DDSCL_NORMAL' : undeclared identifier C:\VS work\dx\dx.cpp(399) : error C2065: 'g_dwLastTick' : undeclared identifier C:\VS work\dx\dx.cpp(399) : error C2065: 'timeGetTime' : undeclared identifier C:\VS work\dx\dx.cpp(435) : error C2065: 'g_bActive' : undeclared identifier C:\VS work\dx\dx.cpp(443) : error C2065: 'ProcessNextFrame' : undeclared identifier C:\VS work\dx\dx.cpp(447) : error C2065: 'SAFE_DELETE' : undeclared identifier Error executing cl.exe. dx.exe - 71 error(s), 0 warning(s) 请问如何运行啊……我对VC很有兴趣,不过现在还是只停留在MFC的菜菜及。呵呵

Saddism 于 2005-04-22 16:51:59.0 编辑 [回复该贴]

re:C++源代码游戏编程--WinMain()函数集
以后多多向你请教了。 偶以后的工作就是游戏开发了,不过是新手,还没有入门。我的 e-mail:hustym@163.com. 网易泡泡:hustym@163.com (公司不能用QQ)

ISeekYou 于 2004-07-21 16:20:35.0 编辑 [回复该贴]

re:C++源代码游戏编程--WinMain()函数集
早就该来发贴子了嘛!

jerry 于 2004-06-20 17:25:02.0 编辑 [回复该贴]