Jeck - [Requires JGE rebuild] Extensive cache improvements.

* Numerous cache fixes, reduced filesystem access.
 * Cache fails very gracefully. 
 * Cache is now a templated class, with individual caches per data-type.
 * Much easier to extend.
 * Extensively debugged. Try compiling with -DDEBUG_CACHE.
 * Caches limits can be set on a per-item basis.
 * hgeParticleSystemInfo are now cached, mana particles now fall back to defaults.
 * Samples are not cached, but track filesystem misses using the cache backbone. 
 * Avatars are cached. Default baka avatar is now baka.jpg, to prevent collision with player.

A note on the retrieval types: 
  RETRIEVE_MANAGE puts a resource into a seperate, managed resource list.
    Managed resources are guarenteed valid for the lifetime of the program. 
    Retrieving a managed quad promotes the associated texture to managed. Don't do that by mistake.
    Calls to Resources.Refresh() will attempt to reload managed resources in place.

  RETRIVE_LOCK (and by extension, RETRIEVE_VRAM), returns a resource after locking it.
    A resource may have many locks, and remains in cache until they are all released. 
    If the resource is managed, it returns it unmodified.

A note on quads:
Unlike all other RetrieveWhatever() functions, the default behavior for RetrieveQuad is RETRIEVE_LOCK. Worse, Release(JQuad*) is slow, and will /always/ release one lock from the associated texture.   

There's a long and complicated explanation for this, involving support for live relinking of textures to existing quads, but basically what it means is that we only use RetrieveQuad for quads we intend to store and later Release(). If a temporary quad is needed, the preferred method is to use NEW JQuad* and SAFE_DELETE with RetrieveTexture(). RetrieveTempQuad is also provided, but is only guaranteed until the next call to the cache.

