Skip to content
This repository has been archived by the owner on Sep 4, 2019. It is now read-only.

Commit

Permalink
Merge branch 'master' of https://github.com/haxenme/NME
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby0x1 committed Oct 12, 2013
2 parents 5689495 + d8b6a37 commit 483ac57
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 34 deletions.
9 changes: 9 additions & 0 deletions project/android/AndroidFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Utils.h>
#include <jni.h>
#include <ByteArray.h>
#include <Sound.h>

#include <android/log.h>
#include "AndroidCommon.h"
Expand Down Expand Up @@ -85,6 +86,14 @@ class AndroidStage : public Stage
{
Event evt( inVal==1 ? etActivate : etDeactivate );
HandleEvent(evt);
if (inVal == 1)
{
Sound::Resume();
}
else
{
Sound::Suspend();
}
}
}

Expand Down
19 changes: 7 additions & 12 deletions project/common/ExternalInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,14 +1208,12 @@ value nme_stage_resize_window(value inStage, value inWidth, value inHeight)
{
#if (defined(HX_WINDOWS) || defined(HX_MACOS) || defined(HX_LINUX))
Stage *stage;

if (AbstractToObject(inStage,stage))
{
stage->ResizeWindow(val_int(inHeight), val_int(inWidth));
}

return alloc_null();
#endif
return alloc_null();
}
DEFINE_PRIM(nme_stage_resize_window,3);

Expand Down Expand Up @@ -1293,23 +1291,20 @@ value nme_stage_show_cursor(value inStage,value inShow)
}
DEFINE_PRIM(nme_stage_show_cursor,2);

value nme_stage_constrain_cursor_to_window_frame(value inStage, value inLock) {


value nme_stage_constrain_cursor_to_window_frame(value inStage, value inLock)
{
Stage *stage;

if (AbstractToObject(inStage,stage)) {
bool lock = val_bool(inLock);
stage->ConstrainCursorToWindowFrame( lock );
}

}
return alloc_null();
}
DEFINE_PRIM(nme_stage_constrain_cursor_to_window_frame,2);

