Tetris background sound - tetris

I am making a tetris game using glut libraries.I want to have sound in background.
I have this code.Will it work?
BOOL sndPlaySound(
LPCTSTR lpszSound,
UINT fuSound
);
What I'm doing wrong? How can I solve it?

sndPlaySound is deprecated, and you should use PlaySound from winmm.{lib|dll}
For example:
PlaySound(TEXT("tetris.wav"), NULL, SND_FILENAME | SND_ASYNC);
The last parameter is flags: SND_FILENAME says the first parameter should be interpreted as a file to be loaded. SND_ASYNC says that the function should return immediately, not wait for the sound to finish before returning the control.
Note that this is all winMM library (#include "windows.h"), and has nothing to do with GLUT.

Related

Correct use of PlaySound function in C++

I'm having some issues utilizing the built-in function PlaySound. I continuously receive two errors, the first being:
argument of type "const char *" is incompatible with parameter of type "LPCWSTR",
and the second being:
'BOOL PlaySoundW(LPCWSTR,HMODULE,DWORD)': cannot convert argument 1 from 'const char [35]' to 'LPCWSTR'.
I can't seem to resolve these issues on my own, and would like some help with figuring out how to get rid of the errors. Here's a section of my source code, including what I believe to be causing the error.
#include <iostream>
#include <string>
#include <iomanip>
#include <dos.h>
#include <windows.h>
#include <playsoundapi.h>
#include <mmsystem.h>
using namespace std;
int main()
{
PlaySound("C:\\Users\\Cristian\\Desktop\\cafe.mp3", NULL, SND_FILENAME | SND_ASYNC);
return 0;
}
If I am using the PlaySound function incorrectly,please point me to the correct direction.
LPCWSTR is a macro for const wchar_t * - so you need to use a wide-character wchar_t string L"" instead of a normal char string "".
const wchar_t* path = L"C:\\Users\\Cristian\\Desktop\\cafe.mp3";
PlaySound( path , NULL, SND_FILENAME | SND_ASYNC );
The old-school Win32 way would be to use TCHAR with optional #define UNICODE but this is considered an anachronism as the "ANSI" Win32 functions don't support UCS-2/UTF-16 (and MBCS does not refer to UTF-8, surprisingly).
Note that you probably want to use SND_SYNC instead of SND_ASYNC because your program will terminate before the sound finishes plaiyng.
Finally, PlaySound does not support MP3 files - only Wave files - so your code won't work regardless.
To play MP3 files in Win32 you need to use either:
MCI (Media Control Interface - an ancient API from the Win3x days, yet surprisingly is the simplest - needing only a two function calls):
mciSendString("open \"fileName.mp3\" type mpegvideo alias mp3", NULL, 0, NULL);
mciSendString("play mp3", NULL, 0, NULL);
DirectShow - This is the official Windows multimedia API, but it's based on COM and requires you to create a component-graph (file parser, decoders, output devices, etc) so it has a steep learning curve. See here for the minimum code required - almost 60 lines ( https://msdn.microsoft.com/en-us/library/windows/desktop/dd389098.aspx )
Windows Vista introduced MediaFoundation to replace DirectShow, but in my experience it's not much better than DirectShow in terms of programmer-ergonomics: https://msdn.microsoft.com/en-us/library/windows/desktop/ms703190(v=vs.85).aspx
For Windows 10, there is a WinRT API for playback - but I haven't done much research and I don't know if you can call it from "real" Win32 programs or if it's reserved only for sandboxed UWP applications: https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/media-playback

How do i play sound in my C++ Windows Game?

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.
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.
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;
int main()
{
PlaySound("shotgun.wav",NULL,SND_SYNC);
return 0;
}
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 :
PlaySound(TEXT("shotgun.wav"), NULL, SND_FILENAME | SND_ASYNC);
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) :
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....
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.
You need to specify the full file path. Use double slash \\ in between. For example:
sndPlaySound("D:\\New folder(2)\\poiuy\\sound.wav",SND_ASYNC|SND_LOOP);

Load wavs into memory then play sounds asynchronously using Win32 API

