1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-12-11 16:05:02 +00:00
Funkin/source/funkin/graphics/framebuffer/FrameBuffer.hx

133 lines
3.1 KiB
Haxe
Raw Normal View History

2023-09-16 22:39:20 +00:00
package funkin.graphics.framebuffer;
2023-10-02 12:05:13 +00:00
import flixel.FlxSprite;
2023-09-16 22:39:20 +00:00
import flixel.FlxCamera;
2023-09-23 18:59:24 +00:00
import flixel.util.FlxColor;
2023-09-16 22:39:20 +00:00
import openfl.Lib;
2023-09-23 18:59:24 +00:00
import openfl.display.BitmapData;
2023-09-16 22:39:20 +00:00
import openfl.display3D.textures.TextureBase;
2023-09-23 18:59:24 +00:00
import openfl.geom.Matrix;
import openfl.geom.Rectangle;
2023-09-16 22:39:20 +00:00
2023-09-17 12:11:11 +00:00
/**
* A single frame buffer. Used by `FrameBufferManager`.
*/
2023-09-16 22:39:20 +00:00
class FrameBuffer
{
/**
* The bitmap data of the frame buffer.
*/
public var bitmap(default, null):BitmapData = null;
var texture:TextureBase;
final camera:FlxCamera;
final spriteCopies:Array<SpriteCopy> = [];
public function new()
{
camera = new FlxCamera();
2023-09-17 11:45:10 +00:00
camera.antialiasing = false;
2023-09-16 22:39:20 +00:00
camera.bgColor = FlxColor.TRANSPARENT;
2023-09-17 11:45:10 +00:00
@:privateAccess camera.flashSprite.stage = Lib.current.stage;
2023-09-16 22:39:20 +00:00
}
/**
* Creates a frame buffer with the given size.
* @param width the width
* @param height the height
2023-09-17 11:45:10 +00:00
* @param bgColor the background color
2023-09-16 22:39:20 +00:00
*/
2023-09-17 11:45:10 +00:00
public function create(width:Int, height:Int, bgColor:FlxColor):Void
2023-09-16 22:39:20 +00:00
{
dispose();
2023-09-17 12:11:11 +00:00
texture = Lib.current.stage.context3D.createTexture(width, height, BGRA, true);
2023-09-23 18:59:24 +00:00
bitmap = FixedBitmapData.fromTexture(texture);
2023-09-17 11:45:10 +00:00
camera.bgColor = bgColor;
2023-09-16 22:39:20 +00:00
}
/**
* Makes the internal camera follows the target camera.
* @param target the target camera
*/
public function follow(target:FlxCamera):Void
{
2023-09-17 11:45:10 +00:00
camera.x = target.x;
camera.y = target.y;
camera.width = target.width;
camera.height = target.height;
camera.scroll.x = target.scroll.x;
camera.scroll.y = target.scroll.y;
2023-09-16 22:39:20 +00:00
camera.setScale(target.scaleX, target.scaleY);
}
/**
* Locks the frame buffer and clears the buffer.
*/
@:access(flixel.FlxCamera)
public function lock():Void
{
camera.clearDrawStack();
camera.canvas.graphics.clear();
camera.fill(camera.bgColor.to24Bit(), camera.useBgAlphaBlending, camera.bgColor.alphaFloat);
2023-09-17 11:45:10 +00:00
#if FLX_DEBUG
camera.debugLayer.graphics.clear();
#end
2023-09-16 22:39:20 +00:00
}
/**
* Renders all sprite copies.
*/
2023-09-17 11:45:10 +00:00
@:access(flixel.FlxCamera)
2023-09-16 22:39:20 +00:00
public function render():Void
{
for (spriteCopy in spriteCopies)
{
spriteCopy.render(camera);
}
2023-09-17 11:45:10 +00:00
camera.render();
2023-09-16 22:39:20 +00:00
}
/**
* Unlocks the frame buffer and makes the bitmap ready to use.
*/
public function unlock():Void
{
bitmap.fillRect(new Rectangle(0, 0, bitmap.width, bitmap.height), 0);
bitmap.draw(camera.flashSprite, new Matrix(1, 0, 0, 1, camera.flashSprite.x, camera.flashSprite.y));
}
2023-09-17 12:11:11 +00:00
/**
* Diposes stuff. Call `create` again if you want to reuse the instance.
*/
2023-09-16 22:39:20 +00:00
public function dispose():Void
{
if (texture != null)
{
texture.dispose();
texture = null;
bitmap.dispose();
bitmap = null;
}
2023-09-23 18:59:24 +00:00
spriteCopies.resize(0);
2023-09-16 22:39:20 +00:00
}
2023-09-17 12:11:11 +00:00
/**
* Adds a sprite copy to the frame buffer.
* @param spriteCopy the sprite copy
*/
2023-09-16 22:39:20 +00:00
public function addSpriteCopy(spriteCopy:SpriteCopy):Void
{
spriteCopies.push(spriteCopy);
}
2023-10-02 12:05:13 +00:00
/**
* Adds the sprite to the frame buffer. The sprite will only be seen from
* the frame buffer.
* @param sprite the sprite
*/
public function moveSprite(sprite:FlxSprite):Void
{
sprite.cameras = [camera];
}
2023-09-16 22:39:20 +00:00
}