Пример #1
0
        private void MainLoop()
        {
#if DEBUG
            Glorg2.Debugging.Debug.LogEnabled = true;
#endif
            long old_time;
            Init();
            old_time       = System.Diagnostics.Stopwatch.GetTimestamp();
            target.Resize += (sender, e) => { GraphicInvoke(new Action(InternalSizeChanged)); };

            threads_ready |= ThreadReady.MainThread;

            while (threads_ready != ThreadReady.All)
            {
                System.Windows.Forms.Application.DoEvents();
                Thread.Sleep(0);
            }


            while (running)
            {
                long new_time = System.Diagnostics.Stopwatch.GetTimestamp();
                frame_time = (new_time - old_time) / (float)System.Diagnostics.Stopwatch.Frequency;


                lock (logic_invoke)
                {
                    while (logic_invoke.Count > 0)
                    {
                        var item = logic_invoke.Dequeue();
                        item();
                    }
                }
                if (scene.Camera != null)
                {
                    if (!scene.Camera.absolute_transform.Invert(out scene.camera_mat))
                    {
                        throw new Exception("WTF! Invert failed!");
                    }

                    scene.sim_time += frame_time;
                }
                else
                {
                    scene.camera_mat = Matrix.Identity;
                }
                FrameStep(frame_time);

                scene.local_transform = Matrix.Identity;

                scene.ParentNode.InternalProcess(frame_time);
                total_time    += frame_time;
                provoke_render = true;
                System.Threading.Thread.Sleep(0);
                old_time = new_time;
            }
            Closing();
        }
Пример #2
0
        private void ResourceLoop()
        {
            /*System.Windows.Forms.NativeWindow render_offscreen = new System.Windows.Forms.NativeWindow()
             * {
             *
             * };
             * render_offscreen.CreateHandle(new System.Windows.Forms.CreateParams()
             * {
             *      Caption = "Offsecreen resource loading window"
             * });
             * res_ctx = Graphics.OpenGL.OpenGLContext.GetContext();
             * res_ctx.CreateContext(render_offscreen.Handle, IntPtr.Zero, dev.Context);*/
            threads_ready |= ThreadReady.ResourceThread;
            return;

            while (threads_ready != ThreadReady.All)
            {
                Thread.Sleep(0);
            }

            long old_time = System.Diagnostics.Stopwatch.GetTimestamp();

            while (running)
            {
                long new_time = System.Diagnostics.Stopwatch.GetTimestamp();
                frame_time = (new_time - old_time) / (float)System.Diagnostics.Stopwatch.Frequency;
                old_time   = new_time;

                lock (resource_invoke)
                {
                    while (resource_invoke.Count > 0)
                    {
                        var item = resource_invoke.Dequeue();
                        item();
                    }
                }

                var res = scene.Resources.Janitorial();
                if (res.Count > 0)
                {
                    GraphicInvoke(new Action(delegate
                    {
                        foreach (var r in res)
                        {
                            r.DoDispose();
                        }
                        lock (scene.Resources.resources)
                        {
                            scene.Resources.Remove(res);
                        }
                    }));
                }
                System.Threading.Thread.Sleep(500);
            }
            //res_ctx.Dispose();
            //render_offscreen.DestroyHandle();
        }
Пример #3
0
        private void PhysicsLoop()
        {
            threads_ready |= ThreadReady.PhysicsThread;
            while (threads_ready != ThreadReady.All)
            {
                Thread.Sleep(0);
            }

            long old_time = System.Diagnostics.Stopwatch.GetTimestamp();

            while (running)
            {
                long new_time = System.Diagnostics.Stopwatch.GetTimestamp();
                frame_time = (new_time - old_time) / (float)System.Diagnostics.Stopwatch.Frequency;
                old_time   = new_time;
                lock (physics_invoke)
                {
                    while (physics_invoke.Count > 0)
                    {
                        var item = physics_invoke.Dequeue();
                        item();
                    }
                }
                if (scene.physics.Count == 0)
                {
                    System.Threading.Thread.Sleep(50);
                }
                else
                {
                    foreach (var obj in scene.physics)
                    {
                        obj.SimulationStep(frame_time);
                    }
                }
                System.Threading.Thread.Sleep(0);
            }
        }
