C++ 需要这个来播放音乐吗

C++ 需要这个来播放音乐吗,c++,allegro5,C++,Allegro5,游戏开发类。试图理解我做错了什么。我加错东西了,还是放错地方了。 我的目标是增加音乐 #include "allegro5/allegro.h" #include <allegro5/allegro_image.h> #include <allegro5/allegro_font.h> #include <allegro5/allegro_ttf.h> #include <allegro5/allegro_native_dialog.h> #inc

游戏开发类。试图理解我做错了什么。我加错东西了,还是放错地方了。 我的目标是增加音乐

#include "allegro5/allegro.h"
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_native_dialog.h>
#include <stdio.h>
#include <string>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>


#define FPS 60
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define BLACK al_map_rgb(0, 0, 0)
#define WHITE al_map_rgb(255, 255, 255)

//Prototypes
bool initializeAllegro();

//Essential Allegro pointers
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *eventQueue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_FONT *arial14;

int main()
{
    ALLEGRO_BITMAP *backgroundImage;    //Bitmap for the background image(star field)
    int backgroundImageWidth = 0, backgroundImageHeight = 0;

    bool redrawRequired = true;
    //Using std:string for name, so length is not a factor
    std::string userName = "";
    std::string prompt1 = "Enter your Player's name below";
    std::string prompt2 = "(Letters & digits only. Backspace to erase.)";
    std::string prompt3 = "Press Enter to accept...";
    std::string prompt4 = "Press Escape to exit...";
    char keyToAdd = ' ';
    bool enteringUserName = true;
    bool quit = false;

    //Initialize allegro, etc
    if (!initializeAllegro()) return -1;

    //load the arial font
    arial14 = al_load_font("arial-mt-black.ttf", 14, 0);
    if (!arial14)
    {
        return -3;
    }
    //test running music
    al_init_acodec_addon();

    ALLEGRO_SAMPLE *song = al_load_sample("hideandseek.wav");
    ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(song);
    al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);
    al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());

    //Load the background picture
    if (!(backgroundImage = al_load_bitmap("starbackground.bmp")))
    {
        return -5;
    }

    //Get dimensions of the background image
    backgroundImageWidth = al_get_bitmap_width(backgroundImage);
    backgroundImageHeight = al_get_bitmap_height(backgroundImage);

    //Set the back buffer as the drawing bitmap for the display
    al_set_target_bitmap(al_get_backbuffer(display));

    //Initialize the event queue
    eventQueue = al_create_event_queue();
    if (!eventQueue)
    {
        al_destroy_display(display);
        al_destroy_timer(timer);
        return -1;
    }

    //Register events as arriving from these sources:  display, timer, keyboard
    al_register_event_source(eventQueue, al_get_display_event_source(display));
    al_register_event_source(eventQueue, al_get_timer_event_source(timer));
    al_register_event_source(eventQueue, al_get_keyboard_event_source());

    al_flip_display();

    //play song
    al_play_sample_instance(songInstance);

    //Start up the timer.  Don't do this until just before the game is to start!
    al_start_timer(timer);

    //This would be a loop solely for entering the user's name, not starting the game
    //Allegro keycodes are laid out as follows.  (Full set of key codes is included in keycodes.h)
    //Key codes for capital letters are 1 - 26 for A - Z
    //Digit values are 27 - 36 for the top digits (0 - 9), and 37 - 46 for the keypad
    while (!quit)
    {
        ALLEGRO_EVENT evt;
        //Wait for one of the allegro-defined events
        al_wait_for_event(eventQueue, &evt);

        //An event was generated. Check for all possible event types
        switch (evt.type)
        {
            case ALLEGRO_EVENT_KEY_CHAR:
                if (evt.keyboard.keycode >= 1 && evt.keyboard.keycode <= 26) //It's a capital letter
                {
                    //Convert to ASCII capital
                    keyToAdd = *al_keycode_to_name(evt.keyboard.keycode);
                    userName += keyToAdd;
                }
                else if (evt.keyboard.keycode >= 27 && evt.keyboard.keycode <= 36)
                {
                    //Convert to digit
                    keyToAdd =  evt.keyboard.keycode + 21;
                    userName += keyToAdd;
                }
                //Handle digits on keypad
                else if (evt.keyboard.keycode >= 37 && evt.keyboard.keycode <= 46)
                {
                    //Convert to digit
                    keyToAdd =  evt.keyboard.keycode + 11;
                    userName += keyToAdd;
                }
                else //Handle a few other keys
                {
                    switch(evt.keyboard.keycode)
                    {   
                        case ALLEGRO_KEY_BACKSPACE: 
                            if (userName.length() > 0)
                                userName = userName.substr(0, userName.length() - 1);
                            break;

                        //Enter key stops username entry
                        case ALLEGRO_KEY_ENTER:
                        case ALLEGRO_KEY_PAD_ENTER:
                            enteringUserName = false;
                            break;
                        case ALLEGRO_KEY_ESCAPE:
                            quit = true;
                            break;
                    }
                }
                break;

            case ALLEGRO_EVENT_DISPLAY_CLOSE:
                quit = true;
                break;

            case ALLEGRO_EVENT_TIMER:
                redrawRequired = true;
                break;

        }//END switch evt.type

        //Rerender the entire scene
        if (redrawRequired && al_is_event_queue_empty(eventQueue))
        {
            redrawRequired = false;

            al_clear_to_color(BLACK);   //Just in case

            //Draw background
            al_draw_scaled_bitmap(backgroundImage, 0, 0, backgroundImageWidth, backgroundImageHeight,
                0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);

            //Prompt for name to be entered
            al_draw_textf (arial14, WHITE, 0, 30, 0, "%s", prompt1.c_str());
            al_draw_textf (arial14, WHITE, 0, 50, 0, "%s", prompt2.c_str());
            if (userName.length() > 0)
            {
                if (enteringUserName)
                {
                    al_draw_textf (arial14, WHITE, 0, 70, 0, "%s", userName.c_str());
                    al_draw_textf (arial14, WHITE, 0, 90, 0, "%s", prompt3.c_str());
                }
                else
                {
                    al_draw_textf (arial14, WHITE, 0, 70, 0, "You entered your name as-->%s", userName.c_str());
                }
            }

            al_draw_textf(arial14, WHITE, 0, 400, 0, "%s", prompt4.c_str());

            al_flip_display();
        }//END rendering the screen

    }//END input Loop

    //Release all dynamically allocated memory
    al_destroy_bitmap(backgroundImage);
    al_destroy_font(arial14);
    al_destroy_timer(timer);
    al_destroy_display(display);
    al_destroy_event_queue(eventQueue);
    //destroy songs
    al_destroy_sample(song);
    al_destroy_sample_instance(songInstance);

    return 0;
}//END main

