Skip to content

Commit

Permalink
ADD: ambient sounds (using climate names and beach and forest
Browse files Browse the repository at this point in the history
git-svn-id: svn://tron.homeunix.org/simutrans/simutrans/trunk@2652 8aca7d54-2c30-db11-9de9-000461428c89
  • Loading branch information
prissi committed Sep 22, 2009
1 parent c383371 commit 6718177
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 17 deletions.
8 changes: 8 additions & 0 deletions besch/reader/sound_reader.cc
Expand Up @@ -34,6 +34,14 @@ obj_besch_t * sound_reader_t::read_node(FILE *fp, obj_node_info_t &node)
// Versioned node, version 2
besch->nr = decode_uint16(p);
}
else if( version == 2 ) {
// Versioned node, version 2
besch->nr = decode_uint16(p);
uint16 len = decode_uint16(p);
if( len>0 ) {
besch->nr = besch->get_sound_id(p);
}
}
else {
dbg->fatal("sound_reader_t::read_node()","version %i not supported. File corrupt?", version);
}
Expand Down
39 changes: 25 additions & 14 deletions besch/sound_besch.cc
Expand Up @@ -21,22 +21,19 @@

#include "spezial_obj_tpl.h"
#include "sound_besch.h"
#include "grund_besch.h"

/* sound of the program *
* @author prissi
*/

class sound_ids {
public:
char filename[120];
cstring_t filename;
sint16 id;
sound_ids() { id=NO_SOUND; filename[0]=0; }
sound_ids(sint16 i) { id=i; filename[0]=0; }
sound_ids(sint16 i, const char* fn)
{
id = i;
tstrncpy(filename, fn, lengthof(filename));
}
sound_ids() { id=NO_SOUND; }
sound_ids(sint16 i) { id=i; }
sound_ids(sint16 i, const char* fn) : filename(fn), id(i) {}
};


Expand All @@ -52,10 +49,15 @@ sint16 sound_besch_t::compatible_sound_id[MAX_OLD_SOUNDS]=
NO_SOUND, NO_SOUND, NO_SOUND, NO_SOUND
};

// sound with the names of climates and "beach" and "forest" are reserved for ambient noises
sint16 sound_besch_t::beach_sound;
sint16 sound_besch_t::forest_sound;
sint16 sound_besch_t::climate_sounds[MAX_CLIMATES];


/* init sounds */
/* standard sounds and old sounds are found in the file pak/sound/sound.tab */
void
sound_besch_t::init()
void sound_besch_t::init()
{
// ok, now init
sound_on = true;
Expand All @@ -78,15 +80,22 @@ DBG_MESSAGE("sound_besch_t::init()","reading sound %s", fn );
DBG_MESSAGE("sound_besch_t::init()","assigned system sound %d to sound %s (id=%i)", i, fn, compatible_sound_id[i] );
}
}
// now assign special sounds for climates, beaches and forest
beach_sound = get_sound_id( "beaches.wav" );
forest_sound = get_sound_id( "forest.wav" );
for( int i=0; i<MAX_CLIMATES; i++ ) {
char name[64];
sprintf( name, "%s.wav", grund_besch_t::get_climate_name_from_bit((climate)i) );
climate_sounds[i] = get_sound_id( name );
}
}
}




/* return sound id from index */
sint16
sound_besch_t::get_sound_id(const char *name)
sint16 sound_besch_t::get_sound_id(const char *name)
{
if(!sound_on) {
return NO_SOUND;
Expand Down Expand Up @@ -126,9 +135,9 @@ sound_besch_t::register_besch(sound_besch_t *besch)
if(besch->nr>=0 && besch->nr<=8) {
compatible_sound_id[besch->nr] = besch->sound_id;
DBG_MESSAGE("sound_besch_t::get_sound_id()","successfully registered sound %s internal id %i as compatible sound %i", besch->get_name(), besch->sound_id, besch->nr );
delete besch;
return true;
}
delete besch;
return true;
}
dbg->warning("sound_besch_t::get_sound_id()","failed to register sound %s internal id %i", besch->get_name() );
delete besch;
Expand All @@ -141,3 +150,5 @@ bool sound_besch_t::alles_geladen()
DBG_MESSAGE("sound_besch_t::alles_geladen()","sounds");
return true; // no mandatory objects here
}


7 changes: 7 additions & 0 deletions besch/sound_besch.h
Expand Up @@ -13,6 +13,8 @@
#define NO_SOUND (sint16)(0xFFFFu)
#define LOAD_SOUND (sint8)(0xFFFEu)

#define AMBIENT_SOUND_INTERVALL (13000)