Пример #4
0
        private void RenderLoop()
        {
            long old_time = 0;

            // Wait for control to recieve a handle
            target.HandleCreated += (sender, e) => ready = true;

            while (!ready)
            {
                IntPtr h = (IntPtr)target.Invoke(new Func <IntPtr>(() => target.Handle));
                if (h != IntPtr.Zero)
                {
                    ready = true;
                }
                System.Threading.Thread.Sleep(0);
            }
            IntPtr handle = (IntPtr)target.Invoke(new Func <IntPtr>(() => target.Handle));

            dev = new Glorg2.Graphics.GraphicsDevice(handle);

            ResourceThread      = new Thread(new ThreadStart(ResourceLoop));
            ResourceThread.Name = "Resource thread";
            ResourceThread.Start();

            threads_ready |= ThreadReady.RenderThread;
            while (threads_ready != ThreadReady.All)
            {
                Thread.Sleep(0);
            }


            InitializeGraphics();
            old_time = System.Diagnostics.Stopwatch.GetTimestamp();

            dev.State.MultiSample = true;
            while (running)
            {
                long  new_time = System.Diagnostics.Stopwatch.GetTimestamp();
                float time     = (new_time - old_time) / ((float)System.Diagnostics.Stopwatch.Frequency);
                while (graphic_invoke.Count > 0)
                {
                    Action act = null;
                    lock (graphic_invoke)
                    {
                        act = graphic_invoke.Dequeue();
                    }
                    if (act != null)
                    {
                        act();
                    }
                }

                if (sort_render_list)
                {
                }
                var res = scene.Resources.Janitorial();
                if (res.Count > 0)
                {
                    foreach (var item in res)
                    {
                        item.Dispose();
                    }
                    lock (scene.Resources.resources)
                    {
                        scene.Resources.Remove(res);
                    }
                }
                // Wait until simulation thread has finished with one frame
                // or else it is not necessary to render the next frame (since nothing has happened)
                while (!provoke_render && running)
                {
                    System.Threading.Thread.Sleep(0);
                }

                Render(dev, frame_time, total_time);
                //System.Threading.Thread.Sleep(0);
                fps            = 1 / time;
                provoke_render = false;
                old_time       = new_time;
            }

            scene.Dispose();
            CleanupResources();
            CleanupResources();             // Do this again to ensure that resources which are referenced by other resources are freed
            GraphicsClosing();
            scene.GraphicsDispose();
            dev.Dispose();
        }
Пример #5
0
 void OnThreadReady()
 {
     threadsCompleted++;
     ThreadReady?.Invoke(threadsCompleted);
 }
Пример #6
0
        private void ResourceLoop()
        {
            /*System.Windows.Forms.NativeWindow render_offscreen = new System.Windows.Forms.NativeWindow()
            {

            };
            render_offscreen.CreateHandle(new System.Windows.Forms.CreateParams()
            {
                Caption = "Offsecreen resource loading window"
            });
            res_ctx = Graphics.OpenGL.OpenGLContext.GetContext();
            res_ctx.CreateContext(render_offscreen.Handle, IntPtr.Zero, dev.Context);*/
            threads_ready |= ThreadReady.ResourceThread;
            return;
            while (threads_ready != ThreadReady.All)
                Thread.Sleep(0);

            long old_time = System.Diagnostics.Stopwatch.GetTimestamp();
            while (running)
            {
                long new_time = System.Diagnostics.Stopwatch.GetTimestamp();
                frame_time = (new_time - old_time) / (float)System.Diagnostics.Stopwatch.Frequency;
                old_time = new_time;

                lock (resource_invoke)
                {
                    while (resource_invoke.Count > 0)
                    {
                        var item = resource_invoke.Dequeue();
                        item();
                    }
                }

                var res = scene.Resources.Janitorial();
                if (res.Count > 0)
                {
                    GraphicInvoke(new Action(delegate
                    {
                        foreach (var r in res)
                        {
                            r.DoDispose();
                        }
                        lock (scene.Resources.resources)
                        {
                            scene.Resources.Remove(res);
                        }
                    }));
                }
                System.Threading.Thread.Sleep(500);
            }
            //res_ctx.Dispose();
            //render_offscreen.DestroyHandle();
        }
