Visual C++ or C play sounds - c

I'm developping an easy game written in C (Visual C++)
and I want to know if there is a way to play sounds,
thanks
(I'm using Visual Studio)

Take a look at the PlaySound() function.
If you call PlaySound() with the SND_ASYNC flag, the function returns immediately after beginning the sound.
For example:
#include <windows.h>
#include <mmsystem.h>
PlaySound(L"test.wav", NULL, SND_ASYNC | SND_FILENAME);
You'll also have to add Winmm.lib in your project settings.
Here's a quick example that should work:
#pragma once
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
int _tmain(int argc, _TCHAR* argv[])
{
if(!PlaySound(L"test.wav", NULL, SND_ASYNC | SND_FILENAME))
printf("error\n");
else
printf("ok\n");
getch();
return 0;
}
and in stdafx.h:
#pragma once
#include <iostream>
#include <tchar.h>
#include <conio.h>

Related

When I try and use Playsound() in c the compiler doesn't even compile it

So I've been trying to use this function called Playsound() but it just throws an error. the error is C:\Users\ETHANZ~1\AppData\Local\Temp\ctmDDBF.tmp:C:\Users\ETHANZ~1\AppData\Local\Temp\ctmDCC3.tmp:(.text+0xd): undefined reference to `PlaySoundA#12'
also here is my code
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main() {
PlaySound("intro.mp3", NULL, SND_ASYNC);
}
If you are using MSVC as your compiler you can use a library pragma in your source.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment(lib, "winmm")
int main()
{
PlaySound("intro.mp3", NULL, SND_ASYNC);
}

Why does the agwrite function in the cgraph library unexpectedly fail on any config/platform but Win64 release?

I've been trying to get cgraph (https://graphviz.gitlab.io/_pages/pdf/cgraph.pdf) working so I read and write some graph files. I tried writing some very basic code:
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <mysql.h>
#include <graphviz/cgraph.h>
int main() {
FILE *fp = NULL;
fp = fopen("test.dot", "w+");
if (fp == NULL) {
return -1;
}
Agraph_t *g;
g = agopen("test", Agdirected, NULL);
Agnode_t *signal1;
signal1 = agnode(g, "Signal1_ON", TRUE);
Agnode_t *signal2;
signal2 = agnode(g, "Signal1_OFF", TRUE);
Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
agattr(g, AGEDGE, "label", "transitionlink");
agwrite(g, fp);
fclose(fp);
system("pause");
return 0;
}
What should be happening is that the file should be written to test.dot. This code works perfectly fine on Win64 release, but fails on Win64 debug, Win32 debug, and Win32 release. I have double checked the .lib files and .dll files settings in visual studio and in the file directories, making sure to copy the release and debug versions of each platform correctly. However, the agwrite keeps causing a "Microsoft Visual Studio C Runtime Library has detected a fatal error" crash on Win64 debug, Win32 debug, and Win32 release. The weird thing is if I change
agwrite(g, fp); to agwrite(g, stdout);, the code works on all platforms/configurations. I am so confused why this is happening. Here is the source file which contains the code for agwrite if that helps: https://github.com/ellson/MOTHBALLED-graphviz/blob/master/lib/cgraph/write.c
I cannot debug the issue because the source has been compiled into .dlls, and .libs for each platform/configuration.
I appreciate any suggestions/feedback,
Thank you
Edit:
For anyone godly enough to try and get this working on their own system, here are all my binaries, libs, and include files: https://www.dropbox.com/sh/o9tjz7txu4m0k5q/AAAnp8Wu99q9IsFN7kvqZP7Ta?dl=0
Edit 2:
The compiler I am using is MSVC 14 on Windows 10.
I found out that using cgraph directly results in an error when trying to use agwrite(). The solution is to use the GVC abstraction layer which comes with the Graphviz C API to do file I/O. Here is the code that worked:
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <mysql.h>
#include <graphviz/gvc.h>
int main() {
GVC_t *gvc;
gvc = gvContext();
Agraph_t *g;
g = agopen("test", Agdirected, NULL);
Agnode_t *signal1;
signal1 = agnode(g, "Signal1_ON", TRUE);
Agnode_t *signal2;
signal2 = agnode(g, "Signal1_OFF", TRUE);
Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
agattr(g, AGEDGE, "label", "transitionlink");
gvLayout(gvc, g, "dot");
gvRenderFilename(gvc, g, "dot", "test.dot");
gvFreeLayout(gvc, g);
agclose(g);
gvFreeContext(gvc);
system("pause");
return 0;
}
Edit:
Here is the documentation for GVC: https://graphviz.gitlab.io/_pages/pdf/gvc.3.pdf
The reason of crashing is described on official Graphviz site:
This usually happens when the Graphviz library is built using one version of the stdio library, and the user’s program is compiled using another. If the FILE structure of stdio is different, the call to agread() will cause a crash. This is mainly a problem on Windows where we just provide a binary release built with one version of Visual Studio and stdio changes depending on the version of Visual Studio. It can also occur if the user tries to use cygwin or something similar which may also use an incompatible stdio.
https://graphviz.org/faq/#FaqAgreadCrash

TCHAR characters are not displayed correctly

I have a simple code and argv[1] is "Привет".
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <locale.h>
int _tmain(int argc, TCHAR* argv[])
{
TCHAR buf[100];
_fgetts(buf, 100, stdin);
_tprintf(TEXT("\nargv[1] %s\n"), argv[1]);
_tprintf(TEXT("%s\n"), buf);
}
In the console, I write "Мир" and have this result:
If I use setlocale(LC_ALL, ""), I have this result:
What should I do to get the correct string in both cases?
Evidently your program works, except it cannot print correctly on the console window. This is because Windows console is not fully compatible with Unicode. Use _setmode for Visual Studio. This should work for Russian but there could be additional problems with some Asian languages. Use WriteConsole for other compilers.
Visual Studio Example:
#include <stdio.h>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT
int wmain(int argc, wchar_t* argv[])
{
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"%s", L"Привет\n");
return 0;
}

