Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 30 Jan 2013
Offline Last Active Apr 04 2013 06:30 AM

Topics I've Started

No sound from OpenAL using mac.

22 February 2013 - 12:31 AM

Hi all,


I'm trying to play a simple sound for a game that I'm currently working on.  I don't want to use SDL if I can help it and came across OpenAL.  I found an example and tutorial for creating and playing sound without the alut header but I'm still having issues.  The log output is fine, it's reading the file, knows the size and so on but nothing is played.  I thought that maybe it was my game loop that was affecting it so created a new project with just the sound code to test and same result.  any help is hugely appreciated.  Thanks.



#include <cstdlib>
#include <iostream>
#include <OPENAL/al.h>
#include <OPENAL/alc.h>
using namespace std;

typedef uint32_t DWORD;
int endWithError(char* msg, int error=0)

    //Display error message in console
    cout << msg << "\n";
    return error;


int main(int argc, char *argv[])


    //Loading of the WAVE file

    FILE *fp = NULL;                                                            //Create FILE pointer for the WAVE file

    fp=fopen("Sounds/mushroom_jump.wav","rb");                                            //Open the WAVE file

    if (!fp) return endWithError("Failed to open file");                        //Could not open file
    //Variables to store info about the WAVE file (all of them is not needed for OpenAL)

    char type[4];

    DWORD size,chunkSize;

    short formatType,channels;

    DWORD sampleRate,avgBytesPerSec;

    short bytesPerSample,bitsPerSample;

    DWORD dataSize;

    //Check that the WAVE file is OK

    fread(type,sizeof(char),4,fp);                                              //Reads the first bytes in the file

    if(type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F')            //Should be "RIFF"

        return endWithError ("No RIFF");                                            //Not RIFF

    fread(&size, sizeof(DWORD),1,fp);                                           //Continue to read the file

    fread(type, sizeof(char),4,fp);                                             //Continue to read the file

    if (type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E')           //This part should be "WAVE"

        return endWithError("not WAVE");                                            //Not WAVE

    fread(type,sizeof(char),4,fp);                                              //Continue to read the file

    if (type[0]!='f' || type[1]!='m' || type[2]!='t' || type[3]!=' ')           //This part should be "fmt "

        return endWithError("not fmt ");                                            //Not fmt

    //Now we know that the file is a acceptable WAVE file

    //Info about the WAVE data is now read and stored


    if (type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a')           //This part should be "data"
        return endWithError("Missing DATA");                                        //not data

    fread(&dataSize,sizeof(DWORD),1,fp);                                        //The size of the sound data is read

    //Display the info about the WAVE file

    cout << "Chunk Size: " << chunkSize << "\n";
    cout << "Format Type: " << formatType << "\n";
    cout << "Channels: " << channels << "\n";
    cout << "Sample Rate: " << sampleRate << "\n";
    cout << "Average Bytes Per Second: " << avgBytesPerSec << "\n";
    cout << "Bytes Per Sample: " << bytesPerSample << "\n";
    cout << "Bits Per Sample: " << bitsPerSample << "\n";
    cout << "Data Size: " << dataSize << "\n";

    unsigned char* buf= new unsigned char[dataSize];                            //Allocate memory for the sound data

    //cout << fread(buf,sizeof(BYTE),dataSize,fp) << " bytes loaded\n";           //Read the sound data and display the

                                                                                //number of bytes loaded.

                                                                                //Should be the same as the Data Size if OK

    //Now OpenAL needs to be initialized
    ALCdevice *device;                                                          //Create an OpenAL Device
    ALCcontext *context;                                                        //And an OpenAL Context
    device = alcOpenDevice(NULL);                                               //Open the device
    if(!device) return endWithError("no sound device");                         //Error during device oening
    context = alcCreateContext(device, NULL);                                   //Give the device a context
    alcMakeContextCurrent(context);                                             //Make the context the current
    if(!context) return endWithError("no sound context");                       //Error during context handeling
    ALuint source;                                                              //Is the name of source (where the sound come from)

    ALuint buffer;                                                           //Stores the sound data

    ALuint frequency=sampleRate;;                                               //The Sample Rate of the WAVE file

    ALenum format=0;                                                            //The audio format (bits per sample, number of channels)
    alGenBuffers(1, &buffer);                                                    //Generate one OpenAL Buffer and link to "buffer"
    alGenSources(1, &source);                                                   //Generate one OpenAL Source and link to "source"
    if(alGetError() != AL_NO_ERROR) return endWithError("Error GenSource");     //Error during buffer/source generation

    //Figure out the format of the WAVE file

    if(bitsPerSample == 8)
        if(channels == 1)
            format = AL_FORMAT_MONO8;
        else if(channels == 2)
            format = AL_FORMAT_STEREO8;
    else if(bitsPerSample == 16)
        if(channels == 1)
            format = AL_FORMAT_MONO16;
        else if(channels == 2)
            format = AL_FORMAT_STEREO16;
    if(!format) return endWithError("Wrong BitPerSample");                      //Not valid forma
    alBufferData(buffer, format, buf, dataSize, frequency);                    //Store the sound data in the OpenAL Buffer
    if(alGetError() != AL_NO_ERROR)
        return endWithError("Error loading ALBuffer");                              //Error during buffer loading


    //Sound setting variables

    ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };                                    //Position of the source sound
    ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };                                    //Velocity of the source sound
    ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };                                  //Position of the listener
    ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };                                  //Velocity of the listener
    ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };                 //Orientation of the listener                                                                              //First direction vector, then vector pointing up)                                                                               //Listener
    alListenerfv(AL_POSITION,    ListenerPos);                                  //Set position of the listener
    alListenerfv(AL_VELOCITY,    ListenerVel);                                  //Set velocity of the listener
    alListenerfv(AL_ORIENTATION, ListenerOri);                                  //Set orientation of the listener
    alSourcei (source, AL_BUFFER,   buffer);                                 //Link the buffer to the source
    alSourcef (source, AL_PITCH,    1.0f     );                                 //Set the pitch of the source
    alSourcef (source, AL_GAIN,     1.0f     );                                 //Set the gain of the source
    alSourcefv(source, AL_POSITION, SourcePos);                                 //Set the position of the source
    alSourcefv(source, AL_VELOCITY, SourceVel);                                 //Set the velocity of the source
    alSourcei (source, AL_LOOPING,  AL_FALSE );                                 //Set if source is looping sound

    alSourcePlay(source);                                                       //Play the sound buffer linked to the source
    if(alGetError() != AL_NO_ERROR) return endWithError("Error playing sound"); //Error when playing sound
