im trying to play a looping sound in openGL/SDL with openAL but when im calling it its just looping infinitely and its stuck at the start ive got no idea how to fix this so i came here hoping some1 knows heres my code of how im calling openAL
SDL_Thread * AudioBG;
bool Init(void)
{
m_Music->LoadFile("Sonido/BGSP.wav",true);
}
int MusicThread(void *arg)//Creating the Thread
{
arg+=0;
for(;;)
{
m_Music->PlaySound();
}
return 0;
}
void Draw()//this is my cycle of the game
{
gui->Draw(x,y,z);
AudioBG = SDL_CreateThread(Music,NULL);
}
this is my class Sound.cpp
Sound::Sound()
{
Device = alcOpenDevice((ALCchar*)"DirectSound3D");
Context = alcCreateContext(Device,NULL);
alcMakeContextCurrent(Context);
alGetError();
}
Sound::~Sound()
{
Context = alcGetCurrentContext();
Device = alcGetContextsDevice(Context);
alcMakeContextCurrent(NULL);
alcDestroyContext(Context);
alcCloseDevice(Device);
}
void Sound::LoadFile(char archivo[40],bool looping)
{
ALboolean loop;
loop = looping;
alutLoadWAVFile(archivo,&alFormatBuffer,
(void **)&alBuffer,
(ALsizei *)&alBufferLen,
&alFreqBuffer,&loop);
alGenSources(1,&this->alSource);
alGenBuffers(1,&alSampleSet);
alBufferData(alSampleSet,alFormatBuffer,alBuffer,alBufferLen,alFreqBuffer);
alSourcei(this->alSource,AL_BUFFER,alSampleSet);
alutUnloadWAV(alFormatBuffer,alBuffer,alBufferLen,alFreqBuffer);
alSourcef(this->alSource,AL_PITCH,1.0f);
alSourcef(this->alSource,AL_GAIN,1.0f);
if(looping)
alSourcei(this->alSource,AL_LOOPING,AL_TRUE);
else
alSourcei(this->alSource,AL_LOOPING,AL_FALSE);
}
void Sound::Direction(float x,float y,float z,float vx,float vy,float vz)
{
alSource3f(this->alSource,AL_POSITION,x,y,z);
alSource3f(this->alSource,AL_VELOCITY,vx,vy,vz);
}
void Sound::RelativeSound()
{
alSourcei(this->alSource,AL_SOURCE_RELATIVE,AL_TRUE);
}
void Sound::PlaySound()
{
alSourcePlay(this->alSource);
}
im really lost... if i call the function LoadFile and initialize the song out of the game loop and without a thread it plays well but inside the loop and with or without the thread im getting the error i tried to describe :S
Related
I'm new(-ish) to C++, and I'm trying to learn something new every day. Today I'm trying to figure this out.
Do I have to check if Valmis == 1 so it won't continue/return before Realtest() is complete?
This is just an example.
int valmis = 0;
void test::Main()
{
//just some basic stuff
do
{
if (!strcmp())
{
//here too
float testing = 0;
printf("Test? 1=do it Else=Nothing\n");
scanf(" %f", &testing);
if (testing == 1)
{
Realtest();
//Realtest needs to be completed before continuing
if (valmis == 1) //Do I need this or does it continue after RealTest() is complete without this?
return (somethingmate = true);
}
else
{
return (somethingmate = true);
}
}
} while();
return (somethingmate = false);
}
void test::Realtest()
{
//doing something here that I need to do before continuing in main/letting somethingmate to become true
valmis = 1; //do i need this?
}
so i keep gettin an error on (else) and im not to sure what i did wrong, i cant seem to find the problem, please help, im pretty new to coding so heres the entire code i have so far
{
public float speed;
private Rigidbody2D myRigidbody;
private Vector3 change;
private Animator animator;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
change = Vector3.zero;
change.x = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
change.y = Input.GetAxisRaw("Vertical") * Time.deltaTime * speed;
if (change != Vector3.zero)
transform.Translate(new Vector3(change.x, change.y));
MoveCharacter();
UpdateAnimationAndMove();
}
void UpdateAnimationAndMove()
{
{
animator.SetFloat("moveX", change.x);
animator.SetFloat("moveY", change.y);
animator.SetBool("moving", true);
} else {
animator.SetBool("moving", false);
}
}
void MoveCharacter()
{
myRigidbody.MovePosition(transform.position + change.normalized * speed * Time.deltaTime);
}
}
You can't use else, without a corresponding if before it.
if(condition)
{
// IF condition is true, this gets executed
}
else
{
// ELSE this gets executed
}
I am having issues in a code having structure similar to the following minimum example. There is only one instance of MainClass. It makes new instance of Classlet on each call to its MainClass::makeclasslet()
I have multiple classlets writing to a single list buffer. After some time I need to copy/ dump the values from list buffer (FIFO).
The problem is that I am getting the following output in MainClass::clearbuffer()
>>>>>>>>>> 704 >>>>>>>>>>>>>>>>>>> Buffer size: 65363..... 1
I am unable to understand why the std::list::empty() returns true even when the buffer is locked with an atomic bool flag.
I have tried moving the call to clearbuffer() (in addval()) to the main application thread so that not each Classlet event calls clearbuffer().
I have also tried adding delay QThread::msleep(10); after setting busy = true;.
But some time after the application starts, I am getting the output shown above. Instead of popping all 65363+704 values in the list, it only popped 704 and broke the loop on list::isempty() being true (apparently).
class MainClass : public QObject {
Q_OBJECT
private:
std:: list<int> alist;
std::atomic<bool> busy;
MainClass() {
busy = false;
}
~MainClass() {
// delete all classlets
}
void makeclasslet() {
Classlet newclasslet = new Classlet();
// store the reference
}
void addval(int val) {
alist.push_back(val);
if (alist.size() > 100)
{
if (!busy)
{
clearbuffer();
}
}
}
void clearbuffer() {
if (!busy)
{
busy = true;
int i = 0;
while (!alist.empty())
{
i = i + 1;
// save alist.front() to file
alist.pop_front();
}
printf(">>>>>>>>>> %d >>>>>>>>>>> Buffer size: %d ..... %d\n", i, m_lstCSVBuffer.size(), m_lstCSVBuffer.empty());
busy = false;
}
}
}
class Classlet {
private:
Mainclass* parent;
void onsomeevent(int val) {
parent->addval(val);
}
}
I am using qt5.9 on Ubuntu 18.04. GCC/ G++ 7.5.0
I am new to C++. I've wrote code in C# and PHP.Since I am using Unreal engine I am trying to learn C++. For my project I need to make a screenshot in-game and show it immediately so I want to get it as a texture.
I made a blueprint node which calls this function i've made:
void UMyBlueprintFunctionLibrary::TakeScreenshot()
{
FScreenshotRequest::RequestScreenshot(true);
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Tried to take screenshot");
}
When I hover my mouse above RequestScreenshot I see the following pop-up:
"Screenshot can be read from memory by subscribing to the viewsport OnScreenshopCaptured delegate"
So that is what I try to do but I have no idea how I looked up this:
https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UGameViewportClient/OnScreenshotCaptured/
Can someone tell me how to implement this and how you see/know how to implement it?
I have an alternative, no delegate, but FRenderTarget::ReadPixel() to some buffer you allocated, by implementing your own UGameViewportClient (inherit it), and overriding Draw() function.
I'll show the essential codes, but not complete.
void UMyGameViewportClient::Draw(FViewport* Viewport, FCanvas* SceneCanvas)
{
Super::Draw(Viewport, SceneCanvas);
if (any_condition_you_need) {
CaptureFrame();
}
}
void UMyGameViewportClient::CaptureFrame()
{
if (!Viewport) {
return;
}
if (ViewportSize.X == 0 || ViewportSize.Y == 0) {
return;
}
ColorBuffer.Empty(); // Declare this in header as TArray<FColor>
if (!Viewport->ReadPixels(ColorBuffer, FReadSurfaceDataFlags(),
FIntRect(0, 0, ViewportSize.X, ViewportSize.Y)))
{
return;
}
SaveThumbnailImage();
}
void UMyGameViewportClient::SaveThumbnailImage()
{
IImageWrapperModule& wrappermodule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
auto wrapper_ptr = wrappermodule.CreateImageWrapper(EImageFormat::PNG);
for (int i = 0; i < ColorBuffer.Num(); i++)
{
auto ptr = &ColorBuffer[i];
auto r = ptr->R;
auto b = ptr->B;
ptr->R = b;
ptr->B = r;
ptr->A = 255;
} // not necessary, if you like bgra, just change the following function argument to ERGBFormat::BGRA
wrapper_ptr->SetRaw(&ColorBuffer[0], ColorBuffer.Num() * 4,
ViewportSize.X, ViewportSize.Y, ERGBFormat::RGBA, 8);
FFileHelper::SaveArrayToFile(wrapper_ptr->GetCompressed(), *ThumbnailFile);
}
I was able to run this code without getting an error on the iPhone simulator. However, when I run it on my iPhone, I am getting a EXC_BAD_ACCESS error. Here is the code:
CCAnimate * explosionAnimate = CCAnimate::create(explosionAnimation);
CCCallFuncN * callFuncN = CCCallFuncN::create(this,callfuncN_selector(GameLayer::removeChildFromParent));
CCFiniteTimeAction * explosionSequence = CCSequence::create(explosionAnimate, callFuncN);
CCSprite * explosionSprite = CCSprite::createWithSpriteFrameName("explosion_frame_1");
addChild(explosionSprite);
explosionSprite->setPosition(point);
explosionSprite->runAction(explosionSequence);
}
void GameLayer::removeChildFromParent(CCNode * child)
{
child->removeFromParent();
}
The error occurs when CCSequence::create(...) is called. Debugging through CCSequence::create(...)
CCFiniteTimeAction* CCSequence::create(CCFiniteTimeAction *pAction1, va_list args)
{
CCFiniteTimeAction *pNow;
CCFiniteTimeAction *pPrev = pAction1;
while (pAction1)
{
pNow = va_arg(args, CCFiniteTimeAction*);
if (pNow)
{
pPrev = createWithTwoActions(pPrev, pNow);
}
else
{
break;
}
}
return pPrev;
}
I am seeing that "createWithTwoActions" get's called twice. That doesn't seem right. On the 2nd call to "createWithTwoActions". The error occurs within CCFiniteTimeAction, specifically at the getDuration() inline function:
inline float getDuration(void) { return m_fDuration; }
Any ideas why this would be occurring?
I was able to resolve the issue by calling CCSequence::create() like:
CCSequence::create(explosionAnimate, callFuncN, NULL);
instead of:
CCSequence::create(explosionAnimate, callFuncN);
I'm guessing this has to do with the nature of CCFiniteAction pointers and C++ variable length arguments.