c++ - 在不同的线程中播放声音

标签 c++ multithreading game-engine

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

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

}

女巫只是播放一个声音,但是当我调用这个函数时,所有程序都会崩溃,当声音响起时,程序会继续。

我的代码是调用 fire 函数显示如下

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);
}

谁能帮帮我?谢谢

最佳答案

解决方法在这里

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

对我来说效果很好。

关于c++ - 在不同的线程中播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20583412/

相关文章:

c++ - 在 'Operator>>' 中没有匹配 's>>local"

java - Java中的BufferedReader在尝试从关闭的套接字读取时无法触发异常

java - Java 中的 CountDownLatch 需要额外的同步吗?

c++ - 调用静态成员函数时出错

c++ - 这是正常的 C++ 行为吗?尽管有用户定义的构造函数,但编译器零初始化我的类

javascript - 浏览器 web api 在 Javascript 中运行在哪里?

相当于 Pygame.org 的 Javascript 游戏开发?

c# - 独特组合的纸牌游戏算法

java 。 "Game Loop"理论帮助

c++ - 定制前提条件是否对 move 分配运算符(operator)有效?