]> O.S.I.I.S - jp/crow.git/commitdiff
first tests with Interface update in a separate thread
authorjpbruyere <jp.bruyere@hotmail.com>
Tue, 1 Mar 2016 11:54:52 +0000 (12:54 +0100)
committerjpbruyere <jp.bruyere@hotmail.com>
Tue, 1 Mar 2016 12:12:28 +0000 (13:12 +0100)
Crow.csproj
OTKCrow/OpenTKGameWindow.cs
Tests/GOLIBTestsOTK.cs
src/GraphicObjects/GraphicObject.cs
src/Interface.cs

index 735426e5af9db73746ed8856a283371724da82dd..59b4228cc0a7b097a6f26ac9a656544a79e62f96 100644 (file)
@@ -23,6 +23,7 @@
     <SchemaVersion>2.0</SchemaVersion>
     <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ReleaseVersion>0.4</ReleaseVersion>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
     <DebugSymbols>true</DebugSymbols>
index c3240b99236369ecc67bf5f486205171cbeded3f..df1c426117dd7ffd90a6020f5b8df0346f26a469 100644 (file)
@@ -37,7 +37,8 @@ namespace Crow
                public event EventHandler<ValueChangeEventArgs> ValueChanged;
                public virtual void NotifyValueChanged(string MemberName, object _value)
                {
-                       ValueChanged.Raise(this, new ValueChangeEventArgs(MemberName, _value));
+                       if (ValueChanged != null)                               
+                               ValueChanged.Invoke(this, new ValueChangeEventArgs(MemberName, _value));
                }
                #endregion
 
@@ -101,12 +102,27 @@ namespace Crow
                                3,3,OpenTK.Graphics.GraphicsContextFlags.Debug)
 //             public OpenTKGameWindow(int _width, int _height, string _title="golib")
 //                     : base(_width, _height, new OpenTK.Graphics.GraphicsMode(32, 24, 0, 8), _title)
+               {
+                       Thread t = new Thread (interfaceThread);
+                       t.IsBackground = true;
+                       t.Start ();
+
+//                     interfaceThread ();
+               }
+               #endregion
+
+               void interfaceThread()
                {
                        CrowInterface = new Interface ();
+                       Interface.CurrentInterface = CrowInterface;
                        CrowInterface.Quit += Quit;
                        CrowInterface.MouseCursorChanged += CrowInterface_MouseCursorChanged;
+
+                       while (true) {
+                               CrowInterface.Update ();
+                               Thread.Sleep (1);
+                       }
                }
