sound effects

so i am making an rpg game. it is based in the console im trying to figure out how to put a simple tile map to it but at the moment. not going super well.

game can be found here:
https://sites.google.com/site/temprpgquestion/go-here-for-the-program

what i was wondering is what are the different ways people go about making sound effects? sounds such as what i am looking for. i want to add sound to fishing, mining, and woodcutting. and then probably knocking on doors and cooking, but i think i know what to do for that.

I am trying to do this game as much as on my own as i can. it is poorly written but i dont care.

Any help will be greatly appreciated.
it is based in the console


You are making things unnecessarily difficult on yourself.

Ditch the console. Get a graphic lib. They are often bundled with audio libs as well. SFML, for example, gives you both.

Graphics are not as hard as you might think.




If you are really opposed to that idea... and you really want to keep the console handicap and just want to learn how to output sound in the console....


There is nothing in the C++ standard library to output sound. So you have 2 options:
1) Use an external lib (like SFML, BASS, FMOD, SDL, etc)
or
2) Use the platform specific API (non-portable, but does not require you to download anything extra).


If you are on Windows, WinAPI offers a very simplistic 'PlaySound' function which can be used to load and play .wav files at runtime with ease. The downside is its functionality is very limited and it is not practical for heavy use like you may want in a game.
Last edited on
i have used allegro 5 and managed to get a simple tile map going but other then that i was having a hard time learning how to have actions actually happen. i dont know much at all about collision with tiles and stuff like that. so i put that off and just started working on this instead keeping in the console.
@ OP: I recognize this code and I remember when you posted here last. I'm kind of glad to see that you're still going strong at this but at the same time I'm horrified that you ignored all of our advice from the last time. You're going to kick yourself once you figure out how much even simple objects would help you with this.

Since you're already using the Windows API, you should replace your 'pause()' function with the Windows 'Sleep()' function so that you don't spike your CPU while essentially idling. Just as a 'good practice' kind of consideration: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx
Last edited on
yeah i know i haven't taken in any of the suggestions. i only recently started working on this again about 4 days ago. i have been very busy.

a question relating to that before though. is there a way to have the txt files that have the maps on them held in a folder within the folder containing all of the program? or would i just have to stick all of the txt files in the same folder with the program?
Last edited on
i have used allegro 5 and managed to get a simple tile map going but other then that i was having a hard time learning how to have actions actually happen.


Flow in the console is different from flow in every other program. You actually seem to be mimicing normal gameflow in your program by using GetAsyncKeyState rather than using normal input, so the transition should not be very difficult for you.

The typical flow of a game is something like this:

1
2
3
4
5
6
while( game_is_running )
{
    ProcessUserInputAndMessages();
    UpdateAllObjects();
    DrawTheScreen();
}


In your case, you would spin on this (and keep drawing the same map over and over again) until the user presses a button, in which case you would react, then draw something different the next time around. Since you don't really have any animation or objects which need updating, you could even skip the UpdateAllObjects step.


i dont know much at all about collision with tiles and stuff like that.


You could do collision the exact same way you're doing it now. Have an array of tiles, and have the user move through them. The only difference is you would associate a graphic to draw with each tile instead of a character to print.


That said... since it looks like you're already using Windows specific code... PlaySound might suit your needs.

 
PlaySoundA( "my_sound_effect.wav", NULL, SND_FILENAME );


That will play the given wave file and pause the program until the sound completes. If you do not want to pause the program you can add the async flag:

 
PlaySoundA( "my_sound_effect.wav", NULL, SND_FILENAME | SND_ASYNC );
Also... you appear to be doing the bad newbie move of making the game code-driven. As in... everything is a big else/if chain and the game logic is intermixed with the data.

This makes games very hard to write and maintain. I go over alternative ideas in the below linked thread:

http://www.cplusplus.com/forum/beginner/71141/#msg379639

Note that in that thread I'm talking to someone who's making a different kind of game so my examples won't be directly on point, but hopefully they'll give you ideas of how you could separate your code and data a bit better for your game.
is there a way to have the txt files that have the maps on them held in a folder within the folder containing all of the program?

The easiest way to do this would be to just use fully qualified paths. Remember that backslashes '\' have to be doubled up on (like this: '\\') or replaced with forward slashes '/' in the file path.
so i don't really see what using structs would help with in my game. but when i get around to inventory items that actually have stats such as attack and defense and stuff then i will be using structs.

and another question. can someone explain how i would go about making the inventory system Boolean instead of integers. i don't know what exactly that means.

when i look at it i see the items in the inventory and just think that i should use integers but computergeek you said that i should instead make it Boolean.


also, one of the spots that i hate the most in my code is the banking system. it is so much writing in all the different items and seeing if you are able to withdraw or deposit what and how much. but it works the way i have it. any tips on that one?
Last edited on
I made that suggestion because I saw that, at the time, you were basically testing to see if the player had a certain item or not before performing an action. Equipment never wore down or broke so it seemed appropriate.

Your "banking system" is a prime example of where objects would help you out. You would have to over haul your inventory system for this to work though, it would essentially be an vector of a generic class type like this:
1
2
3
4
5
6
7
8
9
10
11
struct ITEM
{
    public:
        ITEM(std::string name): Item_Name(name)
        {
             Quantity = 0;
         }

        std::string Item_Name;
        unsigned Quantity;
};


