public Resource(Object internalObject, int resourceType, bool isDynamicPlaceholder = false, BoundedStream stream = null) { mInternalObject = internalObject; mResourceType = resourceType; mIsDynamicPlaceholder = isDynamicPlaceholder; mStream = stream; }
public void SetFileStream(BoundedStream stream) { mStream = stream; }
public void Init(Ioctls ioctls, Core core, Runtime runtime) { #if false mAudioInstanceUpdater = new AudioInstanceUpdater(mAudioInstances); Thread thread = new Thread(new ThreadStart(mAudioInstanceUpdater.Loop)); thread.Start(); #endif ioctls.maAudioDataCreateFromURL = delegate(int _mime, int _url, int _flags) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { String url = core.GetDataMemory().ReadStringAtAddress(_url); String mime = core.GetDataMemory().ReadStringAtAddress(_mime); bool shouldStream = (_flags & MoSync.Constants.MA_AUDIO_DATA_STREAM) != 0; IAudioData ad; if (mime == "audio/mpeg") { ad = Mp3Audio.FromUrlOrFilePath(url, shouldStream); } else { ad = Audio.FromUrlOrFilePath(url, shouldStream); } lock (mAudioData) { mAudioData.Add(ad); ret = mAudioData.Count - 1; } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(ret); }; ioctls.maAudioDataCreateFromResource = delegate(int _mime, int _data, int _offset, int _length, int _flags) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { Resource audiores = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); BoundedStream s = new BoundedStream((Stream)audiores.GetInternalObject(), _offset, _length); String mime = core.GetDataMemory().ReadStringAtAddress(_mime); IAudioData ad; if (mime == "audio/mpeg") { ad = Mp3Audio.FromStream(s); } else { ad = Audio.FromStream(s, (_flags & MoSync.Constants.MA_AUDIO_DATA_STREAM) != 0); } lock (mAudioData) { mAudioData.Add(ad); ret = mAudioData.Count - 1; } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(ret); }; ioctls.maAudioDataDestroy = delegate(int _audioData) { try { lock (mAudioData) { IAudioData ad = mAudioData[_audioData]; ad.Dispose(); mAudioData[_audioData] = null; } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; ioctls.maAudioPrepare = delegate(int _audioInstance, int async) { try { lock (mAudioInstances) { IAudioInstance ad = mAudioInstances[_audioInstance]; if (async == 0) { ad.Prepare(null); } else { ad.Prepare(() => { // Send initialized event. MoSync.Memory eventData = new MoSync.Memory(8); eventData.WriteInt32(MoSync.Struct.MAEvent.type, MoSync.Constants.EVENT_TYPE_AUDIO_PREPARED); eventData.WriteInt32(MoSync.Struct.MAEvent.audioInstance, _audioInstance); runtime.PostEvent(new Event(eventData)); } ); } } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; ioctls.maAudioInstanceCreate = delegate(int _audioData) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { lock (mAudioInstances) { IAudioData ad = mAudioData[_audioData]; mAudioInstances.Add(ad.CreateInstance()); ret = mAudioInstances.Count - 1; } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(ret); }; ioctls.maAudioInstanceCreateDynamic = delegate(int _sampleRate, int _numChannels, int _bufferSize) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { lock (mAudioInstances) { AudioInstanceDynamic aid = Audio.CreateDynamic(_sampleRate, _numChannels, _bufferSize); mAudioInstances.Add(aid); ret = mAudioInstances.Count - 1; aid.SetOnBufferNeededCallback(() => { // Send initialized event. MoSync.Memory eventData = new MoSync.Memory(8); eventData.WriteInt32(MoSync.Struct.MAEvent.type, MoSync.Constants.EVENT_TYPE_AUDIO_COMPLETED); eventData.WriteInt32(MoSync.Struct.MAEvent.audioInstance, ret); runtime.PostEvent(new Event(eventData)); }); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(ret); }; ioctls.maAudioGetPendingBufferCount = delegate(int _instance) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { lock (mAudioInstances) { AudioInstanceDynamic ai = (AudioInstanceDynamic)mAudioInstances[_instance]; ret = ai.GetPendingBufferCount(); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(ret); }; ioctls.maAudioSubmitBuffer = delegate(int _instance, int _pointer, int _numBytes) { try { lock (mAudioInstances) { AudioInstanceDynamic ai = (AudioInstanceDynamic)mAudioInstances[_instance]; ai.SubmitBuffer(core.GetDataMemory().GetData(), _pointer, _numBytes); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; ioctls.maAudioInstanceDestroy = delegate(int _audioInstance) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.Dispose(); mAudioInstances[_audioInstance] = null; } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; ioctls.maAudioPlay = delegate(int _audioInstance) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.Play(); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; ioctls.maAudioStop = delegate(int _audioInstance) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.Stop(); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; ioctls.maAudioPause = delegate(int _audioInstance) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.Pause(); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; ioctls.maAudioSetNumberOfLoops = delegate(int _audioInstance, int loops) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.SetNumberOfLoops(loops); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; // SoundEffectInstances nor the MediaPlayer doesn't support setting position, // however we can make a special case where the sound is reset if _milliseconds equals to zero. // We could implement a better SoundEffectInstance using DynamicSoundEffectInstance // parsing wavefiles ourselves.. But that would require some work. ioctls.maAudioSetPosition = delegate(int _audioInstance, int _milliseconds) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.SetPosition(_milliseconds); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; // SoundEffectInstances doesnt support getting the location of the sound // this of course could be approximated by saving a time stamp when the sound // starts to play, buuut no. ioctls.maAudioGetPosition = delegate(int _audioInstance) { int ret = MoSync.Constants.MA_AUDIO_ERR_OK; try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ret = ai.GetPosition(); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(ret); }; ioctls.maAudioGetLength = delegate(int _audioInstance) { int ret = MoSync.Constants.MA_AUDIO_ERR_OK; try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ret = ai.GetLength(); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(ret); }; ioctls.maAudioSetVolume = delegate(int _audioInstance, float volume) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.SetVolume(volume); } } catch (MoSync.Util.ReturnValueException rve) { return(rve.result); } catch (Exception) { return(MoSync.Constants.MA_AUDIO_ERR_GENERIC); } return(MoSync.Constants.MA_AUDIO_ERR_OK); }; }
public void Init(Ioctls ioctls, Core core, Runtime runtime) { #if false mAudioInstanceUpdater = new AudioInstanceUpdater(mAudioInstances); Thread thread = new Thread(new ThreadStart(mAudioInstanceUpdater.Loop)); thread.Start(); #endif ioctls.maAudioDataCreateFromURL = delegate(int _mime, int _url, int _flags) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { String url = core.GetDataMemory().ReadStringAtAddress(_url); IAudioData ad = Audio.FromUrlOrFilePath(url, (_flags & MoSync.Constants.MA_AUDIO_DATA_STREAM) != 0); lock (mAudioData) { mAudioData.Add(ad); ret = mAudioData.Count - 1; } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return ret; }; ioctls.maAudioDataCreateFromResource = delegate(int _mime, int _data, int _offset, int _length, int _flags) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { Resource audiores = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); BoundedStream s = new BoundedStream((Stream)audiores.GetInternalObject(), _offset, _length); IAudioData ad = Audio.FromStream(s, (_flags & MoSync.Constants.MA_AUDIO_DATA_STREAM) != 0); lock (mAudioData) { mAudioData.Add(ad); ret = mAudioData.Count - 1; } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return ret; }; ioctls.maAudioDataDestroy = delegate(int _audioData) { try { lock (mAudioData) { IAudioData ad = mAudioData[_audioData]; ad.Dispose(); mAudioData[_audioData] = null; } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; ioctls.maAudioPrepare = delegate(int _audioInstance, int async) { try { lock (mAudioInstances) { IAudioInstance ad = mAudioInstances[_audioInstance]; if (async == 0) { ad.Prepare(null); } else { ad.Prepare(() => { // Send initialized event. MoSync.Memory eventData = new MoSync.Memory(8); eventData.WriteInt32(MoSync.Struct.MAEvent.type, MoSync.Constants.EVENT_TYPE_AUDIO_PREPARED); eventData.WriteInt32(MoSync.Struct.MAEvent.audioInstance, _audioInstance); runtime.PostEvent(new Event(eventData)); } ); } } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; ioctls.maAudioInstanceCreate = delegate(int _audioData) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { lock (mAudioInstances) { IAudioData ad = mAudioData[_audioData]; mAudioInstances.Add(ad.CreateInstance()); ret = mAudioInstances.Count - 1; } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return ret; }; ioctls.maAudioInstanceCreateDynamic = delegate(int _sampleRate, int _numChannels, int _bufferSize) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { lock (mAudioInstances) { AudioInstanceDynamic aid = Audio.CreateDynamic(_sampleRate, _numChannels, _bufferSize); mAudioInstances.Add(aid); ret = mAudioInstances.Count - 1; aid.SetOnBufferNeededCallback(() => { // Send initialized event. MoSync.Memory eventData = new MoSync.Memory(8); eventData.WriteInt32(MoSync.Struct.MAEvent.type, MoSync.Constants.EVENT_TYPE_AUDIO_COMPLETED); eventData.WriteInt32(MoSync.Struct.MAEvent.audioInstance, ret); runtime.PostEvent(new Event(eventData)); }); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return ret; }; ioctls.maAudioGetPendingBufferCount = delegate(int _instance) { int ret = MoSync.Constants.MA_AUDIO_ERR_GENERIC; try { lock (mAudioInstances) { AudioInstanceDynamic ai = (AudioInstanceDynamic)mAudioInstances[_instance]; ret = ai.GetPendingBufferCount(); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return ret; }; ioctls.maAudioSubmitBuffer = delegate(int _instance, int _pointer, int _numBytes) { try { lock (mAudioInstances) { AudioInstanceDynamic ai = (AudioInstanceDynamic)mAudioInstances[_instance]; ai.SubmitBuffer(core.GetDataMemory().GetData(), _pointer, _numBytes); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; ioctls.maAudioInstanceDestroy = delegate(int _audioInstance) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.Dispose(); mAudioInstances[_audioInstance] = null; } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; ioctls.maAudioPlay = delegate(int _audioInstance) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.Play(); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; ioctls.maAudioStop = delegate(int _audioInstance) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.Stop(); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; ioctls.maAudioPause = delegate(int _audioInstance) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.Pause(); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; ioctls.maAudioSetNumberOfLoops = delegate(int _audioInstance, int loops) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.SetNumberOfLoops(loops); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; // SoundEffectInstances nor the MediaPlayer doesn't support setting position, // however we can make a special case where the sound is reset if _milliseconds equals to zero. // We could implement a better SoundEffectInstance using DynamicSoundEffectInstance // parsing wavefiles ourselves.. But that would require some work. ioctls.maAudioSetPosition = delegate(int _audioInstance, int _milliseconds) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.SetPosition(_milliseconds); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; // SoundEffectInstances doesnt support getting the location of the sound // this of course could be approximated by saving a time stamp when the sound // starts to play, buuut no. ioctls.maAudioGetPosition = delegate(int _audioInstance) { int ret = MoSync.Constants.MA_AUDIO_ERR_OK; try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ret = ai.GetPosition(); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return ret; }; ioctls.maAudioGetLength = delegate(int _audioInstance) { int ret = MoSync.Constants.MA_AUDIO_ERR_OK; try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ret = ai.GetLength(); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return ret; }; ioctls.maAudioSetVolume = delegate(int _audioInstance, float volume) { try { lock (mAudioInstances) { IAudioInstance ai = mAudioInstances[_audioInstance]; ai.SetVolume(volume); } } catch (MoSync.Util.ReturnValueException rve) { return rve.result; } catch (Exception) { return MoSync.Constants.MA_AUDIO_ERR_GENERIC; } return MoSync.Constants.MA_AUDIO_ERR_OK; }; }
public Resource(Object internalObject, int resourceType, bool isDynamicPlaceholder = false, BoundedStream stream = null) { mInternalObject = internalObject; mResourceType = resourceType; mIsDynamicPlaceholder = isDynamicPlaceholder; mStream = stream; }
public void SetFileStream(BoundedStream stream) { mStream = stream; }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual; double screenWidth = System.Windows.Application.Current.Host.Content.ActualWidth; double screenHeight = System.Windows.Application.Current.Host.Content.ActualHeight; if ((int)screenHeight == 0) { throw new Exception("screenHeight"); } PhoneApplicationPage mainPage = (PhoneApplicationPage)frame.Content; mMainImage = new Image(); mainPage.Width = screenWidth; mainPage.Height = screenHeight; mMainImage.Width = screenWidth; mMainImage.Height = screenHeight; mainPage.Content = mMainImage; mClipRect.X = 0.0; mClipRect.Y = 0.0; mClipRect.Width = screenWidth; mClipRect.Height = screenHeight; // no apparent effect on memory leaks. runtime.RegisterCleaner(delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { mainPage.Content = null; }); }); mBackBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mFrontBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mMainImage.Source = mFrontBuffer; // clear front and backbuffer. for (int i = 0; i < mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight; i++) { mBackBuffer.Pixels[i] = mBackBuffer.Pixels[i] = (int)(0xff << 24); } mCurrentDrawTarget = mBackBuffer; mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); syscalls.maSetColor = delegate(int rgb) { int oldColor = (int)mCurrentColor; mCurrentColor = 0xff000000 | (uint)(rgb & 0xffffff); mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); return(oldColor & 0xffffff); }; syscalls.maSetClipRect = delegate(int x, int y, int w, int h) { MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, 0, 0, mCurrentDrawTarget.PixelWidth, mCurrentDrawTarget.PixelHeight, out x, out y, out w, out h); mClipRect.X = x; mClipRect.Y = y; mClipRect.Width = w; mClipRect.Height = h; }; syscalls.maGetClipRect = delegate(int cliprect) { #if !LIB Memory mem = core.GetDataMemory(); #else SystemMemory mem = core.GetDataMemory(); #endif mem.WriteInt32(cliprect + MoSync.Struct.MARect.left, (int)mClipRect.X); mem.WriteInt32(cliprect + MoSync.Struct.MARect.top, (int)mClipRect.Y); mem.WriteInt32(cliprect + MoSync.Struct.MARect.width, (int)mClipRect.Width); mem.WriteInt32(cliprect + MoSync.Struct.MARect.height, (int)mClipRect.Height); }; syscalls.maPlot = delegate(int x, int y) { mCurrentDrawTarget.SetPixel(x, y, (int)mCurrentColor); }; syscalls.maUpdateScreen = delegate() { //System.Array.Copy(mBackBuffer.Pixels, mFrontBuffer.Pixels, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight); System.Buffer.BlockCopy(mBackBuffer.Pixels, 0, mFrontBuffer.Pixels, 0, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight * 4); InvalidateWriteableBitmapOnMainThread(mFrontBuffer); }; syscalls.maFillRect = delegate(int x, int y, int w, int h) { // this function has a bug (it only fills one pixel less than the image.) //mCurrentDrawTarget.FillRectangle(x, y, x + w, y + h, (int)mCurrentColor); MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, 0, 0, mCurrentDrawTarget.PixelWidth, mCurrentDrawTarget.PixelHeight, out x, out y, out w, out h); MoSync.GraphicsUtil.ClipRectangle( x, y, w, h, (int)mClipRect.X, (int)mClipRect.Y, (int)mClipRect.Width, (int)mClipRect.Height, out x, out y, out w, out h); if (w <= 0 || h <= 0) { return; } int index = x + y * mCurrentDrawTarget.PixelWidth; while (h-- != 0) { int width = w; while (width-- != 0) { mCurrentDrawTarget.Pixels[index] = (int)mCurrentColor; index++; } index += -w + mCurrentDrawTarget.PixelWidth; } }; syscalls.maLine = delegate(int x1, int y1, int x2, int y2) { GraphicsUtil.Point p1 = new GraphicsUtil.Point(x1, y1); GraphicsUtil.Point p2 = new GraphicsUtil.Point(x2, y2); if (!GraphicsUtil.ClipLine(p1, p2, (int)mClipRect.X, (int)(mClipRect.X + mClipRect.Width), (int)mClipRect.Y, (int)(mClipRect.Y + mClipRect.Height))) { return; } mCurrentDrawTarget.DrawLine((int)p1.x, (int)p1.y, (int)p2.x, (int)p2.y, (int)mCurrentColor); }; textBlock.FontSize = mCurrentFontSize; syscalls.maDrawText = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); if (text.Length == 0) { return; } DrawText(text, left, top); }; syscalls.maGetTextSize = delegate(int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); int textWidth = 0; int textHeight = 0; GetTextSize(text, out textWidth, out textHeight); return(MoSync.Util.CreateExtent(textWidth, textHeight)); }; syscalls.maDrawTextW = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); if (text.Length == 0) { return; } DrawText(text, left, top); }; syscalls.maGetTextSizeW = delegate(int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); int textWidth = 0; int textHeight = 0; GetTextSize(text, out textWidth, out textHeight); return(MoSync.Util.CreateExtent(textWidth, textHeight)); }; syscalls.maFillTriangleFan = delegate(int points, int count) { int[] newPoints = new int[count * 2 + 2]; for (int i = 0; i < count; i++) { newPoints[i * 2 + 0] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); newPoints[i * 2 + 1] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } newPoints[count * 2 + 0] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.x); newPoints[count * 2 + 1] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.y); mCurrentDrawTarget.FillPolygon(newPoints, (int)mCurrentColor); }; syscalls.maFillTriangleStrip = delegate(int points, int count) { int[] xcoords = new int[count]; int[] ycoords = new int[count]; for (int i = 0; i < count; i++) { xcoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); ycoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } for (int i = 2; i < count; i++) { mCurrentDrawTarget.FillTriangle( xcoords[i - 2], ycoords[i - 2], xcoords[i - 1], ycoords[i - 1], xcoords[i - 0], ycoords[i - 0], (int)mCurrentColor); } }; syscalls.maSetDrawTarget = delegate(int drawTarget) { int oldDrawTarget = mCurrentDrawTargetIndex; if (drawTarget == mCurrentDrawTargetIndex) { return(oldDrawTarget); } if (drawTarget == MoSync.Constants.HANDLE_SCREEN) { mCurrentDrawTarget = mBackBuffer; mCurrentDrawTargetIndex = drawTarget; return(oldDrawTarget); } Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, drawTarget); mCurrentDrawTarget = (WriteableBitmap)res.GetInternalObject(); mCurrentDrawTargetIndex = drawTarget; return(oldDrawTarget); }; syscalls.maGetScrSize = delegate() { int w = mBackBuffer.PixelWidth; int h = mBackBuffer.PixelHeight; MoSync.Util.RunActionOnMainThreadSync(() => { // if the orientation is landscape, the with and height should be swaped PhoneApplicationPage currentPage = (((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage); if (currentPage.Orientation == PageOrientation.Landscape || currentPage.Orientation == PageOrientation.LandscapeLeft || currentPage.Orientation == PageOrientation.LandscapeRight) { int aux = w; w = h; h = aux; } }); return(MoSync.Util.CreateExtent(w, h)); }; syscalls.maGetImageSize = delegate(int handle) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, handle); int w = 0, h = 0; #if !LIB BitmapSource src = (BitmapSource)res.GetInternalObject(); if (src == null) { MoSync.Util.CriticalError("maGetImageSize used with an invalid image resource."); } else { MoSync.Util.RunActionOnMainThreadSync(() => { w = src.PixelWidth; h = src.PixelHeight; }); } #else if (res.GetInternalObject() == null) { MoSync.Util.CriticalError("maGetImageSize used with an invalid image resource."); } else { MoSync.Util.RunActionOnMainThreadSync(() => { if (res.GetInternalObject() is Stream) { BitmapImage bi = new BitmapImage(); bi.SetSource((Stream)res.GetInternalObject()); w = bi.PixelWidth; h = bi.PixelHeight; } else if (res.GetInternalObject() is WriteableBitmap) { BitmapSource src = (BitmapSource)res.GetInternalObject(); w = src.PixelWidth; h = src.PixelHeight; } }); } #endif return(MoSync.Util.CreateExtent(w, h)); }; syscalls.maDrawImage = delegate(int image, int left, int top) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); #if !LIB WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); #else Object src = null; MoSync.Util.RunActionOnMainThreadSync(() => { Object internalObj = res.GetInternalObject(); if (internalObj is Stream) { src = new WriteableBitmap(0, 0); (src as WriteableBitmap).SetSource((Stream)res.GetInternalObject()); } else if (internalObj is WriteableBitmap) { src = res.GetInternalObject(); } }); #endif Rect srcRect = new Rect(0, 0, (src as WriteableBitmap).PixelWidth, (src as WriteableBitmap).PixelHeight); Rect dstRect = new Rect(left, top, (src as WriteableBitmap).PixelWidth, (src as WriteableBitmap).PixelHeight); mCurrentDrawTarget.Blit(dstRect, (src as WriteableBitmap), srcRect, WriteableBitmapExtensions.BlendMode.Alpha); }; syscalls.maDrawImageRegion = delegate(int image, int srcRectPtr, int dstPointPtr, int transformMode) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); #if !LIB WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); #else SystemMemory dataMemory = core.GetDataMemory(); Object src = null; MoSync.Util.RunActionOnMainThreadSync(() => { Object internalObj = res.GetInternalObject(); if (internalObj is Stream) { src = new WriteableBitmap(0, 0); (src as WriteableBitmap).SetSource((Stream)res.GetInternalObject()); } else if (internalObj is WriteableBitmap) { src = res.GetInternalObject(); } }); #endif int srcRectX = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.height); int dstPointX = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.x); int dstPointY = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.y); Rect srcRect = new Rect(srcRectX, srcRectY, srcRectW, srcRectH); Rect dstRect = new Rect(dstPointX, dstPointY, srcRectW, srcRectH); GraphicsUtil.DrawImageRegion(mCurrentDrawTarget, dstPointX, dstPointY, srcRect, (src as WriteableBitmap), transformMode, mClipRect); }; syscalls.maCreateDrawableImage = delegate(int placeholder, int width, int height) { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, placeholder); res.SetResourceType(MoSync.Constants.RT_IMAGE); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); for (int i = 0; i < bitmap.PixelWidth * bitmap.PixelHeight; i++) { bitmap.Pixels[i] = (int)(0xff << 24); } }); if (bitmap == null) { return(MoSync.Constants.RES_OUT_OF_MEMORY); } res.SetInternalObject(bitmap); return(MoSync.Constants.RES_OK); }; syscalls.maCreateImageRaw = delegate(int _placeholder, int _src, int _size, int _alpha) { int width = MoSync.Util.ExtentX(_size); int height = MoSync.Util.ExtentY(_size); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); //core.GetDataMemory().ReadIntegers(bitmap.Pixels, _src, width * height); #if !LIB bitmap.FromByteArray(core.GetDataMemory().GetData(), _src, width * height * 4); #else byte[] bytes = new byte[width * height * 4]; core.GetDataMemory().ReadBytes(bytes, _src, width * height * 4); bitmap.FromByteArray(bytes, 0, width * height * 4); // TO BE TESTED #endif if (_alpha == 0) { int[] pixels = bitmap.Pixels; int numPixels = width * height; for (int i = 0; i < numPixels; i++) { pixels[i] = (int)((uint)pixels[i] | 0xff000000); } } Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeholder); res.SetInternalObject(bitmap); res.SetResourceType(MoSync.Constants.RT_IMAGE); return(MoSync.Constants.RES_OK); }; syscalls.maDrawRGB = delegate(int _dstPoint, int _src, int _srcRect, int _scanlength) { #if !LIB Memory dataMemory = core.GetDataMemory(); #else SystemMemory dataMemory = core.GetDataMemory(); #endif int dstX = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.x); int dstY = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.y); int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int[] pixels = mCurrentDrawTarget.Pixels; // todo: clipRect _scanlength *= 4; // sizeof(int) for (int h = 0; h < srcRectH; h++) { int pixelIndex = dstY * mCurrentDrawTarget.PixelWidth + dstX; int address = _src + (srcRectY + h) * _scanlength; for (int w = 0; w < srcRectW; w++) { uint srcPixel = dataMemory.ReadUInt32(address); uint dstPixel = (uint)pixels[pixelIndex]; uint srcPixelR = (srcPixel & 0x00ff0000) >> 16; uint srcPixelG = (srcPixel & 0x0000ff00) >> 8; uint srcPixelB = (srcPixel & 0x000000ff) >> 0; uint srcPixelA = (srcPixel & 0xff000000) >> 24; uint dstPixelR = (dstPixel & 0x00ff0000) >> 16; uint dstPixelG = (dstPixel & 0x0000ff00) >> 8; uint dstPixelB = (dstPixel & 0x000000ff) >> 0; uint dstPixelA = (dstPixel & 0xff000000) >> 24; dstPixelR += ((srcPixelR - dstPixelR) * srcPixelA) / 255; dstPixelG += ((srcPixelG - dstPixelG) * srcPixelA) / 255; dstPixelB += ((srcPixelB - dstPixelB) * srcPixelA) / 255; dstPixel = (dstPixelA << 24) | (dstPixelR << 16) | (dstPixelG << 8) | (dstPixelB); pixels[pixelIndex] = (int)dstPixel; address += 4; pixelIndex++; } dstY++; } }; syscalls.maGetImageData = delegate(int _image, int _dst, int _srcRect, int _scanlength) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, _image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); #if !LIB Memory dataMemory = core.GetDataMemory(); #else SystemMemory dataMemory = core.GetDataMemory(); #endif int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int lineDst = _dst; byte[] data = src.ToByteArray(srcRectY * src.PixelWidth, srcRectH * src.PixelWidth); // BlockCopy? #if !LIB byte[] coreArray = dataMemory.GetData(); #else byte[] coreArray = new byte[_scanlength]; dataMemory.ReadBytes(coreArray, _srcRect, _scanlength); #endif for (int y = 0; y < srcRectH; y++) { System.Array.Copy(data, y * src.PixelWidth * 4, coreArray, lineDst, src.PixelWidth * 4); lineDst += _scanlength * 4; } }; syscalls.maCreateImageFromData = delegate(int _placeholder, int _data, int _offset, int _size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); if (res == null) { return(MoSync.Constants.RES_BAD_INPUT); } Stream bin = (Stream)res.GetInternalObject(); if (bin == null) { return(MoSync.Constants.RES_BAD_INPUT); } BoundedStream s = new BoundedStream(bin, _offset, _size); WriteableBitmap bitmap = MoSync.Util.CreateWriteableBitmapFromStream(s); s.Close(); if (bitmap == null) { return(MoSync.Constants.RES_BAD_INPUT); } Resource imageRes = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeholder); imageRes.SetInternalObject(bitmap); imageRes.SetResourceType(MoSync.Constants.RT_IMAGE); return(MoSync.Constants.RES_OK); }; }
public Runtime(Core core) { mCore = core; mSyscalls = new Syscalls(); mIoctls = new Ioctls(); mIoctlInvoker = new IoctlInvoker(mCore, mIoctls); mCurrentResourceHandle = 1; mStaticResourceCount = 0; PhoneApplicationFrame mainPage = (PhoneApplicationFrame)Application.Current.RootVisual; mainPage.MouseLeftButtonDown += MouseLeftButtonDown; mainPage.MouseMove += this.MouseMove; mainPage.MouseLeftButtonUp += MouseLeftButtonUp; // clear the list of system property providers // We clear it before we initialize all the modules, because // different modules might register system property providers. SystemPropertyManager.ClearSystemPropertyProviders(); RegisterCleaner(() => { Util.RunActionOnMainThreadSync(() => { mainPage.MouseLeftButtonDown -= MouseLeftButtonDown; mainPage.MouseMove -= this.MouseMove; mainPage.MouseLeftButtonUp -= MouseLeftButtonUp; }); }); InitSyscalls(); mSyscalls.maGetEvent = delegate(int ptr) { if (mEvents.Count != 0) { lock (mEvents) { Event evt = mEvents[0]; Memory eventData = evt.GetEventData(); mEvents.RemoveAt(0); Memory customEventData = evt.GetCustomEventData(); if (customEventData != null) { mCore.GetDataMemory().WriteMemoryAtAddress(mCore.GetCustomEventDataPointer(), customEventData, 0, customEventData.GetSizeInBytes()); eventData.WriteInt32(MoSync.Struct.MAEvent.data, mCore.GetCustomEventDataPointer()); } mCore.GetDataMemory().WriteMemoryAtAddress(ptr, eventData, 0, eventData.GetSizeInBytes()); } return(1); } else { return(0); } }; mSyscalls.maWait = delegate(int timeout) { if (timeout <= 0) { timeout = 1 << 15; } mEventWaiter.WaitOne(timeout); }; #if !LIB mSyscalls.maIOCtl = delegate(int id, int a, int b, int c) #else mSyscalls.maIOCtl = delegate(int id, int a, int b, int c, int args) #endif { #if !LIB return(mIoctlInvoker.InvokeIoctl(id, a, b, c)); #else return(mIoctlInvoker.InvokeIoctl(id, a, b, c, args)); #endif }; mSyscalls.maDestroyObject = delegate(int res) { mResources[res].SetResourceType(MoSync.Constants.RT_PLACEHOLDER); mResources[res].SetInternalObject(null); }; mSyscalls.maLoadResource = delegate(int _handle, int _placeholder, int _flag) { Resource res = mResources[_handle]; BoundedStream stream = res.GetFileStream(); Resource placeholder = mResources[_placeholder]; if (stream == null) { return(0); } if (placeholder.GetInternalObject() != null) { return(0); } stream.Seek(0, SeekOrigin.Begin); LoadResource(stream, (byte)res.GetResourceType(), (uint)stream.Length, placeholder); return(1); }; mSyscalls.maLoadResources = delegate(int _data) { Resource res = GetResource(MoSync.Constants.RT_BINARY, _data); Stream data = (Stream)res.GetInternalObject(); return(LoadResources(data, false)?1:0); }; mSyscalls.maCountResources = delegate() { return(mStaticResourceCount); }; }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { PhoneApplicationFrame frame = (PhoneApplicationFrame)Application.Current.RootVisual; double screenWidth = System.Windows.Application.Current.Host.Content.ActualWidth; double screenHeight = System.Windows.Application.Current.Host.Content.ActualHeight; if ((int)screenHeight == 0) throw new Exception("screenHeight"); PhoneApplicationPage mainPage = (PhoneApplicationPage)frame.Content; mMainImage = new Image(); mainPage.Width = screenWidth; mainPage.Height = screenHeight; mMainImage.Width = screenWidth; mMainImage.Height = screenHeight; mainPage.Content = mMainImage; mClipRect.X = 0.0; mClipRect.Y = 0.0; mClipRect.Width = screenWidth; mClipRect.Height = screenHeight; // no apparent effect on memory leaks. runtime.RegisterCleaner(delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { mainPage.Content = null; }); }); mBackBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mFrontBuffer = new WriteableBitmap( (int)screenWidth, (int)screenHeight); mMainImage.Source = mFrontBuffer; mCurrentDrawTarget = mBackBuffer; mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); syscalls.maSetColor = delegate(int rgb) { int oldColor = (int)mCurrentColor; mCurrentColor = 0xff000000 | (uint)(rgb & 0xffffff); mCurrentWindowsColor = System.Windows.Media.Color.FromArgb(0xff, (byte)(mCurrentColor >> 16), (byte)(mCurrentColor >> 8), (byte)(mCurrentColor)); return oldColor & 0xffffff; }; syscalls.maSetClipRect = delegate(int x, int y, int w, int h) { mClipRect.X = x; mClipRect.Y = y; mClipRect.Width = w; mClipRect.Height = h; }; syscalls.maGetClipRect = delegate(int cliprect) { Memory mem = core.GetDataMemory(); mem.WriteInt32(cliprect + MoSync.Struct.MARect.left, (int)mClipRect.X); mem.WriteInt32(cliprect + MoSync.Struct.MARect.top, (int)mClipRect.Y); mem.WriteInt32(cliprect + MoSync.Struct.MARect.width, (int)mClipRect.Width); mem.WriteInt32(cliprect + MoSync.Struct.MARect.height, (int)mClipRect.Height); }; syscalls.maPlot = delegate(int x, int y) { mCurrentDrawTarget.SetPixel(x, y, (int)mCurrentColor); }; syscalls.maUpdateScreen = delegate() { //System.Array.Copy(mBackBuffer.Pixels, mFrontBuffer.Pixels, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight); System.Buffer.BlockCopy(mBackBuffer.Pixels, 0, mFrontBuffer.Pixels, 0, mFrontBuffer.PixelWidth * mFrontBuffer.PixelHeight * 4); InvalidateWriteableBitmapOnMainThread(mFrontBuffer); }; syscalls.maFillRect = delegate(int x, int y, int w, int h) { mCurrentDrawTarget.FillRectangle(x, y, x + w, y + h, (int)mCurrentColor); }; syscalls.maLine = delegate(int x1, int y1, int x2, int y2) { GraphicsUtil.Point p1 = new GraphicsUtil.Point(x1, y1); GraphicsUtil.Point p2 = new GraphicsUtil.Point(x2, y2); if(!GraphicsUtil.ClipLine(p1, p2, (int)mClipRect.X, (int)(mClipRect.X+mClipRect.Width), (int)mClipRect.Y, (int)(mClipRect.Y+mClipRect.Height))) return; mCurrentDrawTarget.DrawLine((int)p1.x, (int)p1.y, (int)p2.x, (int)p2.y, (int)mCurrentColor); }; textBlock.FontSize = mCurrentFontSize; syscalls.maDrawText = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); if (text.Length == 0) return; WriteableBitmap bitmap = null; double w = 0, h = 0; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textBlock.Foreground = new SolidColorBrush(mCurrentWindowsColor); bitmap = new WriteableBitmap(textBlock, null); w = bitmap.PixelWidth; h = bitmap.PixelHeight; }); mCurrentDrawTarget.Blit(new Rect(left, top, w, h), bitmap, new Rect(0, 0, w, h)); }; syscalls.maGetTextSize = delegate(int str) { String text = core.GetDataMemory().ReadStringAtAddress(str); int textWidth = 0; int textHeight = 0; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textWidth = (int)textBlock.ActualWidth; textHeight = (int)textBlock.ActualHeight; }); return MoSync.Util.CreateExtent(textWidth, textHeight); }; syscalls.maDrawTextW = delegate(int left, int top, int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); if (text.Length == 0) return; WriteableBitmap bitmap = null; double w = 0, h = 0; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textBlock.Foreground = new SolidColorBrush(mCurrentWindowsColor); bitmap = new WriteableBitmap(textBlock, null); w = bitmap.PixelWidth; h = bitmap.PixelHeight; }); mCurrentDrawTarget.Blit(new Rect(left, top, w, h), bitmap, new Rect(0, 0, w, h)); }; syscalls.maGetTextSizeW = delegate(int str) { String text = core.GetDataMemory().ReadWStringAtAddress(str); int textWidth = 0; int textHeight = 0; MoSync.Util.RunActionOnMainThreadSync(() => { textBlock.Text = text; textWidth = (int)textBlock.ActualWidth; textHeight = (int)textBlock.ActualHeight; }); return MoSync.Util.CreateExtent(textWidth, textHeight); }; syscalls.maFillTriangleFan = delegate(int points, int count) { int[] newPoints = new int[count * 2 + 2]; for (int i = 0; i < count; i++) { newPoints[i * 2 + 0] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); newPoints[i * 2 + 1] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } newPoints[count * 2 + 0] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.x); newPoints[count * 2 + 1] = core.GetDataMemory().ReadInt32(points + MoSync.Struct.MAPoint2d.y); mCurrentDrawTarget.FillPolygon(newPoints, (int)mCurrentColor); }; syscalls.maFillTriangleStrip = delegate(int points, int count) { int[] xcoords = new int[count]; int[] ycoords = new int[count]; for (int i = 0; i < count; i++) { xcoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.x); ycoords[i] = core.GetDataMemory().ReadInt32(points + i * 8 + MoSync.Struct.MAPoint2d.y); } for (int i = 2; i < count; i++) { mCurrentDrawTarget.FillTriangle( xcoords[i - 2], ycoords[i - 2], xcoords[i - 1], ycoords[i - 1], xcoords[i - 0], ycoords[i - 0], (int)mCurrentColor); } }; syscalls.maSetDrawTarget = delegate(int drawTarget) { int oldDrawTarget = mCurrentDrawTargetIndex; if (drawTarget == mCurrentDrawTargetIndex) return oldDrawTarget; if (drawTarget == MoSync.Constants.HANDLE_SCREEN) { mCurrentDrawTarget = mBackBuffer; mCurrentDrawTargetIndex = drawTarget; return oldDrawTarget; } Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, drawTarget); mCurrentDrawTarget = (WriteableBitmap)res.GetInternalObject(); mCurrentDrawTargetIndex = drawTarget; return oldDrawTarget; }; syscalls.maGetScrSize = delegate() { return MoSync.Util.CreateExtent(mBackBuffer.PixelWidth, mBackBuffer.PixelHeight); }; syscalls.maGetImageSize = delegate(int handle) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, handle); BitmapSource src = (BitmapSource)res.GetInternalObject(); int w = 0, h = 0; MoSync.Util.RunActionOnMainThreadSync(() => { w = src.PixelWidth; h = src.PixelHeight; }); return MoSync.Util.CreateExtent(w, h); }; syscalls.maDrawImage = delegate(int image, int left, int top) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Rect srcRect = new Rect(0, 0, src.PixelWidth, src.PixelHeight); Rect dstRect = new Rect(left, top, src.PixelWidth, src.PixelHeight); mCurrentDrawTarget.Blit(dstRect, src, srcRect, WriteableBitmapExtensions.BlendMode.Alpha); }; syscalls.maDrawImageRegion = delegate(int image, int srcRectPtr, int dstPointPtr, int transformMode) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); int srcRectX = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(srcRectPtr + MoSync.Struct.MARect.height); int dstPointX = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.x); int dstPointY = dataMemory.ReadInt32(dstPointPtr + MoSync.Struct.MAPoint2d.y); Rect srcRect = new Rect(srcRectX, srcRectY, srcRectW, srcRectH); Rect dstRect = new Rect(dstPointX, dstPointY, srcRectW, srcRectH); // mCurrentDrawTarget.Blit(dstRect, src, srcRect, WriteableBitmapExtensions.BlendMode.Alpha); GraphicsUtil.DrawImageRegion(mCurrentDrawTarget, dstPointX, dstPointY, srcRect, src, transformMode); }; syscalls.maCreateDrawableImage = delegate(int placeholder, int width, int height) { Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, placeholder); res.SetResourceType(MoSync.Constants.RT_IMAGE); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); if (bitmap == null) return MoSync.Constants.RES_OUT_OF_MEMORY; res.SetInternalObject(bitmap); return MoSync.Constants.RES_OK; }; syscalls.maCreateImageRaw = delegate(int _placeholder, int _src, int _size, int _alpha) { int width = MoSync.Util.ExtentX(_size); int height = MoSync.Util.ExtentY(_size); WriteableBitmap bitmap = null; MoSync.Util.RunActionOnMainThreadSync(() => { bitmap = new WriteableBitmap(width, height); }); //core.GetDataMemory().ReadIntegers(bitmap.Pixels, _src, width * height); bitmap.FromByteArray(core.GetDataMemory().GetData(), _src, width * height * 4); if (_alpha == 0) { int[] pixels = bitmap.Pixels; int numPixels = width * height; for (int i = 0; i < numPixels; i++) { pixels[i] = (int)((uint)pixels[i] | 0xff000000); } } Resource res = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeholder); res.SetInternalObject(bitmap); res.SetResourceType(MoSync.Constants.RT_IMAGE); return MoSync.Constants.RES_OK; }; syscalls.maDrawRGB = delegate(int _dstPoint, int _src, int _srcRect, int _scanlength) { Memory dataMemory = core.GetDataMemory(); int dstX = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.x); int dstY = dataMemory.ReadInt32(_dstPoint + MoSync.Struct.MAPoint2d.y); int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int[] pixels = mCurrentDrawTarget.Pixels; // todo: clipRect _scanlength *= 4; // sizeof(int) for (int h = 0; h < srcRectH; h++) { int pixelIndex = dstY * mCurrentDrawTarget.PixelWidth + dstX; int address = _src + (srcRectY + h) * _scanlength; for (int w = 0; w < srcRectW; w++) { uint srcPixel = dataMemory.ReadUInt32(address); uint dstPixel = (uint)pixels[pixelIndex]; uint srcPixelR = (srcPixel & 0x00ff0000) >> 16; uint srcPixelG = (srcPixel & 0x0000ff00) >> 8; uint srcPixelB = (srcPixel & 0x000000ff) >> 0; uint srcPixelA = (srcPixel & 0xff000000) >> 24; uint dstPixelR = (dstPixel & 0x00ff0000) >> 16; uint dstPixelG = (dstPixel & 0x0000ff00) >> 8; uint dstPixelB = (dstPixel & 0x000000ff) >> 0; uint dstPixelA = (dstPixel & 0xff000000) >> 24; dstPixelR += ((srcPixelR - dstPixelR) * srcPixelA) / 255; dstPixelG += ((srcPixelG - dstPixelG) * srcPixelA) / 255; dstPixelB += ((srcPixelB - dstPixelB) * srcPixelA) / 255; dstPixel = (dstPixelA << 24) | (dstPixelR << 16) | (dstPixelG << 8) | (dstPixelB); pixels[pixelIndex] = (int)dstPixel; address += 4; pixelIndex++; } dstY++; } }; syscalls.maGetImageData = delegate(int _image, int _dst, int _srcRect, int _scanlength) { Resource res = runtime.GetResource(MoSync.Constants.RT_IMAGE, _image); WriteableBitmap src = (WriteableBitmap)res.GetInternalObject(); Memory dataMemory = core.GetDataMemory(); int srcRectX = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.left); int srcRectY = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.top); int srcRectW = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.width); int srcRectH = dataMemory.ReadInt32(_srcRect + MoSync.Struct.MARect.height); int lineDst = _dst; byte[] data = src.ToByteArray(srcRectY * src.PixelWidth, srcRectH * src.PixelWidth); // BlockCopy? byte[] coreArray = dataMemory.GetData(); for (int y = 0; y < srcRectH; y++) { System.Array.Copy(data, y * src.PixelWidth * 4, coreArray, lineDst, src.PixelWidth * 4); lineDst += _scanlength * 4; } }; syscalls.maCreateImageFromData = delegate(int _placeholder, int _data, int _offset, int _size) { Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); if (res == null) return MoSync.Constants.RES_BAD_INPUT; Stream bin = (Stream)res.GetInternalObject(); if (bin == null) return MoSync.Constants.RES_BAD_INPUT; BoundedStream s = new BoundedStream(bin, _offset, _size); //Stream s = mem.GetStream(_offset, _size); WriteableBitmap bitmap = MoSync.Util.CreateWriteableBitmapFromStream(s); s.Close(); if (bitmap == null) return MoSync.Constants.RES_BAD_INPUT; Resource imageRes = runtime.GetResource(MoSync.Constants.RT_PLACEHOLDER, _placeholder); imageRes.SetInternalObject(bitmap); imageRes.SetResourceType(MoSync.Constants.RT_IMAGE); return MoSync.Constants.RES_OK; }; }
public void Init(Syscalls mSyscalls, Core mCore, Runtime mRuntime) { mSyscalls.maSoundPlay = delegate(int _data, int _offset, int _size) { mSyscalls.maSoundStop(); Resource audiores = mRuntime.GetResource(MoSync.Constants.RT_BINARY, _data); BoundedStream s = new BoundedStream((Stream)audiores.GetInternalObject(), _offset, _size); // Read MIME type. Mp3MediaStreamSource is not clever enough to bypass it. StringBuilder sb = new StringBuilder(); int b; while ((b = s.ReadByte()) > 0) { sb.Append((char)b); } if (b < 0) { // The MIME type was interrupted. // Bad stream. We don't want to play it. return(-2); } if (sb.ToString() != "audio/mpeg") { // We can only play MP3 files. return(-3); } Mp3MediaStreamSource source = new Mp3MediaStreamSource(s); // all Controls code must be Dispatched to the proper thread, // or you'll get a fatal Exception. Deployment.Current.Dispatcher.BeginInvoke(() => { mElement = new MediaElement(); mElement.Volume = mVolume; mElement.SetSource(source); mElement.Play(); }); return(0); }; mSyscalls.maSoundStop = delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { if (mElement != null) { mElement.Stop(); } }); }; mSyscalls.maSoundIsPlaying = delegate() { int result = 0; MoSync.Util.RunActionOnMainThreadSync(() => { if (mElement != null) { MediaElementState s = mElement.CurrentState; result = (s == MediaElementState.Buffering) || (s == MediaElementState.Playing) ? 1 : 0; } }); return(result); }; mSyscalls.maSoundGetVolume = delegate() { return((int)mVolume); }; mSyscalls.maSoundSetVolume = delegate(int _vol) { mVolume = _vol; if (mVolume > 100) { mVolume = 100; } else if (mVolume < 0) { mVolume = 0; } Deployment.Current.Dispatcher.BeginInvoke(() => { if (mElement != null) { mElement.Volume = mVolume; } }); }; }
public void Init(Syscalls mSyscalls, Core mCore, Runtime mRuntime) { mSyscalls.maSoundPlay = delegate(int _data, int _offset, int _size) { mSyscalls.maSoundStop(); Resource audiores = mRuntime.GetResource(MoSync.Constants.RT_BINARY, _data); BoundedStream s = new BoundedStream((Stream)audiores.GetInternalObject(), _offset, _size); // Read MIME type. Mp3MediaStreamSource is not clever enough to bypass it. StringBuilder sb = new StringBuilder(); int b; while ((b = s.ReadByte()) > 0) { sb.Append((char)b); } if (b < 0) { // The MIME type was interrupted. // Bad stream. We don't want to play it. return -2; } if (sb.ToString() != "audio/mpeg") { // We can only play MP3 files. return -3; } Mp3MediaStreamSource source = new Mp3MediaStreamSource(s); // all Controls code must be Dispatched to the proper thread, // or you'll get a fatal Exception. Deployment.Current.Dispatcher.BeginInvoke(() => { mElement = new MediaElement(); mElement.Volume = mVolume; mElement.SetSource(source); mElement.Play(); }); return 0; }; mSyscalls.maSoundStop = delegate() { MoSync.Util.RunActionOnMainThreadSync(() => { if (mElement != null) { mElement.Stop(); } }); }; mSyscalls.maSoundIsPlaying = delegate() { int result = 0; MoSync.Util.RunActionOnMainThreadSync(() => { if (mElement != null) { MediaElementState s = mElement.CurrentState; result = (s == MediaElementState.Buffering) || (s == MediaElementState.Playing) ? 1 : 0; } }); return result; }; mSyscalls.maSoundGetVolume = delegate() { return (int)mVolume; }; mSyscalls.maSoundSetVolume = delegate(int _vol) { mVolume = _vol; if (mVolume > 100) mVolume = 100; else if (mVolume < 0) mVolume = 0; Deployment.Current.Dispatcher.BeginInvoke(() => { if (mElement != null) { mElement.Volume = mVolume; } }); }; }