C++ Playing Sound Lag - c++

For my program I am making pac-man. Every time the pac-man eats a dot I want to play the munching sound. I already know how to play the sound but it causes lag too much lag. The lag is about 0.5 seconds but overall it slows down my game a bunch. Any suggestions?
#include <iostream>
#include <windows.h>
#pragma comment (lib , "winmm.lib") // Used for sound
using namespace std;
int main()
{
PlaySound(TEXT("Sounds\\pacman chomp.wav"), NULL, SND_FILENAME|SND_ASYNC);
system("pause");
}

If you want real "twitch time" audio you should probably look at DirectSound, that's what it was created for. The waveOutXXX APIs are better than PlaySound, but DirectSound is better still.

Related

Keep MATLAB engine open in C++ dll

I want to create a dll which keeps an instance of the MATLAB engine open for use by other scripts. That is, the goal is to not have to keep initialising and closing the MATLAB instance, which takes time.
For some background, I have a working C++ program which initialises a MATLAB engine and contains a load of functions to do various things in MATLAB. As some kind of minimum example, I have the three scripts below.
header.h
#pragma once
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <tuple>
#pragma comment (lib,"libmat.lib")
#pragma comment (lib,"libmx.lib")
#pragma comment (lib,"libmex.lib")
#pragma comment (lib,"libeng.lib")
// Example function definition
void setWorkingDir(std::unique_ptr<matlab::engine::MATLABEngine>& matlabPtr, std::string dir);
MatlabFunctions.cpp
#include "header.h"
// Example function
void setWorkingDir(std::unique_ptr<matlab::engine::MATLABEngine>& matlabPtr, std::string dir){
std::u16string matlabCommand = matlab::engine::convertUTF8StringToUTF16String("cd '"+dir+"'");
matlabPtr->eval(matlabCommand);
}
Main.cpp
#include "header.h"
int main(){
using namespace matlab::engine;
matlab::data::ArrayFactory factory; // Create MATLAB data array factory
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); // Start MATLAB engine
// Do various functions
}
My goal is to get rid of Main.cpp and create a dll which keeps keeps the MATLAB engine open, allowing the functions in MatlabFunctions.cpp to be run without having to start the MATLAB engine each time.
Is this possible? And if so, how can it be done?
Some people suggested to use IPC here for the task to be done. Using something like COM is not cross-platform and, well, pretty outdated at the moment. As a modern and cross-platform solution, I'd use an actual server application (which will start MATLAB engine and wait for requests) with, for example, gRPC running. Here's tutorial, pretty good one.
Other than that, you can specify your own networking protocol to run your tasks on the server application without the need of IPC, but the gRPC solution works simply out of the box, no bloat coding included.

Need a simple comparison with time() to execute things

I'm using an Adafruit Feather HUZZAH ESP8266 controller for a wordclock, which is a birthday present and should also be a marriage proposal.
Last time I programmed was years ago in school, so I'm rather noobish to that.
My plan is to make a simple condition if the desired time is reached (its only once), then light up some LEDs (marry me and her name) in heartform.
To not be overwritten a minute later i think an infinite while loop would be best. Push Reset and the clock gets time again over NTP.
So I searched for correct unix timestamp and tried things like
#include <iostream>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
int main(void)
{
while (time()==1461337750)
or
time_t t;
t = time(NULL); //oder time(&t);
if (t==1461335820)
}
but it didn't show anything.
I don't know what is the right way and need help, please! I searched the Inet for hours but couldn't find a similar task or I thought it wasn't.

How do I time a process in c++?

I'm working on a project where I'm to time the creation of fork() and pthread_create(). We're supposed to time how long it takes to do each of these tasks by using system calls to create a personalized timer class. For assistance, the professor has told us to check man -k time.
I'm new to both using system calls and using man pages for documentation, so I'm entirely lost. So far the code I'm trying to get working is this:
#include "Timer.h"
#include <iostream>
#include <signal.h>
#include <ctime>
#include <time.h>
using namespace std;
Timer::Timer() {
timer_t * tid;
timer_create(CLOCK_REALTIME, NULL, tid);
int time = timer_gettime(tid, 0);
cout<<time;
}
When compiling, eclipse tosses me these errors:
undefined reference to 'timer_create', line 20
undefined reference to 'timer_gettime', line 21
The internet has pointed me in the direction of including the -lrt library when compiling, but I can't find anything that says how to do that, suggesting that perhaps it's entirely wrong.
So, am I on the right path? If I am, how am I supposed to get this code working?
Edit:
I got the clock() based timer working. The downside is that we're timing fork(), which isn't registering as taking any time at all when timing it. Any ideas?
You can use this
time_t start,end;
start=clock();//predefined function in c
//after the user defined function does its work
end=clock();
t=(end-start)/CLOCKS_PER_SEC;

Communicating with XBoard (chess engine) (C++/C)

I was just toying around with making a basic chess engine. I was able to get a lot of advice from http://web.archive.org/web/20070704121716/http://www.brucemo.com/compchess/programming/alphabeta.htm, but the real site is down and not all the pages are archived. (Anyone know where to find a full version of Bruce's site?)
But now to the real question: how do I communicate with XBoard? I understand it is via stdin and stdout, but I've been having problems in code. Basically, to get started, I just want to
receive input from XBoard and print it to the console/screen
Give a move of hard-coded input to XBoard and have it make the move
program utility functions and have a random chess ai which chooses random moves.
After that, I can start implementing real things like alpha-beta searching.
I am stuck on the first two things right now. Here is some code I have tried to write/borrowed.
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define STR_BUFF 256
using namespace std;
int main (int argc, const char * argv[])
{
char input[STR_BUFF];
char output[STR_BUFF];
while(true){
fflush(stdout);
// read input
if (!fgets(input, STR_BUFF, stdin)){
printf("terminated");
return 0;;
}
printf("%s", input);
}
return 0;
}
I am just getting back into C after about 6 months break and this is the first project that I have used stdin/stdout pipelines to communicate with another program (minus a few basic programs) so I would appreciate any help and any explanations. I know programming a chess engine is a herculean task, but I have already programmed the rules of chess before and what I can find of Bruce's site is absolutely amazing.
You are doing it almost right: get a command from XBoard with fgets, then report a move with printf and fflush. (One thing is wrong, though: you don't need to 'print the command to the console/screen'; you are not communicating with the console/screen; you only read commands from XBoard and send moves back to XBoard).
Probably, it would be easier to start with some existing code. Try to read sources for GNU Chess. Or download sources for any other chess engine, supporting XBoard protocol.
And here is other question with lots of information on chess engine programming: "What are some good resources for writing a chess engine?".
I think that you're looking for pipe(), included in unistd.h. Take a look at Can popen() make bidirectional pipes like pipe() + fork()? for notes on implementation.

performance counter

Here is the code, to get the bandwidth of the system from the system, using counter .
I'm getting "PdhCollectQueryData failed"
ie.error code = "PDH_NO_DATA"
plz tell me where i'm going wrong.?????
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <pdh.h>
#pragma comment(lib,"pdh.lib")
Why didn't you include the output from those Enum calls, which presumably worked for you since they are not commented out?
I'd be surprised if there is a space in COUNTER_PATH after the second opening parenthesis, as you have here. I'd expect the name to not have a leading space.
CONST LPCSTR COUNTER_PATH = TEXT("\\Network Interface( NVIDIA nForce Networking Controller - PacketScheduler Miniport)\\Current Bandwidth");