value nme_stage_set_cursor_position_in_window( value inStage, value inX, value inY ) {

Stage *stage;
value nme_stage_set_cursor_position_in_window( value inStage, value inX, value inY )
{
Stage *stage;
if (AbstractToObject(inStage,stage))
{
int x = val_int(inX);
Expand Down
10 changes: 4 additions & 6 deletions project/common/FreeType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,18 @@ bool GetFontFile(const std::string& inName,std::string &outFile)

#ifdef IPHONEOS
#define FONT_BASE "/System/Library/Fonts/Cache/"
#define TIMES_ROMAN "TimesNewRoman.ttf"
#else
#define FONT_BASE "/Library/Fonts/"
#define TIMES_ROMAN "Times New Roman.ttf"
#endif

if (!strcasecmp(inName.c_str(),"_serif") || !strcasecmp(inName.c_str(),"times.ttf") || !strcasecmp(inName.c_str(),"times"))
outFile = FONT_BASE TIMES_ROMAN;
outFile = FONT_BASE "Georgia.ttf";
else if (!strcasecmp(inName.c_str(),"_sans") || !strcasecmp(inName.c_str(),"helvetica.ttf"))
outFile = FONT_BASE "Arial.ttf"; // Helvetica.dfont does not render
outFile = FONT_BASE "Arial Black.ttf"; // Helvetica.dfont does not render
else if (!strcasecmp(inName.c_str(),"_typewriter") || !strcasecmp(inName.c_str(),"courier.ttf"))
outFile = FONT_BASE "Courier.ttf";
outFile = FONT_BASE "Courier New.ttf";
else if (!strcasecmp(inName.c_str(),"arial.ttf"))
outFile = FONT_BASE "Arial.ttf";
outFile = FONT_BASE "Arial Black.ttf";
else
{
outFile = FONT_BASE + inName;
Expand Down
19 changes: 19 additions & 0 deletions project/common/TextField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,25 @@ void TextField::AddNode(const TiXmlNode *inNode, TextFormat *inFormat,int &ioCha
}
else if (el->ValueTStr()==L"p")
{
if (ioCharCount > 0)
{
if (mCharGroups.size())
{
CharGroup &last = *mCharGroups[ mCharGroups.size()-1 ];
last.mString.push_back('\n');
ioCharCount++;
}
else
{
CharGroup *chars = new CharGroup;
chars->mFormat = inFormat->IncRef();
chars->mFont = 0;
chars->mFontHeight = 0;
chars->mString.push_back('\n');
ioCharCount++;
mCharGroups.push_back(chars);
}
}
}

for (const TiXmlAttribute *att = el->FirstAttribute(); att; att = att->Next())
Expand Down
4 changes: 2 additions & 2 deletions project/include/Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@


#ifdef ANDROID
#define LOG_SOUND(args,...) ELOG(args)
#define LOG_SOUND(args,...) ELOG(args, ##__VA_ARGS__)
#else
#ifdef IPHONE
//#define LOG_SOUND(args,...) printf(args, ##__VA_ARGS__)
#define LOG_SOUND(args...) { }
#else
#define LOG_SOUND(args,...) printf(args)
#define LOG_SOUND(args,...) printf(args, ##__VA_ARGS__)
#endif
#endif
//#define LOG_SOUND(args...) { }
Expand Down
2 changes: 2 additions & 0 deletions project/include/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Sound : public Object
public:
static Sound *Create(const std::string &inFilename, bool inForceMusic);
static Sound *Create(float *inData, int len, bool inForceMusic);
static void Suspend();
static void Resume();

virtual void getID3Value(const std::string &inKey, std::string &outValue)
{
Expand Down
98 changes: 90 additions & 8 deletions project/openal/OpenALSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


namespace nme
{
{

OpenALChannel::OpenALChannel(Object *inSound, unsigned int inBufferID, int startTime, int inLoops, const SoundTransform &inTransform)
OpenALChannel::OpenALChannel(Object *inSound, ALuint inBufferID, int startTime, int inLoops, const SoundTransform &inTransform)
{
//LOG_SOUND("OpenALChannel constructor %d",inBufferID);
mSound = inSound;
Expand All @@ -14,14 +14,15 @@ namespace nme
mDynamicBuffer[0] = 0;
mDynamicBuffer[1] = 0;
mDynamicStackSize = 0;
mWasPlaying = false;
mSampleBuffer = 0;
float seek = 0;
int size = 0;

if (inBufferID>0)
{
// grab a source ID from openAL
alGenSources(1, &mSourceID);
alGenSources(1, &mSourceID);

// attach the buffer to the source
alSourcei(mSourceID, AL_BUFFER, inBufferID);
Expand All @@ -46,14 +47,13 @@ namespace nme

if (seek < 1)
{
//alSourceQueueBuffers(mSourceID, 1, &inBufferID);
alSourcePlay(mSourceID);
alSourcef(mSourceID, AL_BYTE_OFFSET, seek * size);
}

if (seek > 0 && seek < 1)
{
// TODO: Do we need to call this before playing?
alSourcei(mSourceID, AL_BYTE_OFFSET, seek * size);
}
mWasPlaying = true;
sgOpenChannels.push_back((intptr_t)this);
}
}

Expand All @@ -68,6 +68,7 @@ namespace nme
mDynamicBuffer[1] = 0;
mDynamicStackSize = 0;
mSampleBuffer = 0;
mWasPlaying = true;

alGenBuffers(2, mDynamicBuffer);
if (!mDynamicBuffer[0])
Expand All @@ -93,6 +94,8 @@ namespace nme

alSourcePlay(mSourceID);
}

//sgOpenChannels.push_back((intptr_t)this);
}


Expand Down Expand Up @@ -188,6 +191,15 @@ namespace nme
delete [] mSampleBuffer;
if (mSound)
mSound->DecRef();

for (int i = 0; i < sgOpenChannels.size(); i++)
{
if (sgOpenChannels[i] == (intptr_t)this)
{
sgOpenChannels.erase (i, 1);
break;
}
}
}


Expand Down Expand Up @@ -274,8 +286,36 @@ namespace nme

if (state == AL_PLAYING)
{
mWasPlaying = true;
alSourceStop(mSourceID);
}

mWasPlaying = false;
}


void OpenALChannel::pause()
{
ALint state;
alGetSourcei(mSourceID, AL_SOURCE_STATE, &state);

if (state == AL_PLAYING)
{
alSourcePause(mSourceID);
mWasPlaying = true;
return;
}

mWasPlaying = false;
}


void OpenALChannel::resume()
{
if (mWasPlaying)
{
alSourcePlay(mSourceID);
}
}


Expand Down Expand Up @@ -534,4 +574,46 @@ namespace nme
}
#endif


void Sound::Suspend()
{
//Always check if openal is initialized
if (!OpenALInit())
return;

OpenALChannel* channel = 0;
for (int i = 0; i < sgOpenChannels.size(); i++)
{
channel = (OpenALChannel*)(sgOpenChannels[i]);
if (channel)
{
channel->pause();
}
}

alcMakeContextCurrent(0);
alcSuspendContext(sgContext);
}


void Sound::Resume()
{
//Always check if openal is initialized
if (!OpenALInit())
return;

alcMakeContextCurrent(sgContext);
alcProcessContext(sgContext);

OpenALChannel* channel = 0;
for (int i = 0; i < sgOpenChannels.size(); i++)
{
channel = (OpenALChannel*)(sgOpenChannels[i]);
if (channel)
{
channel->resume();
}
}
}

} // end namespace nme
21 changes: 15 additions & 6 deletions project/openal/OpenALSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,29 @@ namespace nme
{


class OpenALChannel;


static ALCdevice *sgDevice = 0;
static ALCcontext *sgContext = 0;
static QuickVec<intptr_t> sgOpenChannels;

static bool openal_is_init = false;

static bool OpenALInit()
{
//LOG_SOUND("Sound.mm OpenALInit()");

static bool is_init = false;
if (!is_init)
if (!openal_is_init)
{
is_init = true;
openal_is_init = true;
sgDevice = alcOpenDevice(0); // select the "preferred device"
if (sgDevice)
{
sgContext=alcCreateContext(sgDevice,0);
alcMakeContextCurrent(sgContext);
}
sgOpenChannels = QuickVec<intptr_t>();
}
return sgContext;
}
Expand All @@ -48,7 +54,7 @@ static bool OpenALInit()
class OpenALChannel : public SoundChannel
{
public:
OpenALChannel(Object *inSound, unsigned int inBufferID, int startTime, int inLoops, const SoundTransform &inTransform);
OpenALChannel(Object *inSound, ALuint inBufferID, int startTime, int inLoops, const SoundTransform &inTransform);
OpenALChannel(const ByteArray &inBytes,const SoundTransform &inTransform);
void QueueBuffer(ALuint inBuffer, const ByteArray &inBytes);
void unqueueBuffers();
Expand All @@ -61,17 +67,20 @@ class OpenALChannel : public SoundChannel
double getPosition();
void setTransform(const SoundTransform &inTransform);
void stop();
void pause();
void resume();

protected:
~OpenALChannel();
Object *mSound;
unsigned int mSourceID;
ALuint mSourceID;
short *mSampleBuffer;
bool mDynamicDone;
ALuint mDynamicStackSize;
ALuint mDynamicStack[2];
ALuint mDynamicBuffer[2];
enum { STEREO_SAMPLES = 2 };
bool mWasPlaying;

};

Expand All @@ -97,7 +106,7 @@ class OpenALSound : public Sound
ALint bitsPerSample;
ALint channels;

unsigned int mBufferID;
ALuint mBufferID;
std::string mError;

};
Expand Down

0 comments on commit 483ac57

Please sign in to comment.