TORCS  1.3.9
The Open Racing Car Simulator
musicplayer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : musicplayer.cpp
4  created : Fri Dec 23 17:35:18 CET 2011
5  copyright : (C) 2011 Bernhard Wymann
6  email : berniw@bluewin.ch
7  version : $Id$
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 #include "musicplayer.h"
21 
22 #include <GL/glut.h>
23 #include <string.h>
24 #include <tgf.h>
25 #include <portability.h>
26 
27 #include "OggSoundStream.h"
28 #include "OpenALMusicPlayer.h"
29 
30 
31 static bool isEnabled()
32 {
33  const int BUFSIZE = 1024;
34  char buf[BUFSIZE];
35  snprintf(buf, BUFSIZE, "%s%s", GetLocalDir(), MM_SOUND_PARM_CFG);
36  bool enabled = false;
37 
38  void *handle = GfParmReadFile(buf, GFPARM_RMODE_STD | GFPARM_RMODE_CREAT);
40 
41  if (strcmp(s, MM_VAL_SOUND_ENABLED) == 0) {
42  enabled = true;
43  }
44 
45  GfParmReleaseHandle(handle);
46  return enabled;
47 }
48 
49 
50 // Path relative to CWD, e.g "data/music/torcs1.ogg"
51 static SoundStream* getMenuSoundStream(char* oggFilePath)
52 {
53  static OggSoundStream stream(oggFilePath);
54  return &stream;
55 }
56 
57 
59 {
60  const int BUFSIZE = 1024;
61  char oggFilePath[BUFSIZE];
62  strncpy(oggFilePath, "data/music/torcs1.ogg", BUFSIZE);
63 
64  static OpenALMusicPlayer player(getMenuSoundStream(oggFilePath));
65  return &player;
66 }
67 
68 
69 static void playMenuMusic(int /* value */)
70 {
71  const int nextcallinms = 100;
72 
74  if (player->playAndManageBuffer()) {
75  glutTimerFunc(nextcallinms, playMenuMusic, 0);
76  }
77 }
78 
79 
81 {
82  if (isEnabled()) {
84  player->start();
85  playMenuMusic(0);
86  }
87 }
88 
89 
91 {
93  player->stop();
94  player->rewind();
95 }
void * GfParmReadFile(const char *file, int mode)
Read parameter set from file and return handle to parameter set.
Definition: params.cpp:1157
#define GFPARM_RMODE_STD
if handle already openned return it
Definition: tgf.h:265
#define MM_ATT_SOUND_ENABLE
Definition: musicplayer.h:25
char * GetLocalDir(void)
Definition: tgf.cpp:231
void GfParmReleaseHandle(void *parmHandle)
Release given parameter set handle parmHandle.
Definition: params.cpp:1834
#define MM_VAL_SOUND_DISABLED
Definition: musicplayer.h:27
#define MM_SOUND_PARM_CFG
Definition: musicplayer.h:23
const char * GfParmGetStr(void *parmHandle, const char *path, const char *key, const char *deflt)
Get a string parameter from the parameter set handle.
Definition: params.cpp:2311
static void playMenuMusic(int)
Definition: musicplayer.cpp:69
static bool isEnabled()
Definition: musicplayer.cpp:31
The Gaming Framework API.
static OpenALMusicPlayer * getMusicPlayer()
Definition: musicplayer.cpp:58
#define MM_SCT_SOUND
Definition: musicplayer.h:24
virtual bool playAndManageBuffer()
static SoundStream * getMenuSoundStream(char *oggFilePath)
Definition: musicplayer.cpp:51
void startMenuMusic()
Definition: musicplayer.cpp:80
void stopMenuMusic()
Definition: musicplayer.cpp:90
#define GFPARM_RMODE_CREAT
Create the file if doesn&#39;t exist.
Definition: tgf.h:267
#define MM_VAL_SOUND_ENABLED
Definition: musicplayer.h:26