Implement code for Menu sound effects #1174
git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@8093 30fe4595-0a0c-4342-8851-515496e4dcbd Former-commit-id: 48a50e0508539841e3b786213f18b89148391041 Former-commit-id: eda017f6448b841eec67ff0292d4bed635f44e29
This commit is contained in:
parent
f467b9175c
commit
95a76ae8ae
7 changed files with 357 additions and 10 deletions
|
@ -43,6 +43,13 @@
|
|||
#define SND_VAL_MUSIC_STATE_DISABLED "disabled"
|
||||
#define SND_ATT_MUSIC_DEFAULT_MUSIC "default music"
|
||||
|
||||
#define SND_SCT_MENUSFX "Menu SFX Settings"
|
||||
|
||||
#define SND_ATT_MENUSFX_VOLUME "menusfx volume"
|
||||
#define SND_ATT_MENUSFX_STATE "menusfx state"
|
||||
#define SND_VAL_MENUSFX_STATE_ENABLED "enabled"
|
||||
#define SND_VAL_MENUSFX_STATE_DISABLED "disabled"
|
||||
|
||||
#endif /* _SOUND_H_ */
|
||||
|
||||
|
||||
|
|
|
@ -23,7 +23,8 @@ SET(_SOURCES control.cpp glfeatures.cpp guibutton.cpp guifont.cpp
|
|||
musicplayer.cpp musicplayer.h
|
||||
sdl2musicplayer.cpp sdl2musicplayer.h
|
||||
glfeatures.h gui.h guiscreen.h guimenu.h tgfclient.h guifont.h
|
||||
forcefeedback.cpp forcefeedback.h)
|
||||
forcefeedback.cpp forcefeedback.h
|
||||
guimenusfx.cpp guimenusfx.h)
|
||||
|
||||
IF(OPTION_WEBSERVER)
|
||||
SET(_SOURCES ${_SOURCES} webserver.cpp webserver.h)
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
#include "gui.h"
|
||||
#include "guimenu.h"
|
||||
#include "musicplayer.h"
|
||||
#include "guimenusfx.h"
|
||||
|
||||
#ifdef WIN32
|
||||
PFNGLUSEPROGRAMOBJECTARBPROC glUseProgram = NULL;
|
||||
|
@ -121,6 +122,7 @@ gfuiInit(void)
|
|||
gfuiInitLabel();
|
||||
gfuiInitHelp();
|
||||
gfuiInitMenu();
|
||||
gfuiInitMenuSfx();
|
||||
initMusic();
|
||||
|
||||
#ifdef WIN32
|
||||
|
@ -135,6 +137,7 @@ gfuiShutdown(void)
|
|||
{
|
||||
gfctrlJoyShutdown();
|
||||
gfuiFreeFonts();
|
||||
gfuiShutdownMenuSfx();
|
||||
shutdownMusic();
|
||||
}
|
||||
|
||||
|
|
190
src/libs/tgfclient/guimenusfx.cpp
Normal file
190
src/libs/tgfclient/guimenusfx.cpp
Normal file
|
@ -0,0 +1,190 @@
|
|||
/***************************************************************************
|
||||
|
||||
file : guimenusfx.cpp
|
||||
created : Mon March 28 2022
|
||||
copyright : (C) 2022
|
||||
email :
|
||||
version : $Id:
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
#include "guimenusfx.h"
|
||||
|
||||
#include <tgf.h>
|
||||
#include "tgfclient.h"
|
||||
#include <portability.h>
|
||||
#include <sound.h>
|
||||
#include <SDL_mixer.h>
|
||||
|
||||
|
||||
static bool mixerInitialized = false;
|
||||
|
||||
const char *sfxDisabledStr = SND_VAL_MENUSFX_STATE_DISABLED;
|
||||
|
||||
bool sfxenabled = true;
|
||||
int sfxVolume = MIX_MAX_VOLUME;
|
||||
Mix_Chunk* MenuSfx[2];
|
||||
const char* filename[2] = {"data/menu/menu-click.ogg",
|
||||
"data/menu/menu-rollover.ogg"};
|
||||
int numSfx = 2;
|
||||
|
||||
bool isSfxEnabled();
|
||||
void loadMenuSfx();
|
||||
void unloadMenuSfx();
|
||||
bool initMixer();
|
||||
void readSfxConfig();
|
||||
|
||||
bool isSfxEnabled()
|
||||
{
|
||||
return sfxenabled;
|
||||
}
|
||||
|
||||
void loadMenuSfx()
|
||||
{
|
||||
for (int i = 0; i < numSfx; i++)
|
||||
{
|
||||
MenuSfx[i] = Mix_LoadWAV(filename[i]);
|
||||
if (MenuSfx[i] == NULL)
|
||||
GfLogError("Mix_LoadWAV() failed %s \n", Mix_GetError());
|
||||
}
|
||||
|
||||
for (int i = 0; i < numSfx; i++)
|
||||
{
|
||||
if (MenuSfx[i])
|
||||
Mix_VolumeChunk(MenuSfx[i], sfxVolume);
|
||||
}
|
||||
}
|
||||
|
||||
void unloadMenuSfx()
|
||||
{
|
||||
for (int i = 0; i < numSfx; i++)
|
||||
{
|
||||
Mix_FreeChunk(MenuSfx[i]);
|
||||
MenuSfx[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool initMixer()
|
||||
{
|
||||
if (!mixerInitialized)
|
||||
{
|
||||
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) >= 0 )
|
||||
{
|
||||
mixerInitialized = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
GfLogError("Mix_OpenAudio() failed %s \n", Mix_GetError());
|
||||
}
|
||||
}
|
||||
return mixerInitialized;
|
||||
}
|
||||
|
||||
void readSfxConfig()
|
||||
{
|
||||
GfLogInfo("Reading Menu SFX config\n");
|
||||
void *paramHandle = GfParmReadFileLocal(SND_PARAM_FILE, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
|
||||
const char *sfxEnabledstr = GfParmGetStr(paramHandle, SND_SCT_MENUSFX, SND_ATT_MENUSFX_STATE, sfxDisabledStr);
|
||||
|
||||
float sfx_volume = GfParmGetNum(paramHandle, SND_SCT_MENUSFX, SND_ATT_MENUSFX_VOLUME, "%", 100.0f);
|
||||
|
||||
if (sfx_volume>100.0f)
|
||||
sfx_volume = 100.0f;
|
||||
else if (sfx_volume < 0.0f)
|
||||
sfx_volume = 0.0f;
|
||||
|
||||
sfxVolume = (int)(sfx_volume * MIX_MAX_VOLUME/100.0f);
|
||||
|
||||
if (0 == strcmp(sfxEnabledstr, SND_VAL_MENUSFX_STATE_ENABLED))
|
||||
sfxenabled = true;
|
||||
else
|
||||
sfxenabled = false;
|
||||
|
||||
|
||||
GfParmReleaseHandle(paramHandle);
|
||||
paramHandle = NULL;
|
||||
}
|
||||
|
||||
void gfuiInitMenuSfx()
|
||||
{
|
||||
for (int i = 0; i < numSfx; i++)
|
||||
{
|
||||
MenuSfx[i] = NULL;
|
||||
}
|
||||
|
||||
readSfxConfig();
|
||||
if (isSfxEnabled())
|
||||
{
|
||||
GfLogInfo("(Re-)Initializing Menu SFX \n");
|
||||
if(initMixer())
|
||||
{
|
||||
loadMenuSfx();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GfLogInfo("Menu SFX is disabled \n");
|
||||
}
|
||||
}
|
||||
|
||||
void gfuiShutdownMenuSfx()
|
||||
{
|
||||
if (mixerInitialized)
|
||||
{
|
||||
unloadMenuSfx();
|
||||
Mix_Quit();
|
||||
mixerInitialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
void setMenuSfxVolume(float vol /* 100.0f */)
|
||||
{
|
||||
if (vol>100.0f)
|
||||
vol = 100.0f;
|
||||
else if (vol < 0.0f)
|
||||
vol = 0.0f;
|
||||
|
||||
sfxVolume = (int)(vol * MIX_MAX_VOLUME/100.0f);
|
||||
|
||||
if (isSfxEnabled())
|
||||
{
|
||||
for(int i = 0; i < numSfx; i++)
|
||||
{
|
||||
if (MenuSfx[i])
|
||||
Mix_VolumeChunk(MenuSfx[i], sfxVolume);
|
||||
}
|
||||
}
|
||||
GfLogInfo("Menu SFX volume set to %.2f\n", vol);
|
||||
}
|
||||
|
||||
void enableMenuSfx(bool enable /* true */)
|
||||
{
|
||||
if (isSfxEnabled())
|
||||
{
|
||||
if(enable == false)
|
||||
gfuiShutdownMenuSfx();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(enable == true)
|
||||
gfuiInitMenuSfx();
|
||||
}
|
||||
sfxenabled = enable;
|
||||
}
|
||||
|
||||
void playMenuSfx(int sfxIndex)
|
||||
{
|
||||
if (isSfxEnabled())
|
||||
{
|
||||
Mix_PlayChannel(-1, MenuSfx[sfxIndex], 0);
|
||||
}
|
||||
}
|
44
src/libs/tgfclient/guimenusfx.h
Normal file
44
src/libs/tgfclient/guimenusfx.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
#ifndef __guimenusfx_h__
|
||||
#define __guimenusfx_h__
|
||||
|
||||
/***************************************************************************
|
||||
|
||||
file : guimenusfx.h
|
||||
created : Mon March 28 2022
|
||||
copyright : (C) 2022
|
||||
email :
|
||||
version : $Id:
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
/***************************************************************************
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 2 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
// DLL exported symbols declarator for Windows.
|
||||
#ifdef WIN32
|
||||
# ifdef TGFCLIENT_DLL
|
||||
# define TGFCLIENT_API __declspec(dllexport)
|
||||
# else
|
||||
# define TGFCLIENT_API __declspec(dllimport)
|
||||
# endif
|
||||
#else
|
||||
# define TGFCLIENT_API
|
||||
#endif
|
||||
|
||||
TGFCLIENT_API void gfuiInitMenuSfx();
|
||||
TGFCLIENT_API void gfuiShutdownMenuSfx();
|
||||
TGFCLIENT_API void enableMenuSfx(bool enable = true);
|
||||
TGFCLIENT_API void setMenuSfxVolume(float volume = 100.0f);
|
||||
TGFCLIENT_API void playMenuSfx(int sfxIndex);
|
||||
|
||||
#define SFX_CLICK 0
|
||||
#define SFX_FOCUS 1
|
||||
|
||||
#endif //__guimenusfx_h__
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
#include "tgfclient.h"
|
||||
#include "gui.h"
|
||||
#include "guimenusfx.h"
|
||||
|
||||
|
||||
// Mouse cursor graphic properties.
|
||||
|
@ -310,6 +311,7 @@ gfuiSetFocus(tGfuiObject *obj)
|
|||
tGfuiButton* button = &(obj->u.button);
|
||||
if (button->onFocus)
|
||||
button->onFocus(button->userDataOnFocus);
|
||||
playMenuSfx(SFX_FOCUS);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -318,6 +320,7 @@ gfuiSetFocus(tGfuiObject *obj)
|
|||
tGfuiGrButton* grbutton = &(obj->u.grbutton);
|
||||
if (grbutton->onFocus)
|
||||
grbutton->onFocus(grbutton->userDataOnFocus);
|
||||
playMenuSfx(SFX_FOCUS);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -326,6 +329,7 @@ gfuiSetFocus(tGfuiObject *obj)
|
|||
tGfuiEditbox* editbox = &(obj->u.editbox);
|
||||
if (editbox->onFocus)
|
||||
editbox->onFocus(editbox->userDataOnFocus);
|
||||
playMenuSfx(SFX_FOCUS);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -334,6 +338,7 @@ gfuiSetFocus(tGfuiObject *obj)
|
|||
tGfuiProgressbar* progress = &(obj->u.progressbar);
|
||||
if (progress->onFocus)
|
||||
progress->onFocus(progress->userDataOnFocus);
|
||||
playMenuSfx(SFX_FOCUS);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -342,6 +347,7 @@ gfuiSetFocus(tGfuiObject *obj)
|
|||
tGfuiLabel* label = &(obj->u.label);
|
||||
if (label->onFocus)
|
||||
label->onFocus(label->userDataOnFocus);
|
||||
//playMenuSfx(SFX_FOCUS);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -350,6 +356,7 @@ gfuiSetFocus(tGfuiObject *obj)
|
|||
tGfuiCombobox* combo = &(obj->u.combobox);
|
||||
if (combo->onFocus)
|
||||
combo->onFocus(combo->userDataOnFocus);
|
||||
playMenuSfx(SFX_FOCUS);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -561,18 +568,23 @@ gfuiMouseAction(void *vaction)
|
|||
switch (curObject->widget) {
|
||||
case GFUI_BUTTON:
|
||||
gfuiButtonAction((int)action);
|
||||
break;
|
||||
playMenuSfx(SFX_CLICK);
|
||||
break;
|
||||
case GFUI_GRBUTTON:
|
||||
gfuiGrButtonAction((int)action);
|
||||
break;
|
||||
gfuiGrButtonAction((int)action);
|
||||
playMenuSfx(SFX_CLICK);
|
||||
break;
|
||||
case GFUI_SCROLLIST:
|
||||
gfuiScrollListAction((int)action);
|
||||
break;
|
||||
gfuiScrollListAction((int)action);
|
||||
playMenuSfx(SFX_CLICK);
|
||||
break;
|
||||
case GFUI_EDITBOX:
|
||||
gfuiEditboxAction((int)action);
|
||||
break;
|
||||
gfuiEditboxAction((int)action);
|
||||
playMenuSfx(SFX_CLICK);
|
||||
break;
|
||||
case GFUI_COMBOBOX:
|
||||
gfuiComboboxAction((int)action);
|
||||
gfuiComboboxAction((int)action);
|
||||
playMenuSfx(SFX_CLICK);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include <tgfclient.h>
|
||||
#include <sound.h>
|
||||
#include <musicplayer.h>
|
||||
#include <guimenusfx.h>
|
||||
|
||||
#include "soundconfig.h"
|
||||
|
||||
|
@ -63,6 +64,22 @@ static float MusicVolumeValue = 100.0f;
|
|||
static float OriginalMusicVolumeValue = 100.0f;
|
||||
static int MusicVolumeValueId;
|
||||
|
||||
|
||||
// list of MenuSfx states.
|
||||
static const char *menusfxStateList[] = {SND_VAL_MENUSFX_STATE_ENABLED,
|
||||
SND_VAL_MENUSFX_STATE_DISABLED};
|
||||
static const int nbMenuSfxStates = sizeof(menusfxStateList) / sizeof(menusfxStateList[0]);
|
||||
static int curMenuSfxState = 0;
|
||||
static int OriginalMenuSfxState = 0;
|
||||
|
||||
// gui label id.
|
||||
static int MenuSfxStateId;
|
||||
|
||||
// volume
|
||||
static float MenuSfxVolumeValue = 100.0f;
|
||||
static float OriginalMenuSfxVolumeValue = 100.0f;
|
||||
static int MenuSfxVolumeValueId;
|
||||
|
||||
// gui screen handles.
|
||||
static void *scrHandle = NULL;
|
||||
static void *prevHandle = NULL;
|
||||
|
@ -125,6 +142,41 @@ static void readSoundCfg(void)
|
|||
sprintf(buf, "%g", MusicVolumeValue);
|
||||
GfuiEditboxSetString(scrHandle, MusicVolumeValueId, buf);
|
||||
|
||||
optionName = GfParmGetStr(paramHandle, SND_SCT_MENUSFX, SND_ATT_MENUSFX_STATE, menusfxStateList[0]);
|
||||
|
||||
for (i = 0; i < nbMenuSfxStates; i++) {
|
||||
if (strcmp(optionName, menusfxStateList[i]) == 0) {
|
||||
curMenuSfxState = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
OriginalMenuSfxState = curMenuSfxState;
|
||||
|
||||
GfuiLabelSetText(scrHandle, MenuSfxStateId, menusfxStateList[curMenuSfxState]);
|
||||
|
||||
// Music volume.
|
||||
MenuSfxVolumeValue = GfParmGetNum(paramHandle, SND_SCT_MENUSFX, SND_ATT_MENUSFX_VOLUME, "%", 100.0f);
|
||||
if (MenuSfxVolumeValue>100.0f) {
|
||||
MenuSfxVolumeValue = 100.0f;
|
||||
}
|
||||
else if (MenuSfxVolumeValue < 0.0f) {
|
||||
MenuSfxVolumeValue = 0.0f;
|
||||
}
|
||||
OriginalMenuSfxVolumeValue = MenuSfxVolumeValue;
|
||||
|
||||
sprintf(buf, "%g", MenuSfxVolumeValue);
|
||||
GfuiEditboxSetString(scrHandle, MenuSfxVolumeValueId, buf);
|
||||
|
||||
GfParmReleaseHandle(paramHandle);
|
||||
}
|
||||
|
||||
static void saveMenuSfxSettings()
|
||||
{
|
||||
void *paramHandle = GfParmReadFileLocal(SND_PARAM_FILE, GFPARM_RMODE_REREAD | GFPARM_RMODE_CREAT);
|
||||
GfParmSetStr(paramHandle, SND_SCT_MENUSFX, SND_ATT_MENUSFX_STATE, menusfxStateList[curMenuSfxState]);
|
||||
GfParmSetNum(paramHandle, SND_SCT_MENUSFX, SND_ATT_MENUSFX_VOLUME, "%", MenuSfxVolumeValue);
|
||||
|
||||
GfParmWriteFile(NULL, paramHandle, "sound");
|
||||
GfParmReleaseHandle(paramHandle);
|
||||
}
|
||||
|
||||
|
@ -149,6 +201,8 @@ static void saveSoundOption()
|
|||
GfParmSetNum(paramHandle, SND_SCT_SOUND, SND_ATT_SOUND_VOLUME, "%", VolumeValue);
|
||||
GfParmSetStr(paramHandle, SND_SCT_MUSIC, SND_ATT_MUSIC_STATE, musicStateList[curMusicState]);
|
||||
GfParmSetNum(paramHandle, SND_SCT_MUSIC, SND_ATT_MUSIC_VOLUME, "%", MusicVolumeValue);
|
||||
GfParmSetStr(paramHandle, SND_SCT_MENUSFX, SND_ATT_MENUSFX_STATE, menusfxStateList[curMenuSfxState]);
|
||||
GfParmSetNum(paramHandle, SND_SCT_MENUSFX, SND_ATT_MENUSFX_VOLUME, "%", MenuSfxVolumeValue);
|
||||
|
||||
GfParmWriteFile(NULL, paramHandle, "sound");
|
||||
GfParmReleaseHandle(paramHandle);
|
||||
|
@ -162,9 +216,14 @@ static void onAccept(void *)
|
|||
|
||||
saveMusicSettings();
|
||||
|
||||
saveMenuSfxSettings();
|
||||
|
||||
enableMusic(1 - curMusicState);
|
||||
setMusicVolume(MusicVolumeValue);
|
||||
|
||||
enableMenuSfx(1 - curMenuSfxState);
|
||||
setMenuSfxVolume(MenuSfxVolumeValue);
|
||||
|
||||
// Return to previous screen.
|
||||
GfuiScreenActivate(prevHandle);
|
||||
}
|
||||
|
@ -223,6 +282,30 @@ static void changeMusicVolume(void * )
|
|||
GfuiEditboxSetString(scrHandle, MusicVolumeValueId, buf);
|
||||
}
|
||||
|
||||
// Toggle Menu SFX state enabled/disabled.
|
||||
static void changeMenuSfxState(void *vp)
|
||||
{
|
||||
curMenuSfxState = (curMenuSfxState + (int)(long)vp + nbMenuSfxStates) % nbMenuSfxStates;
|
||||
|
||||
GfuiLabelSetText(scrHandle, MenuSfxStateId, menusfxStateList[curMenuSfxState]);
|
||||
|
||||
}
|
||||
|
||||
// Menu SFX Volume
|
||||
static void changeMenuSfxVolume(void * )
|
||||
{
|
||||
char* val = GfuiEditboxGetString(scrHandle, MenuSfxVolumeValueId);
|
||||
sscanf(val, "%g", &MenuSfxVolumeValue);
|
||||
if (MenuSfxVolumeValue > 100.0f)
|
||||
MenuSfxVolumeValue = 100.0f;
|
||||
else if (MenuSfxVolumeValue < 0.0f)
|
||||
MenuSfxVolumeValue = 0.0f;
|
||||
|
||||
char buf[32];
|
||||
sprintf(buf, "%g", MenuSfxVolumeValue);
|
||||
GfuiEditboxSetString(scrHandle, MenuSfxVolumeValueId, buf);
|
||||
}
|
||||
|
||||
static void onActivate(void * /* dummy */)
|
||||
{
|
||||
readSoundCfg();
|
||||
|
@ -260,8 +343,15 @@ void* SoundMenuInit(void *prevMenu)
|
|||
|
||||
MusicVolumeValueId = GfuiMenuCreateEditControl(scrHandle,param,"musicvolumeedit",NULL,NULL,changeMusicVolume);
|
||||
|
||||
GfuiMenuCreateButtonControl(scrHandle,param,"menusfxleftarrow",(void*)-1,changeMenuSfxState);
|
||||
GfuiMenuCreateButtonControl(scrHandle,param,"menusfxrightarrow",(void*)1,changeMenuSfxState);
|
||||
|
||||
MenuSfxStateId = GfuiMenuCreateLabelControl(scrHandle,param,"menusfxlabel");
|
||||
|
||||
MenuSfxVolumeValueId = GfuiMenuCreateEditControl(scrHandle,param,"menusfxvolumeedit",NULL,NULL,changeMenuSfxVolume);
|
||||
|
||||
GfParmReleaseHandle(param);
|
||||
|
||||
|
||||
GfuiAddKey(scrHandle, GFUIK_RETURN, "Apply", NULL, onAccept, NULL);
|
||||
GfuiAddKey(scrHandle, GFUIK_ESCAPE, "Cancel", NULL, onCancel, NULL);
|
||||
GfuiAddKey(scrHandle, GFUIK_F1, "Help", scrHandle, GfuiHelpScreen, NULL);
|
||||
|
|
Loading…
Reference in a new issue