I'm writing a simple game, using C++ and the Win32 API. I'd like to load a few sound effects into memory during the initialisation phase (before the game starts). Then I want to be able to trigger those sounds asynchronously during the game.
I've researched a few posts that recommend mmlib, (PlaySound), this works but the examples seem to load from file each time, something like this:
PlaySound("rocket_launch.wav", NULL, SND_FILENAME | SND_ASYNC);
I'd like to load my sounds into memory at the start, then play them whenever. Hopefully I won't need to use a resource file.
How can I do this?
The PlaySound docs say to pass in SND_MEMORY to indicate that the first parameter points to a memory buffer.
So first, load the file into memory, and then pass in the pointer to the buffer, and swap out the SND_FILENAME flag for the SND_MEMORY flag.
To anyone looking for a simple example:
std::string sound = "RIFFªÛ\x5....."; //(Binary of a .wav file)
PlaySoundA(sound.c_str(), NULL, SND_MEMORY | SND_SYNC); //Extracting the binary to a c-style string & playing

PlaySound() mmslib does not play existing sound

EDIT: Solved. Simply the .wav file was not accepted by Windows. I plucked one of Windows own files and renamed it to what my previous file was called and it plays without problem.
I don't know why this can't play the existing file. Windows gives a chime in that something is wrong but I have no clue what.
I added a check right before to make sure it exists. I have also tried absolute paths.
string wavPath = "c:\\frog.wav";
struct stat stFileInfo;
bool blnReturn = (stat(wavPath.c_str(), &stFileInfo) == 0); //this returns true
FILE* fp = fopen(wavPath.c_str(), "r");
if (fp) {
fclose(fp); //this triggers
}
PlaySound(wavPath.c_str(), NULL, SND_FILENAME | SND_ASYNC); //m_hinstance
//C:\\Users\\Wollan\\My Code\\A\\Debug\\frog.wav
//TEXT("frog.wav")
//TEXT(wavPath.c_str())
//(LPCSTR)"frog.wav¨
The file plays fine in WMP.
This following code perfectly works:
PlaySound(L"C:\\Windows\\Media\\Cityscape\\Windows Balloon.wav", 0, SND_FILENAME );
Adding SND_ASYNC fails to play.
The documentation says:
The pszSound parameter is a file name. If the file cannot be found,
the function plays the default sound unless the SND_NODEFAULT flag is
set.
And:
PlaySound searches the following directories for sound files: the
current directory; the Windows directory; the Windows system
directory; directories listed in the PATH environment variable; and
the list of directories mapped in a network. If the function cannot
find the specified sound and the SND_NODEFAULT flag is not specified,
PlaySound uses the default system event sound instead.
No other case is specified for this outcome.
Therefore, that you hear a chime indicates that the file is not being found, despite your assurances to the contrary.
I'd double-check the result of that stat call; I can't even find stat in the documentation; it doesn't appear to be part of Windows.
PlaySound(L"C:\Windows\Media\Cityscape\Windows Balloon.wav", 0, SND_FILENAME );
Adding SND_ASYNC fails to play.
This answer is right!
It is because the ASYNC mode plays the music after the function returns.
Your code may have exited before the music plays.
use int x, cin>>x, after PlaySound function, you will find that it works well.

First and last window don't show up

I'm creating a WinApi application for my programming course. The program is supposed to show an LED clock using a separate window for each 'block'. I have figured most of it out, except for one thing: when creating the two-dimensional array of windows, the first and last window never show up. Here's the piece of code from the InitInstance function:
for (int x=0;x<8;x++)
for (int y=0;y<7;y++) {
digitWnd[x][y] = CreateWindowEx((WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE | WS_EX_STATICEDGE),
szWindowClass, szTitle, (WS_POPUP| WS_BORDER), NULL, NULL, NULL, NULL, dummyWnd, NULL, hInstance, NULL);
ShowWindow(digitWnd[x][y], nCmdShow);
UpdateWindow(digitWnd[x][y]);
}
The same loop bounds are used everytime I interact with the windows (set position and enable/disable). All the windows seem to be working fine, except for digitWnd[0][0] and digitWnd[7][6]... Any ideas as to what is happening?
Open Spy++ and check if the missing windows are really missing or just overlapped by other windows. It's possible that you have some small error in the position calculations code that puts them behind another window or outside of the screen.
To validate your creation mechanism I would check:
the array initialisation HWND digitWnd[8][7]
if the parent window dummyWnd is valid
the return value of CreateWindowEx() != NULL
Another point which comes to my mind is, that you create windows with dimension 0 - no width or height. So maybe it would be a good idea to set the size within CreateWindowEx(...)
Is this your first call to ShowWindow()? If so, according to MSDN, "nCmdShow: [in] Specifies how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow". This could mean that you can fix your program by simply calling ShowWindow() twice. Give it a try and see if it works. Other than that, you'll probably have to provide more of the code for us to look at.