Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
soundserver.mac.notworking.cpp File Reference
#include "lin_time.h"
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <SDL/SDL_mixer.h>
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <string>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "inet.h"

Go to the source code of this file.

Classes

struct  Music
 

Macros

#define HAVE_SDL
 

Functions

int GetMaxVolume ()
 
void errorv (char *str, va_list ap)
 
void cleanExit (char *str,...)
 
void changehome (bool to, bool linuxhome=true)
 
Music PlayMusic (const char *file, Music &oldmusic)
 
void music_finished ()
 
int main (int argc, char **argv)
 

Variables

struct Music fadeout = 0
 
struct Music fadein = 0
 
float volume = 0
 
int bits = 0
 
int done = 0
 
bool sende = true
 
bool invalid_string = true
 
std::string curmus
 
int mysocket = -1
 

Macro Definition Documentation

#define HAVE_SDL

Definition at line 93 of file soundserver.mac.notworking.cpp.

Referenced by main().

Function Documentation

void changehome ( bool  to,
bool  linuxhome = true 
)

Definition at line 213 of file soundserver.mac.notworking.cpp.

214 {
215  static std::vector< std::string >paths;
216  if (to) {
217  char mycurpath[8192];
218  getcwd( mycurpath, 8191 );
219  mycurpath[8191] = '\0';
220  paths.push_back( std::string( mycurpath ) );
221 #ifndef _WIN32
222  if (linuxhome) {
223  struct passwd *pwent;
224  pwent = getpwuid( getuid() );
225  chdir( pwent->pw_dir );
226  }
227 #endif
228  chdir( ".vegastrike" );
229  } else if ( !paths.empty() ) {
230  chdir( paths.back().c_str() );
231  paths.pop_back();
232  }
233 }
void cleanExit ( char *  str,
  ... 
)

Definition at line 195 of file soundserver.mac.notworking.cpp.

References errorv().

196 {
197 #ifdef HAVE_SDL
198  va_list ap;
199 
200  va_start( ap, str );
201  errorv( str, ap );
202  va_end( ap );
203  Mix_CloseAudio();
204  SDL_Quit();
205 #endif
206  exit( 1 );
207 }
void errorv ( char *  str,
va_list  ap 
)

Definition at line 172 of file soundserver.mac.notworking.cpp.

References fprintf.

173 {
174 #ifdef HAVE_SDL
175  vfprintf( stderr, str, ap );
176 
177  fprintf( stderr, ": %s.\n", SDL_GetError() );
178 #endif
179 }
int GetMaxVolume ( )
int main ( int  argc,
char **  argv 
)

Definition at line 275 of file soundserver.mac.notworking.cpp.

References cleanExit(), done, fadein, fadeout, GetMaxVolume(), i, INET_AcceptFrom(), INET_cleanup(), INET_close(), INET_fgetc(), INET_startup(), Music::m, music_finished(), PlayMusic(), and Music::SetVolume().

