Well I need to have an instruction running in the same time as the main thread,
example: instruction would be play an audio then destroy the sample. Normally I need to call that function that plays the audio then destroys it and wait until it does so. What I want is to call that class function in a separate thread and start it so that the game can continue without waiting for the sound to be played and then destroyed.
ALLEGRO_THREAD *t = al_create_thread(playdead,&sound); al_start_thread(t); void *playdead(ALLEGRO_THREAD *me,void *arg) { sound.playkill(); return NULL; }
this gives an error at sound.playkill() : IntelliSense: a nonstatic member reference must be relative to a specific object
Am I doing it wrong ?
void * playdead(ALLEGRO_THREAD * me, void * arg){ Sound * sound = (Sound*) arg; sound->playKill(); return NULL; }
Thats what the arg argument is for.
Got it thanks ! hope it works
EDIT: it works though when I play another sample after it crashes ....
strange that I got it to play 2 samples in the same time from main thread and the thread I created... but the third sound just makes the program crash
"Unhandled exception at 0x5bd1d190 in game.exe: 0xC0000005: Access violation writing location 0x5c09d000."
code:
game:
ALLEGRO_THREAD *t = al_create_thread(playdead,&sound); al_start_thread(t); //stuff //press 1 return to main menu: al_destroy_thread(t); sound.stopfightmusic(1); sound.startbackmusic(); //this line triggers the error return 0;
sound class
You can't pass stack allocated variables to a thread and expect the thread to work if the function that created the thread returns. You should allocate your sounds dynamically (malloc or new) in that case.
hmm will see if it works
That code is HORRIBLY formatted. You will find it difficult to get people to help you with code formatted like that. I suggest you read online about how to properly style C or C++ code (there are different ways that are purely subjective, but your way is just unacceptable).
You seem to be lacking some of the core knowledge of C++ classes and objects and parameter passing in C or C++. Do you understand what kazzmir is talking about?
yeap I understand it and yea I know my code style is horrible but I get it way better my way will try and change it in time