HaxeFlixel / HaxeFlixel/flixel

Copying a FlxCamera's pixels to a FlxSprite doesn't keep zoom, scroll, etc. of the camera

Open
#2,145 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haxe
Stars
2.2k
Forks
522
Avg merge
34m
Merged PRs (30d)
1

Description

  • Haxe version: 3.4.7
  • Flixel version: git
  • OpenFL version: git
  • Lime version: git
  • Affected targets: cpp

Code snippet reproducing the issue:

package;

using flixel.util.FlxSpriteUtil;

import flash.geom.Point;
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;

class PlayState extends FlxState
{
	var cameraOriginal:FlxCamera;
	var circleSprite:FlxSprite;
	var cameraCopy:FlxCamera;
	var cameraCopySprite:FlxSprite;

	override public function create():Void
	{
		super.create();
		cameraOriginal = new FlxCamera(0, 0, Std.int(FlxG.width / 2), Std.int(FlxG.height));
		FlxG.cameras.add(cameraOriginal);
		cameraOriginal.zoom = 3;
		circleSprite = new FlxSprite((FlxG.width / 4) - 32, (FlxG.height / 2) - 32);
		circleSprite.makeGraphic(64, 64, 0, true);
		circleSprite.drawCircle(32, 32, 32, 0xffff0000);
		circleSprite.updateHitbox();
		circleSprite.cameras = [cameraOriginal];
		add(circleSprite);
		cameraCopy = new FlxCamera(Std.int(FlxG.width / 2), 0, Std.int(FlxG.width / 2), Std.int(FlxG.height));
		FlxG.cameras.add(cameraCopy);
		cameraCopySprite = new FlxSprite(0, 0);
		cameraCopySprite.makeGraphic(Std.int(FlxG.width / 2), Std.int(FlxG.height), 0, true);
		cameraCopySprite.cameras = [cameraCopy];
		add(cameraCopySprite);
	}

	override public function update(elapsed:Float):Void
	{
		super.update(elapsed);
		cameraCopySprite.fill(0);
		if (FlxG.renderBlit) {
			cameraCopySprite.pixels.copyPixels(cameraOriginal.buffer, cameraOriginal.buffer.rect, new Point());
		} else {
			cameraCopySprite.pixels.draw(cameraOriginal.canvas);
		}
	}
}

Observed behavior:

I'm trying to copy the contents of a camera to a sprite so that I can apply effects to that sprite, such as applying an alpha mask by using copyChannel() on the resulting BitmapData (e.g. https://stackoverflow.com/questions/48075991/is-there-a-way-to-apply-an-alpha-mask-to-a-flxcamera?rq=1). Before getting to the point of applying effects however, I'm having difficulty getting the camera pixels to copy over 1:1 while observing camera zoom, scroll, etc. The above example creates a single sprite containing a red circle, which is placed in the source camera. That camera's zoom is then set to 3, resulting in the circle being rendered at 300% of its original size. When I copy the pixels from this camera to a new sprite, which is displayed in a second camera, it seems that the current zoom of the source camera is not observed and this results in the circle displaying at its original size in the target sprite.

2018-04-28 10_30_54-flxproject

Expected behavior:

I would expect the target sprite to be a 1:1 copy of the source camera's current view, including zoom, scroll, etc.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducing PlayState code in the issue and run it on the cpp target. Inspect FlxCamera.buffer, FlxCamera.canvas, and FlxSprite.pixels along the copy paths, comparing the source camera's zoom and scroll with the copied sprite. Done means the target sprite matches the source camera's current view, including zoom and scroll.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
game-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.