Display and FMOD fighting on multi threads (solved)

Started by rjhenry, December 08, 2007, 13:10:36

Previous topic - Next topic

rjhenry

Edit: solved thanks to help on IRC channel. Cheers!

Hello

I'm a bit of a n00b with LWJGL and indeed java game development. I come from C++ background so sorry if this is a daft thing.

So, I've started to create a game engine. I thought it'd be nice if I could use multi threads as I have 8 cores in my Mac. I decided to let the renderer run in its own thread and use FMOD for the sound. I am guessing that FMOD will handle its own threading.

I've come across a problem though. As the engine starts, I initialise the rendering. I keep all rendering stuff in the same thread as advised. Here's the init. code for the renderer:
public void run() {
		
		// Lock LWJGL while things are initialising
		Engine.lwjglLock.acquire();
		
		// Initialise the lwjgl display
		try {
			Display.setDisplayMode(findScreenResolution(targetWidth, targetHeight));
			Display.setFullscreen(fullScreen);
			Display.setVSyncEnabled(vSync);
			Display.create();
			Display.update();
		}
		
		catch (LWJGLException e) { e.printStackTrace(); running = false; }
		
		// Release the lock so other things can initialise
		Engine.lwjglLock.release();
		
		// Do the rendering
		while (running) { render(); }
		
		// Destroy the lwjgl display
		Display.destroy();
	}


While this is being done, the main thread goes on to initialise the FMOD sound. That (unfinished) code is
public SoundEngine() {
		
		// Lock LWJGL while things are initialising
		Engine.lwjglLock.acquire();
		
		// Create and initialise FMOD
		try { FMOD.create(); }

		catch (FMODException e) { e.printStackTrace(); }
		
		FSound.FSOUND_Init(44100, 32, 0);
		
		
		// Initialise music player
		musicPlayer = FMusic.FMUSIC_LoadSong("resource/test.mod");
		
		//FMusic.FMUSIC_PlaySong(musicPlayer);
		
		//Initialise sound player
		soundPlayer = FSound.FSOUND_Sample_Load(0, "resource/test.mp3", 0, 0, 0);
		FSound.FSOUND_PlaySound(0, soundPlayer);
		// Release the lock so other things can initialise
		Engine.lwjglLock.release();
	}


I've bunged a mutex in (Engine.lwjglLock) to stop these initialisations being done concurrently but I still have a race condition in that if FMOD initialises first, I get no display at all (the window fails to open). No exceptions are thrown or owt, it fails silently.

Anybody provide me with a clue to what's going on here?

Cheers

PS: Preview post doesn't seem to work on Safari 3 :(