From 864098bdc86d338535bc936182895ec7e7fb3828 Mon Sep 17 00:00:00 2001 From: driAn Date: Sun, 24 Aug 2008 16:12:53 +0000 Subject: [PATCH] better loop for mapsounds, const fixes git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@2377 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/entity.h | 3 +- source/src/main.cpp | 6 +-- source/src/protos.h | 2 - source/src/sound.cpp | 98 ++++++++++++++++++++++++++---------------- source/src/worldio.cpp | 2 +- 5 files changed, 67 insertions(+), 44 deletions(-) diff --git a/source/src/entity.h b/source/src/entity.h index 8bdf97c3..477af949 100644 --- a/source/src/entity.h +++ b/source/src/entity.h @@ -28,8 +28,7 @@ struct persistent_entity // map entity struct entity : public persistent_entity { bool spawned; //the only dynamic state of a map entity - bool soundinuse; - entity(short x, short y, short z, uchar type, short attr1, uchar attr2, uchar attr3, uchar attr4) : persistent_entity(x, y, z, type, attr1, attr2, attr3, attr4), spawned(false), soundinuse(false) {} + entity(short x, short y, short z, uchar type, short attr1, uchar attr2, uchar attr3, uchar attr4) : persistent_entity(x, y, z, type, attr1, attr2, attr3, attr4), spawned(false) {} entity() {} }; diff --git a/source/src/main.cpp b/source/src/main.cpp index e85b7780..d980a1fb 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -503,9 +503,9 @@ int main(int argc, char **argv) } initing = NOT_INITING; - #ifdef WIN32 - SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS); - #endif + //#ifdef WIN32 + //SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS); + //#endif initlog("sdl"); int par = 0; diff --git a/source/src/protos.h b/source/src/protos.h index e4db51a1..c01864c8 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -501,8 +501,6 @@ extern void playsound(int n, const vec *v, int priority = SP_NORMAL); extern void playsoundc(int n, physent *p = NULL); extern void initsound(); extern void soundcleanup(); -extern void checkmapsounds(); -extern void checkplayerloopsounds(); extern void musicsuggest(int id, int millis = 0, bool rndofs = false); extern void musicfadeout(int id); extern void clearworldsounds(); diff --git a/source/src/sound.cpp b/source/src/sound.cpp index 74589b93..1f195dc8 100644 --- a/source/src/sound.cpp +++ b/source/src/sound.cpp @@ -21,6 +21,8 @@ static bool nosound = true; ALCdevice *device = NULL; ALCcontext *context = NULL; +void playsound(int n, const struct worldobjreference &r, int priority); + void alclearerr() { alGetError(); @@ -400,7 +402,7 @@ struct worldobjreference worldobjreference(int t) : type(t) {}; virtual ~worldobjreference() {}; virtual worldobjreference *clone() const = 0; - virtual vec& currentposition() = 0; + virtual const vec ¤tposition() const = 0; virtual bool nodistance() = 0; virtual bool operator==(const worldobjreference &other) = 0; virtual bool operator!=(const worldobjreference &other) { return !(*this==other); } @@ -412,7 +414,7 @@ struct camerareference : worldobjreference { camerareference() : worldobjreference(WR_CAMERA) {}; worldobjreference *clone() const { return new camerareference(*this); } - vec& currentposition() { return camera1->o; } + const vec ¤tposition() const { return camera1->o; } bool nodistance() { return true; } bool operator==(const worldobjreference &other) { return type==other.type; } }; @@ -428,7 +430,7 @@ struct physentreference : worldobjreference } worldobjreference *clone() const { return new physentreference(*this); } - vec ¤tposition() { return phys->o; } + const vec ¤tposition() const { return phys->o; } bool nodistance() { return phys==camera1; } bool operator==(const worldobjreference &other) { return type==other.type && phys==((physentreference &)other).phys; } }; @@ -436,7 +438,6 @@ struct physentreference : worldobjreference struct entityreference : worldobjreference { entity *ent; - vec tmp; entityreference(entity *ref) : worldobjreference(WR_ENTITY) { @@ -446,16 +447,14 @@ struct entityreference : worldobjreference worldobjreference *clone() const { return new entityreference(*this); } - vec ¤tposition() + const vec ¤tposition() const { - tmp = vec(ent->x, ent->y, ent->z); + static vec tmp = vec(ent->x, ent->y, ent->z); return tmp; } bool nodistance() { return ent->attr2>0; } bool operator==(const worldobjreference &other) { return type==other.type && ent==((entityreference &)other).ent; } - void attach() { ent->soundinuse = true; } - void detach() { ent->soundinuse = false; } }; struct staticreference : worldobjreference @@ -468,7 +467,7 @@ struct staticreference : worldobjreference } worldobjreference *clone() const { return new staticreference(*this); } - vec ¤tposition() { return pos; } + const vec ¤tposition() const { return pos; } bool nodistance() { return false; } bool operator==(const worldobjreference &other) { return type==other.type && pos==((staticreference &)other).pos; } }; @@ -897,11 +896,9 @@ struct location : sourceowner bool stale; - location(int sound, worldobjreference *r, int priority = SP_NORMAL) : cfg(NULL), src(NULL), ref(NULL), stale(false) + location(int sound, const worldobjreference &r, int priority = SP_NORMAL) : cfg(NULL), src(NULL), ref(NULL), stale(false) { - ASSERT(r); - - vector &sounds = (r->type==worldobjreference::WR_ENTITY ? mapsounds : gamesounds); + vector &sounds = (r.type==worldobjreference::WR_ENTITY ? mapsounds : gamesounds); if(!sounds.inrange(sound)) { conoutf("unregistered sound: %d", sound); @@ -912,7 +909,7 @@ struct location : sourceowner // get sound config cfg = &sounds[sound]; cfg->onattach(); - if(r->type==worldobjreference::WR_ENTITY && cfg->maxuses >= 0 && cfg->uses >= cfg->maxuses) // check max-use limits + if(r.type==worldobjreference::WR_ENTITY && cfg->maxuses >= 0 && cfg->uses >= cfg->maxuses) // check max-use limits { stale = true; return; @@ -927,7 +924,7 @@ struct location : sourceowner } // obtain source - src = scheduler.newsource(priority, r->currentposition()); + src = scheduler.newsource(priority, r.currentposition()); // apply configuration if(!src || !src->valid || !src->buffer(cfg->buf->id) || !src->looping(cfg->loop) || !src->gain(cfg->vol/100.0f*((float)gainscale)/100.0f)) { @@ -953,9 +950,9 @@ struct location : sourceowner // attach a reference to a world object to get the 3D position from - void attachworldobjreference(worldobjreference *r) + void attachworldobjreference(const worldobjreference &r) { - ASSERT(r && !stale && src && src->valid); + ASSERT(!stale && src && src->valid); if(stale) return; if(ref) @@ -963,7 +960,7 @@ struct location : sourceowner ref->detach(); DELETEP(ref); } - ref = r; + ref = r.clone(); evaluateworldobjref(); ref->attach(); } @@ -989,7 +986,7 @@ struct location : sourceowner ASSERT(!stale && ref); if(stale) return; - vec &pos = ref->currentposition(); + const vec &pos = ref->currentposition(); switch(ref->type) { @@ -1038,24 +1035,31 @@ struct location : sourceowner } } - void play() + void play(bool loop = false) { if(stale) return; updatepos(); + if(loop) src->looping(loop); src->play(); } + + void drop() + { + src->stop(); + stale = true; // drop from collection on next update cycle + } }; struct locvector : vector { virtual ~locvector(){}; - int find(int sound, worldobjreference *ref = NULL) + int find(int sound, worldobjreference *ref = NULL, const vector &soundcollection = gamesounds) { loopi(ulen) if(buf[i] && !buf[i]->stale) { - if(buf[i]->cfg != &gamesounds[sound]) continue; // check if its the same sound + if(buf[i]->cfg != &soundcollection[sound]) continue; // check if its the same sound if(ref && *(buf[i]->ref)!=*ref) continue; // optionally check if its the same reference return i; // found } @@ -1075,7 +1079,7 @@ struct locvector : vector { location *l = buf[i]; if(!l) continue; - if(*(l->ref)==oldr) l->attachworldobjreference(newr.clone()); + if(*(l->ref)==oldr) l->attachworldobjreference(newr); } } @@ -1465,8 +1469,24 @@ void updateaudio() { entity &e = ents[i]; vec o(e.x, e.y, e.z); - if(e.type!=SOUND || e.soundinuse || camera1->o.dist(o)>=e.attr2) continue; - playsound(e.attr1, &e); + if(e.type!=SOUND) continue; + + int sound = e.attr1; + bool hearable = camera1->o.dist(o)=0 ? locations[locidx] : NULL; + + if(hearable && !loc) // play + { + playsound(sound, entref, SP_HIGH); + } + else if(!hearable && loc) // stop + { + loc->drop(); + } } // update all sound locations @@ -1501,34 +1521,40 @@ void updateaudio() alcProcessContext(context); } -VARP(maxsoundsatonce, 0, 40, 100); +VARP(maxsoundsatonce, 0, 10, 100); -void playsound(int n, worldobjreference *r, int priority) +void playsound(int n, const worldobjreference &r, int priority) { - ASSERT(r); if(nosound || !soundvol) return; - // avoid bursts of sounds with heavy packetloss and in sp - if(r->type!=worldobjreference::WR_ENTITY) + bool loop = false; + + if(r.type==worldobjreference::WR_ENTITY) loop = true; // loop map sounds + else { + // avoid bursts of sounds with heavy packetloss and in sp static int soundsatonce = 0, lastsoundmillis = 0; if(totalmillis==lastsoundmillis) soundsatonce++; else soundsatonce = 1; lastsoundmillis = totalmillis; - if(maxsoundsatonce && soundsatonce>maxsoundsatonce) return; + if(maxsoundsatonce && soundsatonce>maxsoundsatonce) + { + if(audiodebug) conoutf("sound %d filtered by maxsoundsatonce (soundsatonce %d)", n, soundsatonce); + return; + } } location *loc = new location(n, r, priority); locations.add(loc); - loc->play(); + loc->play(loop); if(audiodebug) conoutf("played sound no %d with prio %d", n, priority); } -void playsound(int n, int priority) { playsound(n, new camerareference(), priority); }; -void playsound(int n, physent *p, int priority) { playsound(n, new physentreference(p), priority); }; -void playsound(int n, entity *e, int priority) { playsound(n, new entityreference(e), priority); }; -void playsound(int n, const vec *v, int priority) { playsound(n, new staticreference(*v), priority); }; +void playsound(int n, int priority) { playsound(n, camerareference(), priority); }; +void playsound(int n, physent *p, int priority) { playsound(n, physentreference(p), priority); }; +void playsound(int n, entity *e, int priority) { playsound(n, entityreference(e), priority); }; +void playsound(int n, const vec *v, int priority) { playsound(n, staticreference(*v), priority); }; void playsoundname(char *s, const vec *loc, int vol) { diff --git a/source/src/worldio.cpp b/source/src/worldio.cpp index 4943ea0e..2ad4c376 100644 --- a/source/src/worldio.cpp +++ b/source/src/worldio.cpp @@ -260,7 +260,7 @@ bool load_world(char *mname) // still supports all map formats that have entity &e = ents.add(); gzread(f, &e, sizeof(persistent_entity)); endianswap(&e, sizeof(short), 4); - e.spawned = e.soundinuse = false; + e.spawned = false; if(e.type==LIGHT) { if(!e.attr2) e.attr2 = 255; // needed for MAPVERSION<=2