/*
* Autor:
* prissi
Expand Down Expand Up @@ -50,6 +52,11 @@ class sound_besch_t : public obj_besch_std_name_t {
sint16 nr; // for old sounds/system sounds etc.

public:
// sounds for ambient
static sint16 beach_sound;
static sint16 forest_sound;
static sint16 climate_sounds[MAX_CLIMATES];

static sint16 get_sound_id(const char *name);

static bool register_besch(sound_besch_t *besch);
Expand Down
15 changes: 13 additions & 2 deletions besch/writer/sound_writer.cc
Expand Up @@ -7,18 +7,29 @@

void sound_writer_t::write_obj(FILE* fp, obj_node_t& parent, tabfileobj_t& obj)
{
obj_node_t node(this, 4, &parent);
// eventuall direct name input
cstring_t str = obj.get("sound_name");
uint16 len = str.len();

obj_node_t node(this, 6+len+1, &parent);

write_head(fp, node, obj);

// Hajo: version number
// Hajo: Version needs high bit set as trigger -> this is required
// as marker because formerly nodes were unversionend
uint16 uv16 = 0x8001;
uint16 uv16 = 0x8002;
node.write_uint16(fp, uv16, 0);

uv16 = obj.get_int("sound_nr", NO_SOUND); // for compatibility reasons; the old nr of a sound
node.write_uint16(fp, uv16, 2);

node.write_uint16(fp, len, 4);
if( len > 0 ) {
node.write_data_at(fp, (const char *)str, 6, len );
}
node.write_data_at(fp, (const char *)"", 6+len, 1 );


node.write(fp);
}
1 change: 1 addition & 0 deletions besch/writer/tunnel_writer.cc
Expand Up @@ -100,6 +100,7 @@ void tunnel_writer_t::write_obj(FILE* fp, obj_node_t& parent, tabfileobj_t& obj)
}
}
}

str = obj.get("way");
if (str.len() > 0) {
xref_writer_t::instance()->write_obj(fp, node, obj_way, str, true);
Expand Down
42 changes: 41 additions & 1 deletion simworld.cc
Expand Up @@ -4064,6 +4064,8 @@ karte_t::reset_timer()
DBG_MESSAGE("karte_t::reset_timer()","called");
// Reset timers
uint32 last_tick_sync = dr_time();
mouse_rest_time = last_tick_sync;
sound_wait_time = AMBIENT_SOUND_INTERVALL;
intr_set_last_time(last_tick_sync);
intr_enable();

Expand Down Expand Up @@ -4258,7 +4260,7 @@ void karte_t::bewege_zeiger(const event_t *ev)

// zeiger bewegen
const koord3d prev_pos = zeiger->get_pos();
if(prev_pos != pos || ev->button_state != mb_alt) {
if( prev_pos != pos || ev->button_state != mb_alt) {

mb_alt = ev->button_state;

Expand All @@ -4276,6 +4278,12 @@ void karte_t::bewege_zeiger(const event_t *ev)
is_dragging = true;
werkzeug->move( this, get_active_player(), 1, pos );
}

if( (ev->button_state&7)==0 ) {
// time, since mouse got here
mouse_rest_time = dr_time();
sound_wait_time = AMBIENT_SOUND_INTERVALL; // 13s no movement: play sound
}
}
}
}
Expand Down Expand Up @@ -4504,6 +4512,38 @@ karte_t::interactive()
// check for too much time eaten by frame updates ...
if(!fast_forward && !pause) {
last_interaction = dr_time();
if( mouse_rest_time+sound_wait_time < last_interaction ) {
// we play an ambient sound, if enabled
grund_t *gr = lookup(zeiger->get_pos());
if( gr ) {
if( gr->ist_natur() ) {
sint16 id = NO_SOUND;
if( gr->get_pos().z >= get_snowline() ) {
id = sound_besch_t::climate_sounds[ arctic_climate ];
}
else {
sound_besch_t::climate_sounds[ get_climate(zeiger->get_pos().z) ];
}
if( id==NO_SOUND ) {
// try, if there is another sound ready
if( zeiger->get_pos().z==grundwasser && !gr->ist_wasser() ) {
id = sound_besch_t::beach_sound;
}
else if( gr->get_top()>0 && gr->obj_bei(0)->get_typ()==ding_t::baum ) {
id = sound_besch_t::forest_sound;
}
}
if( id!=NO_SOUND ) {
struct sound_info ambient_sound;
ambient_sound.index = id;
ambient_sound.volume = 255;
ambient_sound.pri = 0;
sound_play( ambient_sound );
}
}
}
sound_wait_time *= 2;
}
}

// get an event
Expand Down
4 changes: 4 additions & 0 deletions simworld.h
Expand Up @@ -131,6 +131,10 @@ class karte_t
*/
sint32 mi, mj;

/* time when last mouse moved to check for ambient sound events */
uint32 mouse_rest_time;
uint32 sound_wait_time; // waiting time before next event

/**
* If this is true, the map will not be scrolled
* on right-drag
Expand Down

0 comments on commit 6718177

Please sign in to comment.