Sounds in C?

This is a discussion on Sounds in C? within the Game Programming forums, part of the General Programming Boards category; I saw a code in the net yesterday about playing sounds, and I managed to play it too, using a ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    20

    Sounds in C?

    I saw a code in the net yesterday about playing sounds, and I managed to play it too, using a dev-c++ project file. What I'm now trying to do is put it in my game, though I get compiler errors when I compile it because we have a custom header file named mp.h that contains most of the functions that we need in our game. Here's the code I tried.

    Code:
    #include <windows.h>
    #include <stdlib.h>
    
    
    using namespace std;
    
    int main()
    { int x = 1;
       while (x == 1)
        PlaySound("gameover.wav", NULL, SND_FILENAME);
    
        system("PAUSE");    
        return 0;
    }

    I saw another code in the internet about mmstream.h and I managed to get pass the mp.h compiler error, but it gets a linker error saying "Undefined reference to PlaySoundA@12". Here is the revised code.

    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <mp.h>
    #include <mmsystem.h>
    #pragma comment(lib, "winmm.lib")
    
    int main()
    { int x = 1;
       while (x == 1)
        PlaySound("gameover.wav", NULL, SND_FILENAME);
    
        system("PAUSE");    
        return 0;
    }
    Any information helpful would be much appreciated. Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > #pragma comment(lib, "winmm.lib")
    I don't think dev-C++ understands these pragmas (it's another MS thing)

    In the project settings, look for "linker" and "additional libraries".
    Add winmm.lib to that list (noting any specific naming conventions of existing libraries)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Is there any way I can do it without making the source code a project? I haven't asked our professor if we can submit our project code in .dev format, but our specifications say that we only submit the .c code. I tried recompiling the .c file from the project but it didn't work.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    From the command line, it would be something like
    gcc prog.c -lwinmm.lib

    But then your prof would need to know to do that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    >>using namespace std;
    This is entirely C++ code. You might want to drop it if you are writing C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Thanks a bunch!

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    how do I play two wav files at the same time? putting |SND_NOSTOP at the first wav file doesn't work. The only nearest thing I got to was playing the two sounds, but one had to stop to let the other one play, and when it finished, the song restarted. I need to let one be a background music while the other one handles the effects.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    RTM?
    SND_ASYNC


    The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Already there, but not working. Also, how do you store the wave file into the application so that I can play the sounds without needing the wave files? I'm using a Dev-C++ compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sounds in game
    By Gordon in forum Windows Programming
    Replies: 7
    Last Post: 10-07-2008, 10:14 AM
  2. Looking for tiles and sounds
    By ~Kyo~ in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 11-13-2004, 04:32 PM
  3. Replies: 7
    Last Post: 06-20-2003, 12:05 PM
  4. Sounds
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-17-2003, 06:27 AM
  5. Sounds, or no sounds?
    By face_master in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2001, 05:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21