![]() |
|
Classes and Threads |
alex Ioan
Member #12,015
June 2010
|
Well I need to have an instruction running in the same time as the main thread, 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 ?
|
kazzmir
Member #1,786
December 2001
![]() |
void * playdead(ALLEGRO_THREAD * me, void * arg){ Sound * sound = (Sound*) arg; sound->playKill(); return NULL; } Thats what the arg argument is for. |
alex Ioan
Member #12,015
June 2010
|
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 1 ALLEGRO_SAMPLE_ID bkgid;
2
3void sound::startbackmusic()
4 {if(musicon==1)
5 { al_play_sample(bkg,0.55,0,1.0,ALLEGRO_PLAYMODE_LOOP,&bkgid);}}//this line triggers the error
6
7void sound::stopfightmusic(int a)
8 {if(musicon==1)
9 {al_stop_sample(&bkgfid);
10 if (a==1)
11 {al_destroy_sample(bkg_fight);}
12 }
13 }
14
15void sound::playkill()
16 {if (soundon==1)
17 {al_play_sample(kill,1.0,0,1.0,ALLEGRO_PLAYMODE_ONCE,NULL);}}
|
kazzmir
Member #1,786
December 2001
![]() |
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.
|
alex Ioan
Member #12,015
June 2010
|
hmm will see if it works
|
bamccaig
Member #7,536
July 2006
![]() |
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? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
alex Ioan
Member #12,015
June 2010
|
yeap I understand it and yea I know my code style is horrible but I get it way better my way
|
|