Learning C++ 15 years later, Pls help.

I did some c++ programming on my HND years ago, I have just found an old program I wrote that Displays an image then a menu with choices.

I have a few questions, borland c++ had a dos.h header file with some nice functions like sound and delay.

Is there a similar header for DEV C++(which I'm using now)?

Or how can you play a tone at a set frequency?

Also how can you display an image at the begining of you program?

We used to have to call a pcxer.exe file??

Please help.

Andy

Last edited on
You want to build programs with a GUI?
http://www.wxwidgets.org/
http://wxdsgn.sourceforge.net/ (upgrade to wxDev-Cpp)
Or get Microsoft's Visual Studio.

If you want to go beyond a menu, maybe you're looking for a game library/engine...
SFML, SDL, Delta3D.

Welcome to the future. No more quick hacks for you!
Playing a sound in DEV C++.
I don't normally use this compiler/IDE, and this gave me some trouble getting the linker to include "libwinmm.a".

Sounds can be played using the PlaySound command. Here's a sample program:

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
67
68
69
70
71
72
73
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <mmsystem.h>
#include <iostream>
#include <fstream>
#include <conio.h>

//#pragma comment(lib,"libwinmm.a") // this line gave me trouble 
//
//     Had to manually add the library: libwinmm.a
//     in  project options, linker.

HINSTANCE getHinstance();
char *  sndbuf = 0;
bool read_file();

int main(int argc, char *argv[]) 
{
    std::cout << "Trying to play sound ...\n";

    HINSTANCE HInstance = getHinstance();

    bool ok = read_file();
    if (ok)
        PlaySound(sndbuf, HInstance, SND_MEMORY | SND_ASYNC);

    std::cout << "press key to exit";
    
    getch();
    
    PlaySound(NULL, 0, 0); // Stop any playing sound 
    delete [] sndbuf;      // before deleting buffer.

    return 0;
}

//---------------------------------------------------------------------------

HINSTANCE getHinstance() 
{
     // Get the handle of the .exe

    return GetModuleHandle(0); 
}

//---------------------------------------------------------------------------

bool read_file()
{
    using namespace std;

    char filename[] = "sound22.wav";

    ifstream infile(filename, ios::binary);

    if (!infile) 
    {
         std::cout << "file error: "<< filename << std::endl;
        return false;
    }
    
    infile.seekg (0, ios::end);   // get length of file
    int length = infile.tellg();
        
    sndbuf = new char[length];    // allocate memory
    
    infile.seekg (0, ios::beg);   // position to start of file
    infile.read (sndbuf,length);  // read entire file
    
    infile.close();
    
    return true;
}


There is an alternative way to use Playsound, instead of manually reading the file, use a resource file instead. But I couldn't get DEV C++ to compile this resource file sounds.rc
1
2
010     WAVE    "D:\Audio\bong.wav"
020     WAVE    "D:\Audio\bing.wav"
Last edited on
There is a more fundamental question here raised by the line "Is there a similar header for DEV C++?"

Firstly, if you're using a version of Dev C++ from before version 5 destroy it and get something that was made in the last decade.

Secondly, the idea that different IDEs use different headers shows a fundamental misunderstanding of what programming C++ is. Every C++ compiler should treat the input text source code the same (not quite identically - different compilers do things in different ways, but C++ code is C++ code is C++ code and so long as it is not relying on undefined behaviour, every compiler should produce binaries that do the same thing).

When you want to make a sound, you're stepping out of the C++ language definition. C++ has no understanding of sound or a screen, or keyboard, or the mouse, or any of that - it is all left open and dependent on the OS, which is how it should be. To play a sound, all you can do is ask the OS nicely if it would please play the sound for you.

As such, your OS comes with a set of headers and libraries to enable you to interact with it. I'll repeat that, worded another way; there is no header for Dev C++ to do this - the header is part of you OS and should work equally well no matter which IDE and compiler you are using. So, you should not be looking for some Dev C++ header - you should be looking at your OS.


Also how can you display an image at the beginning of you program?

The exact same answer; this is not something to do with your compiler, or your IDE. This is something you have to ask the OS to do for you.

Things have moved on a great deal since the days of DOS, way back in the mid-nineties.

Sometimes, asking the OS is a bit of a pain. The OS has to be responsive and fast and adaptable, and these things mean that the interface (the API - the functions you call and the libraries you link to) will be quite low-level. Sometimes, you don't need the fine control or the speed. In these cases, you can use what is known as a "widget toolkit". This is a set of libraries that provide you with a simpler interface, and then the toolkit handles dealing with the OS for you. If you're using windows, the direct OS interaction is done with the WIN32 API. Popular toolkits are QT and wxWidgets, but there are many many more to meet many needs.
First of all I have to thank everybody for the detailed responses, wow!.


I am using the latest dev c++

The penny has dropped.

I need to forget about all my old programs and start fresh.


My dinner break is over now, I'm sorry that this is a short reply but to be honest you have answered all my questions and pointed me in the right direction.

Thanks everybody and extra credit to mosschops.

Andy










andyedtec wrote:
I need to forget about all my old programs and start fresh.

Well, some of the high-level ideas and algorithms may still have merit. But I often find when I look at (my) old code, it often looks like it needs re-writing using modern methodologies, even if the old code would still work.
Topic archived. No new replies allowed.