//Take care of Allegro initialization tasks
//Return false if any fail.
bool initializeAllegro()
{
    bool success = true;

    //Init basic environment
    if (!al_init())
    {
        al_show_native_message_box(NULL, "ERROR", "Allegro failed to initialize!", NULL, NULL, NULL);
        success = false;
    }

    //Initialize keyboard input
    if (!al_install_keyboard())
    {
        al_show_native_message_box(NULL, "ERROR", "Keyboard failed to initialize!", NULL, NULL, NULL);
        success = false;
    }

    //Initialize timer
    timer = al_create_timer(1.0 / FPS);
    if (!timer)
    {
        al_show_native_message_box(NULL, "ERROR", "Timer failed to initialize!", NULL, NULL, NULL);
        success = false;
    }

    //Initialize display
    display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
    if (!display)
    {
        al_show_native_message_box(NULL, "ERROR", "Display failed to initialize!", NULL, NULL, NULL);
        success = false;
    }

    //Initialize the image mechanism
    if (!al_init_image_addon())
    {
        al_show_native_message_box(NULL, "ERROR", "Image addon failed to initialize!", NULL, NULL, NULL);
        success = false;
    }

    al_init_font_addon();
    al_init_ttf_addon();

    return success;
}//END initializeAllegro
#包括“allegro5/allegro.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义FPS 60
#定义屏幕宽度800
#定义屏幕高度600
#定义黑色al_贴图_rgb(0,0,0)
#定义白色al_贴图_rgb(255、255、255)
//原型
bool initializealegro();
//基本快板指针
快板显示*显示=NULL;
ALLEGRO_EVENT_QUEUE*eventQueue=NULL;
快板计时器*计时器=空;
快板字体*arial14;
int main()
{
ALLEGRO_位图*backgroundImage;//背景图像的位图(星形字段)
int backgroundImageWidth=0,backgroundImageHeight=0;
bool redrawRequired=真;
//使用std:string作为名称,所以长度不是一个因素
std::string userName=“”;
std::string prompt1=“在下面输入玩家姓名”;
std::string prompt2=“(仅限字母和数字。请退格删除)。”;
std::string prompt3=“按Enter键接受…”;
std::string prompt4=“按Escape退出…”;
char keyToAdd='';
bool enteringUserName=true;
bool-quit=false;
//快板等
如果(!initializeAllegro())返回-1;
//加载arial字体
arial14=al_load_字体(“arial mt black.ttf”,14,0);
如果(!arial14)
{
返回-3;
}
//试车音乐
al_init_acodec_addon();
快板样本*歌曲=所有加载样本(“hideandseek.wav”);
ALLEGRO_SAMPLE_INSTANCE*songInstance=al_create_SAMPLE_INSTANCE(歌曲);
al_set_sample_instance_playmode(歌曲实例,快板播放模式);;
al_将_sample_instance_附加到_mixer(songInstance,al_get_default_mixer());
//加载背景图片
如果(!(backgroundImage=al_load_位图(“starbackground.bmp”))
{
返回-5;
}
//获取背景图像的尺寸
backgroundImageWidth=al_get_bitmap_width(backgroundImage);
backgroundImageHeight=al_get_bitmap_height(背景图像);
//将后缓冲区设置为显示的图形位图
al_设置\目标\位图(al_获取\后缓冲(显示));
//初始化事件队列
eventQueue=al_create_event_queue();
如果(!eventQueue)
{
铝显示器(显示器);
al_销毁_定时器(定时器);
返回-1;
}
//注册来自以下来源的事件:显示器、计时器、键盘
al_寄存器\事件\源(事件队列,al_获取\显示\事件\源(显示));
al_寄存器\事件\源(事件队列,al_获取\计时器\事件\源(计时器));
al_寄存器\事件\源(eventQueue,al_get\ u键盘\事件\源());
al_flip_display();
//播放歌曲
al_play_sample_实例(songInstance);
//启动计时器。在游戏开始前不要这样做!
启动定时器(定时器);
//这将是一个仅用于输入用户名的循环,而不是启动游戏
//Allegro键码的布局如下。(键码中包含全套键码。h)
//大写字母的关键代码是A-Z的1-26
//顶部数字(0-9)的数字值为27-36,小键盘的数字值为37-46
而(!退出)
{
快板事件evt;
//等待allegro定义的事件之一
等待事件(事件队列和evt);
//已生成事件。请检查所有可能的事件类型
开关(电动类型)
{
案例快板事件关键字符:

如果(evt.keyboard.keycode>=1&&evt.keyboard.keycode我在我的评论中提到了使用调试器逐步完成任务,但这里的代码味道如下:

ALLEGRO_SAMPLE *song = al_load_sample("hideandseek.wav");
ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(song);
在调用
al\u create\u sample\u instance()


如果到.WAV文件的路径错误,或者由于任何其他原因加载失败,则
al_load_sample()
将返回空值,您不应该尝试使用
歌曲执行任何操作

您可以尝试使用调试器单步检查代码以查看它在哪一行中断吗?这看起来像是您试图访问空指针。为什么我在所有问题上都得到了-我也会尝试。我发现了主要问题,我没有把它放在其中。al_install_audio();al_init_acodec_addon();al_reserve_samples(1);