private Bitmap BuildPackedTexture(SpriteSheet originalSpriteSheet) { Bitmap combinedBitmap = new Bitmap(finalSpriteSheetWidth, finalSpriteSheetHeight); for (int i = 0; i < originalSpriteSheet.Frames.Count; i++) { SpriteSheetFrame spriteSheetFrame = originalSpriteSheet.Frames[i]; Bitmap spriteBitmap = originalSpriteSheet.Texture.Surface.ToBitmap(spriteSheetFrame.BoundingRect.ToRectangle()); Rectangle spriteRect = finalSpriteMapping[spriteSheetFrame.Id].ToRectangle(); for (int x = 0; x < spriteBitmap.Width; x++) { for (int y = 0; y < spriteBitmap.Height; y++) { combinedBitmap.SetPixel(spriteRect.X + x, spriteRect.Y + y, spriteBitmap.GetPixel(x, y)); } } } return(combinedBitmap); }
//public void Draw(int renderQueue, int sprite, Vector2 position, Color color) //{ // Draw(renderQueue, sprite, rectangle, color, SpriteEffects.None, 0f); //} public int add(AxisAlignedBox rect) { var frame = new SpriteSheetFrame(); frame.rect = rect; frames.Add(frame); return(frames.Count - 1); }
public override void Draw(GameTime gameTime) { var borders = new Color(1.0f, 1.0f, 1.0f, 0.2f); try { Matrix transformation = _camera.GetTransformation(); _spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, null, null, null, null, transformation); _landscape.Draw(transformation); Animation animation = _animations.GetById(_animIndex); AnimationFrame animationFrame = animation.CurrentFrame; int key = animationFrame.SpriteSheetId; SpriteSheet sheet = _sheets[key]; // Draw full texture _spriteBatch.Draw(sheet.Texture, new Vector2(0, 0), Color.White); _spriteBatch.DrawRectangle(sheet.Texture.Bounds, borders); // Animation SpriteSheetFrame ssFrame = sheet.Frames[animationFrame.SpriteSheetFrameId]; _spriteBatch.Draw(_sheets, animation, new Vector2(ssFrame.Width / 2, ssFrame.Height / 2 + sheet.Texture.Height)); _spriteBatch.DrawCross(new Vector2(ssFrame.Width / 2, ssFrame.Height / 2 + sheet.Texture.Height), Color.Red); // 16,16 _spriteBatch.DrawString(_font, $"{_animIndex}-{animation.CurrentFrame.SpriteSheetFrameId} : {animation.Name}", new Vector2(120, 12 + sheet.Texture.Height), Color.White); // Frames in full texture for (int i = 0; i < animation.Frames.Length; i++) { AnimationFrame frame = animation.Frames[i]; sheet = _sheets[frame.SpriteSheetId]; SpriteSheetFrame spriteSheetFrame = sheet.Frames[frame.SpriteSheetFrameId]; Rectangle area = new Rectangle(spriteSheetFrame.X, spriteSheetFrame.Y, spriteSheetFrame.Width, spriteSheetFrame.Height); _spriteBatch.DrawRectangle(area, borders); _spriteBatch.DrawCross(area.Location.ToVector2() + new Vector2(spriteSheetFrame.Width / 2.0f, spriteSheetFrame.Height / 2.0f), borders); // frame.Origin.ToVector2(), _spriteBatch.DrawString(_font, $"{i}", area.Location.ToVector2() + new Vector2(4, 4), borders); } _spriteBatch.End(); _statusPane.Draw(_camera); } catch (Exception ex) { _spriteBatch.Begin(); _spriteBatch.DrawString(_font, $"{ex.Message}", Vector2.Zero, Color.White); _spriteBatch.End(); } }
public bool PackSpriteSheet(SpriteSheet originalSpriteSheet, bool requirePowTwo, bool requireSquare, int padding, bool generateMap, int maxWidth, int maxHeight, out SpriteSheet packed) { tempSpriteSheet = originalSpriteSheet.BlankClone(); finalSpriteSheetWidth = maxWidth; finalSpriteSheetHeight = maxHeight; this.requirePowTwo = requirePowTwo; this.requireSquare = requireSquare; this.padding = padding; var sprites = originalSpriteSheet.Frames; sprites.Sort( (i1, i2) => { Size b1 = new Size(i1.Width, i1.Height); Size b2 = new Size(i2.Width, i2.Height); int c = -b1.Width.CompareTo(b2.Width); if (c != 0) { return(c); } c = -b1.Height.CompareTo(b2.Height); if (c != 0) { return(c); } return(0); } ); if (!PackBruteForce(originalSpriteSheet.Frames)) { packed = null; return(false); } Bitmap packedTexture = BuildPackedTexture(originalSpriteSheet); tempSpriteSheet.ResetTextureFromBitmap(packedTexture); if (generateMap) { int[] keys = new int[finalSpriteMapping.Keys.Count]; finalSpriteMapping.Keys.CopyTo(keys, 0); foreach (var key in keys) { Rect r = finalSpriteMapping[key]; r.Right -= padding; r.Bottom -= padding; finalSpriteMapping[key] = r; } foreach (var map in finalSpriteMapping) { SpriteSheetFrame newFrame = new SpriteSheetFrame(map.Value, tempSpriteSheet); newFrame.Id = map.Key; newFrame.TagName = originalSpriteSheet.Frames[newFrame.Id].TagName; tempSpriteSheet.Frames.Add(newFrame); } } packed = tempSpriteSheet; testSpriteMapping.Clear(); finalSpriteMapping.Clear(); return(true); }