diff --git a/uppsrc/RasterPlayer/RasterPlayer.cpp b/uppsrc/RasterPlayer/RasterPlayer.cpp index 816a1c927..ea0601a45 100644 --- a/uppsrc/RasterPlayer/RasterPlayer.cpp +++ b/uppsrc/RasterPlayer/RasterPlayer.cpp @@ -38,6 +38,7 @@ RasterPlayer::RasterPlayer() { running = 0; kill = 1; speed = 1; + mt = false; } void RasterPlayer::Paint(Draw& w) { @@ -78,7 +79,7 @@ bool RasterPlayer::LoadBuffer(const String &buffer) { case 1: case 2: iw.DrawRect(r, White()); break; - //case 2: iw.DrawRect(sz, White()); // It seems files do no comply with standard + //case 2: iw.DrawRect(sz, White()); // It seems gif files do not comply with standard // break; case 4: if (i > 0) previous = ::GetRect(images[i-1], r); @@ -98,6 +99,7 @@ bool RasterPlayer::Load(const String &fileName) { return LoadBuffer(LoadStream(in)); } +#ifdef _MULTITHREADED void RasterPlayerThread(RasterPlayer *animatedClip) { TimeStop t; dword tFrame = 0; @@ -108,7 +110,7 @@ void RasterPlayerThread(RasterPlayer *animatedClip) { ind = 0; tFrame += dword(animatedClip->delays[ind]/animatedClip->speed); } - while (t.Elapsed() < tFrame) + while (t.Elapsed() < tFrame && !animatedClip->kill) Sleep(10); PostCallback(callback(animatedClip, &RasterPlayer::NextFrame)); } @@ -116,6 +118,20 @@ void RasterPlayerThread(RasterPlayer *animatedClip) { animatedClip->running = false; } } +#endif + +void RasterPlayer::TimerFun() { + if (kill || !running) + return; + + if (tTime.Elapsed() < tFrame) + return; + int iFrame = ind+1; + if (iFrame > GetPageCount()-1) + iFrame = 0; + tFrame += dword(delays[iFrame]/speed); + NextFrame(); +} void RasterPlayer::Play() { if (images.GetCount() <= 1) @@ -124,15 +140,44 @@ void RasterPlayer::Play() { running = true; kill = false; } - Thread().Run(callback1(RasterPlayerThread, this)); +#ifdef _MULTITHREADED + if (mt) + Thread().Run(callback1(RasterPlayerThread, this)); + else +#endif + { + tFrame = 0; + tTime.Reset(); + SetTimeCallback(-50, callback(this, &RasterPlayer::TimerFun), 1); + } } void RasterPlayer::Stop() { INTERLOCKED_(mutex) { kill = true; } - while (running) - Sleep(10); +#ifdef _MULTITHREADED + if (mt) { + while (running) + Sleep(10); + } else +#endif + { + KillTimeCallback(1); + running = false; + } +} + +RasterPlayer& RasterPlayer::SetMT(bool _mt) { + bool wasrunning; + INTERLOCKED_(mutex) { + wasrunning = running; + } + Stop(); + mt = _mt; + if (wasrunning) + Play(); + return *this; } RasterPlayer::~RasterPlayer() { diff --git a/uppsrc/RasterPlayer/RasterPlayer.h b/uppsrc/RasterPlayer/RasterPlayer.h index 74455433f..2c57fc3f0 100644 --- a/uppsrc/RasterPlayer/RasterPlayer.h +++ b/uppsrc/RasterPlayer/RasterPlayer.h @@ -1,5 +1,5 @@ -#ifndef _AnimatedClip_AnimatedClip_h_ -#define _AnimatedClip_AnimatedClip_h_ +#ifndef _RasterPlayer_RasterPlayer_h_ +#define _RasterPlayer_RasterPlayer_h_ #include @@ -14,7 +14,12 @@ private: int ind; Color background; double speed; - + bool mt; + + TimeStop tTime; + dword tFrame; + + public: RasterPlayer(); ~RasterPlayer(); @@ -28,6 +33,7 @@ public: inline void NextPage() {NextFrame();}; RasterPlayer& SetBackground(Color c) {background = c; Refresh(); return *this;} RasterPlayer& SetSpeed(double s = 1) {speed = s; Refresh(); return *this;} + RasterPlayer& SetMT(bool _mt = false); Callback WhenShown; @@ -35,9 +41,12 @@ public: int GetFrameCount() {return images.GetCount();}; int GetPage() {return ind;}; void SetPage(int i) {ind = minmax(i, 0, images.GetCount());}; - + +#ifdef _MULTITHREADED friend void RasterPlayerThread(RasterPlayer *animatedClip); - +#endif + void TimerFun(); + protected: volatile Atomic running, kill; }; diff --git a/uppsrc/RasterPlayer/RasterPlayer.upp b/uppsrc/RasterPlayer/RasterPlayer.upp index e60bde5fc..f40c31c0e 100644 --- a/uppsrc/RasterPlayer/RasterPlayer.upp +++ b/uppsrc/RasterPlayer/RasterPlayer.upp @@ -1,13 +1,13 @@ -description "Control to show animated .gif and .tiff files\377"; - -uses - plugin\gif, - plugin\tif; - -file - RasterPlayer.h, - RasterPlayer.cpp, - RasterPlayer.iml, - RasterPlayer.usc, - Credits.txt; - +description "Control to show animated .gif and .tiff files\377"; + +uses + plugin\gif, + plugin\tif; + +file + RasterPlayer.cpp, + RasterPlayer.h, + RasterPlayer.iml, + RasterPlayer.usc, + Credits.txt; + diff --git a/uppsrc/RasterPlayer/init b/uppsrc/RasterPlayer/init index 511269d4f..dd72eb2a1 100644 --- a/uppsrc/RasterPlayer/init +++ b/uppsrc/RasterPlayer/init @@ -1,5 +1,5 @@ -#ifndef _RasterPlayer_icpp_init_stub -#define _RasterPlayer_icpp_init_stub +#ifndef _AnimatedClip_icpp_init_stub +#define _AnimatedClip_icpp_init_stub #include "plugin\gif/init" #include "plugin\tif/init" #endif