Example #1
0
        public void Update(CameraDirectionMovementTracker lastcamera, Matrix4 resmat, float _zn, bool names, bool discs, Color namecolour) // FOREGROUND no thread
        {
            _starnamesbusy = true;                                                                                                         // from update to Transfertoforeground we are busy

            if (_starnamesforeground.Count >= maxstarscached)                                                                              // if we have too many, clean up, to free memory
            {
                List <LinkedListNode <StarNames> > removelist = new List <LinkedListNode <StarNames> >();
                LinkedListNode <StarNames>         pos        = _starnamesforeground.First;

                while (pos != null)
                {
                    StarNames sys = pos.Value;
                    if (!sys.inview)                          // if not painting
                    {
                        removelist.Add(pos);
                    }
                    pos = pos.Next;
                }

                //Tools.LogToFile(String.Format("starnameupd Remove {0}", removelist.Count ));
                //Console.WriteLine("Remove {0}", removelist.Count);

                foreach (LinkedListNode <StarNames> rpos in removelist)
                {
                    StarNames sys = rpos.Value;
                    sys.Dispose();

                    _starnamesforeground.Remove(rpos);
                    _starnamesbackground.Remove(sys.pos);
                }
            }

            _lastcamera = lastcamera;
            _resmat     = resmat;
            _znear      = _zn;
            _nameson    = names;
            _discson    = discs;
            _namecolour = namecolour;

            nsThread = new System.Threading.Thread(NamedStars)
            {
                Name = "Calculate Named Stars", IsBackground = true
            };
            nsThread.Start();
        }
Example #2
0
        public bool Draw()                                      // indicate if we ran out of time
        {
            bool needmoreticks = false;

            if (_starnames.Count > 10000)                       // need to work on parceling this out later..
            {
                if (Monitor.TryEnter(deletelock))               // if we can get in, we are not in the update above, so can clean
                {                                               // its a lazy delete, no rush..
                    List <Vector3> cleanuplist = new List <Vector3>();

                    foreach (Vector3 key in _starnames.Keys)
                    {
                        StarNames sys = _starnames[key];

                        if (!sys.inview)                          // if not painting
                        {
                            cleanuplist.Add(key);                 // add to clean up
                        }
                    }

                    Console.WriteLine("Clean up star names from " + _starnames.Count + " to " + (_starnames.Count - cleanuplist.Count));

                    foreach (Vector3 key in cleanuplist)
                    {
                        StarNames sys = _starnames[key];
                        if (sys.nametexture != null)
                        {
                            sys.nametexture.Dispose();
                        }

                        _starnames.Remove(key);
                    }

                    Monitor.Exit(deletelock);
                    GC.Collect();
                }
            }

            lock (_starnames)                                   // lock so they can't add anything while we draw
            {
                int updated = 0;

                foreach (StarNames sys in _starnames.Values)
                {
                    if (sys.newnametexture != null)          //250 seems okay on my machine, around the 50ms mark
                    {
                        if (updated++ < 250)
                        {
                            if (sys.nametexture != null)
                            {
                                sys.nametexture.Dispose();
                            }

                            sys.nametexture    = sys.newnametexture;   // copy over and take another reference..
                            sys.newnametexture = null;
                        }
                        else
                        {
                            needmoreticks = true;
                        }
                    }

                    if (sys.newnamevertices != null)
                    {
                        if (updated++ < 250)
                        {
                            sys.nametexture.UpdateVertices(sys.newnamevertices);
                            sys.newnamevertices = null;
                        }
                        else
                        {
                            needmoreticks = true;
                        }
                    }

                    if (sys.newstar != null)              // same with newstar
                    {
                        sys.paintstar = sys.newstar;
                        sys.newstar   = null;
                    }

                    if (sys.inview)                            // in view, send it to the renderer
                    {
                        if (sys.paintstar != null && _discson) // if star disk, paint..
                        {
                            sys.paintstar.Draw(_glControl);
                        }

                        if (sys.nametexture != null && _nameson)           // being paranoid by treating these separately. Thread may finish painting one before the other.
                        {
                            sys.nametexture.Draw(_glControl);
                        }
                    }
                }
            }

            //if (needmoreticks)   Console.WriteLine("More please");
            return(needmoreticks);
        }