-               #endregion
 
                public void Quit (object sender, EventArgs e)
                {
@@ -176,21 +192,21 @@ namespace Crow
                        GL.Viewport (0, 0, ClientRectangle.Width, ClientRectangle.Height);
 
                        shader.Enable ();
-
-                       if (CrowInterface.IsDirty) {
-                               byte[] tmp = new byte[4 * CrowInterface.DirtyRect.Width * CrowInterface.DirtyRect.Height];
-                               for (int y = 0; y < CrowInterface.DirtyRect.Height; y++) {
-                                       Array.Copy(CrowInterface.bmp,
-                                               ((CrowInterface.DirtyRect.Top + y) * ClientRectangle.Width * 4) + CrowInterface.DirtyRect.Left * 4,
-                                               tmp, y * CrowInterface.DirtyRect.Width * 4, CrowInterface.DirtyRect.Width *4);
+                       lock (CrowInterface.RenderMutex) {
+                               if (CrowInterface.IsDirty) {
+                                       byte[] tmp = new byte[4 * CrowInterface.DirtyRect.Width * CrowInterface.DirtyRect.Height];
+                                       for (int y = 0; y < CrowInterface.DirtyRect.Height; y++) {
+                                               Array.Copy (CrowInterface.bmp,
+                                                       ((CrowInterface.DirtyRect.Top + y) * ClientRectangle.Width * 4) + CrowInterface.DirtyRect.Left * 4,
+                                                       tmp, y * CrowInterface.DirtyRect.Width * 4, CrowInterface.DirtyRect.Width * 4);
+                                       }
+                                       GL.TexSubImage2D (TextureTarget.Texture2D, 0,
+                                               CrowInterface.DirtyRect.Left, CrowInterface.DirtyRect.Top,
+                                               CrowInterface.DirtyRect.Width, CrowInterface.DirtyRect.Height,
+                                               OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, tmp);
+                                       CrowInterface.IsDirty = false;
                                }
-                               GL.TexSubImage2D (TextureTarget.Texture2D, 0,
-                                       CrowInterface.DirtyRect.Left, CrowInterface.DirtyRect.Top,
-                                       CrowInterface.DirtyRect.Width, CrowInterface.DirtyRect.Height,
-                                       OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, tmp);
-                               CrowInterface.IsDirty = false;
                        }
-
                        uiQuad.Render (PrimitiveType.TriangleStrip);
 
                        GL.BindTexture(TextureTarget.Texture2D, 0);
@@ -232,18 +248,18 @@ namespace Crow
                protected override void OnUpdateFrame(FrameEventArgs e)
                {
                        base.OnUpdateFrame(e);
-                       fps = (int)RenderFrequency;
-
-
-                       if (frameCpt > 50) {
-                               resetFps ();
-                               frameCpt = 0;
-                               GC.Collect();
-                               GC.WaitForPendingFinalizers();
-                               NotifyValueChanged("memory", GC.GetTotalMemory (false).ToString());
-                       }
-                       frameCpt++;
-                       CrowInterface.Update ();
+//                     fps = (int)RenderFrequency;
+//
+//
+//                     if (frameCpt > 50) {
+//                             resetFps ();
+//                             frameCpt = 0;
+//                             GC.Collect();
+//                             GC.WaitForPendingFinalizers();
+//                             NotifyValueChanged("memory", GC.GetTotalMemory (false).ToString());
+//                     }
+//                     frameCpt++;
+                       //CrowInterface.Update ();
                }
                protected override void OnRenderFrame(FrameEventArgs e)
                {
@@ -260,7 +276,7 @@ namespace Crow
                protected override void OnResize(EventArgs e)
                {
                        base.OnResize (e);
-                       CrowInterface.ProcessResize(
+                       CrowInterface.ResizeDelegate.Invoke(
                                new Rectangle(
                                this.ClientRectangle.X,
                                this.ClientRectangle.Y,
@@ -270,7 +286,7 @@ namespace Crow
                }
                #endregion
 
-       #region Mouse Handling
+               #region Mouse Handling
                void update_mouseButtonStates(ref MouseState e, OpenTK.Input.MouseState otk_e){
                        for (int i = 0; i < MouseState.MaxButtons; i++) {
                                if (otk_e.IsButtonDown ((OpenTK.Input.MouseButton)i))
@@ -295,10 +311,10 @@ namespace Crow
         }
                #endregion
 
-       #region keyboard Handling
+               #region keyboard Handling
                KeyboardState Keyboad = new KeyboardState ();
                void Keyboard_KeyDown(object sender, OpenTK.Input.KeyboardKeyEventArgs otk_e)
-       {
+               {
 //                     if (_focusedWidget == null) {
                                KeyboardKeyDown.Raise (this, otk_e);
 //                             return;
index 4256120b7bbcaebba87a8da25652440c8e4497c2..a9416c207fef4b67bdfd0f66a94262c393c4cdc9 100644 (file)
@@ -77,7 +77,7 @@ namespace testOTK
                };
 
 
-               public int intValue = 25;
+               volatile public int intValue = 25;
 
                public int IntValue {
                        get {
@@ -99,7 +99,7 @@ namespace testOTK
                                return;
                        NotifyValueChanged ("alignment", Enum.Parse(typeof(Alignment), rb.Caption));
                }
-               public IList<String> List2 = new List<string>(new string[]
+               volatile public IList<String> List2 = new List<string>(new string[]
                        {
                                "string1",
                                "string2",
@@ -117,7 +117,7 @@ namespace testOTK
                                "string9"
                        }
                );
-               IList<Color> testList = Color.ColorDic.ToList();
+               volatile IList<Color> testList = Color.ColorDic.ToList();
                public IList<Color> TestList {
                        set{
                                testList = value;
@@ -135,8 +135,9 @@ namespace testOTK
                        //this.AddWidget(new test4());
                        KeyboardKeyDown += GOLIBTests_KeyboardKeyDown1;;
 
-                       GraphicObject obj = CrowInterface.LoadInterface("Interfaces/" + testFiles[idx]);
-                       obj.DataSource = this;
+                       CrowInterface.LoadInterfaceDelegate.Invoke ("Interfaces/" + testFiles [idx]);
+                       //GraphicObject obj = CrowInterface.LoadInterface("Interfaces/" + testFiles[idx]);
+                       //obj.DataSource = this;
 
                }
                void GOLIBTests_KeyboardKeyDown1 (object sender, OpenTK.Input.KeyboardKeyEventArgs e)
index b9fc6c3eb439c2fba084b730bf51713f9291265f..2bce334921cb45697fb3f6a5bfefe5cddc67238e 100644 (file)
@@ -646,7 +646,8 @@ namespace Crow
                                return;
                        if (Interface.CurrentInterface == null)
                                return;
-                       Interface.CurrentInterface.gobjsToRedraw.Add (this);
+                       lock(Interface.CurrentInterface.RenderMutex)
+                               Interface.CurrentInterface.gobjsToRedraw.Add (this);
                        IsQueuedForRedraw = true;
                }
 
@@ -690,17 +691,19 @@ namespace Crow
                        Debug.WriteLine ("REGLayout => {1}->{0}", layoutType, this.ToString());
                        #endif
 
-                       //enqueue LQI LayoutingTypes separately
-                       if (layoutType.HasFlag (LayoutingType.Width))
-                               Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.Width, this));
-                       if (layoutType.HasFlag (LayoutingType.Height))
-                               Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.Height, this));
-                       if (layoutType.HasFlag (LayoutingType.X))
-                               Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.X, this));
-                       if (layoutType.HasFlag (LayoutingType.Y))
-                               Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.Y, this));
-                       if (layoutType.HasFlag (LayoutingType.ArrangeChildren))
-                               Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.ArrangeChildren, this));
+                       lock (Interface.LayoutingQueue) {
+                               //enqueue LQI LayoutingTypes separately
+                               if (layoutType.HasFlag (LayoutingType.Width))
+                                       Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.Width, this));
+                               if (layoutType.HasFlag (LayoutingType.Height))
+                                       Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.Height, this));
+                               if (layoutType.HasFlag (LayoutingType.X))
+                                       Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.X, this));
+                               if (layoutType.HasFlag (LayoutingType.Y))
+                                       Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.Y, this));
+                               if (layoutType.HasFlag (LayoutingType.ArrangeChildren))
+                                       Interface.LayoutingQueue.Enqueue (new LayoutingQueueItem (LayoutingType.ArrangeChildren, this));
+                       }
                }
 
                /// <summary> trigger dependant sizing component update </summary>