Пример #7
0
        private void RenderLoop()
        {
            long old_time = 0;
            // Wait for control to recieve a handle
            target.HandleCreated += (sender, e) => ready = true;

            while (!ready)
            {
                IntPtr h = (IntPtr)target.Invoke(new Func<IntPtr>(() => target.Handle));
                if (h != IntPtr.Zero)
                    ready = true;
                System.Threading.Thread.Sleep(0);
            }
            IntPtr handle = (IntPtr)target.Invoke(new Func<IntPtr>(() => target.Handle));
            dev = new Glorg2.Graphics.GraphicsDevice(handle);

            ResourceThread = new Thread(new ThreadStart(ResourceLoop));
            ResourceThread.Name = "Resource thread";
            ResourceThread.Start();

            threads_ready |= ThreadReady.RenderThread;
            while (threads_ready != ThreadReady.All)
                Thread.Sleep(0);

            InitializeGraphics();
            old_time = System.Diagnostics.Stopwatch.GetTimestamp();

            dev.State.MultiSample = true;
            while (running)
            {
                long new_time = System.Diagnostics.Stopwatch.GetTimestamp();
                float time =  (new_time - old_time) / ((float)System.Diagnostics.Stopwatch.Frequency);
                    while (graphic_invoke.Count > 0)
                    {
                        Action act = null;
                        lock (graphic_invoke)
                        {
                            act = graphic_invoke.Dequeue();
                        }
                        if (act != null)
                            act();
                    }

                if (sort_render_list)
                {

                }
                var res = scene.Resources.Janitorial();
                if (res.Count > 0)
                {
                    foreach (var item in res)
                    {
                        item.Dispose();
                    }
                    lock (scene.Resources.resources)
                    {
                        scene.Resources.Remove(res);
                    }
                }
                // Wait until simulation thread has finished with one frame
                // or else it is not necessary to render the next frame (since nothing has happened)
                while (!provoke_render && running)
                {
                    System.Threading.Thread.Sleep(0);
                }

                Render(dev, frame_time, total_time);
                //System.Threading.Thread.Sleep(0);
                fps = 1 / time;
                provoke_render = false;
                old_time = new_time;
            }

            scene.Dispose();
            CleanupResources();
            CleanupResources(); // Do this again to ensure that resources which are referenced by other resources are freed
            GraphicsClosing();
            scene.GraphicsDispose();
            dev.Dispose();
        }
Пример #8
0
        private void PhysicsLoop()
        {
            threads_ready |= ThreadReady.PhysicsThread;
            while (threads_ready != ThreadReady.All)
                Thread.Sleep(0);

            long old_time = System.Diagnostics.Stopwatch.GetTimestamp();

            while (running)
            {
                long new_time = System.Diagnostics.Stopwatch.GetTimestamp();
                frame_time = (new_time - old_time) / (float)System.Diagnostics.Stopwatch.Frequency;
                old_time = new_time;
                lock (physics_invoke)
                {
                    while (physics_invoke.Count > 0)
                    {
                        var item = physics_invoke.Dequeue();
                        item();
                    }
                }
                if (scene.physics.Count == 0)
                    System.Threading.Thread.Sleep(50);
                else
                    foreach (var obj in scene.physics)
                    {
                        obj.SimulationStep(frame_time);
                    }
                System.Threading.Thread.Sleep(0);

            }
        }
Пример #9
0
        private void MainLoop()
        {
            #if DEBUG
            Glorg2.Debugging.Debug.LogEnabled = true;
            #endif
            long old_time;
            Init();
            old_time = System.Diagnostics.Stopwatch.GetTimestamp();
            target.Resize += (sender, e) => { GraphicInvoke(new Action(InternalSizeChanged));};

            threads_ready |= ThreadReady.MainThread;

            while (threads_ready != ThreadReady.All)
            {
                System.Windows.Forms.Application.DoEvents();
                Thread.Sleep(0);
            }

            while (running)
            {
                long new_time = System.Diagnostics.Stopwatch.GetTimestamp();
                frame_time = (new_time - old_time) / (float)System.Diagnostics.Stopwatch.Frequency;

                lock (logic_invoke)
                {
                    while (logic_invoke.Count > 0)
                    {
                        var item = logic_invoke.Dequeue();
                        item();
                    }
                }
                if (scene.Camera != null)
                {
                    if (!scene.Camera.absolute_transform.Invert(out scene.camera_mat))
                        throw new Exception("WTF! Invert failed!");

                    scene.sim_time += frame_time;
                }
                else
                    scene.camera_mat = Matrix.Identity;
                FrameStep(frame_time);

                scene.local_transform = Matrix.Identity;

                scene.ParentNode.InternalProcess(frame_time);
                total_time += frame_time;
                provoke_render = true;
                System.Threading.Thread.Sleep(0);
                old_time = new_time;

            }
            Closing();
        }