Example #3
0
        private void NamedStars() // background thread.. run after timer tick
        {
            try                   // just in case someone tears us down..
            {
                int lylimit = (int)(_starlimitly / _lastcamera.LastZoom);
                lylimit = Math.Max(lylimit, 20);
                //Console.Write("Look down " + _camera_paint_lookdown + " look forward " + _camera_paint_lookforward);
                //Console.Write("Repaint " + _repaintall + " Stars " + _starlimitly + " within " + lylimit + "  ");
                int sqlylimit = lylimit * lylimit;                 // in squared distance limit from viewpoint

                Vector3 modcampos = _lastcamera.CameraPos;
                modcampos.Y = -modcampos.Y;

                StarGrid.TransFormInfo ti = new StarGrid.TransFormInfo(_resmat, _znear, _glControl.Width, _glControl.Height, sqlylimit, modcampos);

                SortedDictionary <float, StarGrid.InViewInfo> inviewlist = new SortedDictionary <float, StarGrid.InViewInfo>(new DuplicateKeyComparer <float>()); // who's in view, sorted by distance

                _stargrids.GetSystemsInView(ref inviewlist, 2000.0, ti);                                                                                          // consider all grids under 2k from current pos.

                float textscalingw = Math.Min(_starnamemaxly, Math.Max(_starnamesizely / _lastcamera.LastZoom, _starnameminly));                                  // per char
                float textscalingh = textscalingw * 4;
                float textoffset   = .20F;
                float starsize     = Math.Min(Math.Max(_lastcamera.LastZoom / 10F, 1.0F), 20F); // Normal stars are at 1F.
                //Console.WriteLine("Per char {0} h {1} sc {2} ", textscalingw, textscalingh, starsize);

                lock (deletelock)                              // can't delete during update, can paint..
                {
                    foreach (StarNames s in _starnames.Values) // all items not processed
                    {
                        s.todispose = true;                    // only items remaining will clear this
                    }
                    int limit   = 1000;                        // max number of stars to show..
                    int painted = 0;

                    using (SQLiteConnectionED cn = new SQLiteConnectionED())
                    {
                        foreach (StarGrid.InViewInfo inview in inviewlist.Values)            // for all in viewport, sorted by distance from camera position
                        {
                            StarNames sys  = null;
                            bool      draw = false;

                            if (_starnames.ContainsKey(inview.position))                   // if already there..
                            {
                                sys           = _starnames[inview.position];
                                sys.todispose = false;                         // forced redraw due to change in orientation, or due to disposal
                                draw          = _flippedorzoomed || (_nameson && sys.newstar == null) || (_discson && sys.nametexture == null);
                                painted++;
                            }
                            else if (painted < limit)
                            {
                                ISystem sc = _formmap.FindSystem(inview.position, cn); // with the open connection, find this star..

                                if (sc != null)                                        // if can't be resolved, ignore
                                {
                                    sys = new StarNames(sc);
                                    lock (_starnames)
                                    {
                                        _starnames.Add(inview.position, sys);               // need to lock over add.. in case display is working
                                    }
                                    draw = true;
                                    painted++;
                                }
                                else
                                {
                                    // Console.WriteLine("Failed to find " + pos.X + "," + pos.Y + "," + pos.Z);
                                }
                            }
                            else
                            {
                                break;      // no point doing any more..  Either the closest ones have been found, or a new one was painted
                            }

                            if (draw)
                            {
                                if (_nameson)
                                {
                                    float width = textscalingw * sys.name.Length;

                                    Bitmap map;                     // now, delete is the only one who removed newtexture
                                                                    // and we are protected against delete..
                                    if (sys.nametexture == null)    // so see if newtexture is there
                                    {
                                        map = DatasetBuilder.DrawString(sys.name, Color.Orange, _starfont);
                                        sys.newnametexture = TexturedQuadData.FromBitmap(map,
                                                                                         new PointData(sys.x, sys.y, sys.z),
                                                                                         _lastcamera.Rotation,
                                                                                         width, textscalingh, textoffset + width / 2, 0);
                                    }
                                    else
                                    {
                                        sys.newnamevertices = TexturedQuadData.CalcVertices(new PointData(sys.x, sys.y, sys.z),
                                                                                            _lastcamera.Rotation,
                                                                                            width, textscalingh, textoffset + width / 2, 0);
                                    }
                                }

                                if (_discson)
                                {
                                    sys.newstar = new PointData(sys.x, sys.y, sys.z, starsize, Color.FromArgb(255, inview.colour & 255, (inview.colour >> 8) & 255, (inview.colour >> 16) & 255));
                                }
                            }
                        }
                    }

                    foreach (StarNames s in _starnames.Values)            // only items above will remain.
                    {
                        s.inview = !s.todispose;                          // copy flag over, causes foreground to start removing them
                    }
                }

                //Console.WriteLine("  " + (Environment.TickCount % 10000) + "Paint " + painted);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Exception watcher: " + ex.Message);
                System.Diagnostics.Trace.WriteLine("Trace: " + ex.StackTrace);
            }

            _formmap.Invoke((System.Windows.Forms.MethodInvoker) delegate              // kick the UI thread to process.
            {
                _formmap.ChangeNamedStars();
            });
        }