276 {
277  Music music;
278  int audio_rate, audio_channels,
279  //set this to any of 512,1024,2048,4096
280  //the higher it is, the more FPS shown and CPU needed
281  audio_buffers = 4096;
282 #ifdef HAVE_SDL
283  Uint16 audio_format;
284  //initialize SDL for audio and video
285  if (SDL_Init( SDL_INIT_AUDIO ) < 0)
286  cleanExit( "SDL_Init\n" );
287  Mix_HookMusicFinished( &music_finished );
288 #else
289 #ifdef __APPLE__
290  if ( !FSOUND_Init( 44100, 64, FSOUND_INIT_GLOBALFOCUS ) ) {
291  printf( "SOUND Error %d\n", FSOUND_GetError() );
292  exit( 1 );
293  }
294 #endif
295 #endif
296  INET_startup();
297  GetMaxVolume();
298  //initialize sdl mixer, open up the audio device
299 #ifdef HAVE_SDL
300  if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, audio_buffers ) < 0)
301  cleanExit( "Mix_OpenAudio\n" );
302  //print out some info on the audio device and stream
303  Mix_QuerySpec( &audio_rate, &audio_format, &audio_channels );
304  bits = audio_format&0xFF;
305 #endif
306  printf( "Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
307  bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers );
308  //load the song
309  for (int i = 0; i < 10 && mysocket == -1; i++)
310  mysocket = INET_AcceptFrom( 4364, "localhost" );
311  if (mysocket == -1)
312  return 1;
313  printf( "\n[CONNECTED]\n" );
314  char ministr[2] = {'\0', '\0'};
315  while (!done) {
316 //if ((Mix_PlayingMusic() || Mix_PausedMusic())&&(!done)) {
317  char arg;
318  std::string str;
319  arg = INET_fgetc( mysocket );
320  printf( "%c", arg );
321  switch (arg)
322  {
323  case 'p':
324  case 'P':
325  arg = INET_fgetc( mysocket );
326  while (arg != '\0' && arg != '\n') {
327  if (arg != '\r') {
328  ministr[0] = arg;
329  str += ministr;
330  }
331  arg = INET_fgetc( mysocket );
332  }
333  printf( "%s", str.c_str() );
334  if ( (str != curmus || invalid_string)
335 #ifdef HAVE_SDL
336  || ( !Mix_PlayingMusic() )
337 #endif
338  ) {
339  music = PlayMusic( str.c_str(), music );
340  if (music.m) {
341  printf( "\n[PLAYING %s WITH %d FADEIN AND %d FADEOUT]\n", str.c_str(), fadein, fadeout );
342  curmus = str;
343  invalid_string = false;
344  } else {
345  printf( "\n[UNABLE TO PLAY %s WITH %d FADEIN AND %d FADEOUT]\n", str.c_str(), fadein, fadeout );
346  music_finished();
347  }
348  } else {
349  printf( "\n[%s WITH %d FADEIN AND %d FADEOUT IS ALREADY PLAYING]\n", str.c_str(), fadein, fadeout );
350  }
351  break;
352  case 'i':
353  case 'I':
354  arg = INET_fgetc( mysocket );
355  while (arg != '\0' && arg != '\n') {
356  if (arg != '\r') {
357  ministr[0] = arg;
358  str += ministr;
359  }
360  arg = INET_fgetc( mysocket );
361  }
362  printf( "%s", str.c_str() );
363  fadein = atoi( str.c_str() );
364  printf( "\n[SETTING FADEIN TO %d]\n", fadein );
365  break;
366  case 'o':
367  case 'O':
368  arg = INET_fgetc( mysocket );
369  while (arg != '\0' && arg != '\n') {
370  if (arg != '\r') {
371  ministr[0] = arg;
372  str += ministr;
373  }
374  arg = INET_fgetc( mysocket );
375  }
376  printf( "%s", str.c_str() );
377  fadeout = atoi( str.c_str() );
378  printf( "\n[SETTING FADEOUT TO %d]\n", fadeout );
379  break;
380  case 'v':
381  case 'V':
382  arg = INET_fgetc( mysocket );
383  while (arg != '\0' && arg != '\n') {
384  if (arg != '\r') {
385  ministr[0] = arg;
386  str += ministr;
387  }
388  arg = INET_fgetc( mysocket );
389  }
390  printf( "%s", str.c_str() );
391  volume = atof( str.c_str() );
392  printf( "\n[SETTING VOLUME TO %f]\n", volume );
393  music.SetVolume( volume );
394  break;
395  case 't':
396  case 'T':
397  case '\0':
398  INET_close( mysocket );
399  done = true;
400  printf( "\n[TERMINATING MUSIC SERVER]\n" );
401  break;
402  }
403  }
404  //free & close
405  INET_cleanup();
406 #ifdef HAVE_SDL
407  Mix_CloseAudio();
408  SDL_Quit();
409 #endif
410 
411  return 0;
412 }
void music_finished ( )

Definition at line 266 of file soundserver.mac.notworking.cpp.

References INET_Write().

267 {
268  if (sende) {
269  char data = 'e';
270  INET_Write( mysocket, sizeof (char), &data );
271  printf( "\ne\n[SONG DONE]\n" );
272  invalid_string = true;
273  }
274 }
Music PlayMusic ( const char *  file,
Music oldmusic 
)

Definition at line 241 of file soundserver.mac.notworking.cpp.

References changehome(), accountXML::file, Music::Load(), Music::m, and Music::Play().

242 {
243  Music music;
244  music.Load( file );
245  if (music.m == NULL) {
246  changehome( true, false );
247  music.Load( file );
248  changehome( false );
249  if (music.m == NULL) {
250  changehome( true, true );
251  music.Load( file );
252  changehome( false );
253  if (music.m == NULL)
254  return oldmusic;
255  }
256  }
257  sende = false;
258  music.Play( fadeout, fadein, oldmusic );
259 
260  sende = true;
261  curmus = file;
262  invalid_string = false;
263  return music;
264 }

Variable Documentation

int bits = 0

Definition at line 166 of file soundserver.mac.notworking.cpp.

std::string curmus

Definition at line 239 of file soundserver.mac.notworking.cpp.

int done = 0

Definition at line 166 of file soundserver.mac.notworking.cpp.

struct Music fadein = 0
struct Music fadeout = 0
bool invalid_string = true

Definition at line 238 of file soundserver.mac.notworking.cpp.

int mysocket = -1

Definition at line 265 of file soundserver.mac.notworking.cpp.

bool sende = true

Definition at line 237 of file soundserver.mac.notworking.cpp.

float volume = 0

Definition at line 165 of file soundserver.mac.notworking.cpp.