Then you could cut your banks display code down to something like:
1
2
3
4
for(unsigned i = 0; i < Player_Bank_Inventory.size(); ++i)
{
      std::cout << "You Have " << Player_Bank_Inventory[i].Quantity << " " << Player_Bank_Inventory[i].Item_Name << " in storage\n";
}
ok. wow yeah that does make sense. a lot of work to go back and change all of that but i can do it.

when i change it over to the struct how would i go about saving that to the txt file when the game is saved?

before it is just a simple saves the integer to the txt file and reads it. but i am not great with objects as you can see from my code which is completely object free.
can i do something like this? or would i have to make each item individual?



struct ITEM
{
public:
ITEM(std::string name): Item_Name(name)
{
Quantity = 0;
}

std::string Item_Name;
unsigned Quantity;
}wood, oak, willow, all the other logs;

and then call them using

wood.quantity = 5; or something along those lines?
so i don't really see what using structs would help with in my game.


It's less about structs and more about separating your code from your data. structs just tend to make that easier.

Have your data describe itself, that way the code does not need to describe it. When you find yourself copy/pasting code and just making minor changes, you are probably doing something very wrong. You should be able to treat multiple instances of minor differences the same way.

The main logic here is simple and short. So the code should be simple and short. What makes this more complex is the number of different available items (ie: the data -- not the code)


Case in point... you have a number of huge else/if chains where the only difference between each of the items is the type of wood you burn. All of that could be reduced to maybe 10 lines of code once you have structures that describe your data in place.


and another question. can someone explain how i would go about making the inventory system Boolean instead of integers. i don't know what exactly that means.


Booleans wouldn't help you with your inventory. The problem with your inventory is you are using unique variable names (again mixing code with data). IE: a variable named 'Arctic_Pine_Log' to indicate how many pieces of arctic pine log the user has. Which again.. forces you to create monstrously big else/if chains to use the right variable for something as simple as printing the inventory... which really should only take like 2-3 lines of code


You want structs to describe the item... and a vector or an array to keep track of which items the user has. The more items you can treat the same, the better.

To keep this example simple, and to not overwhelm you, I will not introduce you to polymorphism, which would probably make this even easier. Instead, I'll use C-ish unions + type identifiers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
    Here is the framework to describe our data
 */
 
// Item descriptors
//  First... the group that the item belongs to
enum ItemGroup
{
    Wood,
    Ore,
    Fish,
    Tool,
    //etc
};

// Now, a structure to describe each individual item
struct Item
{
    std::string     name;           // the name of this item as it is printed to the user
    ItemGroup       id;             // what kind of item do we have?  What group is it in?
    int             purchasePrice;  // how much does it cost to buy this item?
    int             sellPrice;      // how much to sell it?
    int             weight;         // how much does it weigh to carry?
    
    //etc
};

/*
    Now... the actual data.  Ideally this would be stored externally in a text file
    or something, so that new items can be easily added later just by modifying that file.
    To keep this simple, let's just have a big lookup table of all the items in the game
 */
 
 const Item ItemList[] = {
     {  "Wood Log",   ItemGroup::Wood, 100, 50, 5 };  // Wood log, is of type wood, costs 100, sells for 50, weighs 5
     {  "Oak Log",    ItemGroup::Wood, 150, 60, 6 };
     {  "Willow Log", ItemGroup::Wood, 175, 70, 4 };
     //... etc
     {  "Gold Ore",   ItemGroup::Ore, 1000, 500, 10 };
     //... etc
     
     // This will actually be a huge table for how many items you have.
     // But having the table huge here, means the rest of your code can be small.
};


/*
    Now to keep track of inventory
*/
struct InventoryEntry
{
    Item        item;       // the item in your inventory
    int         qty;        // how many of that item you have
};

std::vector<InventoryEntry>   inventory;        // all the items the player has in their inventory


//
// ...
//

//  Now when you want to print the user's inventory, you no longer need a large else/if chain.
//  You can just use a loop and print each item in the inventory vector:
for(auto& i : inventory)
    cout << "You have " << i.qty << " " << i.item.name << "s.\n"; // Ex: "You have 2 Gold Ores.\n" 



EDIT:

Doh, there were like 4 replies in the type I typed this up. Oh well.
Last edited on
You could save and load essentially the same way. Something like this:

1
2
3
4
5
6
7
8
9
10
11
void Save(std::vector Player_Inventory)
{
      Save_File.open("Save.txt");
   
     ///CODE FOR PLAYER SKILLS

      for(unsigned i = 0; i < Player_Inventory.size(); ++i)
      {
             Save_File << Player_Inventory[i].Item_Name << " " << Player_Inventory[i].Quantity << "\n";
        }
// ... 


And this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Load(std::vector Player_Inventory)
{
      Save_File.open("Save.txt");
      
       std::string ItemName;
       unsigned quant = 0;
   
     ///CODE FOR PLAYER SKILLS

      for(unsigned i = 0; i < Player_Inventory.size(); ++i)
      {
             Save_File >> ItemName;
             Save_File >> quant;
              
             Player_Inventory.push_back(ItemName);
             Player_Inventory.back().Quantity = quant;
        }
// ... 


At this point I feel like I'm encouraging you to keep moving forward with writing this on the console though, that is NOT my intention.
Topic archived. No new replies allowed.