Example #4
0
        private void NamedStars() // background thread.. run after Update.  Thread never deletes, only adds to its own structures
        {
            try                   // just in case someone tears us down..
            {
                int lylimit = (int)(_starlimitly / _lastcamera.LastZoom);
                lylimit = Math.Max(lylimit, 1);
                int sqlylimit = lylimit * lylimit;                 // in squared distance limit from viewpoint

                StarGrid.TransFormInfo ti = new StarGrid.TransFormInfo(_resmat, _znear, _glControl.Width, _glControl.Height, sqlylimit, _lastcamera.LastCameraPos);

                SortedDictionary <float, StarGrid.InViewInfo> inviewlist = new SortedDictionary <float, StarGrid.InViewInfo>(new DuplicateKeyComparer <float>());       // who's in view, sorted by distance

                //Stopwatch sw1 = new Stopwatch();sw1.Start(); Tools.LogToFile(String.Format("starnamesest Estimate at {0} len {1}", ti.campos, sqlylimit));

                _stargrids.GetSystemsInView(ref inviewlist, 2000.0F, ti);            // consider all grids under 2k from current pos.

                //Tools.LogToFile(String.Format("starnamesest Took {0} in view {1}", sw1.ElapsedMilliseconds, inviewlist.Count));

                float textscalingw = Math.Min(_starnamemaxly, Math.Max(_starnamesizely / _lastcamera.LastZoom, _starnameminly)); // per char
                float starsize     = Math.Min(Math.Max(_lastcamera.LastZoom / 10F, 1.0F), 20F);                                  // Normal stars are at 1F.
                //Console.WriteLine("Per char {0} h {1} sc {2} ", textscalingw, textscalingh, starsize);

                foreach (StarNames s in _starnamesbackground.Values) // all items not processed
                {
                    s.updatedinview = false;                         // only items remaining will clear this
                }
                _needrepaint = false;                                // assume nothing changes

                int painted = 0;

                //string res = "";  // used to view whats added/removed/draw..

                foreach (StarGrid.InViewInfo inview in inviewlist.Values)            // for all in viewport, sorted by distance from camera position
                {
                    using (SQLiteConnectionSystem cn = new SQLiteConnectionSystem())
                    {
                        StarNames sys  = null;
                        bool      draw = false;

                        if (_starnamesbackground.ContainsKey(inview.position))                   // if already there..
                        {
                            sys = _starnamesbackground[inview.position];
                            sys.updatedinview = true;

                            draw = (_discson && sys.paintstar == null && sys.newstar == null) ||
                                   (_nameson && ((sys.nametexture == null && sys.newnametexture == null)));

                            painted++;
                        }
                        else if (painted < maxstars)
                        {
                            ISystem sc = _formmap.FindSystem(inview.position, cn); // with the open connection, find this star..

                            if (sc != null)                                        // if can't be resolved, ignore
                            {
                                sys = new StarNames(sc, inview.position);          // we keep position in here using same floats as inview so it will match
                                _starnamesbackground.Add(inview.position, sys);    // add to our database
                                _starnamestoforeground.Add(sys);                   // send to foreground for adding
                                draw = true;
                                painted++;

                                //Tools.LogToFile(String.Format("starnamesest: push {0}", sys.Pos));
                                //res += "N";
                            }
                        }
                        else
                        {
                            break;      // no point doing any more..  got our fill of items
                        }

                        if (draw)
                        {
                            _needrepaint = true;                                            // changed a item.. needs a repaint

                            if (_nameson)
                            {
                                float width = textscalingw * sys.name.Length;

                                Bitmap map = DatasetBuilder.DrawString(sys.name, _namecolour, _starfont);

                                sys.newnametexture = TexturedQuadData.FromBitmap(map,
                                                                                 new PointData(sys.pos.X, sys.pos.Y, sys.pos.Z),
                                                                                 _lastcamera.Rotation,
                                                                                 width, textscalingw * 4.0F, _startextoffset + width / 2, 0);

                                sys.rotation = _lastcamera.Rotation;            // remember when we were when we draw it
                                sys.zoom     = _lastcamera.LastZoom;
                            }

                            if (_discson)
                            {
                                sys.newstar = new PointData(sys.pos.X, sys.pos.Y, sys.pos.Z, starsize, inview.AsColor);
                            }
                        }
                    }
                }

                foreach (StarNames s in _starnamesbackground.Values)              // only items above will remain.
                {
                    //if (s.inview != s.updatedinview) res += (s.updatedinview) ? "+" : "-";

                    _needrepaint = _needrepaint || (s.inview != s.updatedinview); // set if we change our mind on any of the items
                    s.inview     = s.updatedinview;                               // copy flag over, causes foreground to start removing them
                }

                //if ( _needrepaint) Console.WriteLine(String.Format("starnamesest in view  {0} limit {1} repaint {2} {3}", inviewlist.Count, lylimit, _needrepaint, res));

                //Tools.LogToFile(String.Format("starnamesest added all delta {0} topaint {1}", sw1.ElapsedMilliseconds, painted));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Exception watcher: " + ex.Message);
                System.Diagnostics.Trace.WriteLine("Trace: " + ex.StackTrace);
            }

            _formmap.Invoke((System.Windows.Forms.MethodInvoker) delegate              // kick the UI thread to process.
            {
                _formmap.ChangeNamedStars();
            });
        }