public Bitmap DrawBitmap() { var _image = new Bitmap(m.PlaneWidth, m.PlaneHeight); foreach (var zeta in m.Zetas) { if (!zeta.isDrawn) { continue; } if (zeta.HasFinished) { //var chosen_color = _colorset[(_colorset.Length - 1) * zeta.IterationsComplete / m.IterationsDone ]; //var chosen_color = _colorset[zeta.IterationsComplete % _colorset.Length]; var smoothness = Math.Log(Math.Log(zeta.Z_n.LengthSqr(), 2)); var chosen_color = _colorset[(zeta.IterationsComplete + _colorset.Length - (int)smoothness) % _colorset.Length]; _image.SetPixel(zeta.P.X, zeta.P.Y, chosen_color); } else { _image.SetPixel(zeta.P.X, zeta.P.Y, ColorCache.GetColor(0, 0, 0)); } } return(_image); }
public override void Update() { if (Input.KeyDown(Keys.F11)) { Screen.ToggleFullscreen(); } if (Input.KeyPressed(Keys.F)) { for (int i = 0; i < 50; i++) { var e = new TestEntity(); e.Center = Input.MouseWorldPos; e.Velocity = Rand.UnitCircle() * Rand.Range(0.25f, 10f) * Tile.SIZE; } } if (Input.KeyDown(Keys.M)) { using (FileStream fs = new FileStream(@"C:\Users\James.000\Desktop\Chunk.txt", FileMode.Create, FileAccess.Write)) { using (IOWriter w = new IOWriter(fs)) { Layer.GetChunk(0, 0).Serialize(w); Debug.Log($"Written {w.Length} bytes."); } } } if (Input.KeyDown(Keys.NumPad0)) { using (FileStream fs = new FileStream(@"C:\Users\James.000\Desktop\Entities.txt", FileMode.Create, FileAccess.Write)) { using (IOWriter w = new IOWriter(fs)) { JEngine.Entities.SerializeAll(w); Debug.Log($"Written {w.Length} bytes for entities."); } } } if (Input.KeyDown(Keys.NumPad1)) { using (FileStream fs = new FileStream(@"C:\Users\James.000\Desktop\Entities.txt", FileMode.Open, FileAccess.Read)) { using (IOReader w = new IOReader(fs)) { JEngine.Entities.DeserializeAllNew(w); Debug.Log($"Read {w.Length} bytes for entities."); } } } if (Input.KeyDown(Keys.N)) { using (FileStream fs = new FileStream(@"C:\Users\James.000\Desktop\Chunk.txt", FileMode.OpenOrCreate, FileAccess.Read)) { using (IOReader r = new IOReader(fs)) { Layer.GetChunk(0, 0).Deserialize(r); Debug.Log($"Read {r.Length} bytes."); } } } if (Input.KeyDown(Keys.Y)) { var e = new TestActive(); e.Center = Input.MouseWorldPos; e.Velocity = Rand.UnitCircle() * Rand.Range(1f, 5f); } if (Input.KeyPressed(Keys.L)) { float r = Tile.SIZE * 5; foreach (var entity in JEngine.Entities.GetAllInRange(Input.MouseWorldPos, r)) { entity.Destroy(); } } if (Input.KeyDown(Keys.Space)) { System.GC.Collect(); } if (Input.KeyDown(Keys.T)) { var missile = new MissileEntity(); missile.Position = Input.MouseWorldPos - missile.Size * 0.5f; } var p = JEngine.TileMap.PixelToTileCoords((int)Input.MouseWorldPos.X, (int)Input.MouseWorldPos.Y); if (Input.KeyPressed(Keys.V)) { JEngine.TileMap.SetTile(p.X, p.Y, 1, new Tile(2, ColorCache.EnsureColor(Color.White))); } if (Input.KeyDown(Keys.B)) { Layer.SetTile(p.X, p.Y, 1, new Tile(4, ColorCache.EnsureColor(Color.White))); } const int MAX_PER_FRAME = 5; int count = 0; foreach (var chunk in Layer.GetLoadedChunks()) { if (chunk != null) { if (chunk.EntityCount != 0) { chunk.FlagAsNeeded(); } if (chunk.TimeSinceNeeded > CHUNK_UNLOAD_TIME && chunk.FramesSinceNeeded > 2) { toBin.Add(chunk.ID); continue; } if (chunk.TimeSinceRendered > CHUNK_UNLOAD_TIME && chunk.FramesSinceRendered > 2) { chunk.UnloadGraphics(); } chunk.Decay(Time.unscaledDeltaTime); if (chunk.RequiresRedraw && count < MAX_PER_FRAME && chunk.Graphics.Texture != null) { count++; RequestRenderTargetDraw(new TargetRenderRequest(chunk.Graphics.Texture, true) { CustomData = chunk }); } } } foreach (var id in toBin) { Layer.UnloadChunk(id); } toBin.Clear(); }
/// Encodes the pixels in the image as a linear sequence of codes. Because /// C# simply lacks any data structure that could be used to represent the /// three types of codes (literals, backward references and color cache /// indices), we encode the sequence of codes as a simple list of integers. /// /// The codes are stored in the sequence as follows: /// 1. [green, red, blue, alpha] is a literal /// 2. [length code + 256, length extra bits, distance code, distance extra bits] /// 3. [color cache index + 256 + 24] /// with the type determined by the first element. static List <int> EncodeImageData(Image image, int colorCacheBits) { var chainTable = new ChainTable(20 * 1000); var colorCache = new ColorCache(colorCacheBits); var encoded = new List <int>(); int pixelCount = image.Pixels.Count(); for (int pix = 0; pix < pixelCount; ++pix) { var argb = image.Pixels[pix]; if (pix + 2 < pixelCount) { var chain = chainTable.GetChain(argb, image.Pixels[pix + 1], image.Pixels[pix + 2]); int longestIndex = 0; int longestLength = 0; for (int i = 0; i < 100 && chain != null; ++i) { // there is a limit to the distance that can be encoded if (pix - chain.Index > 1048576 - 120) { break; } int length = FindMatchLength(image, pix, chain.Index, 4096); if (length > longestLength) { longestIndex = chain.Index; longestLength = length; } chain = chain.NextOrNull; } if (longestLength >= 3) { int lengthExtra, distanceExtra; int distanceCode = DistanceCode(image, pix - longestIndex); int lengthSymbol = PrefixCode(longestLength, out lengthExtra); int distanceSymbol = PrefixCode(distanceCode, out distanceExtra); if (colorCache.Present) { for (int i = 0; i < longestLength; ++i) { colorCache.Insert(image.Pixels[pix + i]); } } encoded.Add(lengthSymbol + 256); encoded.Add(lengthExtra); encoded.Add(distanceSymbol); encoded.Add(distanceExtra); pix += longestLength - 1; continue; } chainTable.AddChain(argb, image.Pixels[pix + 1], image.Pixels[pix + 2], pix); } int colorCacheIndex; if (colorCache.Lookup(argb, out colorCacheIndex)) { encoded.Add(colorCacheIndex + 256 + 24); continue; } encoded.Add(argb.G); encoded.Add(argb.R); encoded.Add(argb.B); encoded.Add(argb.A); colorCache.Insert(argb); } return(encoded); }