PlaySound在另一个主题中

时间:2013-12-14 12:39:48

标签: c++ multithreading game-engine

我正在尝试开发一款带有船只的电脑游戏。我有一个函数fire()

void Sprite::fire()
{
    PlaySound("Sounds/azafire.wav", NULL, SND_FILENAME);//SND_FILENAME SND_SYNC

}

女巫只是发出声音,但是当我调用此功能时,所有程序都会进行frizing,当声音为fish时,程序会继续执行。

我的代码是我致电fire功能show bellow

LRESULT CALLBACK 
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{   
    Circle bc; // Not needed in this demo, leave default.
    Vec2   p0(gClientCenter);
    Vec2   v0(0.0f, 0.0f);

    switch( msg )
    {   
    // Create application resources.
    case WM_CREATE:

        // Create the sprites
        gBackground = new Sprite(ghAppInst,
            IDB_BACKGROUND1024X768, IDB_BACKGROUND1024X768MASK, bc, p0, v0);

        gF15 = new Sprite(ghAppInst, IDB_F15, IDB_F15MASK,
            bc, p0, v0);

        p0.x = 100;
        p0.y = 100;
        gF18 = new Sprite(ghAppInst, IDB_F18, IDB_F18MASK,
            bc, p0, v0);
        gF18->mDirection = -1;

        p0.x = 600;
        p0.y = 100;
        gF117 = new Sprite(ghAppInst, IDB_F117, IDB_F117MASK,
            bc, p0, v0);
        //Start move
        gF117->mDirection = 1;

        p0.x = 0.0f;
        p0.y = 0.0f;
        gBullet = new Sprite(ghAppInst, IDB_BULLET, IDB_BULLETMASK,
            bc, p0, v0);

        //Enemy Bullet
        p0.x = 0.0f;
        p0.y = 0.0f;
        gEnemyBullet = new Sprite(ghAppInst, IDB_ENEMYBULLET, IDB_ENEMYBULLETMASK,
            bc, p0, v0);


        // Create system memory DCs 
        ghSpriteDC = CreateCompatibleDC(0);

        // Create the backbuffer.
        gBackBuffer = new BackBuffer(
            hWnd, 
            gClientWidth,
            gClientHeight);

        return 0;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
        // Destroy the window when the user selects the 'exit'
        // menu item.
        case ID_FILE_EXIT:
            DestroyWindow(ghMainWnd);
            break;
        }
        return 0;

    case WM_KEYDOWN:
        switch(wParam)
        {
        // Accelerate left.
        case 'A':
            gF15->mVelocity.x -= 5.0f;
            break;
        // Accelerate right.
        case 'D':
            gF15->mVelocity.x += 5.0f;
            break;
        // Accelerate up (remember +y goes down and -y goes up)
        case 'W':
            gF15->mVelocity.y -= 5.0f;
            break;
        // Accelerate down.
        case 'S':
            gF15->mVelocity.y += 5.0f;
            break;
        case VK_SPACE:
            // Add a bullet to the bullet list.
            gBulletPos.push_back(gF15->mPosition);
                    gF15->fire(); // !!!! Here is the problem !!!!
            break;
        case VK_LEFT:
            //Add LEFT ARROW key (0x25)
            gF15->mVelocity.x -= 5.0f;
            break;
        case VK_RIGHT:
            //Add Right ARROW Key 
            gF15->mVelocity.x += 5.0f;
            break;
        case VK_UP:
            //Add UP ARROW Key (0x26)
            gF15->mVelocity.y -= 5.0f;
            break;
        case VK_DOWN:
            //Add DOWN ARROW Key (0x28)
            gF15->mVelocity.y += 5.0f;
            break;

        }
        return 0;
    case WM_LBUTTONDOWN:
        //Add Left mouse button
        gBulletPos.push_back(gF15->mPosition);
        gF15->fire(); // !!!! Here is the problem !!!!
        return 0;
    case WM_RBUTTONDOWN:
        //Add Right mouse button
        gEnemyBulletPos.push_back(gF117->mPosition);
        return 0;
    // Destroy application resources.
    case WM_DESTROY:    
        delete gBackground;
        delete gF15;
        delete gF18;
        delete gF117;
        delete gBullet;
        delete gBackBuffer;
        DeleteDC(ghSpriteDC);
        PostQuitMessage(0); 
        return 0;   
    }   
    // Forward any other messages we didn't handle to the
    // default window procedure.
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

任何人都可以帮助我吗?谢谢

2 个答案:

答案 0 :(得分:2)

解决方案就在这里

void Sprite::fire()
{
    PlaySound("Sounds/azafire.wav", NULL, SND_ASYNC);
}

对我来说是完美的。

答案 1 :(得分:1)

您可以使用OGRE库来开发games c ++

http://www.ogre3d.org/

它是免费的开源软件。你可以找到一个带船的示例游戏。看看