Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e67aa5a

Browse files
committed
Merge pull request #321 from vlidholt/master
Updates starfield in game demo to use drawAtlas
2 parents f92e594 + 65e85d5 commit e67aa5a

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

sky/packages/sky/example/game/lib/game_demo_world.dart

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -562,49 +562,43 @@ class Laser extends Sprite {
562562
// Background starfield
563563

564564
class StarField extends Node {
565+
Image _image;
565566
int _numStars;
566567
List<Point> _starPositions;
567568
List<double> _starScales;
568-
List<double> _opacity;
569-
List<Texture> _textures;
569+
List<Rect> _rects;
570+
List<Color> _colors;
571+
Paint _paint = new Paint()
572+
..setFilterQuality(sky.FilterQuality.low)
573+
..isAntiAlias = false
574+
..setTransferMode(sky.TransferMode.plus);
570575

571576
StarField(SpriteSheet spriteSheet, this._numStars) {
572577
_starPositions = [];
573578
_starScales = [];
574-
_opacity = [];
575-
_textures = [];
579+
_colors = [];
580+
_rects = [];
576581

577582
for (int i = 0; i < _numStars; i++) {
578583
_starPositions.add(new Point(_rand.nextDouble() * _gameSizeWidth, _rand.nextDouble() * _gameSizeHeight));
579584
_starScales.add(_rand.nextDouble());
580-
_opacity.add(_rand.nextDouble() * 0.5 + 0.5);
581-
_textures.add(spriteSheet["star_${_rand.nextInt(2)}.png"]);
585+
_colors.add(new Color.fromARGB((255.0 * (_rand.nextDouble() * 0.5 + 0.5)).toInt(), 255, 255, 255));
586+
_rects.add(spriteSheet["star_${_rand.nextInt(2)}.png"].frame);
582587
}
588+
589+
_image = spriteSheet.image;
583590
}
584591

585592
void paint(PaintingCanvas canvas) {
586-
// Setup paint object for opacity and transfer mode
587-
Paint paint = new Paint();
588-
paint.setTransferMode(sky.TransferMode.plus);
589-
590-
double baseScaleX = 64.0 / _textures[0].size.width;
591-
double baseScaleY = 64.0 / _textures[0].size.height;
592-
593-
// Draw each star
593+
// Create a transform for each star
594+
List<sky.RSTransform> transforms = [];
594595
for (int i = 0; i < _numStars; i++) {
595-
Point pos = _starPositions[i];
596-
double scale = _starScales[i];
597-
paint.color = new Color.fromARGB((255.0*_opacity[i]).toInt(), 255, 255, 255);
598-
599-
canvas.save();
600-
601-
canvas.translate(pos.x, pos.y);
602-
canvas.scale(baseScaleX * scale, baseScaleY * scale);
603-
604-
canvas.drawImageRect(_textures[i].image, _textures[i].frame, _textures[i].spriteSourceSize, paint);
605-
606-
canvas.restore();
596+
sky.RSTransform transform = new sky.RSTransform(_starScales[i], 0.0, _starPositions[i].x, _starPositions[i].y);
597+
transforms.add(transform);
607598
}
599+
600+
// Draw the stars
601+
canvas.drawAtlas(_image, transforms, _rects, _colors, sky.TransferMode.modulate, null, _paint);
608602
}
609603

610604
void move(double dx, double dy) {

0 commit comments

Comments
 (0)