Note that RetrieveCard has none of these problems.
This commit is contained in:
wagic.jeck
2009-09-14 08:28:49 +00:00
parent a696b65ffb
commit d55fc91112
27 changed files with 3732 additions and 2992 deletions
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -133,7 +133,8 @@ JSample *JSoundSystem::LoadSample(const char *fileName)
if (sample)
{
sample->mSample = new WAVDATA;
loadWaveData(sample->mSample, s, 1);
if(!loadWaveData(sample->mSample, s, 1))
sample->mSample = NULL;
}
return sample;
+2
View File
@@ -202,6 +202,8 @@ JSample *JSoundSystem::LoadSample(const char *fileName)
delete[] buffer;
fileSystem->CloseFile();
}
else
sample->mSample = NULL;
}
+220 -219
View File
@@ -1,219 +1,220 @@
//-------------------------------------------------------------------------------------
//
// JGE++ is a hardware accelerated 2D game SDK for PSP/Windows.
//
// Licensed under the BSD license, see LICENSE in JGE root for details.
//
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
//
//-------------------------------------------------------------------------------------
#include "../../Dependencies/include/png.h"
#include "../../Dependencies/include/fmod.h"
#include "../../include/JSoundSystem.h"
#include "../../include/JFileSystem.h"
//////////////////////////////////////////////////////////////////////////
JMusic::JMusic()
{
}
void JMusic::Update(){
}
int JMusic::getPlayTime(){
return FSOUND_GetCurrentPosition(JSoundSystem::GetInstance()->mChannel)/44.1; //todo more generic, here it's only 44kHz
}
JMusic::~JMusic()
{
JSoundSystem::GetInstance()->StopMusic(this);
//JSoundSystem::GetInstance()->FreeMusic(this);
if (mTrack)
FSOUND_Sample_Free(mTrack);
}
//////////////////////////////////////////////////////////////////////////
JSample::JSample()
{
}
JSample::~JSample()
{
//JSoundSystem::GetInstance()->FreeSample(this);
if (mSample)
FSOUND_Sample_Free(mSample);
}
//////////////////////////////////////////////////////////////////////////
JSoundSystem* JSoundSystem::mInstance = NULL;
JSoundSystem* JSoundSystem::GetInstance()
{
if (mInstance == NULL)
{
mInstance = new JSoundSystem();
mInstance->InitSoundSystem();
}
return mInstance;
}
void JSoundSystem::Destroy()
{
if (mInstance)
{
mInstance->DestroySoundSystem();
delete mInstance;
mInstance = NULL;
}
}
JSoundSystem::JSoundSystem()
{
}
JSoundSystem::~JSoundSystem()
{
}
void JSoundSystem::InitSoundSystem()
{
FSOUND_Init(44100, 32, 0);
}
void JSoundSystem::DestroySoundSystem()
{
FSOUND_Close();
}
JMusic *JSoundSystem::LoadMusic(const char *fileName)
{
JMusic* music = new JMusic();
if (music)
{
JFileSystem* fileSystem = JFileSystem::GetInstance();
if (fileSystem->OpenFile(fileName))
{
// FMUSIC is for MOD...
// int size = fileSystem->GetFileSize();
// char *buffer = new char[size];
// fileSystem->ReadFile(buffer, size);
// music->mTrack = FMUSIC_LoadSongEx(buffer, 0, size, FSOUND_LOADMEMORY, NULL, 0);
//
// delete[] buffer;
// fileSystem->CloseFile();
int size = fileSystem->GetFileSize();
char *buffer = new char[size];
fileSystem->ReadFile(buffer, size);
music->mTrack = FSOUND_Sample_Load(FSOUND_UNMANAGED, buffer, FSOUND_LOADMEMORY, 0, size);
delete[] buffer;
fileSystem->CloseFile();
}
}
return music;
}
// void JSoundSystem::FreeMusic(JMusic *music)
// {
// if (music)
// {
// // if (music->mTrack)
// // FMUSIC_FreeSong(music->mTrack);
// // delete music;
// // music = NULL;
//
// if (music->mTrack)
// FSOUND_Sample_Free(music->mTrack);
//
// //delete music;
// //music = NULL;
// }
// }
void JSoundSystem::PlayMusic(JMusic *music, bool looping)
{
if (music && music->mTrack)
{
mChannel = FSOUND_PlaySound(FSOUND_FREE, music->mTrack);
if (looping)
FSOUND_SetLoopMode(mChannel, FSOUND_LOOP_NORMAL);
else
FSOUND_SetLoopMode(mChannel, FSOUND_LOOP_OFF);
}
}
void JSoundSystem::StopMusic(JMusic *music)
{
// if (music && music->mTrack)
// FMUSIC_StopSong(music->mTrack);
FSOUND_StopSound(mChannel);
}
void JSoundSystem::SetVolume(int volume)
{
FSOUND_SetSFXMasterVolume(volume);
mVolume = volume;
}
JSample *JSoundSystem::LoadSample(const char *fileName)
{
JSample* sample = new JSample();
if (sample)
{
JFileSystem* fileSystem = JFileSystem::GetInstance();
if (fileSystem->OpenFile(fileName))
{
int size = fileSystem->GetFileSize();
char *buffer = new char[size];
fileSystem->ReadFile(buffer, size);
sample->mSample = FSOUND_Sample_Load(FSOUND_UNMANAGED, buffer, FSOUND_LOADMEMORY, 0, size);
delete[] buffer;
fileSystem->CloseFile();
}
}
return sample;
}
void JSoundSystem::PlaySample(JSample *sample)
{
if (sample && sample->mSample)
FSOUND_PlaySound(FSOUND_FREE, sample->mSample);
}
//-------------------------------------------------------------------------------------
//
// JGE++ is a hardware accelerated 2D game SDK for PSP/Windows.
//
// Licensed under the BSD license, see LICENSE in JGE root for details.
//
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
//
//-------------------------------------------------------------------------------------
#include "../../Dependencies/include/png.h"
#include "../../Dependencies/include/fmod.h"
#include "../../include/JSoundSystem.h"
#include "../../include/JFileSystem.h"
//////////////////////////////////////////////////////////////////////////
JMusic::JMusic()
{
}
void JMusic::Update(){
}
int JMusic::getPlayTime(){
return FSOUND_GetCurrentPosition(JSoundSystem::GetInstance()->mChannel)/44.1; //todo more generic, here it's only 44kHz
}
JMusic::~JMusic()
{
JSoundSystem::GetInstance()->StopMusic(this);
//JSoundSystem::GetInstance()->FreeMusic(this);
if (mTrack)
FSOUND_Sample_Free(mTrack);
}
//////////////////////////////////////////////////////////////////////////
JSample::JSample()
{
}
JSample::~JSample()
{
//JSoundSystem::GetInstance()->FreeSample(this);
if (mSample)
FSOUND_Sample_Free(mSample);
}
//////////////////////////////////////////////////////////////////////////
JSoundSystem* JSoundSystem::mInstance = NULL;
JSoundSystem* JSoundSystem::GetInstance()
{
if (mInstance == NULL)
{
mInstance = new JSoundSystem();
mInstance->InitSoundSystem();
}
return mInstance;
}
void JSoundSystem::Destroy()
{
if (mInstance)
{
mInstance->DestroySoundSystem();
delete mInstance;
mInstance = NULL;
}
}
JSoundSystem::JSoundSystem()
{
}
JSoundSystem::~JSoundSystem()
{
}
void JSoundSystem::InitSoundSystem()
{
FSOUND_Init(44100, 32, 0);
}
void JSoundSystem::DestroySoundSystem()
{
FSOUND_Close();
}
JMusic *JSoundSystem::LoadMusic(const char *fileName)
{
JMusic* music = new JMusic();
if (music)
{
JFileSystem* fileSystem = JFileSystem::GetInstance();
if (fileSystem->OpenFile(fileName))
{
// FMUSIC is for MOD...
// int size = fileSystem->GetFileSize();
// char *buffer = new char[size];
// fileSystem->ReadFile(buffer, size);
// music->mTrack = FMUSIC_LoadSongEx(buffer, 0, size, FSOUND_LOADMEMORY, NULL, 0);
//
// delete[] buffer;
// fileSystem->CloseFile();
int size = fileSystem->GetFileSize();
char *buffer = new char[size];
fileSystem->ReadFile(buffer, size);
music->mTrack = FSOUND_Sample_Load(FSOUND_UNMANAGED, buffer, FSOUND_LOADMEMORY, 0, size);
delete[] buffer;
fileSystem->CloseFile();
}
}
return music;
}
// void JSoundSystem::FreeMusic(JMusic *music)
// {
// if (music)
// {
// // if (music->mTrack)
// // FMUSIC_FreeSong(music->mTrack);
// // delete music;
// // music = NULL;
//
// if (music->mTrack)
// FSOUND_Sample_Free(music->mTrack);
//
// //delete music;
// //music = NULL;
// }
// }
void JSoundSystem::PlayMusic(JMusic *music, bool looping)
{
if (music && music->mTrack)
{
mChannel = FSOUND_PlaySound(FSOUND_FREE, music->mTrack);
if (looping)
FSOUND_SetLoopMode(mChannel, FSOUND_LOOP_NORMAL);
else
FSOUND_SetLoopMode(mChannel, FSOUND_LOOP_OFF);
}
}
void JSoundSystem::StopMusic(JMusic *music)
{
// if (music && music->mTrack)
// FMUSIC_StopSong(music->mTrack);
FSOUND_StopSound(mChannel);
}
void JSoundSystem::SetVolume(int volume)
{
FSOUND_SetSFXMasterVolume(volume);
mVolume = volume;
}
JSample *JSoundSystem::LoadSample(const char *fileName)
{
JSample* sample = new JSample();
if (sample)
{
JFileSystem* fileSystem = JFileSystem::GetInstance();
if (fileSystem->OpenFile(fileName))
{
int size = fileSystem->GetFileSize();
char *buffer = new char[size];
fileSystem->ReadFile(buffer, size);
sample->mSample = FSOUND_Sample_Load(FSOUND_UNMANAGED, buffer, FSOUND_LOADMEMORY, 0, size);
delete[] buffer;
fileSystem->CloseFile();
}else
sample->mSample = NULL;
}
return sample;
}
void JSoundSystem::PlaySample(JSample *sample)
{
if (sample && sample->mSample)
FSOUND_PlaySound(FSOUND_FREE, sample->mSample);
}