//    system("PAUSE"); 
    std::cout << "Press any key to continue ..."; getchar();//Pause to let the sound play

    fclose(fp);                                                                 //Close the WAVE file
    delete[] buf;                                                               //Delete the sound data buffer
    alDeleteSources(1, &source);                                                //Delete the OpenAL Source
    alDeleteBuffers(1, &buffer);                                                 //Delete the OpenAL Buffer
    alcMakeContextCurrent(NULL);                                                //Make no context current
    alcDestroyContext(context);                                                 //Destroy the OpenAL Context
    alcCloseDevice(device);                                                     //Close the OpenAL Device   

    return EXIT_SUCCESS;                                                        



Splash Screen Issues with GLUT & OpenGL

31 January 2013 - 03:04 PM

Hey all,


I'm trying to create a splash / welcome screen for a game I'm working on.  I had a look online and found some good info but I'm still unable to get it to work.


Ideally I'd like my main to run a function that would initially display the splash screen, after input it would then display the main menu and from there each screen as appropriate, high scores, levels of game etc.


I've tried that and then I've tried an uglier approach but I can't get the right results.  I'm pretty new to OpenGL & GLUT and not fantastic when it comes to C++.


Any help would be hugely appreciated.


Below is code that I'm using / trying to use. stripped of unnecessary 'stuff'


At the moment there's no response when pressing space.  I've tried mouse butting, glut special keys etc but still no joy.


If there's also a more efficient way of doing this then please shout! :D




glutInit(&argc, argv);









void screenSequence(){







void displaySplashScreen(){










void idleSplashScreen() {


    int button = 0;

    //if space is pressed...

    if ( button == 32) {