Getter & Setter in C

i'm facing a problem in C where i'm trying to use some getter and setter to share a variable between multiple source file.
I declare here my variable (ok_button) with a getter and a setter:
variable.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include "../libhd_src/libhd.h"
int ok_button;
void set_ok_button(int value){
ok_button=value;
printf("Setting ok");
}
int get_ok_button(){
return ok_button;
}
Here, when i push a button, it sets the variable to 1. (Can't upload the full code of this source file, but i see in my logs that the function set_ok_button is correctly execuded when i press (i see the printf "Setting OK" everytime i press my button))
button.c
#include "../libhd_src/libhd.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
void * button_back_center_short(void *arg){
set_ok_button(1);
return 0;
}
And here, i simply check the value of my variable with the getter function.
read.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include "../../libhd_src/libhd.h"
int main(int argc, char **argv){
while(1){
printf("Value %d", get_ok_button());
usleep(500000);
}
}
The problem is that the value shown in read.c is always "0" even when i press my button and set the value to 1...
Does someone understand what's wrong ? Feel free to tell me if you see a better solution to do that :)
I think your problem may be that you have multiple set_ok_button and get_ok_button functions in different files. Make sure you only have them defined in one file, and in a header add 2 lines declaring (but not defining) the functions:
void set_ok_button(int value);
int get_ok_button();

why I get error: 'strcmp': identifier not found (visual studio 2010)

why do i get error: 'strcmp': identifier not found in visual studio 2010 C++ Express
#include <string.h>
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d",(int)strcmp( "str1", "str2" ));
return 0;
}
Thanks
:( #include <string.h> :(
#include "stdafx.h"
Fun quirk of the MSVC compiler, it generates the exact same error when you compile it like that. Yes, not a lot of 'fun'. It skips everything to find the stdafx.h precompiled header include directive. The string.h doesn't actually get included. Fix:
#include "stdafx.h"
#include <string.h>
Always put the stdafx.h include first.

Resources