PlaySound and char *

Hi,

As most users here, I'm new at C++. I have quite some programming experience, mainly in GML [Game Maker Language], PHP and Visual Basic. I use GML frequently, almost everyday. But it's problem is it does not have a lot of functions. But you can use a DLL, which has a lot more functions. That's why I would really like to learn C++. To start off I thought I could make a simple audio DLL. To load and play files. I've read some tutorials before trying this, but I still have some questions. GML and PHP have an easy-to-understand help, but [I think] the MSDN is too difficult to understand for me [partly because I'm Dutch I think]. But anyway, so far I've got this header file:
1
2
3
4
#ifndef _DLL_H_
#define _DLL_H_
#define export extern "C" __declspec (dllexport)
#endif 

This is just copied from an example, so I do not know exactly what it means. The only thing I actually know is that the word 'export' is defined.
Now a personal problem comes in: I tend to don't understand it if something isn't clear. So I hoped you could explain the rest.

This is the source for the DLL I made:
1
2
3
4
5
6
#include <windows.h>
#include <string.h>

export double sound_load(char *filename, double loop)
	{
	PlaySound("play "+filename,

I know a bit more from this code: '#include' means that a list of functions is included [a library I believe]. 'export' means that the output of the function is given to the calling thing. Furthermore 'double' means a variable of type 'double' is returned at the function and the things between brackets are the arguments and what type they are.
As I said before, I don't get a thing at what they say at MSDN, so I was hoping someone could clearly explain what PlaySound needs [which and what type of arguments].

Btw, it's quite annoying, GML only uses char *s and doubles so could anyone explain to me what a char * is? I know it points to a variable in the memory which points to another one and that variable's value is used.

Ragoune
Read through the Language Tutorial on this site to learn more about C++
http://www.cplusplus.com/doc/tutorial/

You cannot use operator+ on c-strings. You must concatenate them using the usual functions. Windows provides versions of them so you don't need to use <strings.h> if you don't need it. Also, make sure to be const-correct:
1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>

export STDCALL double sound_load( const char* filename, double loop )
  {
  char newfilename[ MAX_PATH ];
  lstrcpyA( newfilename, "play " );   // same as <string.h>'s strcpy()
  lstrcatA( newfilename, filename );  // same as <string.h>'s strcat()
  return PlaySoundA( newfilename, NULL, SND_FILENAME || ((loop) ? SND_LOOP : 0) )
       ? 1.0  // is this the GML 'true' value?
       : 0.0;
  }

I have to wonder, though, if you really mean to do what you are doing. For example, if I call
sound_load( "C:\\WINDOWS\\Media\\tada.wav", 0.0 );
then the program will endeavor to load the file
play C:\WINDOWS\Media\tada.wav
...which is obviously not correct.

Also, you should typically export functions using the STDCALL calling convention -- unless GML expects to read them using the default cdecl calling convention (which I doubt...)

As for only using doubles.. I can't fix that, but you should know what the value for 'true' is. I listed it above as simply '1.0', but you will need to read the GML documentation to know what the value actually is. (If it is any non-zero value, then 1.0 is fine... you could just return PlaySoundA(... directly -- it will properly type-promote to double.)


Before you go too much further, you should read up on the PlaySound() function itself:
http://www.google.com/search?btnI=1&q=msdn+PlaySound
Notice that in order to compile your DLL, you will need to link with Winmm.lib in order to use the function.

Alternatively, you could use LoadModule() directly on the "winmm.dll" (kept in the Windows System directory, so you don't need to provide path information to load it), and GetProcAddress() to find the PlaySoundA() function.


Doesn't Game Maker provide functions for controlling sounds?

Anyway, I've got to go now... sorry I can't give you more information ATM.
Topic archived. No new replies allowed.