LuaYard/MusicPlayer.cpp
Kiritow bc189b4951 完整重构
分离各SDL类的包装代码.
事件分发处理全部转移至Lua Init层. C层只提供获取事件的方法.
这样有助于提高性能,以及今后Coroutine scheduler的添加.
2019-08-25 00:09:28 +08:00

350 lines
7.4 KiB
C++

#include "MusicPlayer.h"
#include "Music.h"
#include "LuaCommon.h"
#include "LuaHelper.h"
#include <iostream>
using namespace std;
inline int MusicPlayer::close(lua_State* L)
{
cout << "In MusicPlayer::close" << endl;
return 0;
}
inline int MusicPlayer::playMusic(lua_State* L)
{
Music* m = (Music*)luaL_checkudata(L, 1, "music");
int nLoop = -1;
if (lua_gettop(L) >= 2)
{
nLoop = luaL_checkinteger(L, 2);
}
int ret = Mix_PlayMusic(m->music.get(), nLoop);
cout << "Play music ret: " << ret << " " << SDL_GetError() << endl;
return 0;
}
inline int MusicPlayer::resumeMusic(lua_State* L)
{
Mix_ResumeMusic();
return 0;
}
inline int MusicPlayer::stopMusic(lua_State* L)
{
Mix_HaltMusic();
return 0;
}
inline int MusicPlayer::pauseMusic(lua_State* L)
{
Mix_PauseMusic();
return 0;
}
inline int MusicPlayer::rewindMusic(lua_State* L)
{
Mix_RewindMusic();
return 0;
}
inline int MusicPlayer::fadeInMusic(lua_State* L)
{
Music* m = (Music*)luaL_checkudata(L, 1, "music");
int fadeTime = luaL_checkinteger(L, 2);
int nLoop = -1;
if (lua_gettop(L) >= 3)
{
nLoop = luaL_checkinteger(L, 3);
}
int ret = Mix_FadeInMusic(m->music.get(), nLoop, fadeTime);
cout << "Fade in music ret: " << ret << " " << SDL_GetError() << endl;
return 0;
}
inline int MusicPlayer::fadeInMusicPos(lua_State* L)
{
Music* m = (Music*)luaL_checkudata(L, 1, "music");
int fadeTime = luaL_checkinteger(L, 2);
double pos = luaL_checknumber(L, 3);
int nLoop = -1;
if (lua_gettop(L) >= 4)
{
nLoop = luaL_checkinteger(L, 4);
}
int ret = Mix_FadeInMusicPos(m->music.get(), nLoop, fadeTime, pos);
cout << "Fade in music ret: " << ret << " " << SDL_GetError() << endl;
return 0;
}
inline int MusicPlayer::fadeOutMusic(lua_State* L)
{
int fadeTime = luaL_checkinteger(L, 1);
int ret = Mix_FadeOutMusic(fadeTime);
cout << "Fade out music ret: " << ret << " " << SDL_GetError() << endl;
return 0;
}
inline int MusicPlayer::setMusicVolume(lua_State* L)
{
int v = luaL_checkinteger(L, 1);
int old = Mix_VolumeMusic(v);
lua_pushinteger(L, old);
return 1;
}
inline int MusicPlayer::setMusicPos(lua_State* L)
{
double pos = luaL_checknumber(L, 1);
int ret = Mix_SetMusicPosition(pos);
if (ret != 0)
{
return LuaSDLError(L);
}
return 0;
}
inline int MusicPlayer::isPlayingMusic(lua_State* L)
{
lua_pushboolean(L, Mix_PlayingMusic());
return 1;
}
inline int MusicPlayer::isPausedMusic(lua_State* L)
{
lua_pushboolean(L, Mix_PausedMusic());
return 1;
}
inline int MusicPlayer::isFadingMusic(lua_State* L)
{
switch (Mix_FadingMusic())
{
case MIX_FADING_OUT:
lua_pushstring(L, "out");
break;
case MIX_FADING_IN:
lua_pushstring(L, "in");
break;
case MIX_NO_FADING:
default:
lua_pushstring(L, "no");
break;
}
return 1;
}
inline int MusicPlayer::playChannel(lua_State* L)
{
Chunk* c = (Chunk*)luaL_checkudata(L, 1, "chunk");
int channel = luaL_checkinteger(L, 2);
int nLoop = -1;
if (lua_gettop(L) >= 3)
{
nLoop = luaL_checkinteger(L, 3);
}
int ret = Mix_PlayChannel(channel, c->chunk.get(), nLoop);
cout << "Play channel ret: " << ret << " " << SDL_GetError() << endl;
return 0;
}
inline int MusicPlayer::fadeInChannel(lua_State* L)
{
Chunk* c = (Chunk*)luaL_checkudata(L, 1, "chunk");
int channel = luaL_checkinteger(L, 2);
int fadeTime = luaL_checkinteger(L, 3);
int nLoop = -1;
if (lua_gettop(L) >= 4)
{
nLoop = luaL_checkinteger(L, 4);
}
int ret = Mix_FadeInChannel(channel, c->chunk.get(), nLoop, fadeTime);
cout << "Fade in channel ret: " << ret << " " << SDL_GetError() << endl;
return 0;
}
inline int MusicPlayer::fadeOutChannel(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
int fadeTime = luaL_checkinteger(L, 2);
Mix_FadeOutChannel(channel, fadeTime);
return 0;
}
inline int MusicPlayer::stopChannel(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
Mix_HaltChannel(channel);
return 0;
}
inline int MusicPlayer::resumeChannel(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
Mix_Resume(channel);
return 0;
}
inline int MusicPlayer::pauseChannel(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
Mix_Pause(channel);
return 0;
}
inline int MusicPlayer::isPlayingChannel(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
if (channel == -1)
{
lua_pushinteger(L, Mix_Playing(channel));
}
else
{
lua_pushboolean(L, Mix_Playing(channel));
}
return 1;
}
inline int MusicPlayer::isPausedChannel(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
if (channel == -1)
{
lua_pushinteger(L, Mix_Paused(channel));
}
else
{
lua_pushboolean(L, Mix_Paused(channel));
}
return 1;
}
inline int MusicPlayer::isFadingChannel(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
if (channel == -1)
{
return luaL_error(L, "Invalid channel id for isFadingChannel.");
}
switch (Mix_FadingChannel(channel))
{
case MIX_FADING_OUT:
lua_pushstring(L, "out");
break;
case MIX_FADING_IN:
lua_pushstring(L, "in");
break;
case MIX_NO_FADING:
default:
lua_pushstring(L, "no");
break;
}
return 1;
}
inline int MusicPlayer::setChannelVolume(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
int v = luaL_checkinteger(L, 2);
int old = Mix_Volume(channel, v);
lua_pushinteger(L, old);
return 1;
}
inline int MusicPlayer::setChannelDistance(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
int dist = luaL_checkinteger(L, 2);
int ret = Mix_SetDistance(channel, dist);
if (ret == 0)
{
return LuaSDLError(L);
}
return 0;
}
inline int MusicPlayer::setChannelPosition(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
int angle = luaL_checkinteger(L, 2);
int dist = luaL_checkinteger(L, 3);
cout << "Mix_SetPosition " << channel << " " << angle << " " << dist << endl;
if (0 == Mix_SetPosition(channel, angle, dist))
{
return LuaSDLError(L);
}
return 0;
}
inline int MusicPlayer::setChannelPanning(lua_State* L)
{
int channel = luaL_checkinteger(L, 1);
int left = luaL_checkinteger(L, 2);
int right = luaL_checkinteger(L, 3);
if (0 == Mix_SetPanning(channel, left, right))
{
return LuaSDLError(L);
}
return 0;
}
inline int MusicPlayer::setTotalChannel(lua_State* L)
{
int nTotal = luaL_checkinteger(L, 1);
int count = Mix_AllocateChannels(nTotal);
lua_pushinteger(L, count);
return 0;
}
int MusicPlayer::create(lua_State* L)
{
cout << "In MusicPlayer::create" << endl;
if (luaL_newmetatable(L, "musicplayer"))
{
setfn(close, "close");
setfn(playMusic, "playMusic");
setfn(resumeMusic, "resumeMusic");
setfn(pauseMusic, "pauseMusic");
setfn(stopMusic, "stopMusic");
setfn(rewindMusic, "rewindMusic");
setfn(fadeInMusic, "fadeInMusic");
setfn(fadeOutMusic, "fadeOutMusic");
setfn(fadeInMusicPos, "fadeInMusicPos");
setfn(setMusicVolume, "setMusicVolume");
setfn(setMusicPos, "setMusicPos");
setfn(isPlayingMusic, "isPlayingMusic");
setfn(isPausedMusic, "isPausedMusic");
setfn(isFadingMusic, "isFadingMusic");
setfn(playChannel, "playChannel");
setfn(resumeChannel, "resumeChannel");
setfn(pauseChannel, "pauseChannel");
setfn(stopChannel, "stopChannel");
setfn(fadeInChannel, "fadeInChannel");
setfn(fadeOutChannel, "fadeOutChannel");
setfn(setChannelVolume, "setChannelVolume");
setfn(setChannelDistance, "setChannelDistance");
setfn(setChannelPosition, "setChannelPosition");
setfn(setChannelPanning, "setChannelPanning");
setfn(setTotalChannel, "setTotalChannel");
setfn(isPlayingChannel, "isPlayingChannel");
setfn(isPausedChannel, "isPausedChannel");
setfn(isFadingChannel, "isFadingChannel");
}
return 1;
}