title: How do i play sound in my C++ Windows Game?
body: <p>Windows 7 Ultimate 64Bit, I am using CodeBlocks 16.01. I am working on my OpenGL college project, I'm trying to add a gun sound effect when the player shoots using the 'spacebar' key.</p>
  
  <p>The sound only works when i create a console project or empty project and the sound plays just fine, I am aware of the library "winmm.lib" that i have to link, and it works just fine as a CONSOLE PROJECT, here is the code for this.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  #include &lt;mmsystem.h&gt;
  
  using namespace std;
  
  int main()
  {
      PlaySound("shotgun.wav",NULL,SND_SYNC);
      return 0;
  }
  </code></pre>
  
  <p>The problem begins when i now use this code in my Glut Project, what happens is instead of playing "shotgun.wav" it plays one of the windows system sounds called "Default Beep", When the shooting button "spacebar" is pressed. The game was freezing a few seconds until the sound has completed, however i fixed this issue by replacing thw above line with this one :</p>
  
  <pre><code>PlaySound(TEXT("shotgun.wav"), NULL, SND_FILENAME | SND_ASYNC);
  </code></pre>
  
  <p>It doesn't freeze anymore, But Still, It continues to play that Windows System sound instead of the shotgun.wav. The .wav file is IN the project, i'm pretty sure it's in the right place. here is the sample for shoot code from my Glut project (i only copied the relevant piece of code) :</p>
  
  <pre><code>void Keys(unsigned char key, int x, int y) {
  key = tolower(key); //Just in case CAPS LOCK is ON.
  
      if(key==27)
          exit(0); //Escape key, Exit the game
  
      switch(key){ 
      case ' ':  //SPACE BAR
  
              /* some code here */
  
              PlaySound(TEXT("shotgun.wav"), NULL, SND_FILENAME | SND_ASYNC);
          break; //.....Code continues to the next case....
  </code></pre>
  
  <p>To force the gun sound effect, i had to go to Windows Sound settings and "Change System sounds", lol yep i actually had to replace "Default Beep" with my "Shotgun.wav", i then returned to the game and compiled, and wala ! It was working. BUT of course this is NOT what i want, cause obviously this will only work on my PC and we don't want that.</p>