index aacca09acbcb392a2cbc66eaa6b17439cd601a35..d82516b0c3fa196480fb7d4c3373416378667ca8 100644 (file)
@@ -37,10 +37,21 @@ namespace Crow
 {
        public class Interface : ILayoutable
        {
+               public delegate void ResizeDelegatePrototype(Rectangle bounds);
+               public delegate void LoaderDelegatePrototype (string path);
+               public ResizeDelegatePrototype ResizeDelegate;
+               public LoaderDelegatePrototype LoadInterfaceDelegate;
+
                #region CTOR
-               static Interface(){
+               static Interface(){                     
                        Interface.LoadCursors ();
                }
+               public Interface(){
+                       Interface.CurrentInterface = this;
+
+                       LoadInterfaceDelegate = new LoaderDelegatePrototype(InterfaceLoad);
+                       ResizeDelegate = new ResizeDelegatePrototype (ProcessResize);
+               }
                #endregion
 
                #region Static and constants
@@ -187,6 +198,11 @@ namespace Crow
                        return result;
                }
 
+               public void InterfaceLoad(string path){
+                       GraphicObject tmp = Interface.Load (path, this);
+                       AddWidget (tmp);
+               }
+
                public GraphicObject LoadInterface (string path)
                {
                        GraphicObject tmp = Interface.Load (path, this);
@@ -214,9 +230,10 @@ namespace Crow
 
                Context ctx;
                Surface surf;
-               public byte[] bmp;
+               volatile public byte[] bmp;
                public bool IsDirty = false;
                public Rectangle DirtyRect;
+               public object RenderMutex = new object();
 
                #region focus
                GraphicObject _activeWidget;    //button is pressed on widget
@@ -283,12 +300,21 @@ namespace Crow
                        layoutTime.Start ();
                        #endif
                        //Debug.WriteLine ("======= Layouting queue start =======");
-
-                       while (Interface.LayoutingQueue.Count > 0) {
-                               LayoutingQueueItem lqi = Interface.LayoutingQueue.Dequeue ();
+                       int queueCount = 0;
+                       LayoutingQueueItem lqi = null;
+
+                       lock (Interface.LayoutingQueue)
+                               queueCount = Interface.LayoutingQueue.Count;
+                       
+                       while (queueCount > 0) {
+                               lock (Interface.LayoutingQueue)
+                                       lqi = Interface.LayoutingQueue.Dequeue ();                              
                                lqi.ProcessLayouting ();
+                               lock (Interface.LayoutingQueue)
+                                       queueCount = Interface.LayoutingQueue.Count;                            
                        }
 
+
                        #if MEASURE_TIME
                        layoutTime.Stop ();
                        #endif
@@ -296,9 +322,12 @@ namespace Crow
                        //Debug.WriteLine ("otd:" + gobjsToRedraw.Count.ToString () + "-");
                        //final redraw clips should be added only when layout is completed among parents,
                        //that's why it take place in a second pass
-                       GraphicObject[] gotr = new GraphicObject[gobjsToRedraw.Count];
-                       gobjsToRedraw.CopyTo (gotr);
-                       gobjsToRedraw.Clear ();
+                       GraphicObject[] gotr = null;
+                       lock (Interface.CurrentInterface.RenderMutex) {
+                               gotr = new GraphicObject[gobjsToRedraw.Count];
+                               gobjsToRedraw.CopyTo (gotr);
+                               gobjsToRedraw.Clear ();
+                       }
                        foreach (GraphicObject p in gotr) {
                                try {
                                        p.IsQueuedForRedraw = false;
@@ -339,16 +368,18 @@ namespace Crow
                                                clipping.stroke (ctx, Color.Red.AdjustAlpha(0.5));
                                                #endif
 
-                                               if (IsDirty)
-                                                       DirtyRect += clipping.Bounds;
-                                               else
-                                                       DirtyRect = clipping.Bounds;
-                                               IsDirty = true;
-
-                                               DirtyRect.Left = Math.Max (0, DirtyRect.Left);
-                                               DirtyRect.Top = Math.Max (0, DirtyRect.Top);
-                                               DirtyRect.Width = Math.Min (ClientRectangle.Width - DirtyRect.Left, DirtyRect.Width);
-                                               DirtyRect.Height = Math.Min (ClientRectangle.Height - DirtyRect.Top, DirtyRect.Height);
+                                               lock (RenderMutex) {
+                                                       if (IsDirty)
+                                                               DirtyRect += clipping.Bounds;
+                                                       else
+                                                               DirtyRect = clipping.Bounds;
+                                                       IsDirty = true;
+
+                                                       DirtyRect.Left = Math.Max (0, DirtyRect.Left);
+                                                       DirtyRect.Top = Math.Max (0, DirtyRect.Top);
+                                                       DirtyRect.Width = Math.Min (ClientRectangle.Width - DirtyRect.Left, DirtyRect.Width);
+                                                       DirtyRect.Height = Math.Min (ClientRectangle.Height - DirtyRect.Top, DirtyRect.Height);
+                                               }
 
                                                clipping.Reset ();
                                        }
@@ -429,6 +460,9 @@ namespace Crow
                        }
                        return null;
                }
+
+
+
                public void ProcessResize(Rectangle bounds){
                        